test.c 1.2 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2021 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. #ifdef 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. _rha, \
  70. _rha == 1 ? "" : "s", \
  71. _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 __GNUC__
  83. _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  84. #endif
  85. #ifdef USE_FLAT_TEST_H
  86. #ifdef HAVE_CONFIG_H
  87. #include "test_paths.h"
  88. #endif
  89. #include "test.h"
  90. #else
  91. #ifdef HAVE_CONFIG_H
  92. #include "wolfcrypt/test/test_paths.h"
  93. #endif
  94. #include "wolfcrypt/test/test.h"
  95. #endif
  96. /* printf mappings */
  97. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  98. #include <mqx.h>
  99. #include <stdlib.h>
  100. /* see wc_port.h for fio.h and nio.h includes */
  101. #elif defined(FREESCALE_KSDK_BM)
  102. #include "fsl_debug_console.h"
  103. #undef printf
  104. #define printf PRINTF
  105. #elif defined(WOLFSSL_APACHE_MYNEWT)
  106. #include <assert.h>
  107. #include <string.h>
  108. #include "sysinit/sysinit.h"
  109. #include "os/os.h"
  110. #ifdef ARCH_sim
  111. #include "mcu/mcu_sim.h"
  112. #endif
  113. #include "os/os_time.h"
  114. #elif defined(WOLFSSL_ESPIDF)
  115. #include <time.h>
  116. #include <sys/time.h>
  117. #elif defined(WOLFSSL_ZEPHYR)
  118. #include <stdio.h>
  119. #define printf printk
  120. #elif defined(MICRIUM)
  121. #include <os.h>
  122. #if (OS_VERSION < 50000)
  123. #include <bsp_ser.h>
  124. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  125. #undef printf
  126. #define printf BSP_Ser_Printf
  127. #else
  128. #include <stdio.h>
  129. #endif
  130. #elif defined(WOLFSSL_PB)
  131. #include <stdarg.h>
  132. int wolfssl_pb_print(const char*, ...);
  133. #undef printf
  134. #define printf wolfssl_pb_print
  135. #elif defined(WOLFSSL_TELIT_M2MB)
  136. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  137. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  138. /* remap printf */
  139. #undef printf
  140. #define printf M2M_LOG_INFO
  141. /* OS requires occasional sleep() */
  142. #ifndef TEST_SLEEP_MS
  143. #define TEST_SLEEP_MS 50
  144. #endif
  145. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  146. /* don't use file system for these tests, since ./certs dir isn't loaded */
  147. #undef NO_FILESYSTEM
  148. #define NO_FILESYSTEM
  149. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && !defined(THREADX_NO_DC_PRINTF)
  150. /* since just testing, use THREADX log printf instead */
  151. int dc_log_printf(char*, ...);
  152. #undef printf
  153. #define printf dc_log_printf
  154. #elif defined(ANDROID)
  155. #ifdef XMALLOC_USER
  156. #include <stdlib.h> /* we're using malloc / free direct here */
  157. #endif
  158. #ifndef STRING_USER
  159. #include <stdio.h>
  160. #endif
  161. #include <android/log.h>
  162. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  163. #ifndef NO_FILESYSTEM
  164. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  165. #endif
  166. #else
  167. #define printf(...) \
  168. __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__)
  169. #define fprintf(fp, ...) \
  170. __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__)
  171. #endif
  172. #elif defined(WOLFSSL_DEOS)
  173. #include <printx.h>
  174. #undef printf
  175. #define printf printx
  176. #else
  177. #ifdef XMALLOC_USER
  178. #include <stdlib.h> /* we're using malloc / free direct here */
  179. #endif
  180. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  181. #include <stdio.h>
  182. #endif
  183. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  184. #undef printf
  185. #define printf(...) ({})
  186. #endif
  187. /* enable way for customer to override test/bench printf */
  188. #ifdef XPRINTF
  189. #undef printf
  190. #define printf XPRINTF
  191. #endif
  192. #endif
  193. #include <wolfssl/wolfcrypt/memory.h>
  194. #include <wolfssl/wolfcrypt/wc_port.h>
  195. #include <wolfssl/wolfcrypt/logging.h>
  196. #include <wolfssl/wolfcrypt/types.h>
  197. #include <wolfssl/wolfcrypt/asn.h>
  198. #include <wolfssl/wolfcrypt/md2.h>
  199. #include <wolfssl/wolfcrypt/md5.h>
  200. #include <wolfssl/wolfcrypt/md4.h>
  201. #include <wolfssl/wolfcrypt/sha.h>
  202. #include <wolfssl/wolfcrypt/sha256.h>
  203. #include <wolfssl/wolfcrypt/sha512.h>
  204. #include <wolfssl/wolfcrypt/rc2.h>
  205. #include <wolfssl/wolfcrypt/arc4.h>
  206. #if defined(WC_NO_RNG)
  207. #include <wolfssl/wolfcrypt/integer.h>
  208. #else
  209. #include <wolfssl/wolfcrypt/random.h>
  210. #endif
  211. #include <wolfssl/wolfcrypt/coding.h>
  212. #include <wolfssl/wolfcrypt/signature.h>
  213. #include <wolfssl/wolfcrypt/rsa.h>
  214. #include <wolfssl/wolfcrypt/des3.h>
  215. #include <wolfssl/wolfcrypt/aes.h>
  216. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  217. #include <wolfssl/wolfcrypt/cmac.h>
  218. #include <wolfssl/wolfcrypt/poly1305.h>
  219. #include <wolfssl/wolfcrypt/camellia.h>
  220. #include <wolfssl/wolfcrypt/hmac.h>
  221. #include <wolfssl/wolfcrypt/dh.h>
  222. #include <wolfssl/wolfcrypt/dsa.h>
  223. #include <wolfssl/wolfcrypt/srp.h>
  224. #include <wolfssl/wolfcrypt/idea.h>
  225. #include <wolfssl/wolfcrypt/hc128.h>
  226. #include <wolfssl/wolfcrypt/rabbit.h>
  227. #include <wolfssl/wolfcrypt/chacha.h>
  228. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  229. #include <wolfssl/wolfcrypt/pwdbased.h>
  230. #include <wolfssl/wolfcrypt/ripemd.h>
  231. #include <wolfssl/wolfcrypt/error-crypt.h>
  232. #ifdef HAVE_ECC
  233. #include <wolfssl/wolfcrypt/ecc.h>
  234. #endif
  235. #ifdef HAVE_CURVE25519
  236. #include <wolfssl/wolfcrypt/curve25519.h>
  237. #endif
  238. #ifdef HAVE_ED25519
  239. #include <wolfssl/wolfcrypt/ed25519.h>
  240. #endif
  241. #ifdef HAVE_CURVE448
  242. #include <wolfssl/wolfcrypt/curve448.h>
  243. #endif
  244. #ifdef HAVE_ED448
  245. #include <wolfssl/wolfcrypt/ed448.h>
  246. #endif
  247. #ifdef WOLFCRYPT_HAVE_ECCSI
  248. #include <wolfssl/wolfcrypt/eccsi.h>
  249. #endif
  250. #ifdef WOLFCRYPT_HAVE_SAKKE
  251. #include <wolfssl/wolfcrypt/sakke.h>
  252. #endif
  253. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  254. #include <wolfssl/wolfcrypt/blake2.h>
  255. #endif
  256. #ifdef WOLFSSL_SHA3
  257. #include <wolfssl/wolfcrypt/sha3.h>
  258. #endif
  259. #ifdef HAVE_LIBZ
  260. #include <wolfssl/wolfcrypt/compress.h>
  261. #endif
  262. #ifdef HAVE_PKCS7
  263. #include <wolfssl/wolfcrypt/pkcs7.h>
  264. #endif
  265. #ifdef HAVE_FIPS
  266. #include <wolfssl/wolfcrypt/fips_test.h>
  267. #endif
  268. #ifdef HAVE_SELFTEST
  269. #include <wolfssl/wolfcrypt/selftest.h>
  270. #endif
  271. #ifdef WOLFSSL_ASYNC_CRYPT
  272. #include <wolfssl/wolfcrypt/async.h>
  273. #endif
  274. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  275. #include <wolfssl/wolfcrypt/logging.h>
  276. #endif
  277. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  278. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  279. #endif
  280. #ifdef WOLF_CRYPTO_CB
  281. #include <wolfssl/wolfcrypt/cryptocb.h>
  282. #ifdef HAVE_INTEL_QA_SYNC
  283. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  284. #endif
  285. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  286. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  287. #endif
  288. #endif
  289. #ifdef _MSC_VER
  290. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  291. #pragma warning(disable: 4996)
  292. #endif
  293. #ifdef OPENSSL_EXTRA
  294. #ifndef WOLFCRYPT_ONLY
  295. #include <wolfssl/openssl/evp.h>
  296. #endif
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/hmac.h>
  299. #include <wolfssl/openssl/aes.h>
  300. #include <wolfssl/openssl/des.h>
  301. #endif
  302. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  303. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  304. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  305. #define USE_CERT_BUFFERS_2048
  306. #endif
  307. #if !defined(USE_CERT_BUFFERS_256)
  308. #define USE_CERT_BUFFERS_256
  309. #endif
  310. #endif
  311. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  312. #define ENABLE_ECC384_CERT_GEN_TEST
  313. #endif
  314. #include <wolfssl/certs_test.h>
  315. #ifdef HAVE_NTRU
  316. #include "libntruencrypt/ntru_crypto.h"
  317. #endif
  318. #ifdef DEVKITPRO
  319. #include <wiiuse/wpad.h>
  320. #endif
  321. #ifdef WOLFSSL_STATIC_MEMORY
  322. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  323. #else
  324. #define HEAP_HINT NULL
  325. #endif /* WOLFSSL_STATIC_MEMORY */
  326. /* these cases do not have intermediate hashing support */
  327. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  328. && !defined(WOLFSSL_XILINX_CRYPT)
  329. #define NO_INTM_HASH_TEST
  330. #endif
  331. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  332. static void initDefaultName(void);
  333. #endif
  334. /* for async devices */
  335. #ifdef WOLFSSL_QNX_CAAM
  336. static int devId = WOLFSSL_CAAM_DEVID;
  337. #else
  338. static int devId = INVALID_DEVID;
  339. #endif
  340. #ifdef HAVE_WNR
  341. const char* wnrConfigFile = "wnr-example.conf";
  342. #endif
  343. #define TEST_STRING "Everyone gets Friday off."
  344. #define TEST_STRING_SZ 25
  345. typedef struct testVector {
  346. const char* input;
  347. const char* output;
  348. size_t inLen;
  349. size_t outLen;
  350. } testVector;
  351. #ifndef WOLFSSL_TEST_SUBROUTINE
  352. #define WOLFSSL_TEST_SUBROUTINE
  353. #endif
  354. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  355. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  356. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  357. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  358. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  359. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  360. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  361. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  362. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  363. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  364. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  365. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  366. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  367. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  368. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  369. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  370. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  371. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  372. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  373. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  375. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  376. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int hc128_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  395. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  396. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  397. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  398. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  399. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  400. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  401. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  402. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  403. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  404. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  405. #ifndef WC_NO_RNG
  406. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  407. #endif /* WC_NO_RNG */
  408. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  409. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  410. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  411. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  412. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  413. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  416. #endif
  417. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  419. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  420. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  421. #ifdef HAVE_ECC
  422. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  423. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  424. defined(WOLFSSL_AES_128)
  425. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  426. #endif
  427. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  428. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  429. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  430. /* skip for ATECC508/608A, cannot import private key buffers */
  431. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  432. #endif
  433. #endif
  434. #ifdef HAVE_CURVE25519
  435. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  436. #endif
  437. #ifdef HAVE_ED25519
  438. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  439. #endif
  440. #ifdef HAVE_CURVE448
  441. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  442. #endif
  443. #ifdef HAVE_ED448
  444. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  445. #endif
  446. #ifdef WOLFCRYPT_HAVE_ECCSI
  447. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  448. #endif
  449. #ifdef WOLFCRYPT_HAVE_SAKKE
  450. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  451. #endif
  452. #ifdef HAVE_BLAKE2
  453. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  454. #endif
  455. #ifdef HAVE_BLAKE2S
  456. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  457. #endif
  458. #ifdef HAVE_LIBZ
  459. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  460. #endif
  461. #ifdef HAVE_PKCS7
  462. #ifndef NO_PKCS7_ENCRYPTED_DATA
  463. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  464. #endif
  465. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  466. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  467. #endif
  468. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  469. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  470. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  471. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  472. #endif
  473. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  474. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  475. word32 keySz);
  476. #endif
  477. #endif
  478. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  479. !defined(NO_FILESYSTEM)
  480. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  481. #endif
  482. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  483. !defined(NO_FILESYSTEM)
  484. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  485. #endif
  486. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  487. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  488. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  489. #endif
  490. #ifdef HAVE_IDEA
  491. WOLFSSL_TEST_SUBROUTINE int idea_test(void);
  492. #endif
  493. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  494. #ifdef HAVE_VALGRIND
  495. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  496. #endif
  497. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  498. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  499. #endif
  500. #ifdef ASN_BER_TO_DER
  501. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  502. #endif
  503. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  504. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  505. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  506. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  507. #endif
  508. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  509. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  510. #endif
  511. #ifdef WOLF_CRYPTO_CB
  512. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  513. #endif
  514. #ifdef WOLFSSL_CERT_PIV
  515. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  516. #endif
  517. /* General big buffer size for many tests. */
  518. #define FOURK_BUF 4096
  519. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  520. #ifdef HAVE_STACK_SIZE
  521. static THREAD_RETURN err_sys(const char* msg, int es)
  522. #else
  523. static int err_sys(const char* msg, int es)
  524. #endif
  525. {
  526. (void)msg;
  527. (void)es;
  528. #ifdef WOLFSSL_LINUXKM
  529. lkm_printf("%s error = %d\n", msg, es);
  530. EXIT_TEST(es);
  531. #else
  532. printf("%s error = %d\n", msg, es);
  533. EXIT_TEST(-1);
  534. #endif
  535. }
  536. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  537. /* func_args from test.h, so don't have to pull in other stuff */
  538. typedef struct func_args {
  539. int argc;
  540. char** argv;
  541. int return_code;
  542. } func_args;
  543. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  544. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  545. static void myFipsCb(int ok, int err, const char* hash)
  546. {
  547. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  548. printf("message = %s\n", wc_GetErrorString(err));
  549. printf("hash = %s\n", hash);
  550. if (err == IN_CORE_FIPS_E) {
  551. printf("In core integrity hash check failure, copy above hash\n");
  552. printf("into verifyCore[] in fips_test.c and rebuild\n");
  553. }
  554. }
  555. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  556. #ifdef WOLFSSL_STATIC_MEMORY
  557. #ifdef BENCH_EMBEDDED
  558. static byte gTestMemory[14000];
  559. #elif defined(WOLFSSL_CERT_EXT)
  560. static byte gTestMemory[140000];
  561. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  562. static byte gTestMemory[160000];
  563. #else
  564. static byte gTestMemory[80000];
  565. #endif
  566. #endif
  567. #ifdef WOLFSSL_PB
  568. static int wolfssl_pb_print(const char* msg, ...)
  569. {
  570. int ret;
  571. va_list args;
  572. char tmpBuf[80];
  573. va_start(args, msg);
  574. ret = vsprint(tmpBuf, msg, args);
  575. va_end(args);
  576. fnDumpStringToSystemLog(tmpBuf);
  577. return ret;
  578. }
  579. #endif /* WOLFSSL_PB */
  580. /* optional macro to add sleep between tests */
  581. #ifdef TEST_SLEEP
  582. #include <stdarg.h> /* for var args */
  583. static WC_INLINE void test_pass(const char* fmt, ...)
  584. {
  585. va_list args;
  586. va_start(args, fmt);
  587. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  588. va_end(args);
  589. PRINT_HEAP_CHECKPOINT();
  590. TEST_SLEEP();
  591. }
  592. #else
  593. /* redirect to printf */
  594. #define test_pass(...) { \
  595. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  596. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  597. return err_sys("post-test check failed", -1); \
  598. } \
  599. PRINT_HEAP_CHECKPOINT(); \
  600. }
  601. /* stub the sleep macro */
  602. #define TEST_SLEEP()
  603. #endif
  604. #ifdef HAVE_STACK_SIZE
  605. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  606. #else
  607. int wolfcrypt_test(void* args)
  608. #endif
  609. {
  610. int ret;
  611. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  612. long heap_baselineAllocs, heap_baselineBytes;
  613. #endif
  614. STACK_SIZE_INIT();
  615. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  616. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  617. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  618. (void)wolfCrypt_heap_peakBytes_checkpoint();
  619. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  620. #endif
  621. printf("------------------------------------------------------------------------------\n");
  622. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  623. printf("------------------------------------------------------------------------------\n");
  624. if (args) {
  625. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  626. int ch;
  627. #endif
  628. ((func_args*)args)->return_code = -1; /* error state */
  629. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  630. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  631. switch(ch) {
  632. case 's':
  633. #ifdef HAVE_STACK_SIZE_VERBOSE
  634. max_relative_stack = (ssize_t)atoi(myoptarg);
  635. break;
  636. #else
  637. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  638. #endif
  639. case 'm':
  640. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  641. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  642. break;
  643. #else
  644. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  645. #endif
  646. case 'a':
  647. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  648. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  649. break;
  650. #else
  651. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  652. #endif
  653. case 'h':
  654. return err_sys("\
  655. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  656. [-a max_relative_heap_allocs] [-h]\n", 0);
  657. default:
  658. return err_sys("unknown test option. try -h.", -1);
  659. }
  660. }
  661. #endif
  662. }
  663. #ifdef WOLFSSL_STATIC_MEMORY
  664. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  665. WOLFMEM_GENERAL, 1) != 0) {
  666. printf("unable to load static memory.\n");
  667. return(EXIT_FAILURE);
  668. }
  669. #endif
  670. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  671. wolfSSL_Debugging_ON();
  672. #endif
  673. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  674. wc_SetLoggingHeap(HEAP_HINT);
  675. #endif
  676. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  677. wolfCrypt_SetCb_fips(myFipsCb);
  678. #endif
  679. #if !defined(NO_BIG_INT)
  680. if (CheckCtcSettings() != 1) {
  681. printf("Sizeof mismatch (build) %x != (run) %x\n",
  682. CTC_SETTINGS, CheckRunTimeSettings());
  683. return err_sys("Build vs runtime math mismatch\n", -1000);
  684. }
  685. #if defined(USE_FAST_MATH) && \
  686. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  687. if (CheckFastMathSettings() != 1)
  688. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  689. -1001);
  690. #endif /* USE_FAST_MATH */
  691. #endif /* !NO_BIG_INT */
  692. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  693. initDefaultName();
  694. #endif
  695. #ifdef WOLFSSL_ASYNC_CRYPT
  696. ret = wolfAsync_DevOpen(&devId);
  697. if (ret < 0) {
  698. printf("Async device open failed\nRunning without async\n");
  699. }
  700. #else
  701. (void)devId;
  702. #endif /* WOLFSSL_ASYNC_CRYPT */
  703. #ifdef WOLF_CRYPTO_CB
  704. #ifdef HAVE_INTEL_QA_SYNC
  705. devId = wc_CryptoCb_InitIntelQa();
  706. if (INVALID_DEVID == devId) {
  707. printf("Couldn't init the Intel QA\n");
  708. }
  709. #endif
  710. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  711. devId = wc_CryptoCb_InitOcteon();
  712. if (INVALID_DEVID == devId) {
  713. printf("Couldn't init the Cavium Octeon\n");
  714. }
  715. #endif
  716. #endif
  717. #ifdef HAVE_SELFTEST
  718. if ( (ret = wolfCrypt_SelfTest()) != 0)
  719. return err_sys("CAVP selftest failed!\n", ret);
  720. else
  721. test_pass("CAVP selftest passed!\n");
  722. #endif
  723. if ( (ret = error_test()) != 0)
  724. return err_sys("error test failed!\n", ret);
  725. else
  726. test_pass("error test passed!\n");
  727. if ( (ret = memory_test()) != 0)
  728. return err_sys("MEMORY test failed!\n", ret);
  729. else
  730. test_pass("MEMORY test passed!\n");
  731. #ifndef NO_CODING
  732. if ( (ret = base64_test()) != 0)
  733. return err_sys("base64 test failed!\n", ret);
  734. else
  735. test_pass("base64 test passed!\n");
  736. #ifdef WOLFSSL_BASE16
  737. if ( (ret = base16_test()) != 0)
  738. return err_sys("base16 test failed!\n", ret);
  739. else
  740. test_pass("base16 test passed!\n");
  741. #endif
  742. #endif /* !NO_CODING */
  743. #ifndef NO_ASN
  744. if ( (ret = asn_test()) != 0)
  745. return err_sys("asn test failed!\n", ret);
  746. else
  747. test_pass("asn test passed!\n");
  748. #endif
  749. #ifndef WC_NO_RNG
  750. if ( (ret = random_test()) != 0)
  751. return err_sys("RANDOM test failed!\n", ret);
  752. else
  753. test_pass("RANDOM test passed!\n");
  754. #endif /* WC_NO_RNG */
  755. #ifndef NO_MD5
  756. if ( (ret = md5_test()) != 0)
  757. return err_sys("MD5 test failed!\n", ret);
  758. else
  759. test_pass("MD5 test passed!\n");
  760. #endif
  761. #ifdef WOLFSSL_MD2
  762. if ( (ret = md2_test()) != 0)
  763. return err_sys("MD2 test failed!\n", ret);
  764. else
  765. test_pass("MD2 test passed!\n");
  766. #endif
  767. #ifndef NO_MD4
  768. if ( (ret = md4_test()) != 0)
  769. return err_sys("MD4 test failed!\n", ret);
  770. else
  771. test_pass("MD4 test passed!\n");
  772. #endif
  773. #ifndef NO_SHA
  774. if ( (ret = sha_test()) != 0)
  775. return err_sys("SHA test failed!\n", ret);
  776. else
  777. test_pass("SHA test passed!\n");
  778. #endif
  779. #ifdef WOLFSSL_SHA224
  780. if ( (ret = sha224_test()) != 0)
  781. return err_sys("SHA-224 test failed!\n", ret);
  782. else
  783. test_pass("SHA-224 test passed!\n");
  784. #endif
  785. #ifndef NO_SHA256
  786. if ( (ret = sha256_test()) != 0)
  787. return err_sys("SHA-256 test failed!\n", ret);
  788. else
  789. test_pass("SHA-256 test passed!\n");
  790. #endif
  791. #ifdef WOLFSSL_SHA384
  792. if ( (ret = sha384_test()) != 0)
  793. return err_sys("SHA-384 test failed!\n", ret);
  794. else
  795. test_pass("SHA-384 test passed!\n");
  796. #endif
  797. #ifdef WOLFSSL_SHA512
  798. if ( (ret = sha512_test()) != 0)
  799. return err_sys("SHA-512 test failed!\n", ret);
  800. else
  801. test_pass("SHA-512 test passed!\n");
  802. #endif
  803. #ifdef WOLFSSL_SHA3
  804. if ( (ret = sha3_test()) != 0)
  805. return err_sys("SHA-3 test failed!\n", ret);
  806. else
  807. test_pass("SHA-3 test passed!\n");
  808. #endif
  809. #ifdef WOLFSSL_SHAKE256
  810. if ( (ret = shake256_test()) != 0)
  811. return err_sys("SHAKE256 test failed!\n", ret);
  812. else
  813. test_pass("SHAKE256 test passed!\n");
  814. #endif
  815. #ifndef NO_HASH_WRAPPER
  816. if ( (ret = hash_test()) != 0)
  817. return err_sys("Hash test failed!\n", ret);
  818. else
  819. test_pass("Hash test passed!\n");
  820. #endif
  821. #ifdef WOLFSSL_RIPEMD
  822. if ( (ret = ripemd_test()) != 0)
  823. return err_sys("RIPEMD test failed!\n", ret);
  824. else
  825. test_pass("RIPEMD test passed!\n");
  826. #endif
  827. #ifdef HAVE_BLAKE2
  828. if ( (ret = blake2b_test()) != 0)
  829. return err_sys("BLAKE2b test failed!\n", ret);
  830. else
  831. test_pass("BLAKE2b test passed!\n");
  832. #endif
  833. #ifdef HAVE_BLAKE2S
  834. if ( (ret = blake2s_test()) != 0)
  835. return err_sys("BLAKE2s test failed!\n", ret);
  836. else
  837. test_pass("BLAKE2s test passed!\n");
  838. #endif
  839. #ifndef NO_HMAC
  840. #ifndef NO_MD5
  841. if ( (ret = hmac_md5_test()) != 0)
  842. return err_sys("HMAC-MD5 test failed!\n", ret);
  843. else
  844. test_pass("HMAC-MD5 test passed!\n");
  845. #endif
  846. #ifndef NO_SHA
  847. if ( (ret = hmac_sha_test()) != 0)
  848. return err_sys("HMAC-SHA test failed!\n", ret);
  849. else
  850. test_pass("HMAC-SHA test passed!\n");
  851. #endif
  852. #ifdef WOLFSSL_SHA224
  853. if ( (ret = hmac_sha224_test()) != 0)
  854. return err_sys("HMAC-SHA224 test failed!\n", ret);
  855. else
  856. test_pass("HMAC-SHA224 test passed!\n");
  857. #endif
  858. #ifndef NO_SHA256
  859. if ( (ret = hmac_sha256_test()) != 0)
  860. return err_sys("HMAC-SHA256 test failed!\n", ret);
  861. else
  862. test_pass("HMAC-SHA256 test passed!\n");
  863. #endif
  864. #ifdef WOLFSSL_SHA384
  865. if ( (ret = hmac_sha384_test()) != 0)
  866. return err_sys("HMAC-SHA384 test failed!\n", ret);
  867. else
  868. test_pass("HMAC-SHA384 test passed!\n");
  869. #endif
  870. #ifdef WOLFSSL_SHA512
  871. if ( (ret = hmac_sha512_test()) != 0)
  872. return err_sys("HMAC-SHA512 test failed!\n", ret);
  873. else
  874. test_pass("HMAC-SHA512 test passed!\n");
  875. #endif
  876. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  877. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  878. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  879. if ( (ret = hmac_sha3_test()) != 0)
  880. return err_sys("HMAC-SHA3 test failed!\n", ret);
  881. else
  882. test_pass("HMAC-SHA3 test passed!\n");
  883. #endif
  884. #ifdef HAVE_HKDF
  885. if ( (ret = hkdf_test()) != 0)
  886. return err_sys("HMAC-KDF test failed!\n", ret);
  887. else
  888. test_pass("HMAC-KDF test passed!\n");
  889. #endif
  890. #endif /* !NO_HMAC */
  891. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  892. if ( (ret = x963kdf_test()) != 0)
  893. return err_sys("X963-KDF test failed!\n", ret);
  894. else
  895. test_pass("X963-KDF test passed!\n");
  896. #endif
  897. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  898. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  899. if ( (ret = gmac_test()) != 0)
  900. return err_sys("GMAC test failed!\n", ret);
  901. else
  902. test_pass("GMAC test passed!\n");
  903. #endif
  904. #ifdef WC_RC2
  905. if ( (ret = rc2_test()) != 0)
  906. return err_sys("RC2 test failed!\n", ret);
  907. else
  908. test_pass("RC2 test passed!\n");
  909. #endif
  910. #ifndef NO_RC4
  911. if ( (ret = arc4_test()) != 0)
  912. return err_sys("ARC4 test failed!\n", ret);
  913. else
  914. test_pass("ARC4 test passed!\n");
  915. #endif
  916. #ifndef NO_HC128
  917. if ( (ret = hc128_test()) != 0)
  918. return err_sys("HC-128 test failed!\n", ret);
  919. else
  920. test_pass("HC-128 test passed!\n");
  921. #endif
  922. #ifndef NO_RABBIT
  923. if ( (ret = rabbit_test()) != 0)
  924. return err_sys("Rabbit test failed!\n", ret);
  925. else
  926. test_pass("Rabbit test passed!\n");
  927. #endif
  928. #ifdef HAVE_CHACHA
  929. if ( (ret = chacha_test()) != 0)
  930. return err_sys("Chacha test failed!\n", ret);
  931. else
  932. test_pass("Chacha test passed!\n");
  933. #endif
  934. #ifdef HAVE_XCHACHA
  935. if ( (ret = XChaCha_test()) != 0)
  936. return err_sys("XChacha test failed!\n", ret);
  937. else
  938. test_pass("XChacha test passed!\n");
  939. #endif
  940. #ifdef HAVE_POLY1305
  941. if ( (ret = poly1305_test()) != 0)
  942. return err_sys("POLY1305 test failed!\n", ret);
  943. else
  944. test_pass("POLY1305 test passed!\n");
  945. #endif
  946. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  947. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  948. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  949. else
  950. test_pass("ChaCha20-Poly1305 AEAD test passed!\n");
  951. #endif
  952. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  953. if ( (ret = XChaCha20Poly1305_test()) != 0)
  954. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  955. else
  956. test_pass("XChaCha20-Poly1305 AEAD test passed!\n");
  957. #endif
  958. #ifndef NO_DES3
  959. if ( (ret = des_test()) != 0)
  960. return err_sys("DES test failed!\n", ret);
  961. else
  962. test_pass("DES test passed!\n");
  963. #endif
  964. #ifndef NO_DES3
  965. if ( (ret = des3_test()) != 0)
  966. return err_sys("DES3 test failed!\n", ret);
  967. else
  968. test_pass("DES3 test passed!\n");
  969. #endif
  970. #ifndef NO_AES
  971. if ( (ret = aes_test()) != 0)
  972. return err_sys("AES test failed!\n", ret);
  973. else
  974. test_pass("AES test passed!\n");
  975. #ifdef WOLFSSL_AES_192
  976. if ( (ret = aes192_test()) != 0)
  977. return err_sys("AES192 test failed!\n", ret);
  978. else
  979. test_pass("AES192 test passed!\n");
  980. #endif
  981. #ifdef WOLFSSL_AES_256
  982. if ( (ret = aes256_test()) != 0)
  983. return err_sys("AES256 test failed!\n", ret);
  984. else
  985. test_pass("AES256 test passed!\n");
  986. #endif
  987. #ifdef WOLFSSL_AES_OFB
  988. if ( (ret = aesofb_test()) != 0)
  989. return err_sys("AES-OFB test failed!\n", ret);
  990. else
  991. test_pass("AESOFB test passed!\n");
  992. #endif
  993. #ifdef HAVE_AESGCM
  994. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  995. if ( (ret = aesgcm_test()) != 0)
  996. return err_sys("AES-GCM test failed!\n", ret);
  997. #endif
  998. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  999. !(defined(WOLF_CRYPTO_CB) && \
  1000. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1001. if ((ret = aesgcm_default_test()) != 0) {
  1002. return err_sys("AES-GCM test failed!\n", ret);
  1003. }
  1004. #endif
  1005. test_pass("AES-GCM test passed!\n");
  1006. #endif
  1007. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1008. if ( (ret = aesccm_test()) != 0)
  1009. return err_sys("AES-CCM test failed!\n", ret);
  1010. else
  1011. test_pass("AES-CCM test passed!\n");
  1012. #endif
  1013. #ifdef HAVE_AES_KEYWRAP
  1014. if ( (ret = aeskeywrap_test()) != 0)
  1015. return err_sys("AES Key Wrap test failed!\n", ret);
  1016. else
  1017. test_pass("AES Key Wrap test passed!\n");
  1018. #endif
  1019. #endif
  1020. #ifdef HAVE_CAMELLIA
  1021. if ( (ret = camellia_test()) != 0)
  1022. return err_sys("CAMELLIA test failed!\n", ret);
  1023. else
  1024. test_pass("CAMELLIA test passed!\n");
  1025. #endif
  1026. #ifdef HAVE_IDEA
  1027. if ( (ret = idea_test()) != 0)
  1028. return err_sys("IDEA test failed!\n", ret);
  1029. else
  1030. test_pass("IDEA test passed!\n");
  1031. #endif
  1032. #ifndef NO_RSA
  1033. #ifdef WC_RSA_NO_PADDING
  1034. if ( (ret = rsa_no_pad_test()) != 0)
  1035. return err_sys("RSA NOPAD test failed!\n", ret);
  1036. else
  1037. test_pass("RSA NOPAD test passed!\n");
  1038. #endif
  1039. if ( (ret = rsa_test()) != 0)
  1040. return err_sys("RSA test failed!\n", ret);
  1041. else
  1042. test_pass("RSA test passed!\n");
  1043. #endif
  1044. #ifndef NO_DH
  1045. if ( (ret = dh_test()) != 0)
  1046. return err_sys("DH test failed!\n", ret);
  1047. else
  1048. test_pass("DH test passed!\n");
  1049. #endif
  1050. #ifndef NO_DSA
  1051. if ( (ret = dsa_test()) != 0)
  1052. return err_sys("DSA test failed!\n", ret);
  1053. else
  1054. test_pass("DSA test passed!\n");
  1055. #endif
  1056. #ifdef WOLFCRYPT_HAVE_SRP
  1057. if ( (ret = srp_test()) != 0)
  1058. return err_sys("SRP test failed!\n", ret);
  1059. else
  1060. test_pass("SRP test passed!\n");
  1061. #endif
  1062. #ifndef NO_PWDBASED
  1063. if ( (ret = pwdbased_test()) != 0)
  1064. return err_sys("PWDBASED test failed!\n", ret);
  1065. else
  1066. test_pass("PWDBASED test passed!\n");
  1067. #endif
  1068. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1069. if ( (ret = openssl_test()) != 0)
  1070. return err_sys("OPENSSL test failed!\n", ret);
  1071. else
  1072. test_pass("OPENSSL test passed!\n");
  1073. if ( (ret = openSSL_evpMD_test()) != 0)
  1074. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1075. else
  1076. test_pass("OPENSSL (EVP MD) passed!\n");
  1077. if ( (ret = openssl_pkey0_test()) != 0)
  1078. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1079. else
  1080. test_pass("OPENSSL (PKEY0) passed!\n");
  1081. if ( (ret = openssl_pkey1_test()) != 0)
  1082. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1083. else
  1084. test_pass("OPENSSL (PKEY1) passed!\n");
  1085. if ( (ret = openssl_evpSig_test()) != 0)
  1086. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1087. else
  1088. test_pass("OPENSSL (EVP Sign/Verify) passed!\n");
  1089. #endif
  1090. #ifdef HAVE_ECC
  1091. if ( (ret = ecc_test()) != 0)
  1092. return err_sys("ECC test failed!\n", ret);
  1093. else
  1094. test_pass("ECC test passed!\n");
  1095. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1096. defined(WOLFSSL_AES_128)
  1097. if ( (ret = ecc_encrypt_test()) != 0)
  1098. return err_sys("ECC Enc test failed!\n", ret);
  1099. else
  1100. test_pass("ECC Enc test passed!\n");
  1101. #endif
  1102. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1103. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1104. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  1105. /* skip for ATECC508/608A, cannot import private key buffers */
  1106. if ( (ret = ecc_test_buffers()) != 0)
  1107. return err_sys("ECC buffer test failed!\n", ret);
  1108. else
  1109. test_pass("ECC buffer test passed!\n");
  1110. #endif
  1111. #endif
  1112. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1113. !defined(NO_FILESYSTEM)
  1114. if ( (ret = cert_test()) != 0)
  1115. return err_sys("CERT test failed!\n", ret);
  1116. else
  1117. test_pass("CERT test passed!\n");
  1118. #endif
  1119. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1120. !defined(NO_FILESYSTEM)
  1121. if ( (ret = certext_test()) != 0)
  1122. return err_sys("CERT EXT test failed!\n", ret);
  1123. else
  1124. test_pass("CERT EXT test passed!\n");
  1125. #endif
  1126. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1127. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1128. if ( (ret = decodedCertCache_test()) != 0)
  1129. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1130. else
  1131. test_pass("DECODED CERT CACHE test passed!\n");
  1132. #endif
  1133. #ifdef HAVE_CURVE25519
  1134. if ( (ret = curve25519_test()) != 0)
  1135. return err_sys("CURVE25519 test failed!\n", ret);
  1136. else
  1137. test_pass("CURVE25519 test passed!\n");
  1138. #endif
  1139. #ifdef HAVE_ED25519
  1140. if ( (ret = ed25519_test()) != 0)
  1141. return err_sys("ED25519 test failed!\n", ret);
  1142. else
  1143. test_pass("ED25519 test passed!\n");
  1144. #endif
  1145. #ifdef HAVE_CURVE448
  1146. if ( (ret = curve448_test()) != 0)
  1147. return err_sys("CURVE448 test failed!\n", ret);
  1148. else
  1149. test_pass("CURVE448 test passed!\n");
  1150. #endif
  1151. #ifdef HAVE_ED448
  1152. if ( (ret = ed448_test()) != 0)
  1153. return err_sys("ED448 test failed!\n", ret);
  1154. else
  1155. test_pass("ED448 test passed!\n");
  1156. #endif
  1157. #ifdef WOLFCRYPT_HAVE_ECCSI
  1158. if ( (ret = eccsi_test()) != 0)
  1159. return err_sys("ECCSI test failed!\n", ret);
  1160. else
  1161. test_pass("ECCSI test passed!\n");
  1162. #endif
  1163. #ifdef WOLFCRYPT_HAVE_SAKKE
  1164. if ( (ret = sakke_test()) != 0)
  1165. return err_sys("SAKKE test failed!\n", ret);
  1166. else
  1167. test_pass("SAKKE test passed!\n");
  1168. #endif
  1169. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1170. if ( (ret = cmac_test()) != 0)
  1171. return err_sys("CMAC test failed!\n", ret);
  1172. else
  1173. test_pass("CMAC test passed!\n");
  1174. #endif
  1175. #ifdef HAVE_LIBZ
  1176. if ( (ret = compress_test()) != 0)
  1177. return err_sys("COMPRESS test failed!\n", ret);
  1178. else
  1179. test_pass("COMPRESS test passed!\n");
  1180. #endif
  1181. #ifdef HAVE_PKCS7
  1182. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1183. if ( (ret = pkcs7encrypted_test()) != 0)
  1184. return err_sys("PKCS7encrypted test failed!\n", ret);
  1185. else
  1186. test_pass("PKCS7encrypted test passed!\n");
  1187. #endif
  1188. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1189. if ( (ret = pkcs7compressed_test()) != 0)
  1190. return err_sys("PKCS7compressed test failed!\n", ret);
  1191. else
  1192. test_pass("PKCS7compressed test passed!\n");
  1193. #endif
  1194. if ( (ret = pkcs7signed_test()) != 0)
  1195. return err_sys("PKCS7signed test failed!\n", ret);
  1196. else
  1197. test_pass("PKCS7signed test passed!\n");
  1198. if ( (ret = pkcs7enveloped_test()) != 0)
  1199. return err_sys("PKCS7enveloped test failed!\n", ret);
  1200. else
  1201. test_pass("PKCS7enveloped test passed!\n");
  1202. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1203. if ( (ret = pkcs7authenveloped_test()) != 0)
  1204. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1205. else
  1206. test_pass("PKCS7authenveloped test passed!\n");
  1207. #endif
  1208. #endif
  1209. #ifdef HAVE_VALGRIND
  1210. if ( (ret = mp_test()) != 0)
  1211. return err_sys("mp test failed!\n", ret);
  1212. else
  1213. test_pass("mp test passed!\n");
  1214. #endif
  1215. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1216. if ( (ret = prime_test()) != 0)
  1217. return err_sys("prime test failed!\n", ret);
  1218. else
  1219. test_pass("prime test passed!\n");
  1220. #endif
  1221. #if defined(ASN_BER_TO_DER) && \
  1222. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1223. defined(OPENSSL_EXTRA_X509_SMALL))
  1224. if ( (ret = berder_test()) != 0)
  1225. return err_sys("ber-der test failed!\n", ret);
  1226. else
  1227. test_pass("ber-der test passed!\n");
  1228. #endif
  1229. if ( (ret = logging_test()) != 0)
  1230. return err_sys("logging test failed!\n", ret);
  1231. else
  1232. test_pass("logging test passed!\n");
  1233. if ( (ret = mutex_test()) != 0)
  1234. return err_sys("mutex test failed!\n", ret);
  1235. else
  1236. test_pass("mutex test passed!\n");
  1237. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1238. if ( (ret = memcb_test()) != 0)
  1239. return err_sys("memcb test failed!\n", ret);
  1240. else
  1241. test_pass("memcb test passed!\n");
  1242. #endif
  1243. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1244. if ( (ret = blob_test()) != 0)
  1245. return err_sys("blob test failed!\n", ret);
  1246. else
  1247. test_pass("blob test passed!\n");
  1248. #endif
  1249. #if defined(WOLF_CRYPTO_CB) && \
  1250. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1251. defined(WOLFSSL_QNX_CAAM))
  1252. if ( (ret = cryptocb_test()) != 0)
  1253. return err_sys("crypto callback test failed!\n", ret);
  1254. else
  1255. test_pass("crypto callback test passed!\n");
  1256. #endif
  1257. #ifdef WOLFSSL_CERT_PIV
  1258. if ( (ret = certpiv_test()) != 0)
  1259. return err_sys("cert piv test failed!\n", ret);
  1260. else
  1261. test_pass("cert piv test passed!\n");
  1262. #endif
  1263. #ifdef WOLF_CRYPTO_CB
  1264. #ifdef HAVE_INTEL_QA_SYNC
  1265. wc_CryptoCb_CleanupIntelQa(&devId);
  1266. #endif
  1267. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1268. wc_CryptoCb_CleanupOcteon(&devId);
  1269. #endif
  1270. #endif
  1271. #ifdef WOLFSSL_ASYNC_CRYPT
  1272. wolfAsync_DevClose(&devId);
  1273. #endif
  1274. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1275. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1276. wc_ecc_fp_free();
  1277. #endif
  1278. if (args)
  1279. ((func_args*)args)->return_code = ret;
  1280. test_pass("Test complete\n");
  1281. EXIT_TEST(ret);
  1282. }
  1283. #ifndef NO_MAIN_DRIVER
  1284. /* so overall tests can pull in test function */
  1285. #ifdef WOLFSSL_ESPIDF
  1286. void app_main( )
  1287. #else
  1288. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1289. int myoptind = 0;
  1290. char* myoptarg = NULL;
  1291. #endif
  1292. int main(int argc, char** argv)
  1293. #endif
  1294. {
  1295. int ret;
  1296. func_args args;
  1297. #ifdef WOLFSSL_ESPIDF
  1298. /* set dummy wallclock time. */
  1299. struct timeval utctime;
  1300. struct timezone tz;
  1301. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1302. utctime.tv_usec = 0;
  1303. tz.tz_minuteswest = 0;
  1304. tz.tz_dsttime = 0;
  1305. settimeofday(&utctime, &tz);
  1306. #endif
  1307. #ifdef WOLFSSL_APACHE_MYNEWT
  1308. #ifdef ARCH_sim
  1309. mcu_sim_parse_args(argc, argv);
  1310. #endif
  1311. sysinit();
  1312. /* set dummy wallclock time. */
  1313. struct os_timeval utctime;
  1314. struct os_timezone tz;
  1315. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1316. utctime.tv_usec = 0;
  1317. tz.tz_minuteswest = 0;
  1318. tz.tz_dsttime = 0;
  1319. os_settimeofday(&utctime, &tz);
  1320. #endif
  1321. #ifdef DEVKITPRO
  1322. void *framebuffer;
  1323. GXRModeObj *rmode = NULL;
  1324. VIDEO_Init();
  1325. WPAD_Init();
  1326. rmode = VIDEO_GetPreferredMode(NULL);
  1327. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1328. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1329. #pragma GCC diagnostic pop
  1330. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1331. VIDEO_Configure(rmode);
  1332. VIDEO_SetNextFramebuffer(framebuffer);
  1333. VIDEO_SetBlack(FALSE);
  1334. VIDEO_Flush();
  1335. VIDEO_WaitVSync();
  1336. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1337. #endif
  1338. #ifdef HAVE_WNR
  1339. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1340. err_sys("Whitewood netRandom global config failed", -1001);
  1341. return -1002;
  1342. }
  1343. #endif
  1344. #ifndef WOLFSSL_ESPIDF
  1345. args.argc = argc;
  1346. args.argv = argv;
  1347. #endif
  1348. if ((ret = wolfCrypt_Init()) != 0) {
  1349. printf("wolfCrypt_Init failed %d\n", ret);
  1350. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1351. }
  1352. #ifdef HAVE_STACK_SIZE
  1353. StackSizeCheck(&args, wolfcrypt_test);
  1354. #else
  1355. wolfcrypt_test(&args);
  1356. #endif
  1357. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1358. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1359. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1360. }
  1361. #ifdef HAVE_WNR
  1362. if (wc_FreeNetRandom() < 0)
  1363. err_sys("Failed to free netRandom context", -1005);
  1364. #endif /* HAVE_WNR */
  1365. #ifdef DOLPHIN_EMULATOR
  1366. /* Returning from main panics the emulator. Just hang
  1367. * and let the user force quit the emulator window. */
  1368. printf("args.return_code: %d\n", args.return_code);
  1369. printf("Testing complete. You may close the window now\n");
  1370. while (1);
  1371. #endif
  1372. #ifndef WOLFSSL_ESPIDF
  1373. printf("Exiting main with return code: %d\n", args.return_code);
  1374. return args.return_code;
  1375. #endif
  1376. }
  1377. #endif /* NO_MAIN_DRIVER */
  1378. /* helper to save DER, convert to PEM and save PEM */
  1379. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1380. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))))
  1381. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1382. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1383. #else
  1384. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1385. #endif
  1386. static int _SaveDerAndPem(const byte* der, int derSz,
  1387. const char* fileDer, const char* filePem, int pemType, int errBase)
  1388. {
  1389. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1390. int ret;
  1391. XFILE derFile;
  1392. derFile = XFOPEN(fileDer, "wb");
  1393. if (!derFile) {
  1394. return errBase + 0;
  1395. }
  1396. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1397. XFCLOSE(derFile);
  1398. if (ret != derSz) {
  1399. return errBase + 1;
  1400. }
  1401. #endif
  1402. #ifdef WOLFSSL_DER_TO_PEM
  1403. if (filePem) {
  1404. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1405. XFILE pemFile;
  1406. #endif
  1407. byte* pem;
  1408. int pemSz;
  1409. /* calculate PEM size */
  1410. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1411. if (pemSz < 0) {
  1412. return pemSz;
  1413. }
  1414. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1415. if (pem == NULL) {
  1416. return MEMORY_E;
  1417. }
  1418. /* Convert to PEM */
  1419. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1420. if (pemSz < 0) {
  1421. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1422. return errBase + 2;
  1423. }
  1424. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1425. pemFile = XFOPEN(filePem, "wb");
  1426. if (!pemFile) {
  1427. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1428. return errBase + 3;
  1429. }
  1430. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1431. XFCLOSE(pemFile);
  1432. if (ret != pemSz) {
  1433. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1434. return errBase + 4;
  1435. }
  1436. #endif
  1437. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1438. }
  1439. #endif /* WOLFSSL_DER_TO_PEM */
  1440. /* suppress unused variable warnings */
  1441. (void)der;
  1442. (void)derSz;
  1443. (void)filePem;
  1444. (void)fileDer;
  1445. (void)pemType;
  1446. (void)errBase;
  1447. return 0;
  1448. }
  1449. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1450. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1451. {
  1452. const char* errStr;
  1453. char out[WOLFSSL_MAX_ERROR_SZ];
  1454. const char* unknownStr = wc_GetErrorString(0);
  1455. #ifdef NO_ERROR_STRINGS
  1456. /* Ensure a valid error code's string matches an invalid code's.
  1457. * The string is that error strings are not available.
  1458. */
  1459. errStr = wc_GetErrorString(OPEN_RAN_E);
  1460. wc_ErrorString(OPEN_RAN_E, out);
  1461. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1462. return -1100;
  1463. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1464. return -1101;
  1465. #else
  1466. int i;
  1467. int j = 0;
  1468. /* Values that are not or no longer error codes. */
  1469. int missing[] = { -122, -123, -124, -127, -128, -129,
  1470. -163, -164, -165, -166, -167, -168, -169,
  1471. -233,
  1472. 0 };
  1473. /* Check that all errors have a string and it's the same through the two
  1474. * APIs. Check that the values that are not errors map to the unknown
  1475. * string.
  1476. */
  1477. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1478. errStr = wc_GetErrorString(i);
  1479. wc_ErrorString(i, out);
  1480. if (i != missing[j]) {
  1481. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  1482. return -1102;
  1483. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  1484. return -1103;
  1485. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  1486. return -1104;
  1487. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1488. return -1105;
  1489. }
  1490. else {
  1491. j++;
  1492. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1493. return -1106;
  1494. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1495. return -1107;
  1496. }
  1497. }
  1498. /* Check if the next possible value has been given a string. */
  1499. errStr = wc_GetErrorString(i);
  1500. wc_ErrorString(i, out);
  1501. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1502. return -1108;
  1503. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1504. return -1109;
  1505. #endif
  1506. return 0;
  1507. }
  1508. #ifndef NO_CODING
  1509. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1510. {
  1511. int ret;
  1512. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1513. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1514. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1515. byte out[128];
  1516. word32 outLen;
  1517. #ifdef WOLFSSL_BASE64_ENCODE
  1518. byte data[3];
  1519. word32 dataLen;
  1520. byte longData[79] = { 0 };
  1521. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1522. #endif
  1523. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1524. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1525. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1526. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1527. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1528. byte goodChar[] =
  1529. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1530. "abcdefghijklmnopqrstuvwxyz"
  1531. "0123456789+/;";
  1532. byte charTest[] = "A+Gd\0\0\0";
  1533. int i;
  1534. /* Good Base64 encodings. */
  1535. outLen = sizeof(out);
  1536. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1537. if (ret != 0)
  1538. return -1200;
  1539. outLen = sizeof(out);
  1540. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1541. if (ret != 0)
  1542. return -1201;
  1543. outLen = sizeof(goodChar);
  1544. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1545. if (ret != 0)
  1546. return -1235;
  1547. if (outLen != 64 / 4 * 3)
  1548. return -1236;
  1549. outLen = sizeof(out);
  1550. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1551. if (ret != 0)
  1552. return -1201;
  1553. /* Bad parameters. */
  1554. outLen = 1;
  1555. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1556. if (ret != BAD_FUNC_ARG)
  1557. return -1202;
  1558. outLen = sizeof(out);
  1559. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1560. if (ret != ASN_INPUT_E)
  1561. return -1203;
  1562. outLen = sizeof(out);
  1563. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1564. if (ret != ASN_INPUT_E)
  1565. return -1203;
  1566. /* Bad character at each offset 0-3. */
  1567. for (i = 0; i < 4; i++) {
  1568. outLen = sizeof(out);
  1569. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1570. if (ret != ASN_INPUT_E)
  1571. return -1204 - i;
  1572. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1573. if (ret != ASN_INPUT_E)
  1574. return -1214 - i;
  1575. }
  1576. /* Invalid character less than 0x2b */
  1577. for (i = 1; i < 0x2b; i++) {
  1578. outLen = sizeof(out);
  1579. charTest[0] = i;
  1580. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1581. if (ret != ASN_INPUT_E)
  1582. return -1240 - i;
  1583. }
  1584. /* Bad characters in range 0x2b - 0x7a. */
  1585. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1586. outLen = sizeof(out);
  1587. charTest[0] = badChar[i];
  1588. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1589. if (ret != ASN_INPUT_E)
  1590. return -1270 - i;
  1591. }
  1592. /* Invalid character greater than 0x7a */
  1593. for (i = 0x7b; i < 0x100; i++) {
  1594. outLen = sizeof(out);
  1595. charTest[0] = i;
  1596. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1597. if (ret != ASN_INPUT_E)
  1598. return -1290 - i;
  1599. }
  1600. #ifdef WOLFSSL_BASE64_ENCODE
  1601. /* Decode and encode all symbols - non-alphanumeric. */
  1602. dataLen = sizeof(data);
  1603. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1604. if (ret != 0)
  1605. return -1224;
  1606. outLen = sizeof(out);
  1607. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1608. if (ret != LENGTH_ONLY_E)
  1609. return -1225;
  1610. outLen = sizeof(out);
  1611. ret = Base64_Encode(data, dataLen, out, &outLen);
  1612. if (ret != 0)
  1613. return -1226;
  1614. outLen = 7;
  1615. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1616. if (ret != BUFFER_E)
  1617. return -1227;
  1618. outLen = sizeof(out);
  1619. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1620. if (ret != LENGTH_ONLY_E)
  1621. return -1228;
  1622. outLen = sizeof(out);
  1623. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1624. if (ret != 0)
  1625. return -1229;
  1626. outLen = sizeof(out);
  1627. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1628. if (ret != 0)
  1629. return -1230;
  1630. /* Data that results in an encoding longer than one line. */
  1631. outLen = sizeof(out);
  1632. dataLen = sizeof(longData);
  1633. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1634. if (ret != 0)
  1635. return -1231;
  1636. outLen = sizeof(out);
  1637. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1638. if (ret != 0)
  1639. return -1232;
  1640. outLen = sizeof(out);
  1641. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1642. if (ret != 0)
  1643. return -1233;
  1644. #endif
  1645. return 0;
  1646. }
  1647. #ifdef WOLFSSL_BASE16
  1648. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1649. {
  1650. int ret;
  1651. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1652. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1653. byte encoded[40];
  1654. word32 encodedLen;
  1655. byte plain[40];
  1656. word32 len;
  1657. /* length returned includes null termination */
  1658. encodedLen = sizeof(encoded);
  1659. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1660. if (ret != 0)
  1661. return -1300;
  1662. len = (word32)XSTRLEN((char*)encoded);
  1663. if (len != encodedLen - 1)
  1664. return -1301;
  1665. len = sizeof(plain);
  1666. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1667. if (ret != 0)
  1668. return -1302;
  1669. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1670. return -1303;
  1671. if (encodedLen != sizeof(encodedTestData) ||
  1672. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1673. return -1304;
  1674. }
  1675. return 0;
  1676. }
  1677. #endif /* WOLFSSL_BASE16 */
  1678. #endif /* !NO_CODING */
  1679. #ifndef NO_ASN
  1680. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1681. {
  1682. int ret;
  1683. /* ASN1 encoded date buffer */
  1684. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1685. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1686. byte format;
  1687. int length;
  1688. const byte* datePart;
  1689. #ifndef NO_ASN_TIME
  1690. struct tm timearg;
  1691. time_t now;
  1692. #endif
  1693. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1694. &length);
  1695. if (ret != 0)
  1696. return -1400;
  1697. #ifndef NO_ASN_TIME
  1698. /* Parameter Validation tests. */
  1699. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1700. return -1401;
  1701. if (wc_GetTime(&now, 0) != BUFFER_E)
  1702. return -1402;
  1703. now = 0;
  1704. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1705. return -1403;
  1706. }
  1707. if (now == 0) {
  1708. printf("RTC/Time not set!\n");
  1709. return -1404;
  1710. }
  1711. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1712. if (ret != 0)
  1713. return -1405;
  1714. #endif /* !NO_ASN_TIME */
  1715. return 0;
  1716. }
  1717. #endif /* !NO_ASN */
  1718. #ifdef WOLFSSL_MD2
  1719. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1720. {
  1721. int ret = 0;
  1722. Md2 md2;
  1723. byte hash[MD2_DIGEST_SIZE];
  1724. testVector a, b, c, d, e, f, g;
  1725. testVector test_md2[7];
  1726. int times = sizeof(test_md2) / sizeof(testVector), i;
  1727. a.input = "";
  1728. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1729. "\x27\x73";
  1730. a.inLen = XSTRLEN(a.input);
  1731. a.outLen = MD2_DIGEST_SIZE;
  1732. b.input = "a";
  1733. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1734. "\xb5\xd1";
  1735. b.inLen = XSTRLEN(b.input);
  1736. b.outLen = MD2_DIGEST_SIZE;
  1737. c.input = "abc";
  1738. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1739. "\xd6\xbb";
  1740. c.inLen = XSTRLEN(c.input);
  1741. c.outLen = MD2_DIGEST_SIZE;
  1742. d.input = "message digest";
  1743. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1744. "\x06\xb0";
  1745. d.inLen = XSTRLEN(d.input);
  1746. d.outLen = MD2_DIGEST_SIZE;
  1747. e.input = "abcdefghijklmnopqrstuvwxyz";
  1748. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1749. "\x94\x0b";
  1750. e.inLen = XSTRLEN(e.input);
  1751. e.outLen = MD2_DIGEST_SIZE;
  1752. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1753. "6789";
  1754. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1755. "\x38\xcd";
  1756. f.inLen = XSTRLEN(f.input);
  1757. f.outLen = MD2_DIGEST_SIZE;
  1758. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1759. "9012345678901234567890";
  1760. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1761. "\xef\xd8";
  1762. g.inLen = XSTRLEN(g.input);
  1763. g.outLen = MD2_DIGEST_SIZE;
  1764. test_md2[0] = a;
  1765. test_md2[1] = b;
  1766. test_md2[2] = c;
  1767. test_md2[3] = d;
  1768. test_md2[4] = e;
  1769. test_md2[5] = f;
  1770. test_md2[6] = g;
  1771. wc_InitMd2(&md2);
  1772. for (i = 0; i < times; ++i) {
  1773. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1774. wc_Md2Final(&md2, hash);
  1775. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1776. return -1500 - i;
  1777. }
  1778. for (i = 0; i < times; ++i) {
  1779. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1780. if (ret != 0) {
  1781. return -1507 - i;
  1782. }
  1783. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1784. return -1507 - i;
  1785. }
  1786. }
  1787. return 0;
  1788. }
  1789. #endif
  1790. #ifndef NO_MD5
  1791. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1792. {
  1793. int ret = 0;
  1794. wc_Md5 md5, md5Copy;
  1795. byte hash[WC_MD5_DIGEST_SIZE];
  1796. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1797. testVector a, b, c, d, e, f;
  1798. testVector test_md5[6];
  1799. int times = sizeof(test_md5) / sizeof(testVector), i;
  1800. a.input = "";
  1801. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1802. "\x7e";
  1803. a.inLen = XSTRLEN(a.input);
  1804. a.outLen = WC_MD5_DIGEST_SIZE;
  1805. b.input = "abc";
  1806. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1807. "\x72";
  1808. b.inLen = XSTRLEN(b.input);
  1809. b.outLen = WC_MD5_DIGEST_SIZE;
  1810. c.input = "message digest";
  1811. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1812. "\xd0";
  1813. c.inLen = XSTRLEN(c.input);
  1814. c.outLen = WC_MD5_DIGEST_SIZE;
  1815. d.input = "abcdefghijklmnopqrstuvwxyz";
  1816. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1817. "\x3b";
  1818. d.inLen = XSTRLEN(d.input);
  1819. d.outLen = WC_MD5_DIGEST_SIZE;
  1820. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1821. "6789";
  1822. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1823. "\x9f";
  1824. e.inLen = XSTRLEN(e.input);
  1825. e.outLen = WC_MD5_DIGEST_SIZE;
  1826. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1827. "9012345678901234567890";
  1828. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1829. "\x7a";
  1830. f.inLen = XSTRLEN(f.input);
  1831. f.outLen = WC_MD5_DIGEST_SIZE;
  1832. test_md5[0] = a;
  1833. test_md5[1] = b;
  1834. test_md5[2] = c;
  1835. test_md5[3] = d;
  1836. test_md5[4] = e;
  1837. test_md5[5] = f;
  1838. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1839. if (ret != 0)
  1840. return -1600;
  1841. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1842. if (ret != 0) {
  1843. wc_Md5Free(&md5);
  1844. return -1601;
  1845. }
  1846. for (i = 0; i < times; ++i) {
  1847. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1848. (word32)test_md5[i].inLen);
  1849. if (ret != 0)
  1850. ERROR_OUT(-1602 - i, exit);
  1851. ret = wc_Md5GetHash(&md5, hashcopy);
  1852. if (ret != 0)
  1853. ERROR_OUT(-1603 - i, exit);
  1854. ret = wc_Md5Copy(&md5, &md5Copy);
  1855. if (ret != 0)
  1856. ERROR_OUT(-1604 - i, exit);
  1857. ret = wc_Md5Final(&md5, hash);
  1858. if (ret != 0)
  1859. ERROR_OUT(-1605 - i, exit);
  1860. wc_Md5Free(&md5Copy);
  1861. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1862. ERROR_OUT(-1606 - i, exit);
  1863. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1864. ERROR_OUT(-1607 - i, exit);
  1865. }
  1866. /* BEGIN LARGE HASH TEST */ {
  1867. byte large_input[1024];
  1868. const char* large_digest =
  1869. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1870. for (i = 0; i < (int)sizeof(large_input); i++) {
  1871. large_input[i] = (byte)(i & 0xFF);
  1872. }
  1873. times = 100;
  1874. #ifdef WOLFSSL_PIC32MZ_HASH
  1875. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1876. #endif
  1877. for (i = 0; i < times; ++i) {
  1878. ret = wc_Md5Update(&md5, (byte*)large_input,
  1879. (word32)sizeof(large_input));
  1880. if (ret != 0)
  1881. ERROR_OUT(-1608, exit);
  1882. }
  1883. ret = wc_Md5Final(&md5, hash);
  1884. if (ret != 0)
  1885. ERROR_OUT(-1609, exit);
  1886. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1887. ERROR_OUT(-1610, exit);
  1888. } /* END LARGE HASH TEST */
  1889. exit:
  1890. wc_Md5Free(&md5);
  1891. wc_Md5Free(&md5Copy);
  1892. return ret;
  1893. }
  1894. #endif /* NO_MD5 */
  1895. #ifndef NO_MD4
  1896. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  1897. {
  1898. Md4 md4;
  1899. byte hash[MD4_DIGEST_SIZE];
  1900. testVector a, b, c, d, e, f, g;
  1901. testVector test_md4[7];
  1902. int times = sizeof(test_md4) / sizeof(testVector), i;
  1903. a.input = "";
  1904. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1905. "\xc0";
  1906. a.inLen = XSTRLEN(a.input);
  1907. a.outLen = MD4_DIGEST_SIZE;
  1908. b.input = "a";
  1909. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1910. "\x24";
  1911. b.inLen = XSTRLEN(b.input);
  1912. b.outLen = MD4_DIGEST_SIZE;
  1913. c.input = "abc";
  1914. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1915. "\x9d";
  1916. c.inLen = XSTRLEN(c.input);
  1917. c.outLen = MD4_DIGEST_SIZE;
  1918. d.input = "message digest";
  1919. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1920. "\x4b";
  1921. d.inLen = XSTRLEN(d.input);
  1922. d.outLen = MD4_DIGEST_SIZE;
  1923. e.input = "abcdefghijklmnopqrstuvwxyz";
  1924. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1925. "\xa9";
  1926. e.inLen = XSTRLEN(e.input);
  1927. e.outLen = MD4_DIGEST_SIZE;
  1928. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1929. "6789";
  1930. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1931. "\xe4";
  1932. f.inLen = XSTRLEN(f.input);
  1933. f.outLen = MD4_DIGEST_SIZE;
  1934. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1935. "9012345678901234567890";
  1936. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1937. "\x36";
  1938. g.inLen = XSTRLEN(g.input);
  1939. g.outLen = MD4_DIGEST_SIZE;
  1940. test_md4[0] = a;
  1941. test_md4[1] = b;
  1942. test_md4[2] = c;
  1943. test_md4[3] = d;
  1944. test_md4[4] = e;
  1945. test_md4[5] = f;
  1946. test_md4[6] = g;
  1947. wc_InitMd4(&md4);
  1948. for (i = 0; i < times; ++i) {
  1949. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1950. wc_Md4Final(&md4, hash);
  1951. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1952. return -1700 - i;
  1953. }
  1954. return 0;
  1955. }
  1956. #endif /* NO_MD4 */
  1957. #ifndef NO_SHA
  1958. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  1959. {
  1960. int ret = 0;
  1961. wc_Sha sha, shaCopy;
  1962. byte hash[WC_SHA_DIGEST_SIZE];
  1963. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1964. testVector a, b, c, d, e;
  1965. testVector test_sha[5];
  1966. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1967. a.input = "";
  1968. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1969. "\x90\xaf\xd8\x07\x09";
  1970. a.inLen = XSTRLEN(a.input);
  1971. a.outLen = WC_SHA_DIGEST_SIZE;
  1972. b.input = "abc";
  1973. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1974. "\x6C\x9C\xD0\xD8\x9D";
  1975. b.inLen = XSTRLEN(b.input);
  1976. b.outLen = WC_SHA_DIGEST_SIZE;
  1977. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1978. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1979. "\xE5\xE5\x46\x70\xF1";
  1980. c.inLen = XSTRLEN(c.input);
  1981. c.outLen = WC_SHA_DIGEST_SIZE;
  1982. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1983. "aaaaaa";
  1984. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1985. "\x2A\x25\xEC\x64\x4D";
  1986. d.inLen = XSTRLEN(d.input);
  1987. d.outLen = WC_SHA_DIGEST_SIZE;
  1988. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1989. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1990. "aaaaaaaaaa";
  1991. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1992. "\x53\x99\x5E\x26\xA0";
  1993. e.inLen = XSTRLEN(e.input);
  1994. e.outLen = WC_SHA_DIGEST_SIZE;
  1995. test_sha[0] = a;
  1996. test_sha[1] = b;
  1997. test_sha[2] = c;
  1998. test_sha[3] = d;
  1999. test_sha[4] = e;
  2000. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2001. if (ret != 0)
  2002. return -1800;
  2003. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2004. if (ret != 0) {
  2005. wc_ShaFree(&sha);
  2006. return -1801;
  2007. }
  2008. for (i = 0; i < times; ++i) {
  2009. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2010. (word32)test_sha[i].inLen);
  2011. if (ret != 0)
  2012. ERROR_OUT(-1802 - i, exit);
  2013. ret = wc_ShaGetHash(&sha, hashcopy);
  2014. if (ret != 0)
  2015. ERROR_OUT(-1803 - i, exit);
  2016. ret = wc_ShaCopy(&sha, &shaCopy);
  2017. if (ret != 0)
  2018. ERROR_OUT(-1804 - i, exit);
  2019. ret = wc_ShaFinal(&sha, hash);
  2020. if (ret != 0)
  2021. ERROR_OUT(-1805 - i, exit);
  2022. wc_ShaFree(&shaCopy);
  2023. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2024. ERROR_OUT(-1806 - i, exit);
  2025. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2026. ERROR_OUT(-1807 - i, exit);
  2027. }
  2028. /* BEGIN LARGE HASH TEST */ {
  2029. byte large_input[1024];
  2030. #ifdef WOLFSSL_RENESAS_TSIP
  2031. const char* large_digest =
  2032. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2033. "\x15\x8c\x6d\xb6";
  2034. #else
  2035. const char* large_digest =
  2036. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2037. "\xc5\xd9\x0a\xac";
  2038. #endif
  2039. for (i = 0; i < (int)sizeof(large_input); i++) {
  2040. large_input[i] = (byte)(i & 0xFF);
  2041. }
  2042. #ifdef WOLFSSL_RENESAS_TSIP
  2043. times = 20;
  2044. #else
  2045. times = 100;
  2046. #endif
  2047. #ifdef WOLFSSL_PIC32MZ_HASH
  2048. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2049. #endif
  2050. for (i = 0; i < times; ++i) {
  2051. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2052. (word32)sizeof(large_input));
  2053. if (ret != 0)
  2054. ERROR_OUT(-1808, exit);
  2055. }
  2056. ret = wc_ShaFinal(&sha, hash);
  2057. if (ret != 0)
  2058. ERROR_OUT(-1809, exit);
  2059. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2060. ERROR_OUT(-1810, exit);
  2061. } /* END LARGE HASH TEST */
  2062. exit:
  2063. wc_ShaFree(&sha);
  2064. wc_ShaFree(&shaCopy);
  2065. return ret;
  2066. }
  2067. #endif /* NO_SHA */
  2068. #ifdef WOLFSSL_RIPEMD
  2069. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2070. {
  2071. RipeMd ripemd;
  2072. int ret;
  2073. byte hash[RIPEMD_DIGEST_SIZE];
  2074. testVector a, b, c, d;
  2075. testVector test_ripemd[4];
  2076. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2077. a.input = "abc";
  2078. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2079. "\xb0\x87\xf1\x5a\x0b\xfc";
  2080. a.inLen = XSTRLEN(a.input);
  2081. a.outLen = RIPEMD_DIGEST_SIZE;
  2082. b.input = "message digest";
  2083. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2084. "\x5f\xfa\x21\x59\x5f\x36";
  2085. b.inLen = XSTRLEN(b.input);
  2086. b.outLen = RIPEMD_DIGEST_SIZE;
  2087. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2088. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2089. "\xf4\x9a\xda\x62\xeb\x2b";
  2090. c.inLen = XSTRLEN(c.input);
  2091. c.outLen = RIPEMD_DIGEST_SIZE;
  2092. d.input = "12345678901234567890123456789012345678901234567890123456"
  2093. "789012345678901234567890";
  2094. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2095. "\x82\xbf\x63\x32\x6b\xfb";
  2096. d.inLen = XSTRLEN(d.input);
  2097. d.outLen = RIPEMD_DIGEST_SIZE;
  2098. test_ripemd[0] = a;
  2099. test_ripemd[1] = b;
  2100. test_ripemd[2] = c;
  2101. test_ripemd[3] = d;
  2102. ret = wc_InitRipeMd(&ripemd);
  2103. if (ret != 0) {
  2104. return -1900;
  2105. }
  2106. for (i = 0; i < times; ++i) {
  2107. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2108. (word32)test_ripemd[i].inLen);
  2109. if (ret != 0) {
  2110. return -1901 - i;
  2111. }
  2112. ret = wc_RipeMdFinal(&ripemd, hash);
  2113. if (ret != 0) {
  2114. return -1911 - i;
  2115. }
  2116. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2117. return -1921 - i;
  2118. }
  2119. return 0;
  2120. }
  2121. #endif /* WOLFSSL_RIPEMD */
  2122. #ifdef HAVE_BLAKE2
  2123. #define BLAKE2B_TESTS 3
  2124. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2125. {
  2126. {
  2127. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2128. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2129. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2130. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2131. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2132. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2133. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2134. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2135. },
  2136. {
  2137. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2138. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2139. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2140. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2141. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2142. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2143. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2144. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2145. },
  2146. {
  2147. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2148. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2149. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2150. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2151. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2152. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2153. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2154. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2155. }
  2156. };
  2157. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2158. {
  2159. Blake2b b2b;
  2160. byte digest[64];
  2161. byte input[64];
  2162. int i, ret;
  2163. for (i = 0; i < (int)sizeof(input); i++)
  2164. input[i] = (byte)i;
  2165. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2166. ret = wc_InitBlake2b(&b2b, 64);
  2167. if (ret != 0)
  2168. return -2000 - i;
  2169. ret = wc_Blake2bUpdate(&b2b, input, i);
  2170. if (ret != 0)
  2171. return -2010 - 1;
  2172. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2173. if (ret != 0)
  2174. return -2020 - i;
  2175. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2176. return -2030 - i;
  2177. }
  2178. }
  2179. return 0;
  2180. }
  2181. #endif /* HAVE_BLAKE2 */
  2182. #ifdef HAVE_BLAKE2S
  2183. #define BLAKE2S_TESTS 3
  2184. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2185. {
  2186. {
  2187. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2188. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2189. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2190. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2191. },
  2192. {
  2193. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2194. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2195. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2196. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2197. },
  2198. {
  2199. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2200. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2201. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2202. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2203. }
  2204. };
  2205. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2206. {
  2207. Blake2s b2s;
  2208. byte digest[32];
  2209. byte input[64];
  2210. int i, ret;
  2211. for (i = 0; i < (int)sizeof(input); i++)
  2212. input[i] = (byte)i;
  2213. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2214. ret = wc_InitBlake2s(&b2s, 32);
  2215. if (ret != 0)
  2216. return -2100 - i;
  2217. ret = wc_Blake2sUpdate(&b2s, input, i);
  2218. if (ret != 0)
  2219. return -2110 - 1;
  2220. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2221. if (ret != 0)
  2222. return -2120 - i;
  2223. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2224. return -2130 - i;
  2225. }
  2226. }
  2227. return 0;
  2228. }
  2229. #endif /* HAVE_BLAKE2S */
  2230. #ifdef WOLFSSL_SHA224
  2231. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2232. {
  2233. wc_Sha224 sha, shaCopy;
  2234. byte hash[WC_SHA224_DIGEST_SIZE];
  2235. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2236. int ret = 0;
  2237. testVector a, b, c;
  2238. testVector test_sha[3];
  2239. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2240. a.input = "";
  2241. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2242. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2243. a.inLen = XSTRLEN(a.input);
  2244. a.outLen = WC_SHA224_DIGEST_SIZE;
  2245. b.input = "abc";
  2246. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2247. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2248. b.inLen = XSTRLEN(b.input);
  2249. b.outLen = WC_SHA224_DIGEST_SIZE;
  2250. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2251. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2252. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2253. c.inLen = XSTRLEN(c.input);
  2254. c.outLen = WC_SHA224_DIGEST_SIZE;
  2255. test_sha[0] = a;
  2256. test_sha[1] = b;
  2257. test_sha[2] = c;
  2258. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2259. if (ret != 0)
  2260. return -2200;
  2261. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2262. if (ret != 0) {
  2263. wc_Sha224Free(&sha);
  2264. return -2201;
  2265. }
  2266. for (i = 0; i < times; ++i) {
  2267. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2268. (word32)test_sha[i].inLen);
  2269. if (ret != 0)
  2270. ERROR_OUT(-2202 - i, exit);
  2271. ret = wc_Sha224GetHash(&sha, hashcopy);
  2272. if (ret != 0)
  2273. ERROR_OUT(-2203 - i, exit);
  2274. ret = wc_Sha224Copy(&sha, &shaCopy);
  2275. if (ret != 0)
  2276. ERROR_OUT(-2204 - i, exit);
  2277. ret = wc_Sha224Final(&sha, hash);
  2278. if (ret != 0)
  2279. ERROR_OUT(-2205 - i, exit);
  2280. wc_Sha224Free(&shaCopy);
  2281. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2282. ERROR_OUT(-2206 - i, exit);
  2283. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2284. ERROR_OUT(-2207 - i, exit);
  2285. }
  2286. exit:
  2287. wc_Sha224Free(&sha);
  2288. wc_Sha224Free(&shaCopy);
  2289. return ret;
  2290. }
  2291. #endif
  2292. #ifndef NO_SHA256
  2293. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2294. {
  2295. wc_Sha256 sha, shaCopy;
  2296. byte hash[WC_SHA256_DIGEST_SIZE];
  2297. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2298. int ret = 0;
  2299. testVector a, b, c;
  2300. testVector test_sha[3];
  2301. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2302. a.input = "";
  2303. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2304. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2305. "\xb8\x55";
  2306. a.inLen = XSTRLEN(a.input);
  2307. a.outLen = WC_SHA256_DIGEST_SIZE;
  2308. b.input = "abc";
  2309. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2310. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2311. "\x15\xAD";
  2312. b.inLen = XSTRLEN(b.input);
  2313. b.outLen = WC_SHA256_DIGEST_SIZE;
  2314. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2315. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2316. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2317. "\x06\xC1";
  2318. c.inLen = XSTRLEN(c.input);
  2319. c.outLen = WC_SHA256_DIGEST_SIZE;
  2320. test_sha[0] = a;
  2321. test_sha[1] = b;
  2322. test_sha[2] = c;
  2323. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2324. if (ret != 0)
  2325. return -2300;
  2326. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2327. if (ret != 0) {
  2328. wc_Sha256Free(&sha);
  2329. return -2301;
  2330. }
  2331. for (i = 0; i < times; ++i) {
  2332. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2333. (word32)test_sha[i].inLen);
  2334. if (ret != 0) {
  2335. ERROR_OUT(-2302 - i, exit);
  2336. }
  2337. ret = wc_Sha256GetHash(&sha, hashcopy);
  2338. if (ret != 0)
  2339. ERROR_OUT(-2303 - i, exit);
  2340. ret = wc_Sha256Copy(&sha, &shaCopy);
  2341. if (ret != 0)
  2342. ERROR_OUT(-2304 - i, exit);
  2343. ret = wc_Sha256Final(&sha, hash);
  2344. if (ret != 0)
  2345. ERROR_OUT(-2305 - i, exit);
  2346. wc_Sha256Free(&shaCopy);
  2347. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2348. ERROR_OUT(-2306 - i, exit);
  2349. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2350. ERROR_OUT(-2307 - i, exit);
  2351. }
  2352. /* BEGIN LARGE HASH TEST */ {
  2353. byte large_input[1024];
  2354. #ifdef WOLFSSL_RENESAS_TSIP_CRYPT
  2355. const char* large_digest =
  2356. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2357. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2358. #else
  2359. const char* large_digest =
  2360. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2361. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2362. #endif
  2363. for (i = 0; i < (int)sizeof(large_input); i++) {
  2364. large_input[i] = (byte)(i & 0xFF);
  2365. }
  2366. #ifdef WOLFSSL_RENESAS_TSIP
  2367. times = 20;
  2368. #else
  2369. times = 100;
  2370. #endif
  2371. #ifdef WOLFSSL_PIC32MZ_HASH
  2372. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2373. #endif
  2374. for (i = 0; i < times; ++i) {
  2375. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2376. (word32)sizeof(large_input));
  2377. if (ret != 0)
  2378. ERROR_OUT(-2308, exit);
  2379. }
  2380. ret = wc_Sha256Final(&sha, hash);
  2381. if (ret != 0)
  2382. ERROR_OUT(-2309, exit);
  2383. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2384. ERROR_OUT(-2310, exit);
  2385. } /* END LARGE HASH TEST */
  2386. exit:
  2387. wc_Sha256Free(&sha);
  2388. wc_Sha256Free(&shaCopy);
  2389. return ret;
  2390. }
  2391. #endif
  2392. #ifdef WOLFSSL_SHA512
  2393. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2394. {
  2395. wc_Sha512 sha, shaCopy;
  2396. byte hash[WC_SHA512_DIGEST_SIZE];
  2397. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2398. int ret = 0;
  2399. testVector a, b, c;
  2400. testVector test_sha[3];
  2401. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2402. a.input = "";
  2403. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2404. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2405. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2406. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2407. "\xf9\x27\xda\x3e";
  2408. a.inLen = XSTRLEN(a.input);
  2409. a.outLen = WC_SHA512_DIGEST_SIZE;
  2410. b.input = "abc";
  2411. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2412. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2413. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2414. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2415. "\xa5\x4c\xa4\x9f";
  2416. b.inLen = XSTRLEN(b.input);
  2417. b.outLen = WC_SHA512_DIGEST_SIZE;
  2418. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2419. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2420. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2421. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2422. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2423. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2424. "\x87\x4b\xe9\x09";
  2425. c.inLen = XSTRLEN(c.input);
  2426. c.outLen = WC_SHA512_DIGEST_SIZE;
  2427. test_sha[0] = a;
  2428. test_sha[1] = b;
  2429. test_sha[2] = c;
  2430. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2431. if (ret != 0)
  2432. return -2400;
  2433. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2434. if (ret != 0) {
  2435. wc_Sha512Free(&sha);
  2436. return -2401;
  2437. }
  2438. for (i = 0; i < times; ++i) {
  2439. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2440. (word32)test_sha[i].inLen);
  2441. if (ret != 0)
  2442. ERROR_OUT(-2402 - i, exit);
  2443. ret = wc_Sha512GetHash(&sha, hashcopy);
  2444. if (ret != 0)
  2445. ERROR_OUT(-2403 - i, exit);
  2446. ret = wc_Sha512Copy(&sha, &shaCopy);
  2447. if (ret != 0)
  2448. ERROR_OUT(-2404 - i, exit);
  2449. ret = wc_Sha512Final(&sha, hash);
  2450. if (ret != 0)
  2451. ERROR_OUT(-2405 - i, exit);
  2452. wc_Sha512Free(&shaCopy);
  2453. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2454. ERROR_OUT(-2406 - i, exit);
  2455. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2456. ERROR_OUT(-2407 - i, exit);
  2457. }
  2458. /* BEGIN LARGE HASH TEST */ {
  2459. byte large_input[1024];
  2460. const char* large_digest =
  2461. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2462. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2463. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2464. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2465. for (i = 0; i < (int)sizeof(large_input); i++) {
  2466. large_input[i] = (byte)(i & 0xFF);
  2467. }
  2468. times = 100;
  2469. for (i = 0; i < times; ++i) {
  2470. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2471. (word32)sizeof(large_input));
  2472. if (ret != 0)
  2473. ERROR_OUT(-2408, exit);
  2474. }
  2475. ret = wc_Sha512Final(&sha, hash);
  2476. if (ret != 0)
  2477. ERROR_OUT(-2409, exit);
  2478. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2479. ERROR_OUT(-2410, exit);
  2480. } /* END LARGE HASH TEST */
  2481. exit:
  2482. wc_Sha512Free(&sha);
  2483. wc_Sha512Free(&shaCopy);
  2484. return ret;
  2485. }
  2486. #endif
  2487. #ifdef WOLFSSL_SHA384
  2488. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2489. {
  2490. wc_Sha384 sha, shaCopy;
  2491. byte hash[WC_SHA384_DIGEST_SIZE];
  2492. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2493. int ret = 0;
  2494. testVector a, b, c;
  2495. testVector test_sha[3];
  2496. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2497. a.input = "";
  2498. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2499. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2500. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2501. "\x98\xb9\x5b";
  2502. a.inLen = XSTRLEN(a.input);
  2503. a.outLen = WC_SHA384_DIGEST_SIZE;
  2504. b.input = "abc";
  2505. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2506. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2507. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2508. "\xc8\x25\xa7";
  2509. b.inLen = XSTRLEN(b.input);
  2510. b.outLen = WC_SHA384_DIGEST_SIZE;
  2511. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2512. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2513. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2514. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2515. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2516. "\x74\x60\x39";
  2517. c.inLen = XSTRLEN(c.input);
  2518. c.outLen = WC_SHA384_DIGEST_SIZE;
  2519. test_sha[0] = a;
  2520. test_sha[1] = b;
  2521. test_sha[2] = c;
  2522. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2523. if (ret != 0)
  2524. return -2500;
  2525. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2526. if (ret != 0) {
  2527. wc_Sha384Free(&sha);
  2528. return -2501;
  2529. }
  2530. for (i = 0; i < times; ++i) {
  2531. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2532. (word32)test_sha[i].inLen);
  2533. if (ret != 0)
  2534. ERROR_OUT(-2502 - i, exit);
  2535. ret = wc_Sha384GetHash(&sha, hashcopy);
  2536. if (ret != 0)
  2537. ERROR_OUT(-2503 - i, exit);
  2538. ret = wc_Sha384Copy(&sha, &shaCopy);
  2539. if (ret != 0)
  2540. ERROR_OUT(-2504 - i, exit);
  2541. ret = wc_Sha384Final(&sha, hash);
  2542. if (ret != 0)
  2543. ERROR_OUT(-2505 - i, exit);
  2544. wc_Sha384Free(&shaCopy);
  2545. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2546. ERROR_OUT(-2506 - i, exit);
  2547. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2548. ERROR_OUT(-2507 - i, exit);
  2549. }
  2550. /* BEGIN LARGE HASH TEST */ {
  2551. byte large_input[1024];
  2552. const char* large_digest =
  2553. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2554. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2555. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2556. for (i = 0; i < (int)sizeof(large_input); i++) {
  2557. large_input[i] = (byte)(i & 0xFF);
  2558. }
  2559. times = 100;
  2560. for (i = 0; i < times; ++i) {
  2561. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2562. (word32)sizeof(large_input));
  2563. if (ret != 0)
  2564. ERROR_OUT(-2508, exit);
  2565. }
  2566. ret = wc_Sha384Final(&sha, hash);
  2567. if (ret != 0)
  2568. ERROR_OUT(-2509, exit);
  2569. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2570. ERROR_OUT(-2510, exit);
  2571. } /* END LARGE HASH TEST */
  2572. exit:
  2573. wc_Sha384Free(&sha);
  2574. wc_Sha384Free(&shaCopy);
  2575. return ret;
  2576. }
  2577. #endif /* WOLFSSL_SHA384 */
  2578. #ifdef WOLFSSL_SHA3
  2579. #ifndef WOLFSSL_NOSHA3_224
  2580. static int sha3_224_test(void)
  2581. {
  2582. wc_Sha3 sha;
  2583. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2584. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2585. testVector a, b, c;
  2586. testVector test_sha[3];
  2587. int ret = 0;
  2588. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2589. a.input = "";
  2590. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2591. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2592. a.inLen = XSTRLEN(a.input);
  2593. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2594. b.input = "abc";
  2595. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2596. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2597. b.inLen = XSTRLEN(b.input);
  2598. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2599. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2600. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2601. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2602. c.inLen = XSTRLEN(c.input);
  2603. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2604. test_sha[0] = a;
  2605. test_sha[1] = b;
  2606. test_sha[2] = c;
  2607. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2608. if (ret != 0)
  2609. return -2600;
  2610. for (i = 0; i < times; ++i) {
  2611. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2612. (word32)test_sha[i].inLen);
  2613. if (ret != 0)
  2614. ERROR_OUT(-2601 - i, exit);
  2615. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2616. if (ret != 0)
  2617. ERROR_OUT(-2602 - i, exit);
  2618. ret = wc_Sha3_224_Final(&sha, hash);
  2619. if (ret != 0)
  2620. ERROR_OUT(-2603 - i, exit);
  2621. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2622. ERROR_OUT(-2604 - i, exit);
  2623. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2624. ERROR_OUT(-2605 - i, exit);
  2625. }
  2626. /* BEGIN LARGE HASH TEST */ {
  2627. byte large_input[1024];
  2628. const char* large_digest =
  2629. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2630. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2631. for (i = 0; i < (int)sizeof(large_input); i++) {
  2632. large_input[i] = (byte)(i & 0xFF);
  2633. }
  2634. times = 100;
  2635. for (i = 0; i < times; ++i) {
  2636. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2637. (word32)sizeof(large_input));
  2638. if (ret != 0)
  2639. ERROR_OUT(-2606, exit);
  2640. }
  2641. ret = wc_Sha3_224_Final(&sha, hash);
  2642. if (ret != 0)
  2643. ERROR_OUT(-2607, exit);
  2644. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2645. ERROR_OUT(-2608, exit);
  2646. } /* END LARGE HASH TEST */
  2647. exit:
  2648. wc_Sha3_224_Free(&sha);
  2649. return ret;
  2650. }
  2651. #endif /* WOLFSSL_NOSHA3_224 */
  2652. #ifndef WOLFSSL_NOSHA3_256
  2653. static int sha3_256_test(void)
  2654. {
  2655. wc_Sha3 sha;
  2656. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2657. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2658. testVector a, b, c;
  2659. testVector test_sha[3];
  2660. int ret = 0;
  2661. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2662. byte large_input[1024];
  2663. const char* large_digest =
  2664. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2665. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2666. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2667. /* test vector with hash of empty string */
  2668. const char* Keccak256EmptyOut =
  2669. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2670. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2671. #endif
  2672. a.input = "";
  2673. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2674. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2675. "\x43\x4a";
  2676. a.inLen = XSTRLEN(a.input);
  2677. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2678. b.input = "abc";
  2679. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2680. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2681. "\x15\x32";
  2682. b.inLen = XSTRLEN(b.input);
  2683. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2684. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2685. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2686. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2687. "\x33\x76";
  2688. c.inLen = XSTRLEN(c.input);
  2689. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2690. test_sha[0] = a;
  2691. test_sha[1] = b;
  2692. test_sha[2] = c;
  2693. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2694. if (ret != 0)
  2695. return -2700;
  2696. for (i = 0; i < times; ++i) {
  2697. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2698. (word32)test_sha[i].inLen);
  2699. if (ret != 0)
  2700. ERROR_OUT(-2701 - i, exit);
  2701. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2702. if (ret != 0)
  2703. ERROR_OUT(-2702 - i, exit);
  2704. ret = wc_Sha3_256_Final(&sha, hash);
  2705. if (ret != 0)
  2706. ERROR_OUT(-2703 - i, exit);
  2707. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2708. ERROR_OUT(-2704 - i, exit);
  2709. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2710. ERROR_OUT(-2705 - i, exit);
  2711. }
  2712. /* BEGIN LARGE HASH TEST */ {
  2713. for (i = 0; i < (int)sizeof(large_input); i++) {
  2714. large_input[i] = (byte)(i & 0xFF);
  2715. }
  2716. times = 100;
  2717. for (i = 0; i < times; ++i) {
  2718. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2719. (word32)sizeof(large_input));
  2720. if (ret != 0)
  2721. ERROR_OUT(-2706, exit);
  2722. }
  2723. ret = wc_Sha3_256_Final(&sha, hash);
  2724. if (ret != 0)
  2725. ERROR_OUT(-2707, exit);
  2726. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2727. ERROR_OUT(-2708, exit);
  2728. } /* END LARGE HASH TEST */
  2729. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2730. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2731. /* Test for Keccak256 */
  2732. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2733. if (ret != 0) {
  2734. ERROR_OUT(-2709, exit);
  2735. }
  2736. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2737. if (ret != 0) {
  2738. ERROR_OUT(-2710, exit);
  2739. }
  2740. ret = wc_Sha3_256_Final(&sha, hash);
  2741. if (ret != 0) {
  2742. ERROR_OUT(-2711, exit);
  2743. }
  2744. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2745. ERROR_OUT(-2712, exit);
  2746. }
  2747. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2748. exit:
  2749. wc_Sha3_256_Free(&sha);
  2750. return ret;
  2751. }
  2752. #endif /* WOLFSSL_NOSHA3_256 */
  2753. #ifndef WOLFSSL_NOSHA3_384
  2754. static int sha3_384_test(void)
  2755. {
  2756. wc_Sha3 sha;
  2757. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2758. #ifndef NO_INTM_HASH_TEST
  2759. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2760. #endif
  2761. testVector a, b, c;
  2762. testVector test_sha[3];
  2763. int ret;
  2764. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2765. a.input = "";
  2766. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2767. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2768. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2769. "\xd5\xf0\x04";
  2770. a.inLen = XSTRLEN(a.input);
  2771. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2772. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
  2773. /* NIST test vector with a length that is a multiple of 4 */
  2774. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2775. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2776. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2777. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2778. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2779. "\x19\x87\x22";
  2780. b.inLen = XSTRLEN(b.input);
  2781. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2782. #else
  2783. b.input = "abc";
  2784. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2785. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2786. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2787. "\x37\x6d\x25";
  2788. b.inLen = XSTRLEN(b.input);
  2789. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2790. #endif
  2791. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2792. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2793. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2794. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2795. "\x65\x7c\x22";
  2796. c.inLen = XSTRLEN(c.input);
  2797. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2798. #ifdef WOLFSSL_XILINX_CRYPT
  2799. test_sha[0] = b; /* hardware acc. can not handle "" string */
  2800. #else
  2801. test_sha[0] = a;
  2802. #endif
  2803. test_sha[1] = b;
  2804. test_sha[2] = c;
  2805. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2806. if (ret != 0)
  2807. return -2800;
  2808. for (i = 0; i < times; ++i) {
  2809. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2810. (word32)test_sha[i].inLen);
  2811. if (ret != 0)
  2812. ERROR_OUT(-2801 - i, exit);
  2813. #ifndef NO_INTM_HASH_TEST
  2814. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2815. if (ret != 0)
  2816. ERROR_OUT(-2802 - i, exit);
  2817. #endif
  2818. ret = wc_Sha3_384_Final(&sha, hash);
  2819. if (ret != 0)
  2820. ERROR_OUT(-2803 - i, exit);
  2821. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2822. ERROR_OUT(-2804 - i, exit);
  2823. #ifndef NO_INTM_HASH_TEST
  2824. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2825. ERROR_OUT(-2805 - i, exit);
  2826. #endif
  2827. }
  2828. /* BEGIN LARGE HASH TEST */ {
  2829. byte large_input[1024];
  2830. const char* large_digest =
  2831. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2832. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2833. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2834. for (i = 0; i < (int)sizeof(large_input); i++) {
  2835. large_input[i] = (byte)(i & 0xFF);
  2836. }
  2837. times = 100;
  2838. for (i = 0; i < times; ++i) {
  2839. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2840. (word32)sizeof(large_input));
  2841. if (ret != 0)
  2842. ERROR_OUT(-2806, exit);
  2843. }
  2844. ret = wc_Sha3_384_Final(&sha, hash);
  2845. if (ret != 0)
  2846. ERROR_OUT(-2807, exit);
  2847. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2848. ERROR_OUT(-2808, exit);
  2849. } /* END LARGE HASH TEST */
  2850. exit:
  2851. wc_Sha3_384_Free(&sha);
  2852. return ret;
  2853. }
  2854. #endif /* WOLFSSL_NOSHA3_384 */
  2855. #ifndef WOLFSSL_NOSHA3_512
  2856. static int sha3_512_test(void)
  2857. {
  2858. wc_Sha3 sha;
  2859. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2860. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  2861. testVector a, b, c;
  2862. testVector test_sha[3];
  2863. int ret;
  2864. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2865. a.input = "";
  2866. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2867. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2868. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2869. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2870. "\x28\x1d\xcd\x26";
  2871. a.inLen = XSTRLEN(a.input);
  2872. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2873. b.input = "abc";
  2874. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2875. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2876. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2877. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2878. "\x4e\xec\x53\xf0";
  2879. b.inLen = XSTRLEN(b.input);
  2880. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2881. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2882. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2883. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2884. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2885. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2886. "\x39\xd6\xe7\x5e";
  2887. c.inLen = XSTRLEN(c.input);
  2888. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  2889. test_sha[0] = a;
  2890. test_sha[1] = b;
  2891. test_sha[2] = c;
  2892. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2893. if (ret != 0)
  2894. return -2900;
  2895. for (i = 0; i < times; ++i) {
  2896. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2897. (word32)test_sha[i].inLen);
  2898. if (ret != 0)
  2899. ERROR_OUT(-2901 - i, exit);
  2900. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2901. if (ret != 0)
  2902. ERROR_OUT(-2902 - i, exit);
  2903. ret = wc_Sha3_512_Final(&sha, hash);
  2904. if (ret != 0)
  2905. ERROR_OUT(-2903 - i, exit);
  2906. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  2907. ERROR_OUT(-2904 - i, exit);
  2908. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  2909. ERROR_OUT(-2905 - i, exit);
  2910. }
  2911. /* BEGIN LARGE HASH TEST */ {
  2912. byte large_input[1024];
  2913. const char* large_digest =
  2914. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2915. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2916. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2917. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2918. for (i = 0; i < (int)sizeof(large_input); i++) {
  2919. large_input[i] = (byte)(i & 0xFF);
  2920. }
  2921. times = 100;
  2922. for (i = 0; i < times; ++i) {
  2923. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2924. (word32)sizeof(large_input));
  2925. if (ret != 0)
  2926. ERROR_OUT(-2906, exit);
  2927. }
  2928. ret = wc_Sha3_512_Final(&sha, hash);
  2929. if (ret != 0)
  2930. ERROR_OUT(-2907, exit);
  2931. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  2932. ERROR_OUT(-2908, exit);
  2933. } /* END LARGE HASH TEST */
  2934. exit:
  2935. wc_Sha3_512_Free(&sha);
  2936. return ret;
  2937. }
  2938. #endif /* WOLFSSL_NOSHA3_512 */
  2939. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  2940. {
  2941. int ret;
  2942. (void)ret;
  2943. #ifndef WOLFSSL_NOSHA3_224
  2944. if ((ret = sha3_224_test()) != 0)
  2945. return ret;
  2946. #endif
  2947. #ifndef WOLFSSL_NOSHA3_256
  2948. if ((ret = sha3_256_test()) != 0)
  2949. return ret;
  2950. #endif
  2951. #ifndef WOLFSSL_NOSHA3_384
  2952. if ((ret = sha3_384_test()) != 0)
  2953. return ret;
  2954. #endif
  2955. #ifndef WOLFSSL_NOSHA3_512
  2956. if ((ret = sha3_512_test()) != 0)
  2957. return ret;
  2958. #endif
  2959. return 0;
  2960. }
  2961. #endif /* WOLFSSL_SHA3 */
  2962. #ifdef WOLFSSL_SHAKE256
  2963. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  2964. {
  2965. wc_Shake sha;
  2966. byte hash[250];
  2967. testVector a, b, c, d, e;
  2968. testVector test_sha[5];
  2969. int ret = 0;
  2970. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2971. byte large_input[1024];
  2972. const char* large_digest =
  2973. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  2974. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  2975. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  2976. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  2977. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  2978. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  2979. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  2980. "\xea\x26";
  2981. a.input = "";
  2982. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  2983. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  2984. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  2985. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  2986. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  2987. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  2988. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  2989. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  2990. a.inLen = XSTRLEN(a.input);
  2991. a.outLen = 114;
  2992. b.input = "abc";
  2993. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  2994. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  2995. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  2996. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  2997. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  2998. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  2999. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3000. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3001. b.inLen = XSTRLEN(b.input);
  3002. b.outLen = 114;
  3003. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3004. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3005. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3006. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3007. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3008. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3009. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3010. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3011. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3012. c.inLen = XSTRLEN(c.input);
  3013. c.outLen = 114;
  3014. /* Taken from NIST CAVP test vectors - full rate output. */
  3015. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3016. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3017. "\x85\xe0";
  3018. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3019. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3020. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3021. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3022. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3023. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3024. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3025. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3026. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3027. "\xc2";
  3028. d.inLen = 32;
  3029. d.outLen = 136;
  3030. /* Taken from NIST CAVP test vectors - more than one output block. */
  3031. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3032. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3033. "\x47\xe4";
  3034. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3035. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3036. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3037. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3038. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3039. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3040. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3041. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3042. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3043. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3044. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3045. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3046. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3047. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3048. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3049. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3050. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3051. e.inLen = 32;
  3052. e.outLen = 250;
  3053. test_sha[0] = a;
  3054. test_sha[1] = b;
  3055. test_sha[2] = c;
  3056. test_sha[3] = d;
  3057. test_sha[4] = e;
  3058. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3059. if (ret != 0)
  3060. return -3100;
  3061. for (i = 0; i < times; ++i) {
  3062. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3063. (word32)test_sha[i].inLen);
  3064. if (ret != 0)
  3065. ERROR_OUT(-3101 - i, exit);
  3066. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3067. if (ret != 0)
  3068. ERROR_OUT(-3102 - i, exit);
  3069. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3070. ERROR_OUT(-3103 - i, exit);
  3071. }
  3072. /* BEGIN LARGE HASH TEST */ {
  3073. for (i = 0; i < (int)sizeof(large_input); i++) {
  3074. large_input[i] = (byte)(i & 0xFF);
  3075. }
  3076. times = 100;
  3077. for (i = 0; i < times; ++i) {
  3078. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3079. (word32)sizeof(large_input));
  3080. if (ret != 0)
  3081. ERROR_OUT(-3104, exit);
  3082. }
  3083. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3084. if (ret != 0)
  3085. ERROR_OUT(-3105, exit);
  3086. if (XMEMCMP(hash, large_digest, 114) != 0)
  3087. ERROR_OUT(-3106, exit);
  3088. } /* END LARGE HASH TEST */
  3089. exit:
  3090. wc_Shake256_Free(&sha);
  3091. return ret;
  3092. }
  3093. #endif
  3094. #ifndef NO_HASH_WRAPPER
  3095. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3096. {
  3097. wc_HashAlg hash;
  3098. int ret, exp_ret;
  3099. int i, j;
  3100. int digestSz;
  3101. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3102. byte out[WC_MAX_DIGEST_SIZE];
  3103. byte hashOut[WC_MAX_DIGEST_SIZE];
  3104. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3105. enum wc_HashType hashType;
  3106. #endif
  3107. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3108. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3109. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3110. WC_HASH_TYPE_SHA3_224,
  3111. WC_HASH_TYPE_SHA3_256,
  3112. WC_HASH_TYPE_SHA3_384,
  3113. WC_HASH_TYPE_SHA3_512 };
  3114. enum wc_HashType typesNoImpl[] = {
  3115. #ifdef NO_MD5
  3116. WC_HASH_TYPE_MD5,
  3117. #endif
  3118. #ifdef NO_SHA
  3119. WC_HASH_TYPE_SHA,
  3120. #endif
  3121. #ifndef WOLFSSL_SHA224
  3122. WC_HASH_TYPE_SHA224,
  3123. #endif
  3124. #ifdef NO_SHA256
  3125. WC_HASH_TYPE_SHA256,
  3126. #endif
  3127. #ifndef WOLFSSL_SHA384
  3128. WC_HASH_TYPE_SHA384,
  3129. #endif
  3130. #ifndef WOLFSSL_SHA512
  3131. WC_HASH_TYPE_SHA512,
  3132. #endif
  3133. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3134. WC_HASH_TYPE_SHA3_224,
  3135. #endif
  3136. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3137. WC_HASH_TYPE_SHA3_256,
  3138. #endif
  3139. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3140. WC_HASH_TYPE_SHA3_384,
  3141. #endif
  3142. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3143. WC_HASH_TYPE_SHA3_512,
  3144. #endif
  3145. WC_HASH_TYPE_NONE
  3146. };
  3147. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3148. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3149. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3150. WC_HASH_TYPE_BLAKE2B,
  3151. WC_HASH_TYPE_NONE };
  3152. /* Parameter Validation testing. */
  3153. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3154. if (ret != BAD_FUNC_ARG)
  3155. return -3200;
  3156. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3157. if (ret != BAD_FUNC_ARG)
  3158. return -3201;
  3159. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3160. if (ret != BAD_FUNC_ARG)
  3161. return -3202;
  3162. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3163. if (ret != BAD_FUNC_ARG)
  3164. return -3203;
  3165. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3166. if (ret != BAD_FUNC_ARG)
  3167. return -3204;
  3168. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3169. if (ret != BAD_FUNC_ARG)
  3170. return -3205;
  3171. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3172. if (ret != BAD_FUNC_ARG)
  3173. return -3206;
  3174. /* Try invalid hash algorithms. */
  3175. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3176. ret = wc_HashInit(&hash, typesBad[i]);
  3177. if (ret != BAD_FUNC_ARG)
  3178. return -3207 - i;
  3179. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3180. if (ret != BAD_FUNC_ARG)
  3181. return -3217 - i;
  3182. ret = wc_HashFinal(&hash, typesBad[i], out);
  3183. if (ret != BAD_FUNC_ARG)
  3184. return -3227 - i;
  3185. wc_HashFree(&hash, typesBad[i]);
  3186. }
  3187. /* Try valid hash algorithms. */
  3188. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3189. exp_ret = 0;
  3190. if (typesGood[i] == typesNoImpl[j]) {
  3191. /* Recognized but no implementation compiled in. */
  3192. exp_ret = HASH_TYPE_E;
  3193. j++;
  3194. }
  3195. ret = wc_HashInit(&hash, typesGood[i]);
  3196. if (ret != exp_ret)
  3197. return -3237 - i;
  3198. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3199. if (ret != exp_ret)
  3200. return -3247 - i;
  3201. ret = wc_HashFinal(&hash, typesGood[i], out);
  3202. if (ret != exp_ret)
  3203. return -3257 - i;
  3204. wc_HashFree(&hash, typesGood[i]);
  3205. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3206. if (exp_ret < 0 && digestSz != exp_ret)
  3207. return -3267 - i;
  3208. if (exp_ret == 0 && digestSz < 0)
  3209. return -3277 - i;
  3210. if (exp_ret == 0) {
  3211. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3212. digestSz - 1);
  3213. if (ret != BUFFER_E)
  3214. return -3287 - i;
  3215. }
  3216. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3217. if (ret != exp_ret)
  3218. return -3297 - i;
  3219. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3220. return -3307 -i;
  3221. ret = wc_HashGetBlockSize(typesGood[i]);
  3222. if (exp_ret < 0 && ret != exp_ret)
  3223. return -3308 - i;
  3224. if (exp_ret == 0 && ret < 0)
  3225. return -3318 - i;
  3226. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3227. ret = wc_HashGetOID(typesGood[i]);
  3228. if (ret == BAD_FUNC_ARG ||
  3229. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3230. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3231. return -3328 - i;
  3232. }
  3233. hashType = wc_OidGetHash(ret);
  3234. if (exp_ret == 0 && hashType != typesGood[i])
  3235. return -3338 - i;
  3236. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3237. }
  3238. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3239. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3240. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3241. return -3348 - i;
  3242. }
  3243. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3244. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3245. #ifdef WOLFSSL_MD2
  3246. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3247. return -3358;
  3248. #else
  3249. if (ret != HASH_TYPE_E)
  3250. return -3359;
  3251. #endif
  3252. hashType = wc_OidGetHash(646); /* Md2h */
  3253. #ifdef WOLFSSL_MD2
  3254. if (hashType != WC_HASH_TYPE_MD2)
  3255. return -3360;
  3256. #else
  3257. if (hashType != WC_HASH_TYPE_NONE)
  3258. return -3361;
  3259. #endif
  3260. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3261. #ifndef NO_MD5
  3262. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3263. return -3362;
  3264. #else
  3265. if (ret != HASH_TYPE_E)
  3266. return -3363;
  3267. #endif
  3268. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3269. if (ret != BAD_FUNC_ARG)
  3270. return -3364;
  3271. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3272. if (ret != BAD_FUNC_ARG)
  3273. return -3365;
  3274. hashType = wc_OidGetHash(0);
  3275. if (hashType != WC_HASH_TYPE_NONE)
  3276. return -3366;
  3277. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3278. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3279. #ifdef WOLFSSL_MD2
  3280. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3281. return -3367;
  3282. #else
  3283. if (ret != HASH_TYPE_E)
  3284. return -3368;
  3285. #endif
  3286. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3287. #ifdef WOLFSSL_MD2
  3288. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3289. return -3369;
  3290. #else
  3291. if (ret != HASH_TYPE_E)
  3292. return -3370;
  3293. #endif
  3294. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3295. #ifndef NO_MD4
  3296. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3297. return -3371;
  3298. #else
  3299. if (ret != HASH_TYPE_E)
  3300. return -3372;
  3301. #endif
  3302. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3303. #ifndef NO_MD4
  3304. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3305. return -3373;
  3306. #else
  3307. if (ret != HASH_TYPE_E)
  3308. return -3374;
  3309. #endif
  3310. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3311. #if !defined(NO_MD5) && !defined(NO_SHA)
  3312. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3313. return -3375;
  3314. #else
  3315. if (ret != HASH_TYPE_E)
  3316. return -3376;
  3317. #endif
  3318. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3319. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3320. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3321. return -3377;
  3322. #else
  3323. if (ret != HASH_TYPE_E)
  3324. return -3378;
  3325. #endif
  3326. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3327. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3328. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3329. return -3379;
  3330. #else
  3331. if (ret != HASH_TYPE_E)
  3332. return -3380;
  3333. #endif
  3334. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3335. if (ret != BAD_FUNC_ARG)
  3336. return -3381;
  3337. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3338. if (ret != BAD_FUNC_ARG)
  3339. return -3382;
  3340. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3341. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3342. ret = wc_GetCTC_HashOID(MD2);
  3343. if (ret == 0)
  3344. return -3383;
  3345. #endif
  3346. #ifndef NO_MD5
  3347. ret = wc_GetCTC_HashOID(WC_MD5);
  3348. if (ret == 0)
  3349. return -3384;
  3350. #endif
  3351. #ifndef NO_SHA
  3352. ret = wc_GetCTC_HashOID(WC_SHA);
  3353. if (ret == 0)
  3354. return -3385;
  3355. #endif
  3356. #ifdef WOLFSSL_SHA224
  3357. ret = wc_GetCTC_HashOID(WC_SHA224);
  3358. if (ret == 0)
  3359. return -3386;
  3360. #endif
  3361. #ifndef NO_SHA256
  3362. ret = wc_GetCTC_HashOID(WC_SHA256);
  3363. if (ret == 0)
  3364. return -3387;
  3365. #endif
  3366. #ifdef WOLFSSL_SHA384
  3367. ret = wc_GetCTC_HashOID(WC_SHA384);
  3368. if (ret == 0)
  3369. return -3388;
  3370. #endif
  3371. #ifdef WOLFSSL_SHA512
  3372. ret = wc_GetCTC_HashOID(WC_SHA512);
  3373. if (ret == 0)
  3374. return -3389;
  3375. #endif
  3376. ret = wc_GetCTC_HashOID(-1);
  3377. if (ret != 0)
  3378. return -3390;
  3379. #endif
  3380. return 0;
  3381. }
  3382. #endif /* !NO_HASH_WRAPPER */
  3383. #if !defined(NO_HMAC) && !defined(NO_MD5)
  3384. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  3385. {
  3386. Hmac hmac;
  3387. byte hash[WC_MD5_DIGEST_SIZE];
  3388. const char* keys[]=
  3389. {
  3390. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3391. "Jefe",
  3392. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3393. };
  3394. testVector a, b, c;
  3395. testVector test_hmac[3];
  3396. int ret;
  3397. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3398. a.input = "Hi There";
  3399. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  3400. "\x9d";
  3401. a.inLen = XSTRLEN(a.input);
  3402. a.outLen = WC_MD5_DIGEST_SIZE;
  3403. b.input = "what do ya want for nothing?";
  3404. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  3405. "\x38";
  3406. b.inLen = XSTRLEN(b.input);
  3407. b.outLen = WC_MD5_DIGEST_SIZE;
  3408. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3409. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3410. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3411. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3412. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  3413. "\xf6";
  3414. c.inLen = XSTRLEN(c.input);
  3415. c.outLen = WC_MD5_DIGEST_SIZE;
  3416. test_hmac[0] = a;
  3417. test_hmac[1] = b;
  3418. test_hmac[2] = c;
  3419. for (i = 0; i < times; ++i) {
  3420. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3421. if (i == 1) {
  3422. continue; /* cavium can't handle short keys, fips not allowed */
  3423. }
  3424. #endif
  3425. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  3426. return -3400;
  3427. }
  3428. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  3429. (word32)XSTRLEN(keys[i]));
  3430. if (ret != 0)
  3431. return -3401;
  3432. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3433. (word32)test_hmac[i].inLen);
  3434. if (ret != 0)
  3435. return -3402;
  3436. ret = wc_HmacFinal(&hmac, hash);
  3437. if (ret != 0)
  3438. return -3403;
  3439. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  3440. return -3404 - i;
  3441. wc_HmacFree(&hmac);
  3442. }
  3443. #ifndef HAVE_FIPS
  3444. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  3445. return -3414;
  3446. #endif
  3447. return 0;
  3448. }
  3449. #endif /* NO_HMAC && NO_MD5 */
  3450. #if !defined(NO_HMAC) && !defined(NO_SHA)
  3451. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  3452. {
  3453. Hmac hmac;
  3454. byte hash[WC_SHA_DIGEST_SIZE];
  3455. const char* keys[]=
  3456. {
  3457. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3458. "\x0b\x0b\x0b",
  3459. "Jefe",
  3460. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3461. "\xAA\xAA\xAA"
  3462. };
  3463. testVector a, b, c;
  3464. testVector test_hmac[3];
  3465. int ret;
  3466. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3467. a.input = "Hi There";
  3468. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  3469. "\x8e\xf1\x46\xbe\x00";
  3470. a.inLen = XSTRLEN(a.input);
  3471. a.outLen = WC_SHA_DIGEST_SIZE;
  3472. b.input = "what do ya want for nothing?";
  3473. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  3474. "\x9c\x25\x9a\x7c\x79";
  3475. b.inLen = XSTRLEN(b.input);
  3476. b.outLen = WC_SHA_DIGEST_SIZE;
  3477. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3478. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3479. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3480. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3481. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  3482. "\x4f\x63\xf1\x75\xd3";
  3483. c.inLen = XSTRLEN(c.input);
  3484. c.outLen = WC_SHA_DIGEST_SIZE;
  3485. test_hmac[0] = a;
  3486. test_hmac[1] = b;
  3487. test_hmac[2] = c;
  3488. for (i = 0; i < times; ++i) {
  3489. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3490. if (i == 1)
  3491. continue; /* cavium can't handle short keys, fips not allowed */
  3492. #endif
  3493. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3494. return -3500;
  3495. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  3496. (word32)XSTRLEN(keys[i]));
  3497. if (ret != 0)
  3498. return -3501;
  3499. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3500. (word32)test_hmac[i].inLen);
  3501. if (ret != 0)
  3502. return -3502;
  3503. ret = wc_HmacFinal(&hmac, hash);
  3504. if (ret != 0)
  3505. return -3503;
  3506. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  3507. return -3504 - i;
  3508. wc_HmacFree(&hmac);
  3509. }
  3510. #ifndef HAVE_FIPS
  3511. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  3512. return -3514;
  3513. #endif
  3514. return 0;
  3515. }
  3516. #endif
  3517. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  3518. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  3519. {
  3520. Hmac hmac;
  3521. byte hash[WC_SHA224_DIGEST_SIZE];
  3522. const char* keys[]=
  3523. {
  3524. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3525. "\x0b\x0b\x0b",
  3526. "Jefe",
  3527. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3528. "\xAA\xAA\xAA",
  3529. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3530. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3531. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3532. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3533. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3534. };
  3535. testVector a, b, c, d;
  3536. testVector test_hmac[4];
  3537. int ret;
  3538. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3539. a.input = "Hi There";
  3540. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  3541. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  3542. a.inLen = XSTRLEN(a.input);
  3543. a.outLen = WC_SHA224_DIGEST_SIZE;
  3544. b.input = "what do ya want for nothing?";
  3545. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  3546. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  3547. b.inLen = XSTRLEN(b.input);
  3548. b.outLen = WC_SHA224_DIGEST_SIZE;
  3549. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3550. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3551. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3552. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3553. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  3554. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  3555. c.inLen = XSTRLEN(c.input);
  3556. c.outLen = WC_SHA224_DIGEST_SIZE;
  3557. d.input = "Big Key Input";
  3558. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  3559. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  3560. d.inLen = XSTRLEN(d.input);
  3561. d.outLen = WC_SHA224_DIGEST_SIZE;
  3562. test_hmac[0] = a;
  3563. test_hmac[1] = b;
  3564. test_hmac[2] = c;
  3565. test_hmac[3] = d;
  3566. for (i = 0; i < times; ++i) {
  3567. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3568. if (i == 1)
  3569. continue; /* cavium can't handle short keys, fips not allowed */
  3570. #endif
  3571. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3572. return -3600;
  3573. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  3574. (word32)XSTRLEN(keys[i]));
  3575. if (ret != 0)
  3576. return -3601;
  3577. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3578. (word32)test_hmac[i].inLen);
  3579. if (ret != 0)
  3580. return -3602;
  3581. ret = wc_HmacFinal(&hmac, hash);
  3582. if (ret != 0)
  3583. return -3603;
  3584. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3585. return -3604 - i;
  3586. wc_HmacFree(&hmac);
  3587. }
  3588. #ifndef HAVE_FIPS
  3589. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  3590. return -3614;
  3591. #endif
  3592. return 0;
  3593. }
  3594. #endif
  3595. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  3596. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  3597. {
  3598. Hmac hmac;
  3599. byte hash[WC_SHA256_DIGEST_SIZE];
  3600. const char* keys[]=
  3601. {
  3602. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3603. "\x0b\x0b\x0b",
  3604. "Jefe",
  3605. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3606. "\xAA\xAA\xAA",
  3607. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3608. "\xAA\xAA\xAA",
  3609. };
  3610. testVector a, b, c, d;
  3611. testVector test_hmac[4];
  3612. int ret;
  3613. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3614. a.input = "Hi There";
  3615. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  3616. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  3617. "\xcf\xf7";
  3618. a.inLen = XSTRLEN(a.input);
  3619. a.outLen = WC_SHA256_DIGEST_SIZE;
  3620. b.input = "what do ya want for nothing?";
  3621. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  3622. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  3623. "\x38\x43";
  3624. b.inLen = XSTRLEN(b.input);
  3625. b.outLen = WC_SHA256_DIGEST_SIZE;
  3626. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3627. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3628. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3629. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3630. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  3631. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  3632. "\x65\xfe";
  3633. c.inLen = XSTRLEN(c.input);
  3634. c.outLen = WC_SHA256_DIGEST_SIZE;
  3635. d.input = 0;
  3636. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  3637. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  3638. "\x3e\x46";
  3639. d.inLen = 0;
  3640. d.outLen = WC_SHA256_DIGEST_SIZE;
  3641. test_hmac[0] = a;
  3642. test_hmac[1] = b;
  3643. test_hmac[2] = c;
  3644. test_hmac[3] = d;
  3645. for (i = 0; i < times; ++i) {
  3646. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3647. if (i == 1)
  3648. continue; /* cavium can't handle short keys, fips not allowed */
  3649. #endif
  3650. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  3651. if (i == 3)
  3652. continue; /* QuickAssist can't handle empty HMAC */
  3653. #endif
  3654. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3655. return -3700 - i;
  3656. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  3657. (word32)XSTRLEN(keys[i]));
  3658. if (ret != 0)
  3659. return -3710 - i;
  3660. if (test_hmac[i].input != NULL) {
  3661. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3662. (word32)test_hmac[i].inLen);
  3663. if (ret != 0)
  3664. return -3720 - i;
  3665. }
  3666. ret = wc_HmacFinal(&hmac, hash);
  3667. if (ret != 0)
  3668. return -3730 - i;
  3669. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  3670. return -3740 - i;
  3671. wc_HmacFree(&hmac);
  3672. }
  3673. #ifndef HAVE_FIPS
  3674. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  3675. return -3750;
  3676. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  3677. return -3751;
  3678. #endif
  3679. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  3680. return -3752;
  3681. return 0;
  3682. }
  3683. #endif
  3684. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  3685. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  3686. {
  3687. Hmac hmac;
  3688. byte hash[WC_SHA384_DIGEST_SIZE];
  3689. const char* keys[]=
  3690. {
  3691. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3692. "\x0b\x0b\x0b",
  3693. "Jefe",
  3694. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3695. "\xAA\xAA\xAA",
  3696. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3697. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3698. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3699. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3700. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3701. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3702. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3703. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3704. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3705. };
  3706. testVector a, b, c, d;
  3707. testVector test_hmac[4];
  3708. int ret;
  3709. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3710. a.input = "Hi There";
  3711. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  3712. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  3713. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  3714. "\xfa\x9c\xb6";
  3715. a.inLen = XSTRLEN(a.input);
  3716. a.outLen = WC_SHA384_DIGEST_SIZE;
  3717. b.input = "what do ya want for nothing?";
  3718. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  3719. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  3720. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  3721. "\xb2\x16\x49";
  3722. b.inLen = XSTRLEN(b.input);
  3723. b.outLen = WC_SHA384_DIGEST_SIZE;
  3724. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3725. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3726. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3727. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3728. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  3729. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  3730. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  3731. "\xa3\x4f\x27";
  3732. c.inLen = XSTRLEN(c.input);
  3733. c.outLen = WC_SHA384_DIGEST_SIZE;
  3734. d.input = "Big Key Input";
  3735. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  3736. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  3737. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  3738. "\x57\x41\x69";
  3739. d.inLen = XSTRLEN(d.input);
  3740. d.outLen = WC_SHA384_DIGEST_SIZE;
  3741. test_hmac[0] = a;
  3742. test_hmac[1] = b;
  3743. test_hmac[2] = c;
  3744. test_hmac[3] = d;
  3745. for (i = 0; i < times; ++i) {
  3746. #if defined(HAVE_FIPS)
  3747. if (i == 1)
  3748. continue; /* fips not allowed */
  3749. #endif
  3750. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3751. return -3800;
  3752. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  3753. (word32)XSTRLEN(keys[i]));
  3754. if (ret != 0)
  3755. return -3801;
  3756. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3757. (word32)test_hmac[i].inLen);
  3758. if (ret != 0)
  3759. return -3802;
  3760. ret = wc_HmacFinal(&hmac, hash);
  3761. if (ret != 0)
  3762. return -3803;
  3763. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3764. return -3804 - i;
  3765. wc_HmacFree(&hmac);
  3766. }
  3767. #ifndef HAVE_FIPS
  3768. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  3769. return -3814;
  3770. #endif
  3771. return 0;
  3772. }
  3773. #endif
  3774. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  3775. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  3776. {
  3777. Hmac hmac;
  3778. byte hash[WC_SHA512_DIGEST_SIZE];
  3779. const char* keys[]=
  3780. {
  3781. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3782. "\x0b\x0b\x0b",
  3783. "Jefe",
  3784. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3785. "\xAA\xAA\xAA",
  3786. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3787. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3788. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3789. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3790. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3791. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3792. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3793. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3794. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3795. };
  3796. testVector a, b, c, d;
  3797. testVector test_hmac[4];
  3798. int ret;
  3799. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3800. a.input = "Hi There";
  3801. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  3802. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  3803. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  3804. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  3805. "\x3a\x12\x68\x54";
  3806. a.inLen = XSTRLEN(a.input);
  3807. a.outLen = WC_SHA512_DIGEST_SIZE;
  3808. b.input = "what do ya want for nothing?";
  3809. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  3810. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  3811. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  3812. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  3813. "\x38\xbc\xe7\x37";
  3814. b.inLen = XSTRLEN(b.input);
  3815. b.outLen = WC_SHA512_DIGEST_SIZE;
  3816. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3817. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3818. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3819. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3820. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  3821. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  3822. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  3823. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  3824. "\xe1\x32\x92\xfb";
  3825. c.inLen = XSTRLEN(c.input);
  3826. c.outLen = WC_SHA512_DIGEST_SIZE;
  3827. d.input = "Big Key Input";
  3828. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  3829. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  3830. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  3831. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  3832. "\x1e\x18\xfe\xfa";
  3833. d.inLen = XSTRLEN(d.input);
  3834. d.outLen = WC_SHA512_DIGEST_SIZE;
  3835. test_hmac[0] = a;
  3836. test_hmac[1] = b;
  3837. test_hmac[2] = c;
  3838. test_hmac[3] = d;
  3839. for (i = 0; i < times; ++i) {
  3840. #if defined(HAVE_FIPS)
  3841. if (i == 1)
  3842. continue; /* fips not allowed */
  3843. #endif
  3844. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3845. return -3900;
  3846. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  3847. (word32)XSTRLEN(keys[i]));
  3848. if (ret != 0)
  3849. return -3901;
  3850. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3851. (word32)test_hmac[i].inLen);
  3852. if (ret != 0)
  3853. return -3902;
  3854. ret = wc_HmacFinal(&hmac, hash);
  3855. if (ret != 0)
  3856. return -3903;
  3857. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3858. return -3904 - i;
  3859. wc_HmacFree(&hmac);
  3860. }
  3861. #ifndef HAVE_FIPS
  3862. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  3863. return -3914;
  3864. #endif
  3865. return 0;
  3866. }
  3867. #endif
  3868. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  3869. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  3870. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  3871. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  3872. {
  3873. Hmac hmac;
  3874. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3875. const char* key[4] =
  3876. {
  3877. "Jefe",
  3878. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3879. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3880. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3881. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3882. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3883. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3884. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3885. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3886. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3887. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3888. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3889. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3890. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3891. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3892. };
  3893. const char* input[4] =
  3894. {
  3895. "what do ya want for nothing?",
  3896. "Hi There",
  3897. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3898. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3899. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3900. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3901. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3902. "Big Key Input"
  3903. };
  3904. const int hashType[4] =
  3905. {
  3906. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  3907. };
  3908. const int hashSz[4] =
  3909. {
  3910. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  3911. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  3912. };
  3913. const char* output[16] =
  3914. {
  3915. /* key = jefe, input = what do ya want for nothing? */
  3916. /* HMAC-SHA3-224 */
  3917. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  3918. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  3919. /* HMAC-SHA3-256 */
  3920. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  3921. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  3922. /* HMAC-SHA3-384 */
  3923. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  3924. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  3925. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  3926. /* HMAC-SHA3-512 */
  3927. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  3928. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  3929. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  3930. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  3931. /* key = 0b..., input = Hi There */
  3932. /* HMAC-SHA3-224 */
  3933. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  3934. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  3935. /* HMAC-SHA3-256 */
  3936. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  3937. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  3938. /* HMAC-SHA3-384 */
  3939. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  3940. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  3941. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  3942. /* HMAC-SHA3-512 */
  3943. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  3944. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  3945. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  3946. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  3947. /* key = aa..., output = dd... */
  3948. /* HMAC-SHA3-224 */
  3949. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  3950. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  3951. /* HMAC-SHA3-256 */
  3952. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  3953. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  3954. /* HMAC-SHA3-384 */
  3955. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  3956. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  3957. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  3958. /* HMAC-SHA3-512 */
  3959. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  3960. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  3961. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  3962. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  3963. /* key = big key, input = Big Key Input */
  3964. /* HMAC-SHA3-224 */
  3965. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  3966. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  3967. /* HMAC-SHA3-256 */
  3968. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  3969. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  3970. /* HMAC-SHA3-384 */
  3971. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  3972. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  3973. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  3974. /* HMAC-SHA3-512 */
  3975. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  3976. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  3977. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  3978. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  3979. };
  3980. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  3981. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  3982. ret;
  3983. #ifdef HAVE_FIPS
  3984. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  3985. * short. Skip it in FIPS builds. */
  3986. i = 1;
  3987. #endif
  3988. for (; i < iMax; i++) {
  3989. for (j = 0; j < jMax; j++) {
  3990. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3991. return -4000;
  3992. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  3993. (word32)XSTRLEN(key[i]));
  3994. if (ret != 0)
  3995. return -4001;
  3996. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  3997. (word32)XSTRLEN(input[i]));
  3998. if (ret != 0)
  3999. return -4002;
  4000. ret = wc_HmacFinal(&hmac, hash);
  4001. if (ret != 0)
  4002. return -4003;
  4003. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4004. return -4004;
  4005. wc_HmacFree(&hmac);
  4006. if (i > 0)
  4007. continue;
  4008. #ifndef HAVE_FIPS
  4009. ret = wc_HmacSizeByType(hashType[j]);
  4010. if (ret != hashSz[j])
  4011. return -4005;
  4012. #endif
  4013. }
  4014. }
  4015. return 0;
  4016. }
  4017. #endif
  4018. #ifdef WC_RC2
  4019. typedef struct rc2TestVector {
  4020. const char* input;
  4021. const char* output;
  4022. const char* key; /* Key, variable up to 128 bytes */
  4023. const char* iv; /* IV, 8-bytes */
  4024. int inLen;
  4025. int outLen;
  4026. int keyLen;
  4027. int effectiveKeyBits; /* Up to 1024 bits supported */
  4028. } rc2TestVector;
  4029. static int rc2_ecb_test(void)
  4030. {
  4031. int ret = 0;
  4032. byte cipher[RC2_BLOCK_SIZE];
  4033. byte plain[RC2_BLOCK_SIZE];
  4034. rc2TestVector a, b, c, d, e, f, g, h;
  4035. rc2TestVector test_rc2[8];
  4036. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4037. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4038. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4039. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4040. a.inLen = RC2_BLOCK_SIZE;
  4041. a.outLen = RC2_BLOCK_SIZE;
  4042. a.keyLen = 8;
  4043. a.effectiveKeyBits = 63;
  4044. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4045. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4046. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4047. b.inLen = RC2_BLOCK_SIZE;
  4048. b.outLen = RC2_BLOCK_SIZE;
  4049. b.keyLen = 8;
  4050. b.effectiveKeyBits = 64;
  4051. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4052. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4053. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4054. c.inLen = RC2_BLOCK_SIZE;
  4055. c.outLen = RC2_BLOCK_SIZE;
  4056. c.keyLen = 8;
  4057. c.effectiveKeyBits = 64;
  4058. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4059. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4060. d.key = "\x88";
  4061. d.inLen = RC2_BLOCK_SIZE;
  4062. d.outLen = RC2_BLOCK_SIZE;
  4063. d.keyLen = 1;
  4064. d.effectiveKeyBits = 64;
  4065. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4066. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4067. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4068. e.inLen = RC2_BLOCK_SIZE;
  4069. e.outLen = RC2_BLOCK_SIZE;
  4070. e.keyLen = 7;
  4071. e.effectiveKeyBits = 64;
  4072. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4073. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4074. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4075. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4076. f.inLen = RC2_BLOCK_SIZE;
  4077. f.outLen = RC2_BLOCK_SIZE;
  4078. f.keyLen = 16;
  4079. f.effectiveKeyBits = 64;
  4080. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4081. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4082. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4083. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4084. g.inLen = RC2_BLOCK_SIZE;
  4085. g.outLen = RC2_BLOCK_SIZE;
  4086. g.keyLen = 16;
  4087. g.effectiveKeyBits = 128;
  4088. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4089. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4090. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4091. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4092. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4093. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4094. "\x1e";
  4095. h.inLen = RC2_BLOCK_SIZE;
  4096. h.outLen = RC2_BLOCK_SIZE;
  4097. h.keyLen = 33;
  4098. h.effectiveKeyBits = 129;
  4099. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4100. test_rc2[0] = a;
  4101. test_rc2[1] = b;
  4102. test_rc2[2] = c;
  4103. test_rc2[3] = d;
  4104. test_rc2[4] = e;
  4105. test_rc2[5] = f;
  4106. test_rc2[6] = g;
  4107. test_rc2[7] = h;
  4108. for (i = 0; i < times; ++i) {
  4109. Rc2 enc;
  4110. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4111. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4112. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4113. NULL, test_rc2[i].effectiveKeyBits);
  4114. if (ret != 0) {
  4115. return -4100;
  4116. }
  4117. /* ECB encrypt */
  4118. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4119. (word32)test_rc2[i].outLen);
  4120. if (ret != 0) {
  4121. return -4101;
  4122. }
  4123. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4124. return -4102;
  4125. }
  4126. /* ECB decrypt */
  4127. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4128. if (ret != 0) {
  4129. return -4103;
  4130. }
  4131. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4132. return -4104;
  4133. }
  4134. }
  4135. return 0;
  4136. }
  4137. static int rc2_cbc_test(void)
  4138. {
  4139. int ret = 0;
  4140. byte cipher[128];
  4141. byte plain[128];
  4142. rc2TestVector a, b, c, d, e, f, g, h, i;
  4143. rc2TestVector test_rc2[9];
  4144. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4145. /* key length = 7, effective key bits = 63 */
  4146. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4147. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4148. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4149. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4150. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4151. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4152. a.inLen = RC2_BLOCK_SIZE*2;
  4153. a.outLen = RC2_BLOCK_SIZE*2;
  4154. a.keyLen = 8;
  4155. a.effectiveKeyBits = 63;
  4156. /* key length = 8, effective key bits = 64, all 0xFF */
  4157. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4158. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4159. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4160. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4161. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4162. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4163. b.inLen = RC2_BLOCK_SIZE*2;
  4164. b.outLen = RC2_BLOCK_SIZE*2;
  4165. b.keyLen = 8;
  4166. b.effectiveKeyBits = 64;
  4167. /* key length = 8, effective key bits = 64 */
  4168. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4169. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4170. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4171. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4172. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4173. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4174. c.inLen = RC2_BLOCK_SIZE*2;
  4175. c.outLen = RC2_BLOCK_SIZE*2;
  4176. c.keyLen = 8;
  4177. c.effectiveKeyBits = 64;
  4178. /* key length = 1, effective key bits = 64 */
  4179. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4180. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4181. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4182. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4183. d.key = "\x88";
  4184. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4185. d.inLen = RC2_BLOCK_SIZE*2;
  4186. d.outLen = RC2_BLOCK_SIZE*2;
  4187. d.keyLen = 1;
  4188. d.effectiveKeyBits = 64;
  4189. /* key length = 7, effective key bits = 64 */
  4190. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4191. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4192. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4193. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4194. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4195. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4196. e.inLen = RC2_BLOCK_SIZE*2;
  4197. e.outLen = RC2_BLOCK_SIZE*2;
  4198. e.keyLen = 7;
  4199. e.effectiveKeyBits = 64;
  4200. /* key length = 16, effective key bits = 64 */
  4201. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4202. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4203. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4204. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4205. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4206. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4207. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4208. f.inLen = RC2_BLOCK_SIZE*2;
  4209. f.outLen = RC2_BLOCK_SIZE*2;
  4210. f.keyLen = 16;
  4211. f.effectiveKeyBits = 64;
  4212. /* key length = 16, effective bits = 128 */
  4213. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4214. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4215. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4216. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4217. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4218. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4219. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4220. g.inLen = RC2_BLOCK_SIZE*2;
  4221. g.outLen = RC2_BLOCK_SIZE*2;
  4222. g.keyLen = 16;
  4223. g.effectiveKeyBits = 128;
  4224. /* key length = 33, effective bits = 129 */
  4225. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4226. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4227. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4228. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4229. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4230. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4231. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4232. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4233. "\x1e";
  4234. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4235. h.inLen = RC2_BLOCK_SIZE*2;
  4236. h.outLen = RC2_BLOCK_SIZE*2;
  4237. h.keyLen = 33;
  4238. h.effectiveKeyBits = 129;
  4239. /* key length = 10, effective bits = 40 */
  4240. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4241. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4242. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4243. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4244. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4245. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4246. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4247. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4248. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4249. "\x3E\x96";
  4250. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4251. i.inLen = RC2_BLOCK_SIZE*4;
  4252. i.outLen = RC2_BLOCK_SIZE*4;
  4253. i.keyLen = 10;
  4254. i.effectiveKeyBits = 40;
  4255. test_rc2[0] = a;
  4256. test_rc2[1] = b;
  4257. test_rc2[2] = c;
  4258. test_rc2[3] = d;
  4259. test_rc2[4] = e;
  4260. test_rc2[5] = f;
  4261. test_rc2[6] = g;
  4262. test_rc2[7] = h;
  4263. test_rc2[8] = i;
  4264. for (j = 0; j < times; ++j) {
  4265. Rc2 rc2;
  4266. XMEMSET(cipher, 0, sizeof(cipher));
  4267. XMEMSET(plain, 0, sizeof(plain));
  4268. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4269. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4270. if (ret != 0) {
  4271. return -4200;
  4272. }
  4273. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4274. test_rc2[j].inLen);
  4275. if (ret != 0) {
  4276. return -4201;
  4277. }
  4278. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4279. return -4202;
  4280. }
  4281. /* reset IV for decrypt, since overriden by encrypt operation */
  4282. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4283. if (ret != 0) {
  4284. return -4203;
  4285. }
  4286. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4287. if (ret != 0) {
  4288. return -4204;
  4289. }
  4290. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4291. return -4205;
  4292. }
  4293. }
  4294. return 0;
  4295. }
  4296. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4297. {
  4298. int ret = 0;
  4299. ret = rc2_ecb_test();
  4300. if (ret != 0) {
  4301. return ret;
  4302. }
  4303. return rc2_cbc_test();
  4304. }
  4305. #endif
  4306. #ifndef NO_RC4
  4307. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4308. {
  4309. byte cipher[16];
  4310. byte plain[16];
  4311. const char* keys[] =
  4312. {
  4313. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4314. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4315. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4316. "\xef\x01\x23\x45"
  4317. };
  4318. testVector a, b, c, d;
  4319. testVector test_arc4[4];
  4320. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4321. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4322. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4323. a.inLen = 8;
  4324. a.outLen = 8;
  4325. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4326. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4327. b.inLen = 8;
  4328. b.outLen = 8;
  4329. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4330. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4331. c.inLen = 8;
  4332. c.outLen = 8;
  4333. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4334. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4335. d.inLen = 10;
  4336. d.outLen = 10;
  4337. test_arc4[0] = a;
  4338. test_arc4[1] = b;
  4339. test_arc4[2] = c;
  4340. test_arc4[3] = d;
  4341. for (i = 0; i < times; ++i) {
  4342. Arc4 enc;
  4343. Arc4 dec;
  4344. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4345. if (i == 3)
  4346. keylen = 4;
  4347. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4348. return -4400;
  4349. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4350. return -4401;
  4351. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4352. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4353. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4354. (word32)test_arc4[i].outLen);
  4355. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4356. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4357. return -4402 - i;
  4358. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4359. return -4412 - i;
  4360. wc_Arc4Free(&enc);
  4361. wc_Arc4Free(&dec);
  4362. }
  4363. return 0;
  4364. }
  4365. #endif
  4366. WOLFSSL_TEST_SUBROUTINE int hc128_test(void)
  4367. {
  4368. #ifdef HAVE_HC128
  4369. byte cipher[16];
  4370. byte plain[16];
  4371. const char* keys[] =
  4372. {
  4373. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4374. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4375. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  4376. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  4377. };
  4378. const char* ivs[] =
  4379. {
  4380. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4381. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4382. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  4383. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  4384. };
  4385. testVector a, b, c, d;
  4386. testVector test_hc128[4];
  4387. int times = sizeof(test_hc128) / sizeof(testVector), i;
  4388. int ret = 0;
  4389. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  4390. HC128 enc[1], dec[1];
  4391. #else
  4392. HC128 *enc = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4393. HC128 *dec = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4394. if ((! enc) || (! dec)) {
  4395. ERROR_OUT(-4500, out);
  4396. }
  4397. #endif
  4398. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4399. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  4400. a.inLen = 8;
  4401. a.outLen = 8;
  4402. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4403. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  4404. b.inLen = 8;
  4405. b.outLen = 8;
  4406. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4407. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  4408. c.inLen = 8;
  4409. c.outLen = 8;
  4410. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4411. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  4412. d.inLen = 15;
  4413. d.outLen = 15;
  4414. test_hc128[0] = a;
  4415. test_hc128[1] = b;
  4416. test_hc128[2] = c;
  4417. test_hc128[3] = d;
  4418. for (i = 0; i < times; ++i) {
  4419. /* align keys/ivs in plain/cipher buffers */
  4420. XMEMCPY(plain, keys[i], 16);
  4421. XMEMCPY(cipher, ivs[i], 16);
  4422. wc_Hc128_SetKey(enc, plain, cipher);
  4423. wc_Hc128_SetKey(dec, plain, cipher);
  4424. /* align input */
  4425. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  4426. if (wc_Hc128_Process(enc, cipher, plain,
  4427. (word32)test_hc128[i].outLen) != 0) {
  4428. ret = -4501;
  4429. goto out;
  4430. }
  4431. if (wc_Hc128_Process(dec, plain, cipher,
  4432. (word32)test_hc128[i].outLen) != 0) {
  4433. ret = -4502;
  4434. goto out;
  4435. }
  4436. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen)) {
  4437. ret = -4503 - i;
  4438. goto out;
  4439. }
  4440. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen)) {
  4441. ret = -4513 - i;
  4442. goto out;
  4443. }
  4444. }
  4445. out:
  4446. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4447. if (enc)
  4448. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4449. if (dec)
  4450. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4451. #endif
  4452. return ret;
  4453. #else
  4454. return 0;
  4455. #endif /* HAVE_HC128 */
  4456. }
  4457. #ifndef NO_RABBIT
  4458. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void)
  4459. {
  4460. byte cipher[16];
  4461. byte plain[16];
  4462. const char* keys[] =
  4463. {
  4464. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4465. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4466. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  4467. };
  4468. const char* ivs[] =
  4469. {
  4470. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4471. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  4472. 0
  4473. };
  4474. testVector a, b, c;
  4475. testVector test_rabbit[3];
  4476. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  4477. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4478. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  4479. a.inLen = 8;
  4480. a.outLen = 8;
  4481. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4482. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  4483. b.inLen = 8;
  4484. b.outLen = 8;
  4485. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4486. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  4487. c.inLen = 8;
  4488. c.outLen = 8;
  4489. test_rabbit[0] = a;
  4490. test_rabbit[1] = b;
  4491. test_rabbit[2] = c;
  4492. for (i = 0; i < times; ++i) {
  4493. Rabbit enc;
  4494. Rabbit dec;
  4495. byte* iv;
  4496. /* align keys/ivs in plain/cipher buffers */
  4497. XMEMCPY(plain, keys[i], 16);
  4498. if (ivs[i]) {
  4499. XMEMCPY(cipher, ivs[i], 8);
  4500. iv = cipher;
  4501. } else
  4502. iv = NULL;
  4503. wc_RabbitSetKey(&enc, plain, iv);
  4504. wc_RabbitSetKey(&dec, plain, iv);
  4505. /* align input */
  4506. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  4507. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  4508. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  4509. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  4510. return -4600 - i;
  4511. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  4512. return -4610 - i;
  4513. }
  4514. return 0;
  4515. }
  4516. #endif /* NO_RABBIT */
  4517. #ifdef HAVE_CHACHA
  4518. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  4519. {
  4520. ChaCha enc;
  4521. ChaCha dec;
  4522. byte cipher[128];
  4523. byte plain[128];
  4524. byte sliver[64];
  4525. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  4526. word32 keySz = 32;
  4527. int ret = 0;
  4528. int i;
  4529. int times = 4;
  4530. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  4531. {
  4532. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4533. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4534. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4535. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4536. };
  4537. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  4538. {
  4539. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4540. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4541. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4542. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  4543. };
  4544. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  4545. {
  4546. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4547. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4548. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4549. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4550. };
  4551. /* 128 bit key */
  4552. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  4553. {
  4554. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4555. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4556. };
  4557. const byte* keys[] = {key1, key2, key3, key4};
  4558. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4559. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4560. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  4561. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4562. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  4563. #ifndef BENCH_EMBEDDED
  4564. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  4565. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  4566. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  4567. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  4568. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  4569. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  4570. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  4571. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  4572. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  4573. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  4574. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  4575. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  4576. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  4577. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  4578. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  4579. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  4580. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  4581. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  4582. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  4583. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  4584. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  4585. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  4586. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  4587. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  4588. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  4589. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  4590. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  4591. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  4592. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  4593. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  4594. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  4595. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  4596. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  4597. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  4598. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  4599. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  4600. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  4601. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  4602. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  4603. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  4604. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  4605. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  4606. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  4607. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  4608. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  4609. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  4610. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  4611. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  4612. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  4613. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  4614. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  4615. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  4616. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  4617. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  4618. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  4619. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  4620. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  4621. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  4622. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  4623. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  4624. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  4625. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  4626. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  4627. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  4628. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  4629. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  4630. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  4631. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  4632. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  4633. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  4634. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  4635. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  4636. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  4637. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  4638. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  4639. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  4640. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  4641. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  4642. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  4643. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  4644. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  4645. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  4646. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  4647. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  4648. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  4649. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  4650. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  4651. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  4652. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  4653. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  4654. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  4655. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  4656. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  4657. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  4658. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  4659. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  4660. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  4661. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  4662. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  4663. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  4664. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  4665. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  4666. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  4667. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  4668. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  4669. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  4670. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  4671. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  4672. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  4673. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  4674. };
  4675. #define CHACHA_BIG_TEST_SIZE 1305
  4676. #ifndef WOLFSSL_SMALL_STACK
  4677. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  4678. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  4679. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  4680. #else
  4681. byte* cipher_big;
  4682. byte* plain_big;
  4683. byte* input_big;
  4684. #endif /* WOLFSSL_SMALL_STACK */
  4685. int block_size;
  4686. #endif /* BENCH_EMBEDDED */
  4687. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  4688. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  4689. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  4690. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  4691. byte* test_chacha[4];
  4692. test_chacha[0] = a;
  4693. test_chacha[1] = b;
  4694. test_chacha[2] = c;
  4695. test_chacha[3] = d;
  4696. #ifndef BENCH_EMBEDDED
  4697. #ifdef WOLFSSL_SMALL_STACK
  4698. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4699. DYNAMIC_TYPE_TMP_BUFFER);
  4700. if (cipher_big == NULL) {
  4701. return MEMORY_E;
  4702. }
  4703. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4704. DYNAMIC_TYPE_TMP_BUFFER);
  4705. if (plain_big == NULL) {
  4706. return MEMORY_E;
  4707. }
  4708. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4709. DYNAMIC_TYPE_TMP_BUFFER);
  4710. if (input_big == NULL) {
  4711. return MEMORY_E;
  4712. }
  4713. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  4714. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  4715. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  4716. #endif /* WOLFSSL_SMALL_STACK */
  4717. #endif /* BENCH_EMBEDDED */
  4718. for (i = 0; i < times; ++i) {
  4719. if (i < 3) {
  4720. keySz = 32;
  4721. }
  4722. else {
  4723. keySz = 16;
  4724. }
  4725. XMEMCPY(plain, keys[i], keySz);
  4726. XMEMSET(cipher, 0, 32);
  4727. XMEMCPY(cipher + 4, ivs[i], 8);
  4728. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  4729. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  4730. if (ret != 0)
  4731. return ret;
  4732. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4733. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  4734. if (ret != 0)
  4735. return ret;
  4736. XMEMCPY(plain, input, 8);
  4737. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  4738. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  4739. if (ret != 0)
  4740. return ret;
  4741. if (XMEMCMP(test_chacha[i], cipher, 8))
  4742. return -4700 - i;
  4743. if (XMEMCMP(plain, input, 8))
  4744. return -4710 - i;
  4745. }
  4746. /* test of starting at a different counter
  4747. encrypts all of the information and decrypts starting at 2nd chunk */
  4748. XMEMSET(plain, 0, sizeof(plain));
  4749. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  4750. XMEMSET(cipher, 0, sizeof(cipher));
  4751. XMEMCPY(cipher + 4, ivs[0], 8);
  4752. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4753. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4754. if (ret != 0)
  4755. return ret;
  4756. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4757. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  4758. if (ret != 0)
  4759. return ret;
  4760. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  4761. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  4762. if (ret != 0)
  4763. return ret;
  4764. if (XMEMCMP(plain + 64, sliver, 64))
  4765. return -4720;
  4766. #ifndef BENCH_EMBEDDED
  4767. /* test of encrypting more data */
  4768. keySz = 32;
  4769. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4770. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4771. if (ret != 0)
  4772. return ret;
  4773. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4774. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4775. if (ret != 0)
  4776. return ret;
  4777. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  4778. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  4779. CHACHA_BIG_TEST_SIZE);
  4780. if (ret != 0)
  4781. return ret;
  4782. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4783. return -4721;
  4784. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4785. return -4722;
  4786. for (i = 0; i < 18; ++i) {
  4787. /* this will test all paths */
  4788. // block sizes: 1 2 3 4 7 8 15 16 31 32 63 64 127 128 255 256 511 512
  4789. block_size = (2 << (i%9)) - (i<9?1:0);
  4790. keySz = 32;
  4791. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4792. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4793. if (ret != 0)
  4794. return ret;
  4795. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4796. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4797. if (ret != 0)
  4798. return ret;
  4799. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, block_size);
  4800. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big, block_size);
  4801. if (ret != 0)
  4802. return ret;
  4803. if (XMEMCMP(plain_big, input_big, block_size))
  4804. return -4723-i;
  4805. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  4806. return -4724-i;
  4807. }
  4808. /* Streaming test */
  4809. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  4810. int j, rem;
  4811. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  4812. if (ret != 0)
  4813. return -4725;
  4814. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  4815. if (ret != 0)
  4816. return -4726;
  4817. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  4818. if (ret != 0)
  4819. return -4727;
  4820. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  4821. if (ret != 0)
  4822. return -4728;
  4823. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  4824. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  4825. if (ret != 0)
  4826. return -4729;
  4827. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  4828. if (ret != 0)
  4829. return -4730;
  4830. }
  4831. rem = CHACHA_BIG_TEST_SIZE - j;
  4832. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  4833. if (ret != 0)
  4834. return -4731;
  4835. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  4836. if (ret != 0)
  4837. return -4732;
  4838. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4839. return -4733;
  4840. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4841. return -4734;
  4842. }
  4843. #ifdef WOLFSSL_SMALL_STACK
  4844. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4845. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4846. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4847. #endif /* WOLFSSL_SMALL_STACK */
  4848. #endif /* BENCH_EMBEDDED */
  4849. return 0;
  4850. }
  4851. #endif /* HAVE_CHACHA */
  4852. #ifdef HAVE_POLY1305
  4853. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  4854. {
  4855. int ret = 0;
  4856. int i;
  4857. byte tag[16];
  4858. Poly1305 enc;
  4859. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  4860. {
  4861. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  4862. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  4863. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  4864. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  4865. 0x75,0x70
  4866. };
  4867. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  4868. {
  4869. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  4870. 0x6c,0x64,0x21
  4871. };
  4872. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  4873. {
  4874. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4875. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4876. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4877. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4878. };
  4879. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  4880. {
  4881. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4882. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4883. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4884. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4885. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4886. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4887. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4888. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4889. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4890. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4891. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4892. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4893. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4894. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4895. 0x61,0x16
  4896. };
  4897. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  4898. {
  4899. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4900. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4901. };
  4902. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  4903. {
  4904. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4905. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4906. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4907. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4908. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4909. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4910. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4911. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4912. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4913. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4914. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4915. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4916. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4917. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4918. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4919. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4920. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4921. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4922. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4923. 0x61,0x16
  4924. };
  4925. byte additional[] =
  4926. {
  4927. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  4928. 0xc4,0xc5,0xc6,0xc7
  4929. };
  4930. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  4931. {
  4932. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4933. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4934. };
  4935. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  4936. {
  4937. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  4938. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  4939. };
  4940. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  4941. {
  4942. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  4943. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  4944. };
  4945. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  4946. {
  4947. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  4948. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  4949. };
  4950. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  4951. {
  4952. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  4953. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  4954. };
  4955. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  4956. {
  4957. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4958. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4959. };
  4960. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  4961. {
  4962. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  4963. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  4964. };
  4965. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  4966. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  4967. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  4968. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4969. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4970. };
  4971. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4972. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  4973. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  4974. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  4975. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  4976. };
  4977. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  4978. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  4979. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  4980. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  4981. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  4982. };
  4983. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  4984. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4985. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4986. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4987. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4988. };
  4989. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  4990. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  4991. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  4992. const byte* keys[] = {key, key, key2, key2, key5, key};
  4993. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  4994. correct6};
  4995. for (i = 0; i < 6; i++) {
  4996. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  4997. if (ret != 0)
  4998. return -4800 - i;
  4999. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5000. if (ret != 0)
  5001. return -4810 - i;
  5002. ret = wc_Poly1305Final(&enc, tag);
  5003. if (ret != 0)
  5004. return -4820 - i;
  5005. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5006. return -4830 - i;
  5007. }
  5008. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5009. XMEMSET(tag, 0, sizeof(tag));
  5010. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5011. if (ret != 0)
  5012. return -4840;
  5013. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5014. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5015. if (ret != 0)
  5016. return -4841;
  5017. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5018. return -4842;
  5019. /* Check fail of TLS MAC function if altering additional data */
  5020. XMEMSET(tag, 0, sizeof(tag));
  5021. additional[0]++;
  5022. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5023. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5024. if (ret != 0)
  5025. return -4843;
  5026. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5027. return -4844;
  5028. return 0;
  5029. }
  5030. #endif /* HAVE_POLY1305 */
  5031. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5032. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5033. {
  5034. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5035. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5036. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5037. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5038. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5039. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5040. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5041. };
  5042. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5043. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5044. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5045. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5046. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5047. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5048. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5049. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5050. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5051. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5052. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5053. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5054. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5055. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5056. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5057. 0x74, 0x2e
  5058. };
  5059. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5060. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5061. 0x44, 0x45, 0x46, 0x47
  5062. };
  5063. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5064. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5065. 0xc4, 0xc5, 0xc6, 0xc7
  5066. };
  5067. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5068. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5069. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5070. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5071. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5072. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5073. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5074. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5075. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5076. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5077. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5078. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5079. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5080. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5081. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5082. 0x61, 0x16
  5083. };
  5084. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5085. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5086. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5087. };
  5088. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5089. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5090. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5091. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5092. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5093. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5094. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5095. };
  5096. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5097. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5098. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5099. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5100. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5101. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5102. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5103. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5104. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5105. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5106. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5107. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5108. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5109. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5110. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5111. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5112. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5113. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5114. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5115. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5116. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5117. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5118. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5119. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5120. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5121. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5122. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5123. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5124. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5125. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5126. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5127. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5128. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5129. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5130. 0x9d
  5131. };
  5132. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5133. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5134. 0x05, 0x06, 0x07, 0x08
  5135. };
  5136. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5137. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5138. 0x00, 0x00, 0x4e, 0x91
  5139. };
  5140. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5141. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5142. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5143. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5144. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5145. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5146. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5147. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5148. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5149. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5150. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5151. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5152. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5153. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5154. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5155. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5156. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5157. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5158. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5159. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5160. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5161. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5162. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5163. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5164. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5165. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5166. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5167. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5168. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5169. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5170. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5171. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5172. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5173. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5174. 0x9b
  5175. };
  5176. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5177. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5178. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5179. };
  5180. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5181. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5182. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5183. int err;
  5184. ChaChaPoly_Aead aead;
  5185. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5186. #define TEST_SMALL_CHACHA_CHUNKS 32
  5187. #else
  5188. #define TEST_SMALL_CHACHA_CHUNKS 64
  5189. #endif
  5190. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5191. word32 testLen;
  5192. #endif
  5193. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5194. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5195. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5196. /* Parameter Validation testing */
  5197. /* Encrypt */
  5198. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5199. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5200. if (err != BAD_FUNC_ARG)
  5201. return -4900;
  5202. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5203. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5204. generatedAuthTag);
  5205. if (err != BAD_FUNC_ARG)
  5206. return -4901;
  5207. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5208. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5209. if (err != BAD_FUNC_ARG)
  5210. return -4902;
  5211. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5212. sizeof(plaintext1), NULL, generatedAuthTag);
  5213. if (err != BAD_FUNC_ARG)
  5214. return -4903;
  5215. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5216. sizeof(plaintext1), generatedCiphertext, NULL);
  5217. if (err != BAD_FUNC_ARG)
  5218. return -4904;
  5219. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5220. 0, generatedCiphertext, generatedAuthTag);
  5221. if (err != BAD_FUNC_ARG)
  5222. return -4905;
  5223. /* Decrypt */
  5224. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5225. sizeof(cipher2), authTag2, generatedPlaintext);
  5226. if (err != BAD_FUNC_ARG)
  5227. return -4906;
  5228. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5229. sizeof(cipher2), authTag2, generatedPlaintext);
  5230. if (err != BAD_FUNC_ARG)
  5231. return -4907;
  5232. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5233. sizeof(cipher2), authTag2, generatedPlaintext);
  5234. if (err != BAD_FUNC_ARG)
  5235. return -4908;
  5236. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5237. sizeof(cipher2), NULL, generatedPlaintext);
  5238. if (err != BAD_FUNC_ARG)
  5239. return -4909;
  5240. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5241. sizeof(cipher2), authTag2, NULL);
  5242. if (err != BAD_FUNC_ARG)
  5243. return -4910;
  5244. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5245. 0, authTag2, generatedPlaintext);
  5246. if (err != BAD_FUNC_ARG)
  5247. return -4911;
  5248. /* Test #1 */
  5249. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5250. aad1, sizeof(aad1),
  5251. plaintext1, sizeof(plaintext1),
  5252. generatedCiphertext, generatedAuthTag);
  5253. if (err) {
  5254. return err;
  5255. }
  5256. /* -- Check the ciphertext and authtag */
  5257. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5258. return -4912;
  5259. }
  5260. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5261. return -4913;
  5262. }
  5263. /* -- Verify decryption works */
  5264. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5265. aad1, sizeof(aad1),
  5266. cipher1, sizeof(cipher1),
  5267. authTag1, generatedPlaintext);
  5268. if (err) {
  5269. return err;
  5270. }
  5271. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5272. return -4914;
  5273. }
  5274. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5275. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5276. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5277. /* Test #2 */
  5278. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5279. aad2, sizeof(aad2),
  5280. plaintext2, sizeof(plaintext2),
  5281. generatedCiphertext, generatedAuthTag);
  5282. if (err) {
  5283. return err;
  5284. }
  5285. /* -- Check the ciphertext and authtag */
  5286. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5287. return -4915;
  5288. }
  5289. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5290. return -4916;
  5291. }
  5292. /* -- Verify decryption works */
  5293. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5294. aad2, sizeof(aad2),
  5295. cipher2, sizeof(cipher2),
  5296. authTag2, generatedPlaintext);
  5297. if (err) {
  5298. return err;
  5299. }
  5300. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5301. return -4917;
  5302. }
  5303. /* AEAD init/update/final - bad argument tests */
  5304. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5305. CHACHA20_POLY1305_AEAD_DECRYPT);
  5306. if (err != BAD_FUNC_ARG)
  5307. return -4918;
  5308. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5309. CHACHA20_POLY1305_AEAD_DECRYPT);
  5310. if (err != BAD_FUNC_ARG)
  5311. return -4919;
  5312. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5313. CHACHA20_POLY1305_AEAD_DECRYPT);
  5314. if (err != BAD_FUNC_ARG)
  5315. return -4920;
  5316. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5317. if (err != BAD_FUNC_ARG)
  5318. return -4921;
  5319. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5320. if (err != BAD_FUNC_ARG)
  5321. return -4922;
  5322. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5323. generatedPlaintext, sizeof(plaintext1));
  5324. if (err != BAD_FUNC_ARG)
  5325. return -4923;
  5326. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5327. sizeof(plaintext1));
  5328. if (err != BAD_FUNC_ARG)
  5329. return -4924;
  5330. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5331. sizeof(plaintext1));
  5332. if (err != BAD_FUNC_ARG)
  5333. return -4925;
  5334. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5335. if (err != BAD_FUNC_ARG)
  5336. return -4926;
  5337. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5338. if (err != BAD_FUNC_ARG)
  5339. return -4927;
  5340. /* AEAD init/update/final - bad state tests */
  5341. /* clear struct - make valgrind happy to resolve
  5342. "Conditional jump or move depends on uninitialised value(s)".
  5343. The enum is "int" size and aead.state is "byte" */
  5344. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5345. XMEMSET(&aead, 0, sizeof(aead));
  5346. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5347. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5348. if (err != BAD_STATE_E)
  5349. return -4928;
  5350. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5351. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5352. if (err != BAD_STATE_E)
  5353. return -4929;
  5354. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5355. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5356. generatedPlaintext, sizeof(plaintext1));
  5357. if (err != BAD_STATE_E)
  5358. return -4930;
  5359. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5360. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5361. if (err != BAD_STATE_E)
  5362. return -4931;
  5363. aead.state = CHACHA20_POLY1305_STATE_READY;
  5364. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5365. if (err != BAD_STATE_E)
  5366. return -4932;
  5367. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5368. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5369. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5370. /* Test 1 - Encrypt */
  5371. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5372. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5373. if (err != 0)
  5374. return -4933;
  5375. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5376. if (err != 0)
  5377. return -4934;
  5378. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5379. /* test doing data in smaller chunks */
  5380. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5381. word32 dataLen = sizeof(plaintext1) - testLen;
  5382. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5383. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5384. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5385. &generatedCiphertext[testLen], dataLen);
  5386. if (err != 0)
  5387. return -4935;
  5388. testLen += dataLen;
  5389. }
  5390. #else
  5391. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5392. generatedCiphertext, sizeof(plaintext1));
  5393. #endif
  5394. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5395. if (err != 0)
  5396. return -4936;
  5397. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5398. if (err != 0)
  5399. return -4937;
  5400. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5401. return -4938;
  5402. }
  5403. /* Test 1 - Decrypt */
  5404. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5405. CHACHA20_POLY1305_AEAD_DECRYPT);
  5406. if (err != 0)
  5407. return -4939;
  5408. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5409. if (err != 0)
  5410. return -4940;
  5411. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5412. /* test doing data in smaller chunks */
  5413. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5414. word32 dataLen = sizeof(plaintext1) - testLen;
  5415. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5416. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5417. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5418. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5419. dataLen);
  5420. if (err != 0)
  5421. return -4941;
  5422. testLen += dataLen;
  5423. }
  5424. #else
  5425. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5426. generatedPlaintext, sizeof(cipher1));
  5427. #endif
  5428. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5429. if (err != 0)
  5430. return -4942;
  5431. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5432. if (err != 0)
  5433. return -4943;
  5434. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5435. return -4944;
  5436. }
  5437. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5438. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5439. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5440. /* Test 2 - Encrypt */
  5441. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5442. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5443. if (err != 0)
  5444. return -4945;
  5445. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5446. if (err != 0)
  5447. return -4946;
  5448. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5449. /* test doing data in smaller chunks */
  5450. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5451. word32 dataLen = sizeof(plaintext2) - testLen;
  5452. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5453. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5454. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5455. &generatedCiphertext[testLen], dataLen);
  5456. if (err != 0)
  5457. return -4947;
  5458. testLen += dataLen;
  5459. }
  5460. #else
  5461. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5462. sizeof(plaintext2));
  5463. #endif
  5464. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5465. if (err != 0)
  5466. return -4948;
  5467. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5468. if (err != 0)
  5469. return -4949;
  5470. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5471. return -4950;
  5472. }
  5473. /* Test 2 - Decrypt */
  5474. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5475. CHACHA20_POLY1305_AEAD_DECRYPT);
  5476. if (err != 0)
  5477. return -4951;
  5478. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5479. if (err != 0)
  5480. return -4952;
  5481. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5482. /* test doing data in smaller chunks */
  5483. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5484. word32 dataLen = sizeof(plaintext2) - testLen;
  5485. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5486. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5487. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5488. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5489. dataLen);
  5490. if (err != 0)
  5491. return -4953;
  5492. testLen += dataLen;
  5493. }
  5494. #else
  5495. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5496. generatedPlaintext, sizeof(cipher2));
  5497. #endif
  5498. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5499. if (err != 0)
  5500. return -4954;
  5501. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5502. if (err != 0)
  5503. return -4955;
  5504. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5505. return -4956;
  5506. }
  5507. return err;
  5508. }
  5509. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5510. #ifndef NO_DES3
  5511. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  5512. {
  5513. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  5514. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5515. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5516. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5517. };
  5518. byte plain[24];
  5519. byte cipher[24];
  5520. Des enc;
  5521. Des dec;
  5522. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  5523. {
  5524. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  5525. };
  5526. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  5527. {
  5528. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  5529. };
  5530. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  5531. {
  5532. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  5533. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  5534. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  5535. };
  5536. int ret;
  5537. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  5538. if (ret != 0)
  5539. return -5000;
  5540. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5541. if (ret != 0)
  5542. return -5001;
  5543. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  5544. if (ret != 0)
  5545. return -5002;
  5546. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5547. if (ret != 0)
  5548. return -5003;
  5549. if (XMEMCMP(plain, vector, sizeof(plain)))
  5550. return -5004;
  5551. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  5552. return -5005;
  5553. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  5554. if (ret != 0)
  5555. return -5006;
  5556. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5557. {
  5558. EncryptedInfo info;
  5559. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5560. XMEMCPY(info.iv, iv, sizeof(iv));
  5561. info.ivSz = sizeof(iv);
  5562. info.keySz = sizeof(key);
  5563. info.cipherType = WC_CIPHER_DES;
  5564. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5565. sizeof(key), WC_HASH_TYPE_SHA);
  5566. if (ret != 0)
  5567. return -5007;
  5568. /* Test invalid info ptr */
  5569. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  5570. sizeof(key), WC_HASH_TYPE_SHA);
  5571. if (ret != BAD_FUNC_ARG)
  5572. return -5008;
  5573. #ifndef NO_PWDBASED
  5574. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  5575. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5576. sizeof(key), WC_HASH_TYPE_NONE);
  5577. if (ret == 0)
  5578. return -5009;
  5579. #endif /* !NO_PWDBASED */
  5580. }
  5581. #endif
  5582. return 0;
  5583. }
  5584. #endif /* !NO_DES3 */
  5585. #ifndef NO_DES3
  5586. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  5587. {
  5588. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  5589. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5590. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5591. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5592. };
  5593. byte plain[24];
  5594. byte cipher[24];
  5595. Des3 enc;
  5596. Des3 dec;
  5597. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5598. {
  5599. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  5600. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  5601. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  5602. };
  5603. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5604. {
  5605. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  5606. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  5607. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  5608. };
  5609. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  5610. {
  5611. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  5612. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  5613. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  5614. };
  5615. int ret;
  5616. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  5617. return -5100;
  5618. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  5619. return -5101;
  5620. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  5621. if (ret != 0)
  5622. return -5102;
  5623. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  5624. if (ret != 0)
  5625. return -5103;
  5626. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5627. #if defined(WOLFSSL_ASYNC_CRYPT)
  5628. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5629. #endif
  5630. if (ret != 0)
  5631. return -5104;
  5632. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5633. #if defined(WOLFSSL_ASYNC_CRYPT)
  5634. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5635. #endif
  5636. if (ret != 0)
  5637. return -5105;
  5638. if (XMEMCMP(plain, vector, sizeof(plain)))
  5639. return -5106;
  5640. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5641. return -5107;
  5642. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  5643. /* test the same vectors with using compatibility layer */
  5644. {
  5645. DES_key_schedule ks1;
  5646. DES_key_schedule ks2;
  5647. DES_key_schedule ks3;
  5648. DES_cblock iv4;
  5649. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  5650. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  5651. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  5652. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  5653. XMEMSET(plain, 0, sizeof(plain));
  5654. XMEMSET(cipher, 0, sizeof(cipher));
  5655. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  5656. &iv4, DES_ENCRYPT);
  5657. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  5658. &iv4, DES_DECRYPT);
  5659. if (XMEMCMP(plain, vector, sizeof(plain)))
  5660. return -5108;
  5661. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5662. return -5109;
  5663. }
  5664. #endif /* OPENSSL_EXTRA */
  5665. wc_Des3Free(&enc);
  5666. wc_Des3Free(&dec);
  5667. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5668. {
  5669. EncryptedInfo info;
  5670. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5671. XMEMCPY(info.iv, iv3, sizeof(iv3));
  5672. info.ivSz = sizeof(iv3);
  5673. info.keySz = sizeof(key3);
  5674. info.cipherType = WC_CIPHER_DES3;
  5675. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  5676. sizeof(key3), WC_HASH_TYPE_SHA);
  5677. if (ret != 0)
  5678. return -5110;
  5679. }
  5680. #endif
  5681. return 0;
  5682. }
  5683. #endif /* NO_DES3 */
  5684. #ifndef NO_AES
  5685. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  5686. defined(WOLFSSL_AES_XTS)
  5687. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5688. /* pass in the function, key, iv, plain text and expected and this function
  5689. * tests that the encryption and decryption is successful */
  5690. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  5691. const byte* iv, const byte* plain, int plainSz,
  5692. const byte* expected, int expectedSz)
  5693. {
  5694. #ifdef WOLFSSL_SMALL_STACK
  5695. EVP_CIPHER_CTX *ctx = NULL;
  5696. #else
  5697. EVP_CIPHER_CTX ctx[1];
  5698. #endif
  5699. int idx, ret = 0, cipherSz;
  5700. byte* cipher;
  5701. #ifdef WOLFSSL_SMALL_STACK
  5702. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  5703. return MEMORY_E;
  5704. #endif
  5705. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5706. if (cipher == NULL) {
  5707. ret = -5120;
  5708. goto EVP_TEST_END;
  5709. }
  5710. /* test encrypt */
  5711. EVP_CIPHER_CTX_init(ctx);
  5712. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  5713. ret = -5121;
  5714. goto EVP_TEST_END;
  5715. }
  5716. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  5717. ret = -5122;
  5718. goto EVP_TEST_END;
  5719. }
  5720. cipherSz = idx;
  5721. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5722. ret = -5123;
  5723. goto EVP_TEST_END;
  5724. }
  5725. cipherSz += idx;
  5726. if (XMEMCMP(cipher, expected, plainSz)) {
  5727. ret = -5124;
  5728. goto EVP_TEST_END;
  5729. }
  5730. /* test decrypt */
  5731. EVP_CIPHER_CTX_init(ctx);
  5732. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  5733. ret = -5125;
  5734. goto EVP_TEST_END;
  5735. }
  5736. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  5737. ret = -5126;
  5738. goto EVP_TEST_END;
  5739. }
  5740. cipherSz = idx;
  5741. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5742. ret = -5127;
  5743. goto EVP_TEST_END;
  5744. }
  5745. cipherSz += idx;
  5746. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  5747. ret = -5128;
  5748. goto EVP_TEST_END;
  5749. }
  5750. EVP_TEST_END:
  5751. if (cipher)
  5752. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5753. (void)cipherSz;
  5754. #ifdef WOLFSSL_SMALL_STACK
  5755. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  5756. #endif
  5757. return ret;
  5758. }
  5759. #endif /* OPENSSL_EXTRA */
  5760. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  5761. #ifdef WOLFSSL_AES_OFB
  5762. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  5763. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  5764. {
  5765. #ifdef WOLFSSL_AES_256
  5766. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5767. {
  5768. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  5769. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  5770. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  5771. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  5772. };
  5773. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  5774. {
  5775. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  5776. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  5777. };
  5778. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  5779. {
  5780. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  5781. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  5782. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  5783. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  5784. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  5785. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  5786. };
  5787. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5788. {
  5789. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  5790. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  5791. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  5792. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  5793. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  5794. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  5795. };
  5796. #endif /* WOLFSSL_AES_256 */
  5797. #ifdef WOLFSSL_AES_128
  5798. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5799. {
  5800. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  5801. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  5802. };
  5803. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  5804. {
  5805. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5806. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5807. };
  5808. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  5809. {
  5810. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5811. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5812. };
  5813. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  5814. {
  5815. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  5816. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  5817. };
  5818. #endif /* WOLFSSL_AES_128 */
  5819. #ifdef WOLFSSL_AES_192
  5820. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  5821. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  5822. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  5823. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  5824. };
  5825. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5826. {
  5827. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5828. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5829. };
  5830. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  5831. {
  5832. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  5833. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  5834. };
  5835. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  5836. {
  5837. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5839. };
  5840. #endif /* WOLFSSL_AES_192 */
  5841. #ifdef WOLFSSL_SMALL_STACK
  5842. Aes *enc = NULL;
  5843. #else
  5844. Aes enc[1];
  5845. #endif
  5846. byte cipher[AES_BLOCK_SIZE * 4];
  5847. #ifdef HAVE_AES_DECRYPT
  5848. #ifdef WOLFSSL_SMALL_STACK
  5849. Aes *dec = NULL;
  5850. #else
  5851. Aes dec[1];
  5852. #endif
  5853. byte plain [AES_BLOCK_SIZE * 4];
  5854. #endif
  5855. int ret = 0;
  5856. #ifdef WOLFSSL_SMALL_STACK
  5857. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5858. ERROR_OUT(-1, out);
  5859. #ifdef HAVE_AES_DECRYPT
  5860. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5861. ERROR_OUT(-1, out);
  5862. #endif
  5863. #endif
  5864. XMEMSET(enc, 0, sizeof *enc);
  5865. #ifdef HAVE_AES_DECRYPT
  5866. XMEMSET(dec, 0, sizeof *dec);
  5867. #endif
  5868. #ifdef WOLFSSL_AES_128
  5869. /* 128 key size test */
  5870. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5871. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  5872. cipher2, sizeof(cipher2));
  5873. if (ret != 0) {
  5874. goto out;
  5875. }
  5876. #endif
  5877. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5878. if (ret != 0)
  5879. ERROR_OUT(-5129, out);
  5880. #ifdef HAVE_AES_DECRYPT
  5881. /* decrypt uses AES_ENCRYPTION */
  5882. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5883. if (ret != 0)
  5884. ERROR_OUT(-5130, out);
  5885. #endif
  5886. XMEMSET(cipher, 0, sizeof(cipher));
  5887. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  5888. if (ret != 0)
  5889. ERROR_OUT(-5131, out);
  5890. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  5891. ERROR_OUT(-5132, out);
  5892. #ifdef HAVE_AES_DECRYPT
  5893. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  5894. if (ret != 0)
  5895. ERROR_OUT(-5133, out);
  5896. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  5897. ERROR_OUT(-5134, out);
  5898. #endif /* HAVE_AES_DECRYPT */
  5899. #endif /* WOLFSSL_AES_128 */
  5900. #ifdef WOLFSSL_AES_192
  5901. /* 192 key size test */
  5902. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5903. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  5904. cipher3, sizeof(cipher3));
  5905. if (ret != 0) {
  5906. goto out;
  5907. }
  5908. #endif
  5909. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5910. if (ret != 0)
  5911. ERROR_OUT(-5135, out);
  5912. #ifdef HAVE_AES_DECRYPT
  5913. /* decrypt uses AES_ENCRYPTION */
  5914. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5915. if (ret != 0)
  5916. ERROR_OUT(-5136, out);
  5917. #endif
  5918. XMEMSET(cipher, 0, sizeof(cipher));
  5919. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  5920. if (ret != 0)
  5921. ERROR_OUT(-5137, out);
  5922. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  5923. ERROR_OUT(-5138, out);
  5924. #ifdef HAVE_AES_DECRYPT
  5925. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  5926. if (ret != 0)
  5927. ERROR_OUT(-5139, out);
  5928. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  5929. ERROR_OUT(-5140, out);
  5930. #endif /* HAVE_AES_DECRYPT */
  5931. #endif /* WOLFSSL_AES_192 */
  5932. #ifdef WOLFSSL_AES_256
  5933. /* 256 key size test */
  5934. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5935. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  5936. cipher1, sizeof(cipher1));
  5937. if (ret != 0) {
  5938. goto out;
  5939. }
  5940. #endif
  5941. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5942. if (ret != 0)
  5943. ERROR_OUT(-5141, out);
  5944. #ifdef HAVE_AES_DECRYPT
  5945. /* decrypt uses AES_ENCRYPTION */
  5946. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5947. if (ret != 0)
  5948. ERROR_OUT(-5142, out);
  5949. #endif
  5950. XMEMSET(cipher, 0, sizeof(cipher));
  5951. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  5952. if (ret != 0)
  5953. ERROR_OUT(-5143, out);
  5954. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  5955. ERROR_OUT(-5144, out);
  5956. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  5957. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5958. if (ret != 0)
  5959. ERROR_OUT(-5145, out);
  5960. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  5961. AES_BLOCK_SIZE))
  5962. ERROR_OUT(-5146, out);
  5963. #ifdef HAVE_AES_DECRYPT
  5964. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  5965. if (ret != 0)
  5966. ERROR_OUT(-5147, out);
  5967. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  5968. ERROR_OUT(-5148, out);
  5969. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  5970. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5971. if (ret != 0)
  5972. ERROR_OUT(-5149, out);
  5973. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  5974. AES_BLOCK_SIZE))
  5975. ERROR_OUT(-5150, out);
  5976. #endif /* HAVE_AES_DECRYPT */
  5977. /* multiple blocks at once */
  5978. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5979. if (ret != 0)
  5980. ERROR_OUT(-5151, out);
  5981. #ifdef HAVE_AES_DECRYPT
  5982. /* decrypt uses AES_ENCRYPTION */
  5983. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5984. if (ret != 0)
  5985. ERROR_OUT(-5152, out);
  5986. #endif
  5987. XMEMSET(cipher, 0, sizeof(cipher));
  5988. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  5989. if (ret != 0)
  5990. ERROR_OUT(-5153, out);
  5991. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  5992. ERROR_OUT(-5154, out);
  5993. #ifdef HAVE_AES_DECRYPT
  5994. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  5995. if (ret != 0)
  5996. ERROR_OUT(-5155, out);
  5997. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  5998. ERROR_OUT(-5156, out);
  5999. #endif /* HAVE_AES_DECRYPT */
  6000. /* inline decrypt/encrypt*/
  6001. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6002. if (ret != 0)
  6003. ERROR_OUT(-5157, out);
  6004. #ifdef HAVE_AES_DECRYPT
  6005. /* decrypt uses AES_ENCRYPTION */
  6006. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6007. if (ret != 0)
  6008. ERROR_OUT(-5158, out);
  6009. #endif
  6010. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6011. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6012. if (ret != 0)
  6013. ERROR_OUT(-5159, out);
  6014. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6015. ERROR_OUT(-5160, out);
  6016. #ifdef HAVE_AES_DECRYPT
  6017. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6018. if (ret != 0)
  6019. ERROR_OUT(-5161, out);
  6020. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6021. ERROR_OUT(-5162, out);
  6022. #endif /* HAVE_AES_DECRYPT */
  6023. /* 256 key size test leftover support */
  6024. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6025. if (ret != 0)
  6026. ERROR_OUT(-5163, out);
  6027. #ifdef HAVE_AES_DECRYPT
  6028. /* decrypt uses AES_ENCRYPTION */
  6029. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6030. if (ret != 0)
  6031. ERROR_OUT(-5164, out);
  6032. #endif
  6033. XMEMSET(cipher, 0, sizeof(cipher));
  6034. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6035. if (ret != 0)
  6036. ERROR_OUT(-5165, out);
  6037. if (XMEMCMP(cipher, cipher1, 3))
  6038. ERROR_OUT(-5166, out);
  6039. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6040. if (ret != 0)
  6041. ERROR_OUT(-5167, out);
  6042. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6043. ERROR_OUT(-5168, out);
  6044. #ifdef HAVE_AES_DECRYPT
  6045. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6046. if (ret != 0)
  6047. ERROR_OUT(-5169, out);
  6048. if (XMEMCMP(plain, plain1, 6))
  6049. ERROR_OUT(-5170, out);
  6050. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6051. if (ret != 0)
  6052. ERROR_OUT(-5171, out);
  6053. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6054. ERROR_OUT(-5172, out);
  6055. #endif /* HAVE_AES_DECRYPT */
  6056. out:
  6057. #ifdef WOLFSSL_SMALL_STACK
  6058. if (enc)
  6059. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6060. #ifdef HAVE_AES_DECRYPT
  6061. if (dec)
  6062. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6063. #endif
  6064. #endif
  6065. #endif /* WOLFSSL_AES_256 */
  6066. return ret;
  6067. }
  6068. #endif /* WOLFSSL_AES_OFB */
  6069. #if defined(WOLFSSL_AES_CFB)
  6070. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6071. static int aescfb_test(void)
  6072. {
  6073. #ifdef WOLFSSL_SMALL_STACK
  6074. Aes *enc = NULL;
  6075. #else
  6076. Aes enc[1];
  6077. #endif
  6078. int enc_inited = 0;
  6079. byte cipher[AES_BLOCK_SIZE * 4];
  6080. #ifdef HAVE_AES_DECRYPT
  6081. #ifdef WOLFSSL_SMALL_STACK
  6082. Aes *dec = NULL;
  6083. #else
  6084. Aes dec[1];
  6085. #endif
  6086. int dec_inited = 0;
  6087. byte plain [AES_BLOCK_SIZE * 4];
  6088. #endif
  6089. int ret = 0;
  6090. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6091. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6092. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6093. };
  6094. #ifdef WOLFSSL_AES_128
  6095. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6096. {
  6097. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6098. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6099. };
  6100. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6101. {
  6102. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6103. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6104. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6105. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6106. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6107. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6108. };
  6109. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6110. {
  6111. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6112. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6113. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6114. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6115. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6116. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6117. };
  6118. #endif /* WOLFSSL_AES_128 */
  6119. #ifdef WOLFSSL_AES_192
  6120. /* 192 size key test */
  6121. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6122. {
  6123. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6124. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6125. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6126. };
  6127. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6128. {
  6129. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6130. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6131. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6132. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6133. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6134. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6135. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6136. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6137. };
  6138. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6139. {
  6140. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6141. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6142. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6143. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6144. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6145. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6146. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6147. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6148. };
  6149. #endif /* WOLFSSL_AES_192 */
  6150. #ifdef WOLFSSL_AES_256
  6151. /* 256 size key simple test */
  6152. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6153. {
  6154. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6155. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6156. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6157. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6158. };
  6159. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6160. {
  6161. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6162. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6163. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6164. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6165. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6166. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6167. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6168. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6169. };
  6170. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6171. {
  6172. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6173. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6174. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6175. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6176. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6177. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6178. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6179. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6180. };
  6181. #endif /* WOLFSSL_AES_256 */
  6182. #ifdef WOLFSSL_SMALL_STACK
  6183. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6184. ERROR_OUT(-1, out);
  6185. #ifdef HAVE_AES_DECRYPT
  6186. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6187. ERROR_OUT(-1, out);
  6188. #endif
  6189. #endif
  6190. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6191. ERROR_OUT(-5173, out);
  6192. else
  6193. enc_inited = 1;
  6194. #ifdef HAVE_AES_DECRYPT
  6195. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6196. ERROR_OUT(-5174, out);
  6197. else
  6198. dec_inited = 1;
  6199. #endif
  6200. #ifdef WOLFSSL_AES_128
  6201. /* 128 key tests */
  6202. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6203. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6204. cipher1, sizeof(cipher1));
  6205. if (ret != 0) {
  6206. return ret;
  6207. }
  6208. #endif
  6209. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6210. if (ret != 0)
  6211. ERROR_OUT(-5175, out);
  6212. #ifdef HAVE_AES_DECRYPT
  6213. /* decrypt uses AES_ENCRYPTION */
  6214. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6215. if (ret != 0)
  6216. ERROR_OUT(-5176, out);
  6217. #endif
  6218. XMEMSET(cipher, 0, sizeof(cipher));
  6219. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6220. if (ret != 0)
  6221. ERROR_OUT(-5177, out);
  6222. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6223. ERROR_OUT(-5178, out);
  6224. /* test restarting encryption process */
  6225. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6226. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6227. if (ret != 0)
  6228. ERROR_OUT(-5179, out);
  6229. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6230. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6231. ERROR_OUT(-5180, out);
  6232. #ifdef HAVE_AES_DECRYPT
  6233. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6234. if (ret != 0)
  6235. ERROR_OUT(-5181, out);
  6236. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6237. ERROR_OUT(-5182, out);
  6238. #endif /* HAVE_AES_DECRYPT */
  6239. #endif /* WOLFSSL_AES_128 */
  6240. #ifdef WOLFSSL_AES_192
  6241. /* 192 key size test */
  6242. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6243. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6244. cipher2, sizeof(cipher2));
  6245. if (ret != 0) {
  6246. return ret;
  6247. }
  6248. #endif
  6249. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6250. if (ret != 0)
  6251. ERROR_OUT(-5183, out);
  6252. #ifdef HAVE_AES_DECRYPT
  6253. /* decrypt uses AES_ENCRYPTION */
  6254. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6255. if (ret != 0)
  6256. ERROR_OUT(-5184, out);
  6257. #endif
  6258. XMEMSET(cipher, 0, sizeof(cipher));
  6259. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6260. if (ret != 0)
  6261. ERROR_OUT(-5185, out);
  6262. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6263. ERROR_OUT(-5186, out);
  6264. #ifdef HAVE_AES_DECRYPT
  6265. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6266. if (ret != 0)
  6267. ERROR_OUT(-5187, out);
  6268. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6269. ERROR_OUT(-5188, out);
  6270. #endif /* HAVE_AES_DECRYPT */
  6271. #endif /* WOLFSSL_AES_192 */
  6272. #ifdef WOLFSSL_AES_256
  6273. /* 256 key size test */
  6274. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6275. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6276. cipher3, sizeof(cipher3));
  6277. if (ret != 0) {
  6278. return ret;
  6279. }
  6280. #endif
  6281. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6282. if (ret != 0)
  6283. ERROR_OUT(-5189, out);
  6284. #ifdef HAVE_AES_DECRYPT
  6285. /* decrypt uses AES_ENCRYPTION */
  6286. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6287. if (ret != 0)
  6288. ERROR_OUT(-5190, out);
  6289. #endif
  6290. /* test with data left overs, magic lengths are checking near edges */
  6291. XMEMSET(cipher, 0, sizeof(cipher));
  6292. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6293. if (ret != 0)
  6294. ERROR_OUT(-5191, out);
  6295. if (XMEMCMP(cipher, cipher3, 4))
  6296. ERROR_OUT(-5192, out);
  6297. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6298. if (ret != 0)
  6299. ERROR_OUT(-5193, out);
  6300. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6301. ERROR_OUT(-5194, out);
  6302. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6303. (AES_BLOCK_SIZE * 4) - 31);
  6304. if (ret != 0)
  6305. ERROR_OUT(-5195, out);
  6306. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6307. ERROR_OUT(-5196, out);
  6308. #ifdef HAVE_AES_DECRYPT
  6309. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6310. if (ret != 0)
  6311. ERROR_OUT(-5197, out);
  6312. if (XMEMCMP(plain, msg3, 4))
  6313. ERROR_OUT(-5198, out);
  6314. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6315. if (ret != 0)
  6316. ERROR_OUT(-5199, out);
  6317. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6318. if (ret != 0)
  6319. ERROR_OUT(-5200, out);
  6320. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6321. ERROR_OUT(-5201, out);
  6322. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6323. (AES_BLOCK_SIZE * 4) - 31);
  6324. if (ret != 0)
  6325. ERROR_OUT(-5202, out);
  6326. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6327. ERROR_OUT(-5203, out);
  6328. #endif /* HAVE_AES_DECRYPT */
  6329. #endif /* WOLFSSL_AES_256 */
  6330. out:
  6331. if (enc_inited)
  6332. wc_AesFree(enc);
  6333. if (dec_inited)
  6334. wc_AesFree(dec);
  6335. #ifdef WOLFSSL_SMALL_STACK
  6336. if (enc)
  6337. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6338. #ifdef HAVE_AES_DECRYPT
  6339. if (dec)
  6340. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6341. #endif
  6342. #endif
  6343. return ret;
  6344. }
  6345. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6346. static int aescfb1_test(void)
  6347. {
  6348. #ifdef WOLFSSL_SMALL_STACK
  6349. Aes *enc = NULL;
  6350. #else
  6351. Aes enc[1];
  6352. #endif
  6353. int enc_inited = 0;
  6354. byte cipher[AES_BLOCK_SIZE];
  6355. #ifdef HAVE_AES_DECRYPT
  6356. #ifdef WOLFSSL_SMALL_STACK
  6357. Aes *dec = NULL;
  6358. #else
  6359. Aes dec[1];
  6360. #endif
  6361. int dec_inited = 0;
  6362. byte plain [AES_BLOCK_SIZE];
  6363. #endif
  6364. int ret = 0;
  6365. #ifdef WOLFSSL_AES_128
  6366. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6367. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6368. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6369. };
  6370. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6371. {
  6372. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6373. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6374. };
  6375. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6376. {
  6377. 0x00
  6378. };
  6379. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6380. {
  6381. 0xC0
  6382. };
  6383. #endif /* WOLFSSL_AES_128 */
  6384. #ifdef WOLFSSL_AES_192
  6385. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6386. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6387. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6388. };
  6389. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6390. {
  6391. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6392. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6393. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6394. };
  6395. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6396. {
  6397. 0x30
  6398. };
  6399. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6400. {
  6401. 0x80
  6402. };
  6403. #endif /* WOLFSSL_AES_192 */
  6404. #ifdef WOLFSSL_AES_256
  6405. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6406. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6407. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6408. };
  6409. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6410. {
  6411. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6412. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6413. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6414. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6415. };
  6416. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6417. {
  6418. 0xF7, 0x00
  6419. };
  6420. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6421. {
  6422. 0x41, 0xC0
  6423. };
  6424. #endif /* WOLFSSL_AES_256 */
  6425. #ifdef WOLFSSL_SMALL_STACK
  6426. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6427. ERROR_OUT(-1, out);
  6428. #ifdef HAVE_AES_DECRYPT
  6429. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6430. ERROR_OUT(-1, out);
  6431. #endif
  6432. #endif
  6433. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6434. ERROR_OUT(-5204, out);
  6435. else
  6436. enc_inited = 1;
  6437. #ifdef HAVE_AES_DECRYPT
  6438. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6439. ERROR_OUT(-5205, out);
  6440. else
  6441. dec_inited = 1;
  6442. #endif
  6443. #ifdef WOLFSSL_AES_128
  6444. /* 128 key tests */
  6445. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6446. if (ret != 0)
  6447. ERROR_OUT(-5206, out);
  6448. #ifdef HAVE_AES_DECRYPT
  6449. /* decrypt uses AES_ENCRYPTION */
  6450. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6451. if (ret != 0)
  6452. ERROR_OUT(-5207, out);
  6453. #endif
  6454. XMEMSET(cipher, 0, sizeof(cipher));
  6455. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6456. if (ret != 0)
  6457. ERROR_OUT(-5208, out);
  6458. if (cipher[0] != cipher1[0])
  6459. ERROR_OUT(-5209, out);
  6460. #ifdef HAVE_AES_DECRYPT
  6461. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6462. if (ret != 0)
  6463. ERROR_OUT(-5210, out);
  6464. if (plain[0] != msg1[0])
  6465. ERROR_OUT(-5211, out);
  6466. #endif /* HAVE_AES_DECRYPT */
  6467. #ifdef OPENSSL_EXTRA
  6468. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6469. if (ret != 0)
  6470. ERROR_OUT(-5212, out);
  6471. XMEMSET(cipher, 0, sizeof(cipher));
  6472. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6473. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6474. if (ret != 0)
  6475. ERROR_OUT(-5213, out);
  6476. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6477. cipher, sizeof(msg1));
  6478. if (ret != 0) {
  6479. goto out;
  6480. }
  6481. #endif
  6482. #endif /* WOLFSSL_AES_128 */
  6483. #ifdef WOLFSSL_AES_192
  6484. /* 192 key tests */
  6485. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6486. if (ret != 0)
  6487. ERROR_OUT(-5214, out);
  6488. XMEMSET(cipher, 0, sizeof(cipher));
  6489. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6490. if (ret != 0)
  6491. ERROR_OUT(-5215, out);
  6492. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6493. ERROR_OUT(-5216, out);
  6494. #ifdef OPENSSL_EXTRA
  6495. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6496. if (ret != 0)
  6497. ERROR_OUT(-5217, out);
  6498. XMEMSET(cipher, 0, sizeof(cipher));
  6499. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  6500. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  6501. if (ret != 0)
  6502. ERROR_OUT(-5218, out);
  6503. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  6504. cipher, sizeof(msg2));
  6505. if (ret != 0) {
  6506. goto out;
  6507. }
  6508. #endif
  6509. #endif /* WOLFSSL_AES_192 */
  6510. #ifdef WOLFSSL_AES_256
  6511. /* 256 key tests */
  6512. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6513. if (ret != 0)
  6514. ERROR_OUT(-5219, out);
  6515. XMEMSET(cipher, 0, sizeof(cipher));
  6516. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  6517. if (ret != 0)
  6518. ERROR_OUT(-5220, out);
  6519. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6520. ERROR_OUT(-5221, out);
  6521. #ifdef OPENSSL_EXTRA
  6522. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6523. if (ret != 0)
  6524. ERROR_OUT(-5222, out);
  6525. XMEMSET(cipher, 0, sizeof(cipher));
  6526. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  6527. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  6528. if (ret != 0)
  6529. ERROR_OUT(-5223, out);
  6530. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  6531. cipher, sizeof(msg3));
  6532. if (ret != 0) {
  6533. goto out;
  6534. }
  6535. #endif
  6536. out:
  6537. if (enc_inited)
  6538. wc_AesFree(enc);
  6539. #ifdef HAVE_AES_DECRYPT
  6540. if (dec_inited)
  6541. wc_AesFree(dec);
  6542. #endif
  6543. #ifdef WOLFSSL_SMALL_STACK
  6544. if (enc)
  6545. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6546. #ifdef HAVE_AES_DECRYPT
  6547. if (dec)
  6548. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6549. #endif
  6550. #endif
  6551. #endif /* WOLFSSL_AES_256 */
  6552. return ret;
  6553. }
  6554. static int aescfb8_test(void)
  6555. {
  6556. #ifdef WOLFSSL_SMALL_STACK
  6557. Aes *enc = NULL;
  6558. #else
  6559. Aes enc[1];
  6560. #endif
  6561. int enc_inited = 0;
  6562. byte cipher[AES_BLOCK_SIZE];
  6563. #ifdef HAVE_AES_DECRYPT
  6564. #ifdef WOLFSSL_SMALL_STACK
  6565. Aes *dec = NULL;
  6566. #else
  6567. Aes dec[1];
  6568. #endif
  6569. int dec_inited = 0;
  6570. byte plain [AES_BLOCK_SIZE];
  6571. #endif
  6572. int ret = 0;
  6573. #ifdef WOLFSSL_AES_128
  6574. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6575. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  6576. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  6577. };
  6578. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6579. {
  6580. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  6581. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  6582. };
  6583. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6584. {
  6585. 0xd2,0x76,0x91
  6586. };
  6587. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6588. {
  6589. 0xc9,0x06,0x35
  6590. };
  6591. #endif /* WOLFSSL_AES_128 */
  6592. #ifdef WOLFSSL_AES_192
  6593. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6594. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  6595. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  6596. };
  6597. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6598. {
  6599. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  6600. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  6601. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  6602. };
  6603. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6604. {
  6605. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  6606. 0xb5,0xe8
  6607. };
  6608. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6609. {
  6610. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  6611. 0x3a,0x60
  6612. };
  6613. #endif
  6614. #ifdef WOLFSSL_AES_256
  6615. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6616. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  6617. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  6618. };
  6619. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6620. {
  6621. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  6622. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  6623. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  6624. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  6625. };
  6626. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6627. {
  6628. 0x1c,0xff,0x95
  6629. };
  6630. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6631. {
  6632. 0xb9,0x74,0xfa
  6633. };
  6634. #endif
  6635. #ifdef WOLFSSL_SMALL_STACK
  6636. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6637. ERROR_OUT(-5238, out);
  6638. #ifdef HAVE_AES_DECRYPT
  6639. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6640. ERROR_OUT(-5239, out);
  6641. #endif
  6642. #endif
  6643. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6644. ERROR_OUT(-5224, out);
  6645. else
  6646. enc_inited = 1;
  6647. #ifdef HAVE_AES_DECRYPT
  6648. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6649. ERROR_OUT(-5225, out);
  6650. else
  6651. dec_inited = 1;
  6652. #endif
  6653. #ifdef WOLFSSL_AES_128
  6654. /* 128 key tests */
  6655. #ifdef OPENSSL_EXTRA
  6656. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  6657. cipher1, sizeof(cipher1));
  6658. if (ret != 0) {
  6659. return ret;
  6660. }
  6661. #endif
  6662. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6663. if (ret != 0)
  6664. ERROR_OUT(-5226, out);
  6665. #ifdef HAVE_AES_DECRYPT
  6666. /* decrypt uses AES_ENCRYPTION */
  6667. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6668. if (ret != 0)
  6669. ERROR_OUT(-5227, out);
  6670. #endif
  6671. XMEMSET(cipher, 0, sizeof(cipher));
  6672. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  6673. if (ret != 0)
  6674. ERROR_OUT(-5228, out);
  6675. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  6676. ERROR_OUT(-5229, out);
  6677. #ifdef HAVE_AES_DECRYPT
  6678. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  6679. if (ret != 0)
  6680. ERROR_OUT(-5230, out);
  6681. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  6682. ERROR_OUT(-5231, out);
  6683. #endif /* HAVE_AES_DECRYPT */
  6684. #endif /* WOLFSSL_AES_128 */
  6685. #ifdef WOLFSSL_AES_192
  6686. /* 192 key tests */
  6687. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6688. if (ret != 0)
  6689. ERROR_OUT(-5232, out);
  6690. XMEMSET(cipher, 0, sizeof(cipher));
  6691. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  6692. if (ret != 0)
  6693. ERROR_OUT(-5233, out);
  6694. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  6695. ERROR_OUT(-5234, out);
  6696. #ifdef OPENSSL_EXTRA
  6697. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  6698. cipher2, sizeof(msg2));
  6699. if (ret != 0) {
  6700. return ret;
  6701. }
  6702. #endif
  6703. #endif /* WOLFSSL_AES_192 */
  6704. #ifdef WOLFSSL_AES_256
  6705. /* 256 key tests */
  6706. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6707. if (ret != 0)
  6708. ERROR_OUT(-5235, out);
  6709. XMEMSET(cipher, 0, sizeof(cipher));
  6710. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  6711. if (ret != 0)
  6712. ERROR_OUT(-5236, out);
  6713. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6714. ERROR_OUT(-5237, out);
  6715. #ifdef OPENSSL_EXTRA
  6716. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  6717. cipher3, sizeof(msg3));
  6718. if (ret != 0) {
  6719. goto out;
  6720. }
  6721. #endif
  6722. out:
  6723. if (enc_inited)
  6724. wc_AesFree(enc);
  6725. #ifdef HAVE_AES_DECRYPT
  6726. if (dec_inited)
  6727. wc_AesFree(dec);
  6728. #endif
  6729. #ifdef WOLFSSL_SMALL_STACK
  6730. if (enc)
  6731. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6732. #ifdef HAVE_AES_DECRYPT
  6733. if (dec)
  6734. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6735. #endif
  6736. #endif
  6737. #endif /* WOLFSSL_AES_256 */
  6738. return ret;
  6739. }
  6740. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  6741. #endif /* WOLFSSL_AES_CFB */
  6742. static int aes_key_size_test(void)
  6743. {
  6744. int ret;
  6745. #ifdef WOLFSSL_SMALL_STACK
  6746. Aes *aes;
  6747. #else
  6748. Aes aes[1];
  6749. #endif
  6750. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6751. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6752. #ifndef WOLFSSL_CRYPTOCELL
  6753. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6754. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6755. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  6756. #endif
  6757. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6758. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6759. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6760. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6761. byte iv[] = "1234567890abcdef";
  6762. #ifndef HAVE_FIPS
  6763. word32 keySize;
  6764. #endif
  6765. #ifdef WOLFSSL_SMALL_STACK
  6766. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6767. return -5315;
  6768. #endif
  6769. #if !defined(HAVE_FIPS) || \
  6770. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  6771. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  6772. * supported with that FIPS version */
  6773. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  6774. if (ret != BAD_FUNC_ARG)
  6775. ERROR_OUT(-5300, out);
  6776. #endif
  6777. ret = wc_AesInit(aes, HEAP_HINT, devId);
  6778. /* 0 check OK for FIPSv1 */
  6779. if (ret != 0)
  6780. ERROR_OUT(-5301, out);
  6781. #ifndef HAVE_FIPS
  6782. /* Parameter Validation testing. */
  6783. ret = wc_AesGetKeySize(NULL, NULL);
  6784. if (ret != BAD_FUNC_ARG)
  6785. ERROR_OUT(-5302, out);
  6786. ret = wc_AesGetKeySize(aes, NULL);
  6787. if (ret != BAD_FUNC_ARG)
  6788. ERROR_OUT(-5303, out);
  6789. ret = wc_AesGetKeySize(NULL, &keySize);
  6790. if (ret != BAD_FUNC_ARG)
  6791. ERROR_OUT(-5304, out);
  6792. /* Crashes in FIPS */
  6793. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6794. if (ret != BAD_FUNC_ARG)
  6795. ERROR_OUT(-5305, out);
  6796. #endif
  6797. /* NULL IV indicates to use all zeros IV. */
  6798. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  6799. #ifdef WOLFSSL_AES_128
  6800. if (ret != 0)
  6801. #else
  6802. if (ret != BAD_FUNC_ARG)
  6803. #endif
  6804. ERROR_OUT(-5306, out);
  6805. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  6806. if (ret != BAD_FUNC_ARG)
  6807. ERROR_OUT(-5307, out);
  6808. /* CryptoCell handles rounds internally */
  6809. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  6810. /* Force invalid rounds */
  6811. aes->rounds = 16;
  6812. ret = wc_AesGetKeySize(aes, &keySize);
  6813. if (ret != BAD_FUNC_ARG)
  6814. ERROR_OUT(-5308, out);
  6815. #endif
  6816. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6817. #ifdef WOLFSSL_AES_128
  6818. if (ret != 0)
  6819. #else
  6820. if (ret != BAD_FUNC_ARG)
  6821. #endif
  6822. ERROR_OUT(-5309, out);
  6823. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  6824. ret = wc_AesGetKeySize(aes, &keySize);
  6825. if (ret != 0 || keySize != sizeof(key16))
  6826. ERROR_OUT(-5310, out);
  6827. #endif
  6828. #ifndef WOLFSSL_CRYPTOCELL
  6829. /* Cryptocell only supports AES-128 key size */
  6830. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  6831. #ifdef WOLFSSL_AES_192
  6832. if (ret != 0)
  6833. #else
  6834. if (ret != BAD_FUNC_ARG)
  6835. #endif
  6836. ERROR_OUT(-5311, out);
  6837. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  6838. ret = wc_AesGetKeySize(aes, &keySize);
  6839. if (ret != 0 || keySize != sizeof(key24))
  6840. ERROR_OUT(-5312, out);
  6841. #endif
  6842. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  6843. #ifdef WOLFSSL_AES_256
  6844. if (ret != 0)
  6845. #else
  6846. if (ret != BAD_FUNC_ARG)
  6847. #endif
  6848. ERROR_OUT(-5313, out);
  6849. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  6850. ret = wc_AesGetKeySize(aes, &keySize);
  6851. if (ret != 0 || keySize != sizeof(key32))
  6852. ERROR_OUT(-5314, out);
  6853. #endif
  6854. #endif /* !WOLFSSL_CRYPTOCELL */
  6855. ret = 0; /* success */
  6856. out:
  6857. #ifdef WOLFSSL_SMALL_STACK
  6858. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6859. #endif
  6860. return ret;
  6861. }
  6862. #if defined(WOLFSSL_AES_XTS)
  6863. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  6864. #ifdef WOLFSSL_AES_128
  6865. static int aes_xts_128_test(void)
  6866. {
  6867. #ifdef WOLFSSL_SMALL_STACK
  6868. XtsAes *aes = NULL;
  6869. #else
  6870. XtsAes aes[1];
  6871. #endif
  6872. int aes_inited = 0;
  6873. int ret = 0;
  6874. unsigned char buf[AES_BLOCK_SIZE * 2];
  6875. unsigned char cipher[AES_BLOCK_SIZE * 2];
  6876. /* 128 key tests */
  6877. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6878. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  6879. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  6880. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  6881. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  6882. };
  6883. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6884. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  6885. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6886. };
  6887. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6888. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6889. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  6890. };
  6891. /* plain text test of partial block is not from NIST test vector list */
  6892. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6893. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6894. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6895. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6896. };
  6897. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6898. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  6899. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  6900. };
  6901. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6902. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  6903. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  6904. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  6905. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  6906. };
  6907. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6908. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  6909. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  6910. };
  6911. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6912. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  6913. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  6914. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  6915. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  6916. };
  6917. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6918. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  6919. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  6920. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  6921. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  6922. };
  6923. #ifdef WOLFSSL_SMALL_STACK
  6924. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6925. ERROR_OUT(-5417, out);
  6926. #endif
  6927. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6928. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6929. if (ret != 0) {
  6930. printf("EVP_aes_128_xts failed!\n");
  6931. goto out;
  6932. }
  6933. #endif
  6934. XMEMSET(buf, 0, sizeof(buf));
  6935. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6936. HEAP_HINT, devId) != 0)
  6937. ERROR_OUT(-5400, out);
  6938. else
  6939. aes_inited = 1;
  6940. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6941. #if defined(WOLFSSL_ASYNC_CRYPT)
  6942. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6943. #endif
  6944. if (ret != 0)
  6945. ERROR_OUT(-5401, out);
  6946. if (XMEMCMP(c2, buf, sizeof(c2)))
  6947. ERROR_OUT(-5402, out);
  6948. XMEMSET(buf, 0, sizeof(buf));
  6949. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6950. HEAP_HINT, devId) != 0)
  6951. ERROR_OUT(-5403, out);
  6952. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6953. #if defined(WOLFSSL_ASYNC_CRYPT)
  6954. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6955. #endif
  6956. if (ret != 0)
  6957. ERROR_OUT(-5404, out);
  6958. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6959. ERROR_OUT(-5405, out);
  6960. /* partial block encryption test */
  6961. XMEMSET(cipher, 0, sizeof(cipher));
  6962. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6963. #if defined(WOLFSSL_ASYNC_CRYPT)
  6964. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6965. #endif
  6966. if (ret != 0)
  6967. ERROR_OUT(-5406, out);
  6968. wc_AesXtsFree(aes);
  6969. /* partial block decrypt test */
  6970. XMEMSET(buf, 0, sizeof(buf));
  6971. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  6972. HEAP_HINT, devId) != 0)
  6973. ERROR_OUT(-5407, out);
  6974. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6975. #if defined(WOLFSSL_ASYNC_CRYPT)
  6976. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6977. #endif
  6978. if (ret != 0)
  6979. ERROR_OUT(-5408, out);
  6980. if (XMEMCMP(pp, buf, sizeof(pp)))
  6981. ERROR_OUT(-5409, out);
  6982. /* NIST decrypt test vector */
  6983. XMEMSET(buf, 0, sizeof(buf));
  6984. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  6985. #if defined(WOLFSSL_ASYNC_CRYPT)
  6986. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6987. #endif
  6988. if (ret != 0)
  6989. ERROR_OUT(-5410, out);
  6990. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6991. ERROR_OUT(-5411, out);
  6992. /* fail case with decrypting using wrong key */
  6993. XMEMSET(buf, 0, sizeof(buf));
  6994. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6995. #if defined(WOLFSSL_ASYNC_CRYPT)
  6996. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6997. #endif
  6998. if (ret != 0)
  6999. ERROR_OUT(-5412, out);
  7000. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7001. ERROR_OUT(-5413, out);
  7002. /* set correct key and retest */
  7003. XMEMSET(buf, 0, sizeof(buf));
  7004. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7005. HEAP_HINT, devId) != 0)
  7006. ERROR_OUT(-5414, out);
  7007. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7008. #if defined(WOLFSSL_ASYNC_CRYPT)
  7009. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7010. #endif
  7011. if (ret != 0)
  7012. ERROR_OUT(-5415, out);
  7013. if (XMEMCMP(p2, buf, sizeof(p2)))
  7014. ERROR_OUT(-5416, out);
  7015. out:
  7016. if (aes_inited)
  7017. wc_AesXtsFree(aes);
  7018. #ifdef WOLFSSL_SMALL_STACK
  7019. if (aes)
  7020. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7021. #endif
  7022. return ret;
  7023. }
  7024. #endif /* WOLFSSL_AES_128 */
  7025. #ifdef WOLFSSL_AES_256
  7026. static int aes_xts_256_test(void)
  7027. {
  7028. #ifdef WOLFSSL_SMALL_STACK
  7029. XtsAes *aes = NULL;
  7030. #else
  7031. XtsAes aes[1];
  7032. #endif
  7033. int aes_inited = 0;
  7034. int ret = 0;
  7035. unsigned char buf[AES_BLOCK_SIZE * 3];
  7036. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7037. /* 256 key tests */
  7038. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7039. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7040. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7041. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7042. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7043. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7044. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7045. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7046. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7047. };
  7048. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7049. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7050. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7051. };
  7052. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7053. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7054. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7055. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7056. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7057. };
  7058. /* plain text test of partial block is not from NIST test vector list */
  7059. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7060. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7061. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7062. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7063. };
  7064. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7065. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7066. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7067. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7068. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7069. };
  7070. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7071. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7072. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7073. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7074. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7075. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7076. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7077. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7078. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7079. };
  7080. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7081. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7082. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7083. };
  7084. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7085. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7086. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7087. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7088. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7089. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7090. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7091. };
  7092. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7093. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7094. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7095. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7096. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7097. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7098. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7099. };
  7100. #ifdef WOLFSSL_SMALL_STACK
  7101. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7102. ERROR_OUT(-5515, out);
  7103. #endif
  7104. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7105. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7106. if (ret != 0) {
  7107. printf("EVP_aes_256_xts failed\n");
  7108. goto out;
  7109. }
  7110. #endif
  7111. XMEMSET(buf, 0, sizeof(buf));
  7112. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7113. HEAP_HINT, devId) != 0)
  7114. ERROR_OUT(-5500, out);
  7115. else
  7116. aes_inited = 1;
  7117. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7118. #if defined(WOLFSSL_ASYNC_CRYPT)
  7119. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7120. #endif
  7121. if (ret != 0)
  7122. ERROR_OUT(-5501, out);
  7123. if (XMEMCMP(c2, buf, sizeof(c2)))
  7124. ERROR_OUT(-5502, out);
  7125. XMEMSET(buf, 0, sizeof(buf));
  7126. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7127. HEAP_HINT, devId) != 0)
  7128. ERROR_OUT(-5503, out);
  7129. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7130. #if defined(WOLFSSL_ASYNC_CRYPT)
  7131. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7132. #endif
  7133. if (ret != 0)
  7134. ERROR_OUT(-5504, out);
  7135. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7136. ERROR_OUT(-5505, out);
  7137. /* partial block encryption test */
  7138. XMEMSET(cipher, 0, sizeof(cipher));
  7139. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7140. #if defined(WOLFSSL_ASYNC_CRYPT)
  7141. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7142. #endif
  7143. if (ret != 0)
  7144. ERROR_OUT(-5506, out);
  7145. wc_AesXtsFree(aes);
  7146. /* partial block decrypt test */
  7147. XMEMSET(buf, 0, sizeof(buf));
  7148. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7149. HEAP_HINT, devId) != 0)
  7150. ERROR_OUT(-5507, out);
  7151. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7152. #if defined(WOLFSSL_ASYNC_CRYPT)
  7153. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7154. #endif
  7155. if (ret != 0)
  7156. ERROR_OUT(-5508, out);
  7157. if (XMEMCMP(pp, buf, sizeof(pp)))
  7158. ERROR_OUT(-5509, out);
  7159. /* NIST decrypt test vector */
  7160. XMEMSET(buf, 0, sizeof(buf));
  7161. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7162. #if defined(WOLFSSL_ASYNC_CRYPT)
  7163. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7164. #endif
  7165. if (ret != 0)
  7166. ERROR_OUT(-5510, out);
  7167. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7168. ERROR_OUT(-5511, out);
  7169. XMEMSET(buf, 0, sizeof(buf));
  7170. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7171. HEAP_HINT, devId) != 0)
  7172. ERROR_OUT(-5512, out);
  7173. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7174. #if defined(WOLFSSL_ASYNC_CRYPT)
  7175. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7176. #endif
  7177. if (ret != 0)
  7178. ERROR_OUT(-5513, out);
  7179. if (XMEMCMP(p2, buf, sizeof(p2)))
  7180. ERROR_OUT(-5514, out);
  7181. out:
  7182. if (aes_inited)
  7183. wc_AesXtsFree(aes);
  7184. #ifdef WOLFSSL_SMALL_STACK
  7185. if (aes)
  7186. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7187. #endif
  7188. return ret;
  7189. }
  7190. #endif /* WOLFSSL_AES_256 */
  7191. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7192. /* both 128 and 256 bit key test */
  7193. static int aes_xts_sector_test(void)
  7194. {
  7195. #ifdef WOLFSSL_SMALL_STACK
  7196. XtsAes *aes = NULL;
  7197. #else
  7198. XtsAes aes[1];
  7199. #endif
  7200. int aes_inited = 0;
  7201. int ret = 0;
  7202. unsigned char buf[AES_BLOCK_SIZE * 2];
  7203. /* 128 key tests */
  7204. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7205. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7206. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7207. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7208. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7209. };
  7210. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7211. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7212. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7213. };
  7214. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7215. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7216. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7217. };
  7218. word64 s1 = 141;
  7219. /* 256 key tests */
  7220. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7221. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7222. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7223. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7224. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7225. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7226. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7227. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7228. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7229. };
  7230. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7231. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7232. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7233. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7234. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7235. };
  7236. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7237. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7238. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7239. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7240. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7241. };
  7242. word64 s2 = 187;
  7243. #ifdef WOLFSSL_SMALL_STACK
  7244. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7245. ERROR_OUT(-5612, out);
  7246. #endif
  7247. XMEMSET(buf, 0, sizeof(buf));
  7248. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7249. HEAP_HINT, devId) != 0)
  7250. ERROR_OUT(-5600, out);
  7251. else
  7252. aes_inited = 1;
  7253. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7254. #if defined(WOLFSSL_ASYNC_CRYPT)
  7255. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7256. #endif
  7257. if (ret != 0)
  7258. ERROR_OUT(-5601, out);
  7259. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7260. ERROR_OUT(-5602, out);
  7261. /* decrypt test */
  7262. XMEMSET(buf, 0, sizeof(buf));
  7263. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7264. HEAP_HINT, devId) != 0)
  7265. ERROR_OUT(-5603, out);
  7266. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7267. #if defined(WOLFSSL_ASYNC_CRYPT)
  7268. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7269. #endif
  7270. if (ret != 0)
  7271. ERROR_OUT(-5604, out);
  7272. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7273. ERROR_OUT(-5605, out);
  7274. wc_AesXtsFree(aes);
  7275. /* 256 bit key tests */
  7276. XMEMSET(buf, 0, sizeof(buf));
  7277. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7278. HEAP_HINT, devId) != 0)
  7279. ERROR_OUT(-5606, out);
  7280. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7281. #if defined(WOLFSSL_ASYNC_CRYPT)
  7282. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7283. #endif
  7284. if (ret != 0)
  7285. ERROR_OUT(-5607, out);
  7286. if (XMEMCMP(c2, buf, sizeof(c2)))
  7287. ERROR_OUT(-5608, out);
  7288. /* decrypt test */
  7289. XMEMSET(buf, 0, sizeof(buf));
  7290. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7291. HEAP_HINT, devId) != 0)
  7292. ERROR_OUT(-5609, out);
  7293. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7294. #if defined(WOLFSSL_ASYNC_CRYPT)
  7295. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7296. #endif
  7297. if (ret != 0)
  7298. ERROR_OUT(-5610, out);
  7299. if (XMEMCMP(p2, buf, sizeof(p2)))
  7300. ERROR_OUT(-5611, out);
  7301. out:
  7302. if (aes_inited)
  7303. wc_AesXtsFree(aes);
  7304. #ifdef WOLFSSL_SMALL_STACK
  7305. if (aes)
  7306. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7307. #endif
  7308. return ret;
  7309. }
  7310. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7311. #ifdef WOLFSSL_AES_128
  7312. /* testing of bad arguments */
  7313. static int aes_xts_args_test(void)
  7314. {
  7315. #ifdef WOLFSSL_SMALL_STACK
  7316. XtsAes *aes = NULL;
  7317. #else
  7318. XtsAes aes[1];
  7319. #endif
  7320. int aes_inited = 0;
  7321. int ret;
  7322. unsigned char buf[AES_BLOCK_SIZE * 2];
  7323. /* 128 key tests */
  7324. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7325. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7326. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7327. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7328. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7329. };
  7330. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7331. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7332. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7333. };
  7334. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7335. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7336. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7337. };
  7338. word64 s1 = 141;
  7339. #ifdef WOLFSSL_SMALL_STACK
  7340. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7341. ERROR_OUT(-5708, out);
  7342. #endif
  7343. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7344. HEAP_HINT, devId) == 0)
  7345. ERROR_OUT(-5700, out);
  7346. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7347. HEAP_HINT, devId) == 0)
  7348. ERROR_OUT(-5701, out);
  7349. /* encryption operations */
  7350. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7351. HEAP_HINT, devId) != 0)
  7352. ERROR_OUT(-5702, out);
  7353. else
  7354. aes_inited = 1;
  7355. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7356. #if defined(WOLFSSL_ASYNC_CRYPT)
  7357. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7358. #endif
  7359. if (ret == 0)
  7360. ERROR_OUT(-5703, out);
  7361. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7362. #if defined(WOLFSSL_ASYNC_CRYPT)
  7363. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7364. #endif
  7365. if (ret == 0)
  7366. ERROR_OUT(-5704, out);
  7367. wc_AesXtsFree(aes);
  7368. /* decryption operations */
  7369. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7370. HEAP_HINT, devId) != 0)
  7371. ERROR_OUT(-5705, out);
  7372. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7373. #if defined(WOLFSSL_ASYNC_CRYPT)
  7374. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7375. #endif
  7376. if (ret == 0)
  7377. ERROR_OUT(-5706, out);
  7378. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  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(-5707, out);
  7384. ret = 0;
  7385. out:
  7386. if (aes_inited)
  7387. wc_AesXtsFree(aes);
  7388. #ifdef WOLFSSL_SMALL_STACK
  7389. if (aes)
  7390. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7391. #endif
  7392. return ret;
  7393. }
  7394. #endif /* WOLFSSL_AES_128 */
  7395. #endif /* WOLFSSL_AES_XTS */
  7396. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7397. static int aes_cbc_test(void)
  7398. {
  7399. byte cipher[AES_BLOCK_SIZE];
  7400. byte plain[AES_BLOCK_SIZE];
  7401. int ret;
  7402. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7403. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7404. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7405. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7406. };
  7407. byte key[] = "0123456789abcdef "; /* align */
  7408. byte iv[] = "1234567890abcdef "; /* align */
  7409. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7410. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7411. /* Parameter Validation testing. */
  7412. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7413. if (ret != BAD_FUNC_ARG)
  7414. return -5800;
  7415. #ifdef HAVE_AES_DECRYPT
  7416. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7417. if (ret != BAD_FUNC_ARG)
  7418. return -5801;
  7419. #endif
  7420. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7421. AES_BLOCK_SIZE, iv);
  7422. if (ret != 0)
  7423. return -5802;
  7424. #ifdef HAVE_AES_DECRYPT
  7425. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7426. AES_BLOCK_SIZE, iv);
  7427. if (ret != 0)
  7428. return -5803;
  7429. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7430. return -5804;
  7431. #endif /* HAVE_AES_DECRYPT */
  7432. (void)plain;
  7433. return 0;
  7434. }
  7435. #endif
  7436. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  7437. {
  7438. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7439. #ifdef WOLFSSL_SMALL_STACK
  7440. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7441. #else
  7442. Aes enc[1];
  7443. #endif
  7444. byte cipher[AES_BLOCK_SIZE * 4];
  7445. #ifdef HAVE_AES_DECRYPT
  7446. #ifdef WOLFSSL_SMALL_STACK
  7447. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7448. #else
  7449. Aes dec[1];
  7450. #endif
  7451. byte plain [AES_BLOCK_SIZE * 4];
  7452. #endif /* HAVE_AES_DECRYPT */
  7453. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  7454. int ret = 0;
  7455. #ifdef HAVE_AES_CBC
  7456. #ifdef WOLFSSL_AES_128
  7457. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7458. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7459. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7460. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7461. };
  7462. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7463. {
  7464. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  7465. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  7466. };
  7467. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  7468. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  7469. #ifdef WOLFSSL_SMALL_STACK
  7470. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7471. if (enc == NULL)
  7472. ERROR_OUT(-5948, out);
  7473. #endif
  7474. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7475. if (dec == NULL)
  7476. ERROR_OUT(-5949, out);
  7477. #endif
  7478. #endif
  7479. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7480. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  7481. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7482. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7483. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  7484. #endif
  7485. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7486. if (ret != 0)
  7487. ERROR_OUT(-5902, out);
  7488. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7489. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  7490. if (ret != 0)
  7491. ERROR_OUT(-5903, out);
  7492. #endif
  7493. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  7494. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  7495. #if defined(WOLFSSL_ASYNC_CRYPT)
  7496. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7497. #endif
  7498. if (ret != 0)
  7499. ERROR_OUT(-5904, out);
  7500. #ifdef HAVE_AES_DECRYPT
  7501. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  7502. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  7503. #if defined(WOLFSSL_ASYNC_CRYPT)
  7504. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7505. #endif
  7506. if (ret != 0)
  7507. ERROR_OUT(-5905, out);
  7508. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  7509. ERROR_OUT(-5906, out);
  7510. #endif /* HAVE_AES_DECRYPT */
  7511. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  7512. ERROR_OUT(-5907, out);
  7513. #endif /* WOLFSSL_AES_128 */
  7514. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  7515. {
  7516. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  7517. /* "All work and no play makes Jack a dull boy. " */
  7518. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7519. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7520. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7521. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7522. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7523. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7524. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7525. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7526. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7527. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7528. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7529. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7530. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7531. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7532. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7533. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7534. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7535. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7536. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7537. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7538. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7539. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7540. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7541. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7542. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7543. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7544. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7545. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7546. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7547. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7548. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7549. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7550. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7551. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7552. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7553. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7554. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7555. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7556. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7557. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7558. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7559. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7560. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7561. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7562. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7563. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7564. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7565. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  7566. };
  7567. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  7568. word32 keySz, msgSz;
  7569. #ifdef WOLFSSL_SMALL_STACK
  7570. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7571. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7572. if ((bigCipher == NULL) ||
  7573. (bigPlain == NULL)) {
  7574. if (bigCipher != NULL)
  7575. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7576. ERROR_OUT(-5947, out);
  7577. }
  7578. #else
  7579. byte bigCipher[sizeof(bigMsg)];
  7580. byte bigPlain[sizeof(bigMsg)];
  7581. #endif
  7582. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  7583. * message by AES_BLOCK_SIZE for each size of AES key. */
  7584. for (keySz = 16; keySz <= 32; keySz += 8) {
  7585. for (msgSz = AES_BLOCK_SIZE;
  7586. msgSz <= sizeof(bigMsg);
  7587. msgSz += AES_BLOCK_SIZE) {
  7588. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  7589. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  7590. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  7591. if (ret != 0) {
  7592. ret = -5908;
  7593. break;
  7594. }
  7595. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  7596. if (ret != 0) {
  7597. ret = -5909;
  7598. break;
  7599. }
  7600. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  7601. #if defined(WOLFSSL_ASYNC_CRYPT)
  7602. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7603. #endif
  7604. if (ret != 0) {
  7605. ret = -5910;
  7606. break;
  7607. }
  7608. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  7609. #if defined(WOLFSSL_ASYNC_CRYPT)
  7610. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7611. #endif
  7612. if (ret != 0) {
  7613. ret = -5911;
  7614. break;
  7615. }
  7616. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  7617. ret = -5912;
  7618. break;
  7619. }
  7620. }
  7621. if (ret != 0)
  7622. break;
  7623. }
  7624. #ifdef WOLFSSL_SMALL_STACK
  7625. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7626. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7627. #endif
  7628. if (ret != 0)
  7629. goto out;
  7630. }
  7631. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  7632. /* Test of AES IV state with encrypt/decrypt */
  7633. #ifdef WOLFSSL_AES_128
  7634. {
  7635. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  7636. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  7637. */
  7638. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7639. {
  7640. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  7641. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  7642. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  7643. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  7644. };
  7645. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  7646. {
  7647. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  7648. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  7649. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  7650. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  7651. };
  7652. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7653. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  7654. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  7655. };
  7656. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7657. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7658. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  7659. };
  7660. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7661. if (ret != 0)
  7662. ERROR_OUT(-5913, out);
  7663. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  7664. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  7665. #if defined(WOLFSSL_ASYNC_CRYPT)
  7666. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7667. #endif
  7668. if (ret != 0)
  7669. ERROR_OUT(-5914, out);
  7670. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  7671. ERROR_OUT(-5915, out);
  7672. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7673. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7674. #if defined(WOLFSSL_ASYNC_CRYPT)
  7675. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7676. #endif
  7677. if (ret != 0)
  7678. ERROR_OUT(-5916, out);
  7679. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  7680. AES_BLOCK_SIZE))
  7681. ERROR_OUT(-5917, out);
  7682. #if defined(HAVE_AES_DECRYPT)
  7683. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  7684. if (ret != 0)
  7685. ERROR_OUT(-5918, out);
  7686. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  7687. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  7688. #if defined(WOLFSSL_ASYNC_CRYPT)
  7689. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7690. #endif
  7691. if (ret != 0)
  7692. ERROR_OUT(-5919, out);
  7693. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  7694. ERROR_OUT(-5920, out);
  7695. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  7696. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7697. #if defined(WOLFSSL_ASYNC_CRYPT)
  7698. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7699. #endif
  7700. if (ret != 0)
  7701. ERROR_OUT(-5921, out);
  7702. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  7703. AES_BLOCK_SIZE))
  7704. ERROR_OUT(-5922, out);
  7705. #endif /* HAVE_AES_DECRYPT */
  7706. }
  7707. #endif /* WOLFSSL_AES_128 */
  7708. #endif /* HAVE_AES_CBC */
  7709. #ifdef WOLFSSL_AES_COUNTER
  7710. {
  7711. /* test vectors from "Recommendation for Block Cipher Modes of
  7712. * Operation" NIST Special Publication 800-38A */
  7713. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  7714. {
  7715. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  7716. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  7717. };
  7718. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  7719. {
  7720. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7721. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7722. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7723. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7724. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7725. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7726. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7727. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7728. };
  7729. #ifdef WOLFSSL_AES_128
  7730. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  7731. {
  7732. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  7733. 0xc2
  7734. };
  7735. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  7736. {
  7737. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7738. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7739. };
  7740. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  7741. {
  7742. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  7743. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  7744. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  7745. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  7746. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  7747. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  7748. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  7749. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  7750. };
  7751. #endif /* WOLFSSL_AES_128 */
  7752. #ifdef WOLFSSL_AES_192
  7753. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  7754. {
  7755. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7756. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7757. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7758. };
  7759. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  7760. {
  7761. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  7762. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  7763. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  7764. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  7765. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  7766. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  7767. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  7768. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  7769. };
  7770. #endif
  7771. #ifdef WOLFSSL_AES_256
  7772. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  7773. {
  7774. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7775. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7776. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7777. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7778. };
  7779. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  7780. {
  7781. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  7782. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  7783. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  7784. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  7785. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  7786. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  7787. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  7788. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  7789. };
  7790. #endif
  7791. #ifdef WOLFSSL_AES_128
  7792. wc_AesSetKeyDirect(enc, ctr128Key, sizeof(ctr128Key),
  7793. ctrIv, AES_ENCRYPTION);
  7794. /* Ctr only uses encrypt, even on key setup */
  7795. wc_AesSetKeyDirect(dec, ctr128Key, sizeof(ctr128Key),
  7796. ctrIv, AES_ENCRYPTION);
  7797. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(ctrPlain));
  7798. if (ret != 0) {
  7799. ERROR_OUT(-5923, out);
  7800. }
  7801. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(ctrPlain));
  7802. if (ret != 0) {
  7803. ERROR_OUT(-5924, out);
  7804. }
  7805. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7806. ERROR_OUT(-5925, out);
  7807. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  7808. ERROR_OUT(-5926, out);
  7809. /* let's try with just 9 bytes, non block size test */
  7810. wc_AesSetKeyDirect(enc, ctr128Key, AES_BLOCK_SIZE,
  7811. ctrIv, AES_ENCRYPTION);
  7812. /* Ctr only uses encrypt, even on key setup */
  7813. wc_AesSetKeyDirect(dec, ctr128Key, AES_BLOCK_SIZE,
  7814. ctrIv, AES_ENCRYPTION);
  7815. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7816. if (ret != 0) {
  7817. ERROR_OUT(-5927, out);
  7818. }
  7819. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7820. if (ret != 0) {
  7821. ERROR_OUT(-5928, out);
  7822. }
  7823. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7824. ERROR_OUT(-5929, out);
  7825. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  7826. ERROR_OUT(-5930, out);
  7827. /* and an additional 9 bytes to reuse tmp left buffer */
  7828. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7829. if (ret != 0) {
  7830. ERROR_OUT(-5931, out);
  7831. }
  7832. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7833. if (ret != 0) {
  7834. ERROR_OUT(-5932, out);
  7835. }
  7836. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7837. ERROR_OUT(-5933, out);
  7838. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  7839. ERROR_OUT(-5934, out);
  7840. #endif /* WOLFSSL_AES_128 */
  7841. #ifdef WOLFSSL_AES_192
  7842. /* 192 bit key */
  7843. wc_AesSetKeyDirect(enc, ctr192Key, sizeof(ctr192Key),
  7844. ctrIv, AES_ENCRYPTION);
  7845. /* Ctr only uses encrypt, even on key setup */
  7846. wc_AesSetKeyDirect(dec, ctr192Key, sizeof(ctr192Key),
  7847. ctrIv, AES_ENCRYPTION);
  7848. XMEMSET(plain, 0, sizeof(plain));
  7849. ret = wc_AesCtrEncrypt(enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  7850. if (ret != 0) {
  7851. ERROR_OUT(-5935, out);
  7852. }
  7853. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  7854. ERROR_OUT(-5936, out);
  7855. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7856. if (ret != 0) {
  7857. ERROR_OUT(-5937, out);
  7858. }
  7859. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  7860. ERROR_OUT(-5938, out);
  7861. #endif /* WOLFSSL_AES_192 */
  7862. #ifdef WOLFSSL_AES_256
  7863. /* 256 bit key */
  7864. wc_AesSetKeyDirect(enc, ctr256Key, sizeof(ctr256Key),
  7865. ctrIv, AES_ENCRYPTION);
  7866. /* Ctr only uses encrypt, even on key setup */
  7867. wc_AesSetKeyDirect(dec, ctr256Key, sizeof(ctr256Key),
  7868. ctrIv, AES_ENCRYPTION);
  7869. XMEMSET(plain, 0, sizeof(plain));
  7870. ret = wc_AesCtrEncrypt(enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  7871. if (ret != 0) {
  7872. ERROR_OUT(-5939, out);
  7873. }
  7874. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7875. ERROR_OUT(-5940, out);
  7876. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7877. if (ret != 0) {
  7878. ERROR_OUT(-5941, out);
  7879. }
  7880. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  7881. ERROR_OUT(-5942, out);
  7882. #endif /* WOLFSSL_AES_256 */
  7883. }
  7884. #endif /* WOLFSSL_AES_COUNTER */
  7885. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  7886. {
  7887. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7888. {
  7889. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7890. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7891. };
  7892. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7893. {
  7894. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7895. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7896. };
  7897. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7898. {
  7899. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7900. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7901. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7902. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7903. };
  7904. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7905. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7906. if (ret != 0)
  7907. ERROR_OUT(-5943, out);
  7908. wc_AesEncryptDirect(enc, cipher, niPlain);
  7909. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  7910. ERROR_OUT(-5944, out);
  7911. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7912. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  7913. if (ret != 0)
  7914. ERROR_OUT(-5945, out);
  7915. wc_AesDecryptDirect(dec, plain, niCipher);
  7916. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  7917. ERROR_OUT(-5946, out);
  7918. }
  7919. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  7920. ret = aes_key_size_test();
  7921. if (ret != 0)
  7922. goto out;
  7923. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7924. ret = aes_cbc_test();
  7925. if (ret != 0)
  7926. goto out;
  7927. #endif
  7928. #if defined(WOLFSSL_AES_XTS)
  7929. #ifdef WOLFSSL_AES_128
  7930. ret = aes_xts_128_test();
  7931. if (ret != 0)
  7932. goto out;
  7933. #endif
  7934. #ifdef WOLFSSL_AES_256
  7935. ret = aes_xts_256_test();
  7936. if (ret != 0)
  7937. goto out;
  7938. #endif
  7939. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7940. ret = aes_xts_sector_test();
  7941. if (ret != 0)
  7942. goto out;
  7943. #endif
  7944. #ifdef WOLFSSL_AES_128
  7945. ret = aes_xts_args_test();
  7946. if (ret != 0)
  7947. goto out;
  7948. #endif
  7949. #endif
  7950. #if defined(WOLFSSL_AES_CFB)
  7951. ret = aescfb_test();
  7952. if (ret != 0)
  7953. goto out;
  7954. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7955. ret = aescfb1_test();
  7956. if (ret != 0)
  7957. goto out;
  7958. ret = aescfb8_test();
  7959. if (ret != 0)
  7960. goto out;
  7961. #endif
  7962. #endif
  7963. out:
  7964. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7965. #ifdef WOLFSSL_SMALL_STACK
  7966. if (enc) {
  7967. if (ret != -5900) /* note this must match ERRROR_OUT() code
  7968. * for wc_AesInit(enc, ...) failure above.
  7969. */
  7970. wc_AesFree(enc);
  7971. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7972. }
  7973. #else
  7974. if (ret != -5900)
  7975. wc_AesFree(enc);
  7976. #endif
  7977. (void)cipher;
  7978. #ifdef HAVE_AES_DECRYPT
  7979. #ifdef WOLFSSL_SMALL_STACK
  7980. if (dec) {
  7981. if ((ret != -5900) && (ret != -5901))
  7982. /* note these codes must match the ERRROR_OUT() codes for
  7983. * wc_AesInit() failures above.
  7984. */
  7985. wc_AesFree(dec);
  7986. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7987. }
  7988. #else
  7989. if ((ret != -5900) && (ret != -5901))
  7990. wc_AesFree(dec);
  7991. #endif
  7992. (void)plain;
  7993. #endif /* HAVE_AES_DECRYPT */
  7994. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  7995. return ret;
  7996. }
  7997. #ifdef WOLFSSL_AES_192
  7998. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  7999. {
  8000. #ifdef HAVE_AES_CBC
  8001. #ifdef WOLFSSL_SMALL_STACK
  8002. Aes *enc = NULL;
  8003. #else
  8004. Aes enc[1];
  8005. #endif
  8006. byte cipher[AES_BLOCK_SIZE];
  8007. #ifdef HAVE_AES_DECRYPT
  8008. #ifdef WOLFSSL_SMALL_STACK
  8009. Aes *dec = NULL;
  8010. #else
  8011. Aes dec[1];
  8012. #endif
  8013. byte plain[AES_BLOCK_SIZE];
  8014. #endif
  8015. #endif /* HAVE_AES_CBC */
  8016. int ret = 0;
  8017. #ifdef HAVE_AES_CBC
  8018. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  8019. * Appendix F.2.3 */
  8020. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  8021. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8022. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8023. };
  8024. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8025. {
  8026. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  8027. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  8028. };
  8029. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  8030. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8031. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8032. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8033. };
  8034. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  8035. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8036. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  8037. };
  8038. #ifdef WOLFSSL_SMALL_STACK
  8039. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8040. ERROR_OUT(-6008, out);
  8041. #ifdef HAVE_AES_DECRYPT
  8042. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8043. ERROR_OUT(-6009, out);
  8044. #endif
  8045. #endif
  8046. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8047. ERROR_OUT(-6000, out);
  8048. #ifdef HAVE_AES_DECRYPT
  8049. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8050. ERROR_OUT(-6001, out);
  8051. #endif
  8052. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  8053. if (ret != 0)
  8054. ERROR_OUT(-6002, out);
  8055. #ifdef HAVE_AES_DECRYPT
  8056. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  8057. if (ret != 0)
  8058. ERROR_OUT(-6003, out);
  8059. #endif
  8060. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8061. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  8062. #if defined(WOLFSSL_ASYNC_CRYPT)
  8063. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8064. #endif
  8065. if (ret != 0)
  8066. ERROR_OUT(-6004, out);
  8067. #ifdef HAVE_AES_DECRYPT
  8068. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8069. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  8070. #if defined(WOLFSSL_ASYNC_CRYPT)
  8071. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8072. #endif
  8073. if (ret != 0)
  8074. ERROR_OUT(-6005, out);
  8075. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  8076. ERROR_OUT(-6006, out);
  8077. }
  8078. #endif
  8079. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  8080. ERROR_OUT(-6007, out);
  8081. wc_AesFree(enc);
  8082. #ifdef HAVE_AES_DECRYPT
  8083. wc_AesFree(dec);
  8084. #endif
  8085. out:
  8086. #ifdef WOLFSSL_SMALL_STACK
  8087. if (enc)
  8088. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8089. #ifdef HAVE_AES_DECRYPT
  8090. if (dec)
  8091. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8092. #endif
  8093. #endif
  8094. #endif /* HAVE_AES_CBC */
  8095. return ret;
  8096. }
  8097. #endif /* WOLFSSL_AES_192 */
  8098. #ifdef WOLFSSL_AES_256
  8099. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  8100. {
  8101. #ifdef HAVE_AES_CBC
  8102. #ifdef WOLFSSL_SMALL_STACK
  8103. Aes *enc = NULL;
  8104. #else
  8105. Aes enc[1];
  8106. #endif
  8107. byte cipher[AES_BLOCK_SIZE];
  8108. #ifdef HAVE_AES_DECRYPT
  8109. #ifdef WOLFSSL_SMALL_STACK
  8110. Aes *dec = NULL;
  8111. #else
  8112. Aes dec[1];
  8113. #endif
  8114. byte plain[AES_BLOCK_SIZE];
  8115. #endif
  8116. #endif /* HAVE_AES_CBC */
  8117. int ret = 0;
  8118. #ifdef HAVE_AES_CBC
  8119. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  8120. * Appendix F.2.5 */
  8121. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  8122. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8123. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8124. };
  8125. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8126. {
  8127. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  8128. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  8129. };
  8130. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  8131. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8132. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8133. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8134. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8135. };
  8136. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  8137. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8138. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  8139. };
  8140. #ifdef WOLFSSL_SMALL_STACK
  8141. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8142. ERROR_OUT(-6108, out);
  8143. #ifdef HAVE_AES_DECRYPT
  8144. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8145. ERROR_OUT(-6109, out);
  8146. #endif
  8147. #endif
  8148. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8149. ERROR_OUT(-6100, out);
  8150. #ifdef HAVE_AES_DECRYPT
  8151. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8152. ERROR_OUT(-6101, out);
  8153. #endif
  8154. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  8155. if (ret != 0)
  8156. ERROR_OUT(-6102, out);
  8157. #ifdef HAVE_AES_DECRYPT
  8158. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  8159. if (ret != 0)
  8160. ERROR_OUT(-6103, out);
  8161. #endif
  8162. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8163. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  8164. #if defined(WOLFSSL_ASYNC_CRYPT)
  8165. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8166. #endif
  8167. if (ret != 0)
  8168. ERROR_OUT(-6104, out);
  8169. #ifdef HAVE_AES_DECRYPT
  8170. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8171. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  8172. #if defined(WOLFSSL_ASYNC_CRYPT)
  8173. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8174. #endif
  8175. if (ret != 0)
  8176. ERROR_OUT(-6105, out);
  8177. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  8178. ERROR_OUT(-6106, out);
  8179. }
  8180. #endif
  8181. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  8182. ERROR_OUT(-6107, out);
  8183. wc_AesFree(enc);
  8184. #ifdef HAVE_AES_DECRYPT
  8185. wc_AesFree(dec);
  8186. #endif
  8187. out:
  8188. #ifdef WOLFSSL_SMALL_STACK
  8189. if (enc)
  8190. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8191. #ifdef HAVE_AES_DECRYPT
  8192. if (dec)
  8193. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8194. #endif
  8195. #endif
  8196. #endif /* HAVE_AES_CBC */
  8197. return ret;
  8198. }
  8199. #endif /* WOLFSSL_AES_256 */
  8200. #ifdef HAVE_AESGCM
  8201. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  8202. byte* plain, int plainSz, byte* cipher, int cipherSz,
  8203. byte* aad, int aadSz, byte* tag, int tagSz)
  8204. {
  8205. int ret, enc_inited = 0, dec_inited = 0;
  8206. #ifdef WOLFSSL_SMALL_STACK
  8207. Aes *enc = NULL;
  8208. Aes *dec = NULL;
  8209. #else
  8210. Aes enc[1];
  8211. Aes dec[1];
  8212. #endif
  8213. byte resultT[AES_BLOCK_SIZE];
  8214. byte resultP[AES_BLOCK_SIZE * 3];
  8215. byte resultC[AES_BLOCK_SIZE * 3];
  8216. int result;
  8217. #ifdef WOLFSSL_SMALL_STACK
  8218. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8219. ERROR_OUT(-6118, out);
  8220. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8221. ERROR_OUT(-6119, out);
  8222. #endif
  8223. XMEMSET(resultT, 0, sizeof(resultT));
  8224. XMEMSET(resultC, 0, sizeof(resultC));
  8225. XMEMSET(resultP, 0, sizeof(resultP));
  8226. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8227. ERROR_OUT(-6110, out);
  8228. else
  8229. enc_inited = 1;
  8230. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8231. ERROR_OUT(-6111, out);
  8232. else
  8233. dec_inited = 1;
  8234. result = wc_AesGcmSetKey(enc, key, keySz);
  8235. if (result != 0)
  8236. ERROR_OUT(-6112, out);
  8237. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8238. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  8239. resultT, tagSz, aad, aadSz);
  8240. #if defined(WOLFSSL_ASYNC_CRYPT)
  8241. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8242. #endif
  8243. if (result != 0)
  8244. ERROR_OUT(-6113, out);
  8245. if (cipher != NULL) {
  8246. if (XMEMCMP(cipher, resultC, cipherSz))
  8247. ERROR_OUT(-6114, out);
  8248. }
  8249. if (XMEMCMP(tag, resultT, tagSz))
  8250. ERROR_OUT(-6115, out);
  8251. #ifdef HAVE_AES_DECRYPT
  8252. result = wc_AesGcmSetKey(dec, key, keySz);
  8253. if (result != 0)
  8254. ERROR_OUT(-6116, out);
  8255. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  8256. iv, ivSz, resultT, tagSz, aad, aadSz);
  8257. #if defined(WOLFSSL_ASYNC_CRYPT)
  8258. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8259. #endif
  8260. if (result != 0)
  8261. ERROR_OUT(-6117, out);
  8262. if (plain != NULL) {
  8263. if (XMEMCMP(plain, resultP, plainSz))
  8264. ERROR_OUT(-6118, out);
  8265. }
  8266. #endif /* HAVE_AES_DECRYPT */
  8267. ret = 0;
  8268. out:
  8269. if (enc_inited)
  8270. wc_AesFree(enc);
  8271. if (dec_inited)
  8272. wc_AesFree(dec);
  8273. #ifdef WOLFSSL_SMALL_STACK
  8274. if (enc)
  8275. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8276. if (dec)
  8277. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8278. #endif
  8279. return ret;
  8280. }
  8281. /* tests that only use 12 byte IV and 16 or less byte AAD
  8282. * test vectors are from NIST SP 800-38D
  8283. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  8284. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  8285. {
  8286. byte key1[] = {
  8287. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  8288. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  8289. };
  8290. byte iv1[] = {
  8291. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  8292. 0xe4, 0xed, 0x2f, 0x6d
  8293. };
  8294. ALIGN64 byte plain1[] = {
  8295. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  8296. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  8297. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  8298. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  8299. };
  8300. byte aad1[] = {
  8301. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  8302. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  8303. };
  8304. ALIGN64 byte cipher1[] = {
  8305. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  8306. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  8307. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  8308. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  8309. };
  8310. byte tag1[] = {
  8311. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  8312. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  8313. };
  8314. byte key2[] = {
  8315. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  8316. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  8317. };
  8318. byte iv2[] = {
  8319. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  8320. 0xc9, 0x8a, 0xff, 0xe3
  8321. };
  8322. ALIGN64 byte plain2[] = {
  8323. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  8324. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  8325. };
  8326. ALIGN64 byte cipher2[] = {
  8327. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  8328. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  8329. };
  8330. byte tag2[] = {
  8331. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  8332. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  8333. };
  8334. byte key3[] = {
  8335. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  8336. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  8337. };
  8338. byte iv3[] = {
  8339. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  8340. 0x6a, 0x4b, 0xd5, 0xe1
  8341. };
  8342. byte tag3[] = {
  8343. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  8344. 0x11, 0x64, 0xb2, 0xff
  8345. };
  8346. int ret;
  8347. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  8348. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  8349. aad1, sizeof(aad1), tag1, sizeof(tag1));
  8350. if (ret != 0) {
  8351. return ret;
  8352. }
  8353. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  8354. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  8355. NULL, 0, tag2, sizeof(tag2));
  8356. if (ret != 0) {
  8357. return ret;
  8358. }
  8359. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  8360. NULL, 0, NULL, 0,
  8361. NULL, 0, tag3, sizeof(tag3));
  8362. if (ret != 0) {
  8363. return ret;
  8364. }
  8365. return 0;
  8366. }
  8367. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  8368. {
  8369. #ifdef WOLFSSL_SMALL_STACK
  8370. Aes *enc = NULL;
  8371. Aes *dec = NULL;
  8372. #else
  8373. Aes enc[1];
  8374. Aes dec[1];
  8375. #endif
  8376. /*
  8377. * This is Test Case 16 from the document Galois/
  8378. * Counter Mode of Operation (GCM) by McGrew and
  8379. * Viega.
  8380. */
  8381. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8382. {
  8383. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  8384. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  8385. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  8386. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  8387. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  8388. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  8389. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  8390. 0xba, 0x63, 0x7b, 0x39
  8391. };
  8392. #if defined(WOLFSSL_AES_256)
  8393. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8394. {
  8395. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8396. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8397. 0xab, 0xad, 0xda, 0xd2
  8398. };
  8399. #endif
  8400. #ifdef WOLFSSL_AES_256
  8401. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8402. {
  8403. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8404. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8405. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8406. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  8407. };
  8408. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8409. {
  8410. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  8411. 0xde, 0xca, 0xf8, 0x88
  8412. };
  8413. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  8414. {
  8415. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  8416. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  8417. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  8418. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  8419. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  8420. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  8421. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  8422. 0xbc, 0xc9, 0xf6, 0x62
  8423. };
  8424. #endif /* WOLFSSL_AES_256 */
  8425. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8426. {
  8427. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  8428. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  8429. };
  8430. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  8431. #if !defined(HAVE_FIPS) && \
  8432. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  8433. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  8434. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8435. !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  8436. !(defined(WOLF_CRYPTO_CB) && \
  8437. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  8438. #define ENABLE_NON_12BYTE_IV_TEST
  8439. #ifdef WOLFSSL_AES_192
  8440. /* Test Case 12, uses same plaintext and AAD data. */
  8441. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8442. {
  8443. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8444. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8445. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  8446. };
  8447. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8448. {
  8449. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  8450. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  8451. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  8452. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  8453. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  8454. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  8455. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  8456. 0xa6, 0x37, 0xb3, 0x9b
  8457. };
  8458. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  8459. {
  8460. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  8461. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  8462. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  8463. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  8464. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  8465. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  8466. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  8467. 0xe9, 0xb7, 0x37, 0x3b
  8468. };
  8469. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8470. {
  8471. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  8472. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  8473. };
  8474. #endif /* WOLFSSL_AES_192 */
  8475. #ifdef WOLFSSL_AES_128
  8476. /* The following is an interesting test case from the example
  8477. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  8478. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  8479. {
  8480. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  8481. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  8482. };
  8483. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  8484. {
  8485. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  8486. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  8487. };
  8488. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  8489. {
  8490. 0xca
  8491. };
  8492. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  8493. {
  8494. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  8495. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  8496. };
  8497. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  8498. {
  8499. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  8500. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  8501. };
  8502. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  8503. {
  8504. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  8505. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  8506. };
  8507. #endif /* WOLFSSL_AES_128 */
  8508. #ifdef WOLFSSL_AES_256
  8509. int ivlen;
  8510. #endif
  8511. #endif
  8512. byte resultT[sizeof(t1)];
  8513. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  8514. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  8515. int result = 0;
  8516. int ret;
  8517. #ifdef WOLFSSL_AES_256
  8518. int alen;
  8519. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8520. int plen;
  8521. #endif
  8522. #endif
  8523. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8524. #if !defined(BENCH_AESGCM_LARGE)
  8525. #define BENCH_AESGCM_LARGE 1024
  8526. #endif
  8527. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8528. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8529. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8530. if ((! large_input) || (! large_output) || (! large_outdec))
  8531. ERROR_OUT(MEMORY_E, out);
  8532. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  8533. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  8534. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  8535. #endif
  8536. #ifdef WOLFSSL_SMALL_STACK
  8537. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8538. ERROR_OUT(-6342, out);
  8539. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8540. ERROR_OUT(-6343, out);
  8541. #endif
  8542. (void)result;
  8543. XMEMSET(resultT, 0, sizeof(resultT));
  8544. XMEMSET(resultC, 0, sizeof(resultC));
  8545. XMEMSET(resultP, 0, sizeof(resultP));
  8546. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  8547. ERROR_OUT(-6300, out);
  8548. }
  8549. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  8550. ERROR_OUT(-6301, out);
  8551. }
  8552. #ifdef WOLFSSL_AES_256
  8553. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8554. if (result != 0)
  8555. ERROR_OUT(-6302, out);
  8556. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8557. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8558. resultT, sizeof(resultT), a, sizeof(a));
  8559. #if defined(WOLFSSL_ASYNC_CRYPT)
  8560. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8561. #endif
  8562. if (result != 0)
  8563. ERROR_OUT(-6303, out);
  8564. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8565. ERROR_OUT(-6304, out);
  8566. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  8567. ERROR_OUT(-6305, out);
  8568. #ifdef HAVE_AES_DECRYPT
  8569. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8570. if (result != 0)
  8571. ERROR_OUT(-6306, out);
  8572. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  8573. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8574. #if defined(WOLFSSL_ASYNC_CRYPT)
  8575. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8576. #endif
  8577. if (result != 0)
  8578. ERROR_OUT(-6307, out);
  8579. if (XMEMCMP(p, resultP, sizeof(p)))
  8580. ERROR_OUT(-6308, out);
  8581. #endif /* HAVE_AES_DECRYPT */
  8582. /* Large buffer test */
  8583. #ifdef BENCH_AESGCM_LARGE
  8584. /* setup test buffer */
  8585. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  8586. large_input[alen] = (byte)alen;
  8587. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8588. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8589. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  8590. resultT, sizeof(resultT), a, sizeof(a));
  8591. #if defined(WOLFSSL_ASYNC_CRYPT)
  8592. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8593. #endif
  8594. if (result != 0)
  8595. ERROR_OUT(-6309, out);
  8596. #ifdef HAVE_AES_DECRYPT
  8597. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8598. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  8599. sizeof(resultT), a, sizeof(a));
  8600. #if defined(WOLFSSL_ASYNC_CRYPT)
  8601. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8602. #endif
  8603. if (result != 0)
  8604. ERROR_OUT(-6310, out);
  8605. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  8606. ERROR_OUT(-6311, out);
  8607. #endif /* HAVE_AES_DECRYPT */
  8608. #endif /* BENCH_AESGCM_LARGE */
  8609. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  8610. /* Variable IV length test */
  8611. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  8612. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8613. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  8614. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8615. #if defined(WOLFSSL_ASYNC_CRYPT)
  8616. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8617. #endif
  8618. if (result != 0)
  8619. ERROR_OUT(-6312, out);
  8620. #ifdef HAVE_AES_DECRYPT
  8621. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  8622. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8623. #if defined(WOLFSSL_ASYNC_CRYPT)
  8624. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8625. #endif
  8626. if (result != 0)
  8627. ERROR_OUT(-6313, out);
  8628. #endif /* HAVE_AES_DECRYPT */
  8629. }
  8630. #endif
  8631. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  8632. /* Variable authenticated data length test */
  8633. for (alen=0; alen<(int)sizeof(p); alen++) {
  8634. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8635. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  8636. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8637. #if defined(WOLFSSL_ASYNC_CRYPT)
  8638. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8639. #endif
  8640. if (result != 0)
  8641. ERROR_OUT(-6314, out);
  8642. #ifdef HAVE_AES_DECRYPT
  8643. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  8644. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8645. #if defined(WOLFSSL_ASYNC_CRYPT)
  8646. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8647. #endif
  8648. if (result != 0)
  8649. ERROR_OUT(-6315, out);
  8650. #endif /* HAVE_AES_DECRYPT */
  8651. }
  8652. #endif
  8653. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8654. #ifdef BENCH_AESGCM_LARGE
  8655. /* Variable plain text length test */
  8656. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  8657. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8658. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8659. plen, iv1, sizeof(iv1), resultT,
  8660. sizeof(resultT), a, sizeof(a));
  8661. #if defined(WOLFSSL_ASYNC_CRYPT)
  8662. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8663. #endif
  8664. if (result != 0)
  8665. ERROR_OUT(-6316, out);
  8666. #ifdef HAVE_AES_DECRYPT
  8667. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8668. plen, iv1, sizeof(iv1), resultT,
  8669. sizeof(resultT), a, sizeof(a));
  8670. #if defined(WOLFSSL_ASYNC_CRYPT)
  8671. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8672. #endif
  8673. if (result != 0)
  8674. ERROR_OUT(-6317, out);
  8675. #endif /* HAVE_AES_DECRYPT */
  8676. }
  8677. #else /* BENCH_AESGCM_LARGE */
  8678. /* Variable plain text length test */
  8679. for (plen=1; plen<(int)sizeof(p); plen++) {
  8680. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8681. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  8682. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8683. #if defined(WOLFSSL_ASYNC_CRYPT)
  8684. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8685. #endif
  8686. if (result != 0)
  8687. ERROR_OUT(-6318, out);
  8688. #ifdef HAVE_AES_DECRYPT
  8689. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  8690. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8691. #if defined(WOLFSSL_ASYNC_CRYPT)
  8692. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8693. #endif
  8694. if (result != 0)
  8695. ERROR_OUT(-6319, out);
  8696. #endif /* HAVE_AES_DECRYPT */
  8697. }
  8698. #endif /* BENCH_AESGCM_LARGE */
  8699. #endif
  8700. #endif /* WOLFSSL_AES_256 */
  8701. /* test with IV != 12 bytes */
  8702. #ifdef ENABLE_NON_12BYTE_IV_TEST
  8703. XMEMSET(resultT, 0, sizeof(resultT));
  8704. XMEMSET(resultC, 0, sizeof(resultC));
  8705. XMEMSET(resultP, 0, sizeof(resultP));
  8706. #ifdef WOLFSSL_AES_192
  8707. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  8708. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8709. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  8710. resultT, sizeof(resultT), a, sizeof(a));
  8711. #if defined(WOLFSSL_ASYNC_CRYPT)
  8712. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8713. #endif
  8714. if (result != 0)
  8715. ERROR_OUT(-6320, out);
  8716. if (XMEMCMP(c2, resultC, sizeof(c2)))
  8717. ERROR_OUT(-6321, out);
  8718. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  8719. ERROR_OUT(-6322, out);
  8720. #ifdef HAVE_AES_DECRYPT
  8721. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  8722. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  8723. #if defined(WOLFSSL_ASYNC_CRYPT)
  8724. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8725. #endif
  8726. if (result != 0)
  8727. ERROR_OUT(-6323, out);
  8728. if (XMEMCMP(p, resultP, sizeof(p)))
  8729. ERROR_OUT(-6324, out);
  8730. #endif /* HAVE_AES_DECRYPT */
  8731. XMEMSET(resultT, 0, sizeof(resultT));
  8732. XMEMSET(resultC, 0, sizeof(resultC));
  8733. XMEMSET(resultP, 0, sizeof(resultP));
  8734. #endif /* WOLFSSL_AES_192 */
  8735. #ifdef WOLFSSL_AES_128
  8736. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  8737. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8738. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  8739. resultT, sizeof(t3), a3, sizeof(a3));
  8740. #if defined(WOLFSSL_ASYNC_CRYPT)
  8741. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8742. #endif
  8743. if (result != 0)
  8744. ERROR_OUT(-6325, out);
  8745. if (XMEMCMP(c3, resultC, sizeof(c3)))
  8746. ERROR_OUT(-6326, out);
  8747. if (XMEMCMP(t3, resultT, sizeof(t3)))
  8748. ERROR_OUT(-6327, out);
  8749. #ifdef HAVE_AES_DECRYPT
  8750. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  8751. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  8752. #if defined(WOLFSSL_ASYNC_CRYPT)
  8753. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8754. #endif
  8755. if (result != 0)
  8756. ERROR_OUT(-6328, out);
  8757. if (XMEMCMP(p3, resultP, sizeof(p3)))
  8758. ERROR_OUT(-6329, out);
  8759. #endif /* HAVE_AES_DECRYPT */
  8760. #endif /* WOLFSSL_AES_128 */
  8761. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  8762. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8763. !defined(WOLFSSL_XILINX_CRYPT) && \
  8764. !(defined(WOLF_CRYPTO_CB) && \
  8765. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  8766. XMEMSET(resultT, 0, sizeof(resultT));
  8767. XMEMSET(resultC, 0, sizeof(resultC));
  8768. XMEMSET(resultP, 0, sizeof(resultP));
  8769. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8770. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8771. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8772. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8773. #if defined(WOLFSSL_ASYNC_CRYPT)
  8774. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8775. #endif
  8776. if (result != 0)
  8777. ERROR_OUT(-6330, out);
  8778. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8779. ERROR_OUT(-6331, out);
  8780. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  8781. ERROR_OUT(-6332, out);
  8782. #ifdef HAVE_AES_DECRYPT
  8783. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  8784. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8785. #if defined(WOLFSSL_ASYNC_CRYPT)
  8786. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8787. #endif
  8788. if (result != 0)
  8789. ERROR_OUT(-6333, out);
  8790. if (XMEMCMP(p, resultP, sizeof(p)))
  8791. ERROR_OUT(-6334, out);
  8792. #endif /* HAVE_AES_DECRYPT */
  8793. #endif /* WOLFSSL_AES_256 */
  8794. #if !defined(HAVE_FIPS) || \
  8795. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  8796. /* Test encrypt with internally generated IV */
  8797. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  8798. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  8799. {
  8800. WC_RNG rng;
  8801. byte randIV[12];
  8802. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  8803. if (result != 0)
  8804. ERROR_OUT(-6335, out);
  8805. XMEMSET(randIV, 0, sizeof(randIV));
  8806. XMEMSET(resultT, 0, sizeof(resultT));
  8807. XMEMSET(resultC, 0, sizeof(resultC));
  8808. XMEMSET(resultP, 0, sizeof(resultP));
  8809. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8810. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  8811. if (result != 0)
  8812. ERROR_OUT(-6336, out);
  8813. result = wc_AesGcmEncrypt_ex(enc,
  8814. resultC, p, sizeof(p),
  8815. randIV, sizeof(randIV),
  8816. resultT, sizeof(resultT),
  8817. a, sizeof(a));
  8818. #if defined(WOLFSSL_ASYNC_CRYPT)
  8819. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8820. #endif
  8821. if (result != 0)
  8822. ERROR_OUT(-6337, out);
  8823. /* Check the IV has been set. */
  8824. {
  8825. word32 i, ivSum = 0;
  8826. for (i = 0; i < sizeof(randIV); i++)
  8827. ivSum += randIV[i];
  8828. if (ivSum == 0)
  8829. ERROR_OUT(-6338, out);
  8830. }
  8831. #ifdef HAVE_AES_DECRYPT
  8832. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8833. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  8834. if (result != 0)
  8835. ERROR_OUT(-6339, out);
  8836. result = wc_AesGcmDecrypt(dec,
  8837. resultP, resultC, sizeof(c1),
  8838. randIV, sizeof(randIV),
  8839. resultT, sizeof(resultT),
  8840. a, sizeof(a));
  8841. #if defined(WOLFSSL_ASYNC_CRYPT)
  8842. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8843. #endif
  8844. if (result != 0)
  8845. ERROR_OUT(-6340, out);
  8846. if (XMEMCMP(p, resultP, sizeof(p)))
  8847. ERROR_OUT(-6341, out);
  8848. #endif /* HAVE_AES_DECRYPT */
  8849. wc_FreeRng(&rng);
  8850. }
  8851. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  8852. #endif /* HAVE_FIPS_VERSION >= 2 */
  8853. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8854. #ifdef WOLFSSL_AES_256
  8855. #ifdef WOLFSSL_AESGCM_STREAM
  8856. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  8857. if (result != 0)
  8858. ERROR_OUT(-6360, out);
  8859. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  8860. if (result != 0)
  8861. ERROR_OUT(-6361, out);
  8862. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(resultT));
  8863. if (result != 0)
  8864. ERROR_OUT(-6362, out);
  8865. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  8866. ERROR_OUT(-6363, out);
  8867. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  8868. ERROR_OUT(-6364, out);
  8869. #ifdef HAVE_AES_DECRYPT
  8870. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  8871. if (result != 0)
  8872. ERROR_OUT(-6370, out);
  8873. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  8874. if (result != 0)
  8875. ERROR_OUT(-6371, out);
  8876. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  8877. if (result != 0)
  8878. ERROR_OUT(-6372, out);
  8879. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  8880. ERROR_OUT(-6373, out);
  8881. #endif
  8882. /* alen is the size to pass in with each update. */
  8883. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  8884. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  8885. if (result != 0)
  8886. ERROR_OUT(-6380, out);
  8887. /* plen is the offset into AAD to update with. */
  8888. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  8889. int len = sizeof(a) - plen;
  8890. if (len > alen) len = alen;
  8891. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  8892. if (result != 0)
  8893. ERROR_OUT(-6381, out);
  8894. }
  8895. /* plen is the offset into plaintext to update with. */
  8896. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  8897. int len = sizeof(p) - plen;
  8898. if (len > alen) len = alen;
  8899. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  8900. NULL, 0);
  8901. if (result != 0)
  8902. ERROR_OUT(-6382, out);
  8903. }
  8904. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(resultT));
  8905. if (result != 0)
  8906. ERROR_OUT(-6383, out);
  8907. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  8908. ERROR_OUT(-6384, out);
  8909. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  8910. ERROR_OUT(-6385, out);
  8911. }
  8912. #ifdef HAVE_AES_DECRYPT
  8913. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  8914. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  8915. if (result != 0)
  8916. ERROR_OUT(-6390, out);
  8917. /* plen is the offset into AAD to update with. */
  8918. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  8919. int len = sizeof(a) - plen;
  8920. if (len > alen) len = alen;
  8921. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  8922. if (result != 0)
  8923. ERROR_OUT(-6391, out);
  8924. }
  8925. /* plen is the offset into cipher text to update with. */
  8926. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  8927. int len = sizeof(c1) - plen;
  8928. if (len > alen) len = alen;
  8929. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  8930. NULL, 0);
  8931. if (result != 0)
  8932. ERROR_OUT(-6392, out);
  8933. }
  8934. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  8935. if (result != 0)
  8936. ERROR_OUT(-6393, out);
  8937. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  8938. ERROR_OUT(-6394, out);
  8939. }
  8940. #endif /* HAVE_AES_DECRYPT */
  8941. #endif /* WOLFSSL_AESGCM_STREAM */
  8942. #endif /* WOLFSSL_AES_256 */
  8943. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  8944. wc_AesFree(enc);
  8945. wc_AesFree(dec);
  8946. ret = 0;
  8947. out:
  8948. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8949. if (large_input)
  8950. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8951. if (large_output)
  8952. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8953. if (large_outdec)
  8954. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8955. #endif
  8956. #ifdef WOLFSSL_SMALL_STACK
  8957. if (enc)
  8958. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8959. if (dec)
  8960. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8961. #endif
  8962. return ret;
  8963. }
  8964. #ifdef WOLFSSL_AES_128
  8965. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  8966. {
  8967. int ret;
  8968. #ifdef WOLFSSL_SMALL_STACK
  8969. Gmac *gmac;
  8970. #else
  8971. Gmac gmac[1];
  8972. #endif
  8973. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8974. {
  8975. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  8976. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  8977. };
  8978. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8979. {
  8980. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  8981. 0xe2, 0x8c, 0x8f, 0x16
  8982. };
  8983. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  8984. {
  8985. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  8986. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  8987. };
  8988. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8989. {
  8990. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  8991. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8992. };
  8993. #if (!defined(HAVE_FIPS) || \
  8994. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  8995. /* FIPS builds only allow 16-byte auth tags. */
  8996. /* This sample uses a 15-byte auth tag. */
  8997. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8998. {
  8999. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  9000. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  9001. };
  9002. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9003. {
  9004. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  9005. 0x1a, 0x60, 0x24, 0xa7
  9006. };
  9007. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  9008. {
  9009. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  9010. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  9011. };
  9012. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9013. {
  9014. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  9015. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  9016. };
  9017. #endif
  9018. byte tag[16];
  9019. #ifdef WOLFSSL_SMALL_STACK
  9020. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9021. return -6409;
  9022. #endif
  9023. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  9024. (void)wc_AesInit((Aes*)gmac, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  9025. XMEMSET(tag, 0, sizeof(tag));
  9026. wc_GmacSetKey(gmac, k1, sizeof(k1));
  9027. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  9028. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  9029. ERROR_OUT(-6400, out);
  9030. #if (!defined(HAVE_FIPS) || \
  9031. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  9032. XMEMSET(tag, 0, sizeof(tag));
  9033. wc_GmacSetKey(gmac, k2, sizeof(k2));
  9034. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  9035. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  9036. ERROR_OUT(-6401, out);
  9037. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  9038. {
  9039. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  9040. {
  9041. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  9042. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  9043. };
  9044. WC_RNG rng;
  9045. byte iv[12];
  9046. #ifndef HAVE_FIPS
  9047. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  9048. ERROR_OUT(-6402, out);
  9049. #else
  9050. if (wc_InitRng(&rng) != 0)
  9051. ERROR_OUT(-6403, out);
  9052. #endif
  9053. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  9054. t1, sizeof(t1)) != 0)
  9055. ERROR_OUT(-6404, out);
  9056. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  9057. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  9058. ERROR_OUT(-6405, out);
  9059. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  9060. t2, sizeof(t2)) != 0)
  9061. ERROR_OUT(-6406, out);
  9062. XMEMSET(tag, 0, sizeof(tag));
  9063. XMEMSET(iv, 0, sizeof(iv));
  9064. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  9065. tag, sizeof(tag), &rng) != 0)
  9066. ERROR_OUT(-6407, out);
  9067. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  9068. tag, sizeof(tag)) != 0)
  9069. ERROR_OUT(-6408, out);
  9070. wc_FreeRng(&rng);
  9071. }
  9072. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  9073. #endif /* HAVE_FIPS */
  9074. ret = 0;
  9075. out:
  9076. #ifdef WOLFSSL_SMALL_STACK
  9077. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  9078. #endif
  9079. return ret;
  9080. }
  9081. #endif /* WOLFSSL_AES_128 */
  9082. #endif /* HAVE_AESGCM */
  9083. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  9084. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  9085. {
  9086. int ret;
  9087. #ifdef WOLFSSL_SMALL_STACK
  9088. Aes *enc;
  9089. #else
  9090. Aes enc[1];
  9091. #endif
  9092. /* key */
  9093. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  9094. {
  9095. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  9096. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  9097. };
  9098. /* nonce */
  9099. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  9100. {
  9101. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  9102. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  9103. };
  9104. /* plaintext */
  9105. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9106. {
  9107. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  9108. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9109. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  9110. };
  9111. /* plaintext - long */
  9112. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  9113. {
  9114. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  9115. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9116. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  9117. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  9118. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  9119. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  9120. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  9121. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  9122. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  9123. 0x50
  9124. };
  9125. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9126. {
  9127. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9128. };
  9129. /* ciphertext */
  9130. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  9131. {
  9132. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  9133. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  9134. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  9135. };
  9136. /* tag - authentication */
  9137. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  9138. {
  9139. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  9140. };
  9141. /* ciphertext - long */
  9142. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  9143. {
  9144. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  9145. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  9146. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  9147. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  9148. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  9149. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  9150. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  9151. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  9152. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  9153. 0x0b
  9154. };
  9155. /* tag - authentication - long */
  9156. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  9157. {
  9158. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  9159. };
  9160. /* tag - authentication - empty plaintext */
  9161. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  9162. {
  9163. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  9164. };
  9165. byte t2[sizeof(t)];
  9166. byte p2[sizeof(p)];
  9167. byte c2[sizeof(c)];
  9168. byte iv2[sizeof(iv)];
  9169. byte pl2[sizeof(pl)];
  9170. byte cl2[sizeof(cl)];
  9171. byte tl2[sizeof(tl)];
  9172. byte t_empty2[sizeof(t_empty)];
  9173. int result;
  9174. #ifdef WOLFSSL_SMALL_STACK
  9175. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9176. return -6521;
  9177. #endif
  9178. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  9179. XMEMSET(t2, 0, sizeof(t2));
  9180. XMEMSET(c2, 0, sizeof(c2));
  9181. XMEMSET(p2, 0, sizeof(p2));
  9182. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  9183. if (result != 0)
  9184. ERROR_OUT(-6500, out);
  9185. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  9186. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  9187. t2, sizeof(t2), a, sizeof(a));
  9188. if (result != 0)
  9189. ERROR_OUT(-6501, out);
  9190. if (XMEMCMP(c, c2, sizeof(c2)))
  9191. ERROR_OUT(-6502, out);
  9192. if (XMEMCMP(t, t2, sizeof(t2)))
  9193. ERROR_OUT(-6503, out);
  9194. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  9195. t2, sizeof(t2), a, sizeof(a));
  9196. if (result != 0)
  9197. ERROR_OUT(-6504, out);
  9198. if (XMEMCMP(p, p2, sizeof(p2)))
  9199. ERROR_OUT(-6505, out);
  9200. /* Test the authentication failure */
  9201. t2[0]++; /* Corrupt the authentication tag. */
  9202. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  9203. t2, sizeof(t2), a, sizeof(a));
  9204. if (result == 0)
  9205. ERROR_OUT(-6506, out);
  9206. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  9207. * authentication fail. */
  9208. XMEMSET(c2, 0, sizeof(c2));
  9209. if (XMEMCMP(p2, c2, sizeof(p2)))
  9210. ERROR_OUT(-6507, out);
  9211. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  9212. XMEMSET(t2, 0, sizeof(t2));
  9213. XMEMSET(c2, 0, sizeof(c2));
  9214. XMEMSET(p2, 0, sizeof(p2));
  9215. XMEMSET(iv2, 0, sizeof(iv2));
  9216. #ifndef HAVE_SELFTEST
  9217. /* selftest build does not have wc_AesCcmSetNonce() or
  9218. * wc_AesCcmEncrypt_ex() */
  9219. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  9220. ERROR_OUT(-6508, out);
  9221. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  9222. ERROR_OUT(-6509, out);
  9223. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  9224. t2, sizeof(t2), a, sizeof(a)) != 0)
  9225. ERROR_OUT(-6510, out);
  9226. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  9227. ERROR_OUT(-6511, out);
  9228. if (XMEMCMP(c, c2, sizeof(c2)))
  9229. ERROR_OUT(-6512, out);
  9230. if (XMEMCMP(t, t2, sizeof(t2)))
  9231. ERROR_OUT(-6513, out);
  9232. #endif
  9233. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9234. /* test fail on invalid IV sizes */
  9235. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  9236. if (result != 0)
  9237. ERROR_OUT(-6514, out);
  9238. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  9239. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  9240. t2, 1, a, sizeof(a));
  9241. if (result == 0) {
  9242. ERROR_OUT(-6515, out);
  9243. }
  9244. #endif
  9245. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  9246. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  9247. tl2, sizeof(tl2), a, sizeof(a));
  9248. if (result != 0)
  9249. ERROR_OUT(-6516, out);
  9250. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  9251. ERROR_OUT(-6517, out);
  9252. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  9253. ERROR_OUT(-6518, out);
  9254. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  9255. tl2, sizeof(tl2), a, sizeof(a));
  9256. if (result != 0)
  9257. ERROR_OUT(-6519, out);
  9258. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  9259. ERROR_OUT(-6520, out);
  9260. /* test empty message as null input or output with nonzero inSz. */
  9261. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  9262. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  9263. a, sizeof(a));
  9264. if (result != BAD_FUNC_ARG)
  9265. ERROR_OUT(-6527, out);
  9266. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  9267. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  9268. a, sizeof(a));
  9269. if (result != BAD_FUNC_ARG)
  9270. ERROR_OUT(-6528, out);
  9271. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  9272. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9273. sizeof(a));
  9274. if (result != BAD_FUNC_ARG)
  9275. ERROR_OUT(-6529, out);
  9276. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  9277. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9278. sizeof(a));
  9279. if (result != BAD_FUNC_ARG)
  9280. ERROR_OUT(-6530, out);
  9281. /* test empty message as null input and output with zero inSz --
  9282. * must either succeed, or fail early with BAD_FUNC_ARG.
  9283. */
  9284. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  9285. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  9286. a, sizeof(a));
  9287. if (result != BAD_FUNC_ARG) {
  9288. if (result != 0)
  9289. ERROR_OUT(-6521, out);
  9290. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  9291. ERROR_OUT(-6522, out);
  9292. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  9293. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  9294. sizeof(t_empty2), a, sizeof(a));
  9295. if (result != 0)
  9296. ERROR_OUT(-6523, out);
  9297. }
  9298. /* test empty message as zero-length string -- must work. */
  9299. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  9300. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9301. sizeof(a));
  9302. if (result != 0)
  9303. ERROR_OUT(-6524, out);
  9304. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  9305. ERROR_OUT(-6525, out);
  9306. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  9307. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9308. sizeof(a));
  9309. if (result != 0)
  9310. ERROR_OUT(-6526, out);
  9311. ret = 0;
  9312. out:
  9313. #ifdef WOLFSSL_SMALL_STACK
  9314. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9315. #endif
  9316. return ret;
  9317. }
  9318. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  9319. #ifdef HAVE_AES_KEYWRAP
  9320. #define MAX_KEYWRAP_TEST_OUTLEN 40
  9321. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  9322. typedef struct keywrapVector {
  9323. const byte* kek;
  9324. const byte* data;
  9325. const byte* verify;
  9326. word32 kekLen;
  9327. word32 dataLen;
  9328. word32 verifyLen;
  9329. } keywrapVector;
  9330. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  9331. {
  9332. int wrapSz, plainSz, testSz, i;
  9333. /* test vectors from RFC 3394 (kek, data, verify) */
  9334. #ifdef WOLFSSL_AES_128
  9335. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  9336. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  9337. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9338. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9339. };
  9340. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  9341. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9342. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9343. };
  9344. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  9345. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  9346. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  9347. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  9348. };
  9349. #endif /* WOLFSSL_AES_128 */
  9350. #ifdef WOLFSSL_AES_192
  9351. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  9352. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  9353. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9354. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9355. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9356. };
  9357. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  9358. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9359. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9360. };
  9361. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  9362. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  9363. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  9364. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  9365. };
  9366. #endif
  9367. #ifdef WOLFSSL_AES_256
  9368. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  9369. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  9370. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9371. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9372. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9373. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9374. };
  9375. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  9376. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9377. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9378. };
  9379. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  9380. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  9381. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  9382. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  9383. };
  9384. #endif
  9385. #ifdef WOLFSSL_AES_192
  9386. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  9387. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  9388. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9389. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9390. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9391. };
  9392. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  9393. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9394. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9395. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9396. };
  9397. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  9398. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  9399. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  9400. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  9401. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  9402. };
  9403. #endif
  9404. #ifdef WOLFSSL_AES_256
  9405. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  9406. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  9407. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9408. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9409. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9410. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9411. };
  9412. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  9413. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9414. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9415. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9416. };
  9417. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  9418. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  9419. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  9420. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  9421. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  9422. };
  9423. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  9424. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  9425. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9426. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9427. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9428. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9429. };
  9430. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  9431. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9432. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9433. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9434. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9435. };
  9436. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  9437. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  9438. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  9439. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  9440. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  9441. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  9442. };
  9443. #endif /* WOLFSSL_AES_256 */
  9444. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  9445. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  9446. const keywrapVector test_wrap[] =
  9447. {
  9448. #ifdef WOLFSSL_AES_128
  9449. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  9450. #endif
  9451. #ifdef WOLFSSL_AES_192
  9452. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  9453. #endif
  9454. #ifdef WOLFSSL_AES_256
  9455. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  9456. #endif
  9457. #ifdef WOLFSSL_AES_192
  9458. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  9459. #endif
  9460. #ifdef WOLFSSL_AES_256
  9461. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  9462. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  9463. #endif
  9464. };
  9465. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  9466. XMEMSET(output, 0, sizeof(output));
  9467. XMEMSET(plain, 0, sizeof(plain));
  9468. for (i = 0; i < testSz; i++) {
  9469. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  9470. test_wrap[i].data, test_wrap[i].dataLen,
  9471. output, sizeof(output), NULL);
  9472. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  9473. return -6600;
  9474. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  9475. return -6601;
  9476. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  9477. output, wrapSz,
  9478. plain, sizeof(plain), NULL);
  9479. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  9480. return -6602;
  9481. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  9482. return -6603 - i;
  9483. }
  9484. return 0;
  9485. }
  9486. #endif /* HAVE_AES_KEYWRAP */
  9487. #endif /* NO_AES */
  9488. #ifdef HAVE_CAMELLIA
  9489. enum {
  9490. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  9491. };
  9492. typedef struct {
  9493. int type;
  9494. const byte* plaintext;
  9495. const byte* iv;
  9496. const byte* ciphertext;
  9497. const byte* key;
  9498. word32 keySz;
  9499. int errorCode;
  9500. } test_vector_t;
  9501. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  9502. {
  9503. /* Camellia ECB Test Plaintext */
  9504. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  9505. {
  9506. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9507. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9508. };
  9509. /* Camellia ECB Test Initialization Vector */
  9510. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  9511. /* Test 1: Camellia ECB 128-bit key */
  9512. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9513. {
  9514. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9515. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9516. };
  9517. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9518. {
  9519. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  9520. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  9521. };
  9522. /* Test 2: Camellia ECB 192-bit key */
  9523. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9524. {
  9525. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9526. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9527. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  9528. };
  9529. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9530. {
  9531. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  9532. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  9533. };
  9534. /* Test 3: Camellia ECB 256-bit key */
  9535. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9536. {
  9537. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9538. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9539. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9540. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  9541. };
  9542. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9543. {
  9544. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  9545. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  9546. };
  9547. /* Camellia CBC Test Plaintext */
  9548. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  9549. {
  9550. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  9551. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  9552. };
  9553. /* Camellia CBC Test Initialization Vector */
  9554. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  9555. {
  9556. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9557. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9558. };
  9559. /* Test 4: Camellia-CBC 128-bit key */
  9560. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  9561. {
  9562. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  9563. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  9564. };
  9565. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  9566. {
  9567. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  9568. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  9569. };
  9570. /* Test 5: Camellia-CBC 192-bit key */
  9571. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  9572. {
  9573. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  9574. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  9575. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  9576. };
  9577. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  9578. {
  9579. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  9580. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  9581. };
  9582. /* Test 6: CBC 256-bit key */
  9583. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  9584. {
  9585. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  9586. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  9587. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  9588. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  9589. };
  9590. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  9591. {
  9592. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  9593. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  9594. };
  9595. byte out[CAMELLIA_BLOCK_SIZE];
  9596. Camellia cam;
  9597. int i, testsSz, ret;
  9598. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  9599. {
  9600. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  9601. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  9602. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  9603. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  9604. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  9605. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  9606. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  9607. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  9608. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  9609. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  9610. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  9611. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  9612. };
  9613. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  9614. for (i = 0; i < testsSz; i++) {
  9615. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  9616. testVectors[i].iv) != 0)
  9617. return testVectors[i].errorCode;
  9618. switch (testVectors[i].type) {
  9619. case CAM_ECB_ENC:
  9620. ret = wc_CamelliaEncryptDirect(&cam, out,
  9621. testVectors[i].plaintext);
  9622. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9623. CAMELLIA_BLOCK_SIZE))
  9624. return testVectors[i].errorCode;
  9625. break;
  9626. case CAM_ECB_DEC:
  9627. ret = wc_CamelliaDecryptDirect(&cam, out,
  9628. testVectors[i].ciphertext);
  9629. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9630. CAMELLIA_BLOCK_SIZE))
  9631. return testVectors[i].errorCode;
  9632. break;
  9633. case CAM_CBC_ENC:
  9634. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  9635. CAMELLIA_BLOCK_SIZE);
  9636. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9637. CAMELLIA_BLOCK_SIZE))
  9638. return testVectors[i].errorCode;
  9639. break;
  9640. case CAM_CBC_DEC:
  9641. ret = wc_CamelliaCbcDecrypt(&cam, out,
  9642. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  9643. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9644. CAMELLIA_BLOCK_SIZE))
  9645. return testVectors[i].errorCode;
  9646. break;
  9647. default:
  9648. break;
  9649. }
  9650. }
  9651. /* Setting the IV and checking it was actually set. */
  9652. ret = wc_CamelliaSetIV(&cam, ivc);
  9653. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  9654. return -6700;
  9655. /* Setting the IV to NULL should be same as all zeros IV */
  9656. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  9657. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  9658. return -6701;
  9659. /* First parameter should never be null */
  9660. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  9661. return -6702;
  9662. /* First parameter should never be null, check it fails */
  9663. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  9664. return -6703;
  9665. /* Key should have a size of 16, 24, or 32 */
  9666. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  9667. return -6704;
  9668. return 0;
  9669. }
  9670. #endif /* HAVE_CAMELLIA */
  9671. #ifdef HAVE_IDEA
  9672. WOLFSSL_TEST_SUBROUTINE int idea_test(void)
  9673. {
  9674. int ret;
  9675. word16 i, j;
  9676. Idea idea;
  9677. byte data[IDEA_BLOCK_SIZE];
  9678. /* Project NESSIE test vectors */
  9679. #define IDEA_NB_TESTS 6
  9680. #define IDEA_NB_TESTS_EXTRA 4
  9681. WOLFSSL_SMALL_STACK_STATIC const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  9682. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  9683. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9684. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  9685. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9686. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9687. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9688. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9689. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9690. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9691. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9692. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9693. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9694. };
  9695. WOLFSSL_SMALL_STACK_STATIC const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9696. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9697. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9698. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9699. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9700. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  9701. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  9702. };
  9703. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9704. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  9705. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  9706. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  9707. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  9708. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9709. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9710. };
  9711. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9712. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  9713. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  9714. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  9715. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  9716. };
  9717. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9718. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  9719. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  9720. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  9721. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  9722. };
  9723. /* CBC test */
  9724. const char *message = "International Data Encryption Algorithm";
  9725. byte msg_enc[40], msg_dec[40];
  9726. for (i = 0; i < IDEA_NB_TESTS; i++) {
  9727. /* Set encryption key */
  9728. XMEMSET(&idea, 0, sizeof(Idea));
  9729. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9730. NULL, IDEA_ENCRYPTION);
  9731. if (ret != 0) {
  9732. printf("wc_IdeaSetKey (enc) failed\n");
  9733. return -6800;
  9734. }
  9735. /* Data encryption */
  9736. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  9737. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  9738. printf("Bad encryption\n");
  9739. return -6801;
  9740. }
  9741. /* Set decryption key */
  9742. XMEMSET(&idea, 0, sizeof(Idea));
  9743. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9744. NULL, IDEA_DECRYPTION);
  9745. if (ret != 0) {
  9746. printf("wc_IdeaSetKey (dec) failed\n");
  9747. return -6802;
  9748. }
  9749. /* Data decryption */
  9750. ret = wc_IdeaCipher(&idea, data, data);
  9751. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  9752. printf("Bad decryption\n");
  9753. return -6803;
  9754. }
  9755. /* Set encryption key */
  9756. XMEMSET(&idea, 0, sizeof(Idea));
  9757. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9758. v_key[i], IDEA_ENCRYPTION);
  9759. if (ret != 0) {
  9760. printf("wc_IdeaSetKey (enc) failed\n");
  9761. return -6804;
  9762. }
  9763. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  9764. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  9765. (word32)XSTRLEN(message)+1);
  9766. if (ret != 0) {
  9767. printf("wc_IdeaCbcEncrypt failed\n");
  9768. return -6805;
  9769. }
  9770. /* Set decryption key */
  9771. XMEMSET(&idea, 0, sizeof(Idea));
  9772. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9773. v_key[i], IDEA_DECRYPTION);
  9774. if (ret != 0) {
  9775. printf("wc_IdeaSetKey (dec) failed\n");
  9776. return -6806;
  9777. }
  9778. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  9779. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  9780. (word32)XSTRLEN(message)+1);
  9781. if (ret != 0) {
  9782. printf("wc_IdeaCbcDecrypt failed\n");
  9783. return -6807;
  9784. }
  9785. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  9786. printf("Bad CBC decryption\n");
  9787. return -6808;
  9788. }
  9789. }
  9790. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  9791. /* Set encryption key */
  9792. XMEMSET(&idea, 0, sizeof(Idea));
  9793. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9794. NULL, IDEA_ENCRYPTION);
  9795. if (ret != 0) {
  9796. printf("wc_IdeaSetKey (enc) failed\n");
  9797. return -6809;
  9798. }
  9799. /* 100 times data encryption */
  9800. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9801. for (j = 0; j < 100; j++) {
  9802. ret = wc_IdeaCipher(&idea, data, data);
  9803. if (ret != 0) {
  9804. return -6810;
  9805. }
  9806. }
  9807. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  9808. printf("Bad encryption (100 times)\n");
  9809. return -6811;
  9810. }
  9811. /* 1000 times data encryption */
  9812. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9813. for (j = 0; j < 1000; j++) {
  9814. ret = wc_IdeaCipher(&idea, data, data);
  9815. if (ret != 0) {
  9816. return -6812;
  9817. }
  9818. }
  9819. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  9820. printf("Bad encryption (100 times)\n");
  9821. return -6813;
  9822. }
  9823. }
  9824. #ifndef WC_NO_RNG
  9825. /* random test for CBC */
  9826. {
  9827. WC_RNG rng;
  9828. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  9829. rnd[1000], enc[1000], dec[1000];
  9830. /* random values */
  9831. #ifndef HAVE_FIPS
  9832. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9833. #else
  9834. ret = wc_InitRng(&rng);
  9835. #endif
  9836. if (ret != 0)
  9837. return -6814;
  9838. for (i = 0; i < 1000; i++) {
  9839. /* random key */
  9840. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  9841. if (ret != 0)
  9842. return -6815;
  9843. /* random iv */
  9844. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  9845. if (ret != 0)
  9846. return -6816;
  9847. /* random data */
  9848. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  9849. if (ret != 0)
  9850. return -6817;
  9851. /* Set encryption key */
  9852. XMEMSET(&idea, 0, sizeof(Idea));
  9853. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  9854. if (ret != 0) {
  9855. printf("wc_IdeaSetKey (enc) failed\n");
  9856. return -6818;
  9857. }
  9858. /* Data encryption */
  9859. XMEMSET(enc, 0, sizeof(enc));
  9860. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  9861. if (ret != 0) {
  9862. printf("wc_IdeaCbcEncrypt failed\n");
  9863. return -6819;
  9864. }
  9865. /* Set decryption key */
  9866. XMEMSET(&idea, 0, sizeof(Idea));
  9867. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  9868. if (ret != 0) {
  9869. printf("wc_IdeaSetKey (enc) failed\n");
  9870. return -6820;
  9871. }
  9872. /* Data decryption */
  9873. XMEMSET(dec, 0, sizeof(dec));
  9874. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  9875. if (ret != 0) {
  9876. printf("wc_IdeaCbcDecrypt failed\n");
  9877. return -6821;
  9878. }
  9879. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  9880. printf("Bad CBC decryption\n");
  9881. return -6822;
  9882. }
  9883. }
  9884. wc_FreeRng(&rng);
  9885. }
  9886. #endif /* WC_NO_RNG */
  9887. return 0;
  9888. }
  9889. #endif /* HAVE_IDEA */
  9890. #ifdef HAVE_XCHACHA
  9891. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  9892. int ret = -6830;
  9893. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9894. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  9895. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  9896. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  9897. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  9898. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  9899. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  9900. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  9901. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  9902. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  9903. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  9904. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  9905. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  9906. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  9907. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  9908. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  9909. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  9910. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  9911. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  9912. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  9913. };
  9914. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9915. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9916. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9917. };
  9918. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9919. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9920. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  9921. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9922. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  9923. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  9924. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  9925. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  9926. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  9927. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  9928. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  9929. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  9930. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  9931. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  9932. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  9933. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  9934. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  9935. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  9936. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  9937. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  9938. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  9939. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  9940. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  9941. };
  9942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9943. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9944. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9945. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9946. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  9947. ERROR_OUT(MEMORY_E, out);
  9948. #else
  9949. struct ChaCha chacha[1];
  9950. byte buf1[sizeof Plaintext];
  9951. byte buf2[sizeof Plaintext];
  9952. #endif
  9953. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9954. if (ret < 0)
  9955. ERROR_OUT(-6831, out);
  9956. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  9957. if (ret < 0)
  9958. ERROR_OUT(-6832, out);
  9959. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9960. ERROR_OUT(-6833, out);
  9961. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9962. if (ret < 0)
  9963. ERROR_OUT(-6834, out);
  9964. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  9965. if (ret < 0)
  9966. ERROR_OUT(-6835, out);
  9967. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9968. ERROR_OUT(-6836, out);
  9969. out:
  9970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9971. if (chacha)
  9972. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9973. if (buf1)
  9974. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9975. if (buf2)
  9976. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9977. #endif
  9978. return ret;
  9979. }
  9980. #endif /* HAVE_XCHACHA */
  9981. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  9982. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  9983. int ret;
  9984. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9985. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  9986. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  9987. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  9988. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  9989. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  9990. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  9991. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  9992. 0x74, 0x2e }; /* t. */
  9993. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  9994. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9995. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9996. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9997. };
  9998. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9999. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10000. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  10001. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10002. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  10003. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  10004. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  10005. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  10006. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  10007. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  10008. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  10009. 0xb5, 0x2e };
  10010. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  10011. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  10012. };
  10013. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10014. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10015. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10016. if ((buf1 == NULL) || (buf2 == NULL))
  10017. ERROR_OUT(-6480, out);
  10018. #else
  10019. byte buf1[sizeof Ciphertext + sizeof Tag];
  10020. byte buf2[sizeof Plaintext];
  10021. #endif
  10022. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  10023. Plaintext, sizeof Plaintext,
  10024. AAD, sizeof AAD,
  10025. IV, sizeof IV,
  10026. Key, sizeof Key);
  10027. if (ret < 0)
  10028. ERROR_OUT(-6841, out);
  10029. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  10030. ERROR_OUT(-6842, out);
  10031. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  10032. ERROR_OUT(-6843, out);
  10033. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  10034. buf1, sizeof Ciphertext + sizeof Tag,
  10035. AAD, sizeof AAD,
  10036. IV, sizeof IV,
  10037. Key, sizeof Key);
  10038. if (ret < 0)
  10039. ERROR_OUT(-6844, out);
  10040. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10041. ERROR_OUT(-6845, out);
  10042. out:
  10043. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10044. if (buf1 != NULL)
  10045. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10046. if (buf2 != NULL)
  10047. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10048. #endif
  10049. return ret;
  10050. }
  10051. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  10052. #ifndef WC_NO_RNG
  10053. static int _rng_test(WC_RNG* rng, int errorOffset)
  10054. {
  10055. byte block[32];
  10056. int ret, i;
  10057. XMEMSET(block, 0, sizeof(block));
  10058. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  10059. if (ret != 0) {
  10060. ret = -6850;
  10061. goto exit;
  10062. }
  10063. /* Check for 0's */
  10064. for (i=0; i<(int)sizeof(block); i++) {
  10065. if (block[i] == 0) {
  10066. ret++;
  10067. }
  10068. }
  10069. /* All zeros count check */
  10070. if (ret >= (int)sizeof(block)) {
  10071. ret = -6851;
  10072. goto exit;
  10073. }
  10074. ret = wc_RNG_GenerateByte(rng, block);
  10075. if (ret != 0) {
  10076. ret = -6852;
  10077. goto exit;
  10078. }
  10079. /* Parameter validation testing. */
  10080. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  10081. if (ret != BAD_FUNC_ARG) {
  10082. ret = -6853;
  10083. goto exit;
  10084. }
  10085. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  10086. if (ret != BAD_FUNC_ARG) {
  10087. ret = -6854;
  10088. goto exit;
  10089. }
  10090. ret = wc_RNG_GenerateByte(NULL, block);
  10091. if (ret != BAD_FUNC_ARG) {
  10092. ret = -6855;
  10093. goto exit;
  10094. }
  10095. ret = wc_RNG_GenerateByte(rng, NULL);
  10096. if (ret != BAD_FUNC_ARG) {
  10097. ret = -6856;
  10098. goto exit;
  10099. }
  10100. ret = 0;
  10101. exit:
  10102. if (ret != 0)
  10103. ret += errorOffset;
  10104. return ret;
  10105. }
  10106. static int random_rng_test(void)
  10107. {
  10108. WC_RNG localRng;
  10109. WC_RNG* rng;
  10110. int ret;
  10111. rng = &localRng;
  10112. /* Test stack based RNG. */
  10113. #ifndef HAVE_FIPS
  10114. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  10115. #else
  10116. ret = wc_InitRng(rng);
  10117. #endif
  10118. if (ret != 0) return -6900;
  10119. ret = _rng_test(rng, -6300);
  10120. /* Make sure and free RNG */
  10121. wc_FreeRng(rng);
  10122. if (ret != 0) return ret;
  10123. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  10124. {
  10125. byte nonce[8] = { 0 };
  10126. /* Test dynamic RNG. */
  10127. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  10128. if (rng == NULL) return -6901;
  10129. ret = _rng_test(rng, -6310);
  10130. wc_rng_free(rng);
  10131. }
  10132. #endif
  10133. return ret;
  10134. }
  10135. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  10136. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  10137. {
  10138. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  10139. {
  10140. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  10141. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  10142. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  10143. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  10144. };
  10145. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  10146. {
  10147. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  10148. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  10149. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  10150. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  10151. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  10152. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  10153. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  10154. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  10155. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  10156. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  10157. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  10158. };
  10159. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  10160. {
  10161. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  10162. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  10163. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  10164. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  10165. };
  10166. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  10167. {
  10168. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  10169. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  10170. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  10171. };
  10172. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  10173. {
  10174. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  10175. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  10176. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  10177. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  10178. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  10179. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  10180. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  10181. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  10182. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  10183. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  10184. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  10185. };
  10186. byte output[WC_SHA256_DIGEST_SIZE * 4];
  10187. int ret;
  10188. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  10189. output, sizeof(output));
  10190. if (ret != 0)
  10191. return -7000;
  10192. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  10193. return -7001;
  10194. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  10195. test2EntropyB, sizeof(test2EntropyB),
  10196. output, sizeof(output));
  10197. if (ret != 0)
  10198. return -7002;
  10199. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  10200. return -7003;
  10201. /* Basic RNG generate block test */
  10202. if ((ret = random_rng_test()) != 0)
  10203. return ret;
  10204. /* Test the seed check function. */
  10205. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  10206. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  10207. {
  10208. word32 i, outputSz;
  10209. /* Repeat the same byte over and over. Should fail. */
  10210. outputSz = sizeof(output);
  10211. XMEMSET(output, 1, outputSz);
  10212. ret = wc_RNG_TestSeed(output, outputSz);
  10213. if (ret == 0)
  10214. return -7004;
  10215. /* Every byte of the entropy scratch is different,
  10216. * entropy is a single byte that shouldn't match. */
  10217. outputSz = (sizeof(word32) * 2) + 1;
  10218. for (i = 0; i < outputSz; i++)
  10219. output[i] = (byte)i;
  10220. ret = wc_RNG_TestSeed(output, outputSz);
  10221. if (ret != 0)
  10222. return -7005;
  10223. outputSz = sizeof(output);
  10224. for (i = 0; i < outputSz; i++)
  10225. output[i] = (byte)i;
  10226. ret = wc_RNG_TestSeed(output, outputSz);
  10227. if (ret != 0)
  10228. return -7006;
  10229. }
  10230. #endif
  10231. return 0;
  10232. }
  10233. #else
  10234. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  10235. {
  10236. /* Basic RNG generate block test */
  10237. return random_rng_test();
  10238. }
  10239. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  10240. #endif /* WC_NO_RNG */
  10241. #ifndef MEM_TEST_SZ
  10242. #define MEM_TEST_SZ 1024
  10243. #endif
  10244. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  10245. static int simple_mem_test(int sz)
  10246. {
  10247. int ret = 0;
  10248. byte* b;
  10249. int i;
  10250. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10251. if (b == NULL) {
  10252. return -7110;
  10253. }
  10254. /* utilize memory */
  10255. for (i = 0; i < sz; i++) {
  10256. b[i] = (byte)i;
  10257. }
  10258. /* read back and verify */
  10259. for (i = 0; i < sz; i++) {
  10260. if (b[i] != (byte)i) {
  10261. ret = -7111;
  10262. break;
  10263. }
  10264. }
  10265. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10266. return ret;
  10267. }
  10268. #endif
  10269. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  10270. {
  10271. int ret = 0;
  10272. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  10273. byte* b = NULL;
  10274. #endif
  10275. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  10276. int i;
  10277. #endif
  10278. #ifdef WOLFSSL_STATIC_MEMORY
  10279. word32 size[] = { WOLFMEM_BUCKETS };
  10280. word32 dist[] = { WOLFMEM_DIST };
  10281. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  10282. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  10283. /* pad to account for if head of buffer is not at set memory
  10284. * alignment when tests are ran */
  10285. #endif
  10286. #ifdef WOLFSSL_STATIC_MEMORY
  10287. /* check macro settings */
  10288. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  10289. return -7200;
  10290. }
  10291. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  10292. return -7201;
  10293. }
  10294. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  10295. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  10296. /* each element in array should be divisible by alignment size */
  10297. return -7202;
  10298. }
  10299. }
  10300. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  10301. if (size[i - 1] >= size[i]) {
  10302. return -7203; /* sizes should be in increasing order */
  10303. }
  10304. }
  10305. /* check that padding size returned is possible */
  10306. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  10307. return -7204; /* no room for wc_Memory struct */
  10308. }
  10309. if (wolfSSL_MemoryPaddingSz() < 0) {
  10310. return -7205;
  10311. }
  10312. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  10313. return -7206; /* not aligned! */
  10314. }
  10315. /* check function to return optimum buffer size (rounded down) */
  10316. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  10317. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  10318. return -7207; /* not aligned! */
  10319. }
  10320. if (ret < 0) {
  10321. return -7208;
  10322. }
  10323. if ((unsigned int)ret > sizeof(buffer)) {
  10324. return -7209; /* did not round down as expected */
  10325. }
  10326. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  10327. return -7210; /* return value changed when using suggested value */
  10328. }
  10329. ret = wolfSSL_MemoryPaddingSz();
  10330. ret += pad; /* add space that is going to be needed if buffer not aligned */
  10331. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  10332. (ret + (int)size[0])) {
  10333. return -7211; /* did not round down to nearest bucket value */
  10334. }
  10335. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  10336. if ((ret - pad) < 0) {
  10337. return -7212;
  10338. }
  10339. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  10340. return -7213; /* not even chunks of memory for IO size */
  10341. }
  10342. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  10343. return -7214; /* memory not aligned */
  10344. }
  10345. /* check for passing bad or unknown arguments to functions */
  10346. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  10347. return -7215;
  10348. }
  10349. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  10350. return -7216; /* should round to 0 since struct + bucket will not fit */
  10351. }
  10352. (void)dist; /* avoid static analysis warning of variable not used */
  10353. #endif
  10354. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  10355. /* simple test */
  10356. ret = simple_mem_test(MEM_TEST_SZ);
  10357. if (ret != 0)
  10358. return ret;
  10359. #endif
  10360. #ifdef COMPLEX_MEM_TEST
  10361. /* test various size blocks */
  10362. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  10363. ret = simple_mem_test(i);
  10364. if (ret != 0)
  10365. return ret;
  10366. }
  10367. #endif
  10368. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  10369. /* realloc test */
  10370. b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10371. if (b) {
  10372. b = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  10373. DYNAMIC_TYPE_TMP_BUFFER);
  10374. }
  10375. if (b == NULL) {
  10376. return -7217;
  10377. }
  10378. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10379. #endif
  10380. return ret;
  10381. }
  10382. #ifdef HAVE_NTRU
  10383. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  10384. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  10385. {
  10386. static WC_RNG rng;
  10387. if (cmd == INIT)
  10388. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  10389. if (out == NULL)
  10390. return 0;
  10391. if (cmd == GET_BYTE_OF_ENTROPY)
  10392. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  10393. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  10394. *out = 1;
  10395. return 1;
  10396. }
  10397. return 0;
  10398. }
  10399. #endif /* HAVE_NTRU */
  10400. #ifndef NO_FILESYSTEM
  10401. /* Cert Paths */
  10402. #ifdef FREESCALE_MQX
  10403. #define CERT_PREFIX "a:\\"
  10404. #define CERT_PATH_SEP "\\"
  10405. #elif defined(WOLFSSL_uTKERNEL2)
  10406. #define CERT_PREFIX "/uda/"
  10407. #define CERT_PATH_SEP "/"
  10408. #elif defined(_WIN32_WCE)
  10409. #define CERT_PREFIX "\\windows\\"
  10410. #define CERT_PATH_SEP "\\"
  10411. #endif
  10412. #ifndef CERT_PREFIX
  10413. #define CERT_PREFIX "./"
  10414. #endif
  10415. #ifndef CERT_PATH_SEP
  10416. #define CERT_PATH_SEP "/"
  10417. #endif
  10418. #ifndef CERT_WRITE_TEMP_DIR
  10419. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  10420. #endif
  10421. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  10422. /* Generated Test Certs */
  10423. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10424. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  10425. #if !defined(NO_RSA) && !defined(NO_ASN)
  10426. static const char* clientKey = CERT_ROOT "client-key.der";
  10427. static const char* clientCert = CERT_ROOT "client-cert.der";
  10428. #ifdef WOLFSSL_CERT_EXT
  10429. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  10430. #endif
  10431. #endif /* !NO_RSA && !NO_ASN */
  10432. #endif
  10433. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  10434. #if !defined(NO_RSA) && !defined(NO_ASN)
  10435. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  10436. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  10437. #ifdef WOLFSSL_CERT_GEN
  10438. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  10439. #endif
  10440. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  10441. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  10442. #endif
  10443. #ifdef HAVE_PKCS7
  10444. static const char* rsaServerCertDerFile =
  10445. CERT_ROOT "server-cert.der";
  10446. static const char* rsaServerKeyDerFile =
  10447. CERT_ROOT "server-key.der";
  10448. #endif
  10449. #endif
  10450. #endif /* !NO_RSA && !NO_ASN */
  10451. #endif /* !USE_CERT_BUFFER_* */
  10452. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10453. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  10454. !defined(NO_ASN)
  10455. #ifndef NO_DH
  10456. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  10457. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  10458. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  10459. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  10460. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  10461. #endif
  10462. #endif
  10463. #ifndef NO_DSA
  10464. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  10465. #endif
  10466. #endif /* !USE_CERT_BUFFER_* */
  10467. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  10468. #ifdef HAVE_ECC
  10469. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  10470. #ifdef HAVE_ECC_KEY_IMPORT
  10471. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  10472. #endif
  10473. #endif
  10474. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  10475. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  10476. #ifndef NO_RSA
  10477. /* eccKeyPubFile is used in a test that requires RSA. */
  10478. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  10479. #endif
  10480. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  10481. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  10482. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10483. static const char* eccCaKey384File =
  10484. CERT_ROOT "ca-ecc384-key.der";
  10485. static const char* eccCaCert384File =
  10486. CERT_ROOT "ca-ecc384-cert.pem";
  10487. #endif
  10488. #endif
  10489. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  10490. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  10491. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  10492. #endif
  10493. #endif /* HAVE_ECC */
  10494. #ifdef HAVE_ED25519
  10495. #ifdef WOLFSSL_TEST_CERT
  10496. static const char* serverEd25519Cert =
  10497. CERT_ROOT "ed25519/server-ed25519.der";
  10498. static const char* caEd25519Cert =
  10499. CERT_ROOT "ed25519/ca-ed25519.der";
  10500. #endif
  10501. #endif
  10502. #ifdef HAVE_ED448
  10503. #ifdef WOLFSSL_TEST_CERT
  10504. static const char* serverEd448Cert =
  10505. CERT_ROOT "ed448/server-ed448.der";
  10506. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  10507. #endif
  10508. #endif
  10509. #endif /* !USE_CERT_BUFFER_* */
  10510. #ifndef NO_WRITE_TEMP_FILES
  10511. #ifdef HAVE_ECC
  10512. #ifdef WOLFSSL_CERT_GEN
  10513. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  10514. #endif
  10515. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  10516. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  10517. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  10518. #endif
  10519. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  10520. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  10521. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  10522. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  10523. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG)
  10524. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  10525. #endif
  10526. #endif /* HAVE_ECC_KEY_EXPORT */
  10527. #if defined(WOLFSSL_CERT_GEN) || \
  10528. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10529. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  10530. #endif
  10531. #endif /* HAVE_ECC */
  10532. #ifndef NO_RSA
  10533. #if defined(WOLFSSL_CERT_GEN) || \
  10534. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10535. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  10536. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  10537. #endif
  10538. #ifdef WOLFSSL_CERT_GEN
  10539. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  10540. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  10541. #endif
  10542. #ifdef WOLFSSL_CERT_REQ
  10543. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  10544. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  10545. #endif
  10546. #endif /* !NO_RSA */
  10547. #if !defined(NO_RSA) || !defined(NO_DSA)
  10548. #ifdef WOLFSSL_KEY_GEN
  10549. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  10550. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  10551. #endif
  10552. #endif
  10553. #endif /* !NO_WRITE_TEMP_FILES */
  10554. #endif /* !NO_FILESYSTEM */
  10555. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  10556. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  10557. #ifdef WOLFSSL_MULTI_ATTRIB
  10558. static CertName certDefaultName;
  10559. static void initDefaultName(void)
  10560. {
  10561. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  10562. certDefaultName.countryEnc = CTC_PRINTABLE;
  10563. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  10564. certDefaultName.stateEnc = CTC_UTF8;
  10565. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  10566. certDefaultName.localityEnc = CTC_UTF8;
  10567. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  10568. certDefaultName.surEnc = CTC_UTF8;
  10569. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  10570. certDefaultName.orgEnc = CTC_UTF8;
  10571. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  10572. certDefaultName.unitEnc = CTC_UTF8;
  10573. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  10574. certDefaultName.commonNameEnc = CTC_UTF8;
  10575. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  10576. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  10577. #ifdef WOLFSSL_CERT_EXT
  10578. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  10579. certDefaultName.busCatEnc = CTC_UTF8;
  10580. #endif
  10581. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  10582. #ifdef WOLFSSL_TEST_CERT
  10583. {
  10584. NameAttrib* n;
  10585. /* test having additional OUs and setting DC */
  10586. n = &certDefaultName.name[0];
  10587. n->id = ASN_ORGUNIT_NAME;
  10588. n->type = CTC_UTF8;
  10589. n->sz = sizeof("Development-2");
  10590. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  10591. #if CTC_MAX_ATTRIB > 3
  10592. n = &certDefaultName.name[1];
  10593. n->id = ASN_DOMAIN_COMPONENT;
  10594. n->type = CTC_UTF8;
  10595. n->sz = sizeof("com");
  10596. XMEMCPY(n->value, "com", sizeof("com"));
  10597. n = &certDefaultName.name[2];
  10598. n->id = ASN_DOMAIN_COMPONENT;
  10599. n->type = CTC_UTF8;
  10600. n->sz = sizeof("wolfssl");
  10601. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  10602. #endif
  10603. }
  10604. #endif /* WOLFSSL_TEST_CERT */
  10605. }
  10606. #else
  10607. static const CertName certDefaultName = {
  10608. "US", CTC_PRINTABLE, /* country */
  10609. "Oregon", CTC_UTF8, /* state */
  10610. "Portland", CTC_UTF8, /* locality */
  10611. "Test", CTC_UTF8, /* sur */
  10612. "wolfSSL", CTC_UTF8, /* org */
  10613. "Development", CTC_UTF8, /* unit */
  10614. "www.wolfssl.com", CTC_UTF8, /* commonName */
  10615. "wolfSSL12345", CTC_PRINTABLE, /* serial number of device */
  10616. #ifdef WOLFSSL_CERT_EXT
  10617. "Private Organization", CTC_UTF8, /* businessCategory */
  10618. "US", CTC_PRINTABLE, /* jurisdiction country */
  10619. "Oregon", CTC_PRINTABLE, /* jurisdiction state */
  10620. #endif
  10621. "info@wolfssl.com" /* email */
  10622. };
  10623. #endif /* WOLFSSL_MULTI_ATTRIB */
  10624. #ifdef WOLFSSL_CERT_EXT
  10625. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  10626. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  10627. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  10628. "digitalSignature,nonRepudiation";
  10629. #endif
  10630. #if (defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)) && !defined(NO_RSA)
  10631. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  10632. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  10633. #endif
  10634. #endif /* WOLFSSL_CERT_EXT */
  10635. #endif /* WOLFSSL_CERT_GEN */
  10636. #ifndef NO_RSA
  10637. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  10638. !defined(NO_FILESYSTEM)
  10639. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  10640. static byte minName[] = { 0x30, 0x00 };
  10641. static byte nameBad[] = {
  10642. 0x30, 0x08,
  10643. 0x31, 0x06,
  10644. 0x30, 0x04,
  10645. 0x06, 0x02,
  10646. 0x55, 0x04,
  10647. };
  10648. static byte minDates[] = {
  10649. 0x30, 0x1e,
  10650. 0x17, 0x0d,
  10651. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  10652. 0x32, 0x33, 0x31, 0x30, 0x5a,
  10653. 0x17, 0x0d,
  10654. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  10655. 0x32, 0x33, 0x31, 0x30, 0x5a
  10656. };
  10657. static byte minPubKey[] = {
  10658. 0x30, 0x1c,
  10659. 0x30, 0x0d,
  10660. 0x06, 0x09,
  10661. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10662. 0x01,
  10663. 0x05, 0x00,
  10664. 0x03, 0x0b,
  10665. 0x00, 0x30, 0x08,
  10666. 0x02, 0x01,
  10667. 0x03,
  10668. 0x02, 0x03,
  10669. 0x01, 0x00, 0x01
  10670. };
  10671. static byte minSigAlg[] = {
  10672. 0x30, 0x0d,
  10673. 0x06, 0x09,
  10674. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10675. 0x0b,
  10676. 0x05, 0x00
  10677. };
  10678. static byte minSig[] = {
  10679. 0x03, 0x01,
  10680. 0x00
  10681. };
  10682. static int add_seq(byte* certData, int offset, byte* data, byte length)
  10683. {
  10684. XMEMMOVE(certData + offset + 2, data, length);
  10685. certData[offset++] = 0x30;
  10686. certData[offset++] = length;
  10687. return offset + length;
  10688. }
  10689. static int add_data(byte* certData, int offset, byte* data, byte length)
  10690. {
  10691. XMEMCPY(certData + offset, data, length);
  10692. return offset + length;
  10693. }
  10694. static int cert_asn1_test(void)
  10695. {
  10696. int ret;
  10697. int len[3];
  10698. DecodedCert cert;
  10699. byte certData[106];
  10700. byte* badCert = NULL;
  10701. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10702. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10703. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10704. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  10705. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10706. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  10707. len[1] = add_seq(certData, 0, certData, len[2]);
  10708. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  10709. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  10710. len[0] = add_seq(certData, 0, certData, len[1]);
  10711. /* Minimal good certificate */
  10712. InitDecodedCert(&cert, certData, len[0], 0);
  10713. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10714. FreeDecodedCert(&cert);
  10715. if (ret != 0) {
  10716. ERROR_OUT(-7300, done);
  10717. }
  10718. /* Bad issuer name */
  10719. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10720. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10721. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  10722. len[1] = add_seq(certData, 0, certData, len[2]);
  10723. len[0] = add_seq(certData, 0, certData, len[1]);
  10724. /* Put data into allocated buffer to allow access error checking. */
  10725. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10726. XMEMCPY(badCert, certData, len[0]);
  10727. InitDecodedCert(&cert, badCert, len[0], 0);
  10728. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10729. FreeDecodedCert(&cert);
  10730. if (ret != ASN_PARSE_E) {
  10731. ERROR_OUT(-7301, done);
  10732. }
  10733. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10734. badCert = NULL;
  10735. ret = 0;
  10736. done:
  10737. if (badCert != NULL)
  10738. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10739. return ret;
  10740. }
  10741. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  10742. {
  10743. #if !defined(NO_FILESYSTEM)
  10744. DecodedCert cert;
  10745. byte* tmp;
  10746. size_t bytes;
  10747. XFILE file;
  10748. int ret;
  10749. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10750. if (tmp == NULL)
  10751. return -7400;
  10752. /* Certificate with Name Constraints extension. */
  10753. #ifdef FREESCALE_MQX
  10754. file = XFOPEN(".\\certs\\test\\cert-ext-nc.der", "rb");
  10755. #else
  10756. file = XFOPEN("./certs/test/cert-ext-nc.der", "rb");
  10757. #endif
  10758. if (!file) {
  10759. ERROR_OUT(-7401, done);
  10760. }
  10761. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10762. XFCLOSE(file);
  10763. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10764. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10765. if (ret != 0) {
  10766. ERROR_OUT(-7402, done);
  10767. }
  10768. FreeDecodedCert(&cert);
  10769. /* Certificate with Inhibit Any Policy extension. */
  10770. #ifdef FREESCALE_MQX
  10771. file = XFOPEN(".\\certs\\test\\cert-ext-ia.der", "rb");
  10772. #else
  10773. file = XFOPEN("./certs/test/cert-ext-ia.der", "rb");
  10774. #endif
  10775. if (!file) {
  10776. ERROR_OUT(-7403, done);
  10777. }
  10778. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10779. XFCLOSE(file);
  10780. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10781. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10782. if (ret != 0) {
  10783. ERROR_OUT(-7404, done);
  10784. }
  10785. FreeDecodedCert(&cert);
  10786. /* Certificate with Netscape Certificate Type extension. */
  10787. #ifdef FREESCALE_MQX
  10788. file = XFOPEN(".\\certs\\test\\cert-ext-nct.der", "rb");
  10789. #else
  10790. file = XFOPEN("./certs/test/cert-ext-nct.der", "rb");
  10791. #endif
  10792. if (!file) {
  10793. ERROR_OUT(-7405, done);
  10794. }
  10795. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10796. XFCLOSE(file);
  10797. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10798. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10799. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  10800. if (ret != 0) {
  10801. ERROR_OUT(-7406, done);
  10802. }
  10803. #else
  10804. if (ret != ASN_CRIT_EXT_E) {
  10805. ERROR_OUT(-7407, done);
  10806. }
  10807. ret = 0;
  10808. #endif
  10809. done:
  10810. FreeDecodedCert(&cert);
  10811. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10812. #endif /* !NO_FILESYSTEM */
  10813. if (ret == 0)
  10814. ret = cert_asn1_test();
  10815. return ret;
  10816. }
  10817. #endif /* WOLFSSL_TEST_CERT */
  10818. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  10819. !defined(NO_FILESYSTEM)
  10820. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  10821. {
  10822. DecodedCert cert;
  10823. byte* tmp;
  10824. size_t bytes;
  10825. XFILE file;
  10826. int ret;
  10827. /* created from rsa_test : othercert.der */
  10828. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10829. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10830. /* created from rsa_test : othercert.der */
  10831. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  10832. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  10833. #ifdef HAVE_ECC
  10834. /* created from ecc_test_cert_gen : certecc.der */
  10835. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10836. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  10837. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  10838. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  10839. #else
  10840. /* Authority key id from ./certs/ca-ecc-cert.pem */
  10841. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  10842. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  10843. #endif
  10844. #endif /* HAVE_ECC */
  10845. /* created from rsa_test : cert.der */
  10846. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10847. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10848. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10849. if (tmp == NULL)
  10850. return -7500;
  10851. /* load othercert.der (Cert signed by an authority) */
  10852. file = XFOPEN(otherCertDerFile, "rb");
  10853. if (!file) {
  10854. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10855. return -7501;
  10856. }
  10857. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10858. XFCLOSE(file);
  10859. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10860. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10861. if (ret != 0)
  10862. return -7502;
  10863. /* check the SKID from a RSA certificate */
  10864. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10865. return -7503;
  10866. /* check the AKID from an RSA certificate */
  10867. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10868. return -7504;
  10869. /* check the Key Usage from an RSA certificate */
  10870. if (!cert.extKeyUsageSet)
  10871. return -7505;
  10872. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  10873. return -7506;
  10874. /* check the CA Basic Constraints from an RSA certificate */
  10875. if (cert.isCA)
  10876. return -7507;
  10877. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10878. /* check the Certificate Policies Id */
  10879. if (cert.extCertPoliciesNb != 1)
  10880. return -7508;
  10881. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10882. return -7509;
  10883. #endif
  10884. FreeDecodedCert(&cert);
  10885. #ifdef HAVE_ECC
  10886. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  10887. file = XFOPEN(certEccDerFile, "rb");
  10888. if (!file) {
  10889. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10890. return -7510;
  10891. }
  10892. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10893. XFCLOSE(file);
  10894. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10895. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10896. if (ret != 0)
  10897. return -7511;
  10898. /* check the SKID from a ECC certificate - generated dynamically */
  10899. /* check the AKID from an ECC certificate */
  10900. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10901. return -7512;
  10902. /* check the Key Usage from an ECC certificate */
  10903. if (!cert.extKeyUsageSet)
  10904. return -7513;
  10905. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  10906. return -7514;
  10907. /* check the CA Basic Constraints from an ECC certificate */
  10908. if (cert.isCA)
  10909. return -7515;
  10910. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10911. /* check the Certificate Policies Id */
  10912. if (cert.extCertPoliciesNb != 2)
  10913. return -7516;
  10914. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  10915. return -7517;
  10916. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  10917. return -7518;
  10918. #endif
  10919. FreeDecodedCert(&cert);
  10920. #endif /* HAVE_ECC */
  10921. /* load cert.der (self signed certificate) */
  10922. file = XFOPEN(certDerFile, "rb");
  10923. if (!file) {
  10924. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10925. return -7519;
  10926. }
  10927. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10928. XFCLOSE(file);
  10929. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10930. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10931. if (ret != 0)
  10932. return -7520;
  10933. /* check the SKID from a CA certificate */
  10934. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10935. return -7521;
  10936. /* check the AKID from an CA certificate */
  10937. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10938. return -7522;
  10939. /* check the Key Usage from CA certificate */
  10940. if (!cert.extKeyUsageSet)
  10941. return -7523;
  10942. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  10943. return -7524;
  10944. /* check the CA Basic Constraints CA certificate */
  10945. if (!cert.isCA)
  10946. return -7525;
  10947. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10948. /* check the Certificate Policies Id */
  10949. if (cert.extCertPoliciesNb != 2)
  10950. return -7526;
  10951. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10952. return -7527;
  10953. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  10954. return -7528;
  10955. #endif
  10956. FreeDecodedCert(&cert);
  10957. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10958. return 0;
  10959. }
  10960. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  10961. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  10962. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  10963. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  10964. {
  10965. int ret = 0;
  10966. Cert cert;
  10967. FILE* file;
  10968. byte* der;
  10969. word32 derSz;
  10970. derSz = FOURK_BUF;
  10971. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10972. if (der == NULL)
  10973. ret = -7600;
  10974. if (ret == 0) {
  10975. /* load cert.der */
  10976. file = XFOPEN(certDerFile, "rb");
  10977. if (file != NULL) {
  10978. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  10979. XFCLOSE(file);
  10980. }
  10981. else
  10982. ret = -7601;
  10983. }
  10984. if (ret == 0) {
  10985. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  10986. ret = -7602;
  10987. }
  10988. }
  10989. if (ret == 0) {
  10990. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  10991. }
  10992. if (ret == 0) {
  10993. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10994. ret = -7603;
  10995. }
  10996. if (ret == 0) {
  10997. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  10998. ret = -7604;
  10999. }
  11000. if (ret == 0) {
  11001. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  11002. ret = -7605;
  11003. }
  11004. if (ret == 0) {
  11005. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  11006. ret = -7606;
  11007. }
  11008. if (ret == 0) {
  11009. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11010. ret = -7607;
  11011. }
  11012. if (ret == 0) {
  11013. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  11014. ret = -7608;
  11015. }
  11016. if (ret == 0) {
  11017. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  11018. ret = -7609;
  11019. }
  11020. #ifdef WOLFSSL_ALT_NAMES
  11021. if (ret == 0) {
  11022. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  11023. ret = -7610;
  11024. }
  11025. if (ret == 0) {
  11026. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11027. ret = -7611;
  11028. }
  11029. if (ret == 0) {
  11030. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  11031. ret = -7612;
  11032. }
  11033. if (ret == 0) {
  11034. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11035. ret = -7613;
  11036. }
  11037. #endif
  11038. if (ret == 0) {
  11039. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  11040. ret = -7614;
  11041. }
  11042. if (ret == 0) {
  11043. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  11044. ret = -7615;
  11045. }
  11046. wc_SetCert_Free(&cert);
  11047. if (ret == 0) {
  11048. if(cert.decodedCert != NULL)
  11049. ret = -7616;
  11050. }
  11051. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11052. return ret;
  11053. }
  11054. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  11055. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  11056. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  11057. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  11058. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11059. static int rsa_flatten_test(RsaKey* key)
  11060. {
  11061. int ret;
  11062. byte e[RSA_TEST_BYTES];
  11063. byte n[RSA_TEST_BYTES];
  11064. word32 eSz = sizeof(e);
  11065. word32 nSz = sizeof(n);
  11066. /* Parameter Validation testing. */
  11067. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  11068. #ifdef HAVE_USER_RSA
  11069. /* Implementation using IPP Libraries returns:
  11070. * -101 = USER_CRYPTO_ERROR
  11071. */
  11072. if (ret == 0)
  11073. #else
  11074. if (ret != BAD_FUNC_ARG)
  11075. #endif
  11076. return -7620;
  11077. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  11078. #ifdef HAVE_USER_RSA
  11079. /* Implementation using IPP Libraries returns:
  11080. * -101 = USER_CRYPTO_ERROR
  11081. */
  11082. if (ret == 0)
  11083. #else
  11084. if (ret != BAD_FUNC_ARG)
  11085. #endif
  11086. return -7621;
  11087. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  11088. #ifdef HAVE_USER_RSA
  11089. /* Implementation using IPP Libraries returns:
  11090. * -101 = USER_CRYPTO_ERROR
  11091. */
  11092. if (ret == 0)
  11093. #else
  11094. if (ret != BAD_FUNC_ARG)
  11095. #endif
  11096. return -7622;
  11097. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  11098. #ifdef HAVE_USER_RSA
  11099. /* Implementation using IPP Libraries returns:
  11100. * -101 = USER_CRYPTO_ERROR
  11101. */
  11102. if (ret == 0)
  11103. #else
  11104. if (ret != BAD_FUNC_ARG)
  11105. #endif
  11106. return -7623;
  11107. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  11108. #ifdef HAVE_USER_RSA
  11109. /* Implementation using IPP Libraries returns:
  11110. * -101 = USER_CRYPTO_ERROR
  11111. */
  11112. if (ret == 0)
  11113. #else
  11114. if (ret != BAD_FUNC_ARG)
  11115. #endif
  11116. return -7624;
  11117. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  11118. if (ret != 0)
  11119. return -7625;
  11120. eSz = 0;
  11121. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  11122. #ifdef HAVE_USER_RSA
  11123. /* Implementation using IPP Libraries returns:
  11124. * -101 = USER_CRYPTO_ERROR
  11125. */
  11126. if (ret == 0)
  11127. #elif defined(HAVE_FIPS) && \
  11128. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  11129. if (ret != 0)
  11130. #else
  11131. if (ret != RSA_BUFFER_E)
  11132. #endif
  11133. return -7626;
  11134. eSz = sizeof(e);
  11135. nSz = 0;
  11136. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  11137. #ifdef HAVE_USER_RSA
  11138. /* Implementation using IPP Libraries returns:
  11139. * -101 = USER_CRYPTO_ERROR
  11140. */
  11141. if (ret == 0)
  11142. #else
  11143. if (ret != RSA_BUFFER_E)
  11144. #endif
  11145. return -7627;
  11146. return 0;
  11147. }
  11148. #endif /* NO_ASN */
  11149. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  11150. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11151. static int rsa_export_key_test(RsaKey* key)
  11152. {
  11153. int ret;
  11154. byte e[3];
  11155. word32 eSz = sizeof(e);
  11156. byte n[RSA_TEST_BYTES];
  11157. word32 nSz = sizeof(n);
  11158. byte d[RSA_TEST_BYTES];
  11159. word32 dSz = sizeof(d);
  11160. byte p[RSA_TEST_BYTES/2];
  11161. word32 pSz = sizeof(p);
  11162. byte q[RSA_TEST_BYTES/2];
  11163. word32 qSz = sizeof(q);
  11164. word32 zero = 0;
  11165. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11166. if (ret != BAD_FUNC_ARG)
  11167. return -7630;
  11168. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11169. if (ret != BAD_FUNC_ARG)
  11170. return -7631;
  11171. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11172. if (ret != BAD_FUNC_ARG)
  11173. return -7632;
  11174. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11175. if (ret != BAD_FUNC_ARG)
  11176. return -7633;
  11177. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  11178. if (ret != BAD_FUNC_ARG)
  11179. return -7634;
  11180. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  11181. if (ret != BAD_FUNC_ARG)
  11182. return -7635;
  11183. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  11184. if (ret != BAD_FUNC_ARG)
  11185. return -7636;
  11186. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  11187. if (ret != BAD_FUNC_ARG)
  11188. return -7637;
  11189. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  11190. if (ret != BAD_FUNC_ARG)
  11191. return -7638;
  11192. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  11193. if (ret != BAD_FUNC_ARG)
  11194. return -7639;
  11195. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  11196. if (ret != BAD_FUNC_ARG)
  11197. return -7640;
  11198. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11199. if (ret != RSA_BUFFER_E)
  11200. return -7641;
  11201. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  11202. if (ret != RSA_BUFFER_E)
  11203. return -7642;
  11204. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11205. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  11206. if (ret != RSA_BUFFER_E)
  11207. return -7643;
  11208. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  11209. if (ret != RSA_BUFFER_E)
  11210. return -7644;
  11211. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  11212. if (ret != RSA_BUFFER_E)
  11213. return -7645;
  11214. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11215. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11216. if (ret != 0)
  11217. return -7646;
  11218. return 0;
  11219. }
  11220. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  11221. #ifndef NO_SIG_WRAPPER
  11222. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  11223. {
  11224. int ret;
  11225. word32 sigSz;
  11226. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  11227. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  11228. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  11229. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  11230. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  11231. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  11232. };
  11233. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  11234. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  11235. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  11236. 0x00, 0x04, 0x20,
  11237. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  11238. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  11239. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  11240. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  11241. };
  11242. word32 inLen = (word32)XSTRLEN((char*)in);
  11243. byte out[RSA_TEST_BYTES];
  11244. /* Parameter Validation testing. */
  11245. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  11246. if (ret != BAD_FUNC_ARG)
  11247. return -7650;
  11248. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  11249. if (ret != BAD_FUNC_ARG)
  11250. return -7651;
  11251. sigSz = (word32)modLen;
  11252. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  11253. inLen, out, &sigSz, key, keyLen, rng);
  11254. if (ret != BAD_FUNC_ARG)
  11255. return -7652;
  11256. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11257. 0, out, &sigSz, key, keyLen, rng);
  11258. if (ret != BAD_FUNC_ARG)
  11259. return -7653;
  11260. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11261. inLen, NULL, &sigSz, key, keyLen, rng);
  11262. if (ret != BAD_FUNC_ARG)
  11263. return -7654;
  11264. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11265. inLen, out, NULL, key, keyLen, rng);
  11266. if (ret != BAD_FUNC_ARG)
  11267. return -7655;
  11268. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11269. inLen, out, &sigSz, NULL, keyLen, rng);
  11270. if (ret != BAD_FUNC_ARG)
  11271. return -7656;
  11272. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11273. inLen, out, &sigSz, key, 0, rng);
  11274. if (ret != BAD_FUNC_ARG)
  11275. return -7657;
  11276. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11277. inLen, out, &sigSz, key, keyLen, NULL);
  11278. #ifdef HAVE_USER_RSA
  11279. /* Implementation using IPP Libraries returns:
  11280. * -101 = USER_CRYPTO_ERROR
  11281. */
  11282. if (ret == 0)
  11283. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  11284. /* blinding / rng handled with hardware acceleration */
  11285. if (ret != 0)
  11286. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  11287. /* async may not require RNG */
  11288. if (ret != 0 && ret != MISSING_RNG_E)
  11289. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  11290. /* FIPS140 implementation does not do blinding */
  11291. if (ret != 0)
  11292. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  11293. if (ret != SIG_TYPE_E)
  11294. #elif defined(WOLFSSL_CRYPTOCELL)
  11295. /* RNG is handled with the cryptocell */
  11296. if (ret != 0)
  11297. #else
  11298. if (ret != MISSING_RNG_E)
  11299. #endif
  11300. return -7658;
  11301. sigSz = 0;
  11302. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11303. inLen, out, &sigSz, key, keyLen, rng);
  11304. if (ret != BAD_FUNC_ARG)
  11305. return -7659;
  11306. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  11307. inLen, out, (word32)modLen, key, keyLen);
  11308. if (ret != BAD_FUNC_ARG)
  11309. return -7660;
  11310. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11311. 0, out, (word32)modLen, key, keyLen);
  11312. if (ret != BAD_FUNC_ARG)
  11313. return -7661;
  11314. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11315. inLen, NULL, (word32)modLen, key, keyLen);
  11316. if (ret != BAD_FUNC_ARG)
  11317. return -7662;
  11318. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11319. inLen, out, 0, key, keyLen);
  11320. if (ret != BAD_FUNC_ARG)
  11321. return -7663;
  11322. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11323. inLen, out, (word32)modLen, NULL, keyLen);
  11324. if (ret != BAD_FUNC_ARG)
  11325. return -7664;
  11326. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11327. inLen, out, (word32)modLen, key, 0);
  11328. if (ret != BAD_FUNC_ARG)
  11329. return -7665;
  11330. #ifndef HAVE_ECC
  11331. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  11332. if (ret != SIG_TYPE_E)
  11333. return -7666;
  11334. #endif
  11335. /* Use APIs. */
  11336. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  11337. if (ret != modLen)
  11338. return -7667;
  11339. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  11340. if (ret != modLen)
  11341. return -7668;
  11342. sigSz = (word32)ret;
  11343. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11344. XMEMSET(out, 0, sizeof(out));
  11345. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11346. inLen, out, &sigSz, key, keyLen, rng);
  11347. if (ret != 0)
  11348. return -7669;
  11349. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11350. inLen, out, (word32)modLen, key, keyLen);
  11351. if (ret != 0)
  11352. return -7670;
  11353. sigSz = (word32)sizeof(out);
  11354. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11355. in, inLen, out, &sigSz, key, keyLen, rng);
  11356. if (ret != 0)
  11357. return -7671;
  11358. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11359. in, inLen, out, (word32)modLen, key, keyLen);
  11360. if (ret != 0)
  11361. return -7672;
  11362. /* Wrong signature type. */
  11363. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11364. inLen, out, (word32)modLen, key, keyLen);
  11365. if (ret == 0)
  11366. return -7673;
  11367. /* check hash functions */
  11368. sigSz = (word32)sizeof(out);
  11369. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11370. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  11371. if (ret != 0)
  11372. return -7674;
  11373. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11374. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  11375. if (ret != 0)
  11376. return -7675;
  11377. sigSz = (word32)sizeof(out);
  11378. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11379. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  11380. if (ret != 0)
  11381. return -7676;
  11382. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11383. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  11384. if (ret != 0)
  11385. return -7677;
  11386. #else
  11387. (void)hash;
  11388. (void)hashEnc;
  11389. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  11390. return 0;
  11391. }
  11392. #endif /* !NO_SIG_WRAPPER */
  11393. #ifdef WC_RSA_NONBLOCK
  11394. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  11395. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  11396. {
  11397. int ret = 0, count;
  11398. int signSz = 0;
  11399. RsaNb nb;
  11400. byte* inlinePlain = NULL;
  11401. /* Enable non-blocking RSA mode - provide context */
  11402. ret = wc_RsaSetNonBlock(key, &nb);
  11403. if (ret != 0)
  11404. return ret;
  11405. #ifdef WC_RSA_NONBLOCK_TIME
  11406. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  11407. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  11408. if (ret != 0)
  11409. return ret;
  11410. #endif
  11411. count = 0;
  11412. do {
  11413. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  11414. count++; /* track number of would blocks */
  11415. if (ret == FP_WOULDBLOCK) {
  11416. /* do "other" work here */
  11417. }
  11418. } while (ret == FP_WOULDBLOCK);
  11419. if (ret < 0) {
  11420. return ret;
  11421. }
  11422. #ifdef DEBUG_WOLFSSL
  11423. printf("RSA non-block sign: %d times\n", count);
  11424. #endif
  11425. signSz = ret;
  11426. /* Test non-blocking verify */
  11427. XMEMSET(plain, 0, plainSz);
  11428. count = 0;
  11429. do {
  11430. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  11431. count++; /* track number of would blocks */
  11432. if (ret == FP_WOULDBLOCK) {
  11433. /* do "other" work here */
  11434. }
  11435. } while (ret == FP_WOULDBLOCK);
  11436. if (ret < 0) {
  11437. return ret;
  11438. }
  11439. #ifdef DEBUG_WOLFSSL
  11440. printf("RSA non-block verify: %d times\n", count);
  11441. #endif
  11442. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  11443. return SIG_VERIFY_E;
  11444. }
  11445. /* Test inline non-blocking verify */
  11446. count = 0;
  11447. do {
  11448. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  11449. count++; /* track number of would blocks */
  11450. if (ret == FP_WOULDBLOCK) {
  11451. /* do "other" work here */
  11452. }
  11453. } while (ret == FP_WOULDBLOCK);
  11454. if (ret < 0) {
  11455. return ret;
  11456. }
  11457. #ifdef DEBUG_WOLFSSL
  11458. printf("RSA non-block inline verify: %d times\n", count);
  11459. #endif
  11460. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  11461. return SIG_VERIFY_E;
  11462. }
  11463. /* Disabling non-block RSA mode */
  11464. ret = wc_RsaSetNonBlock(key, NULL);
  11465. (void)count;
  11466. return 0;
  11467. }
  11468. #endif
  11469. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  11470. static int rsa_decode_test(RsaKey* keyPub)
  11471. {
  11472. int ret;
  11473. word32 inSz;
  11474. word32 inOutIdx;
  11475. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  11476. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  11477. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  11478. 0x03 };
  11479. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  11480. 0x30, 0x18, 0x30, 0x16,
  11481. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11482. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11483. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  11484. 0x30, 0x1a, 0x30, 0x18,
  11485. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11486. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11487. 0x02, 0x1, 0x03 };
  11488. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  11489. 0x30, 0x1b, 0x30, 0x19,
  11490. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11491. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11492. 0x02, 0x1, 0x03 };
  11493. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  11494. 0x30, 0x18, 0x30, 0x16,
  11495. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11496. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11497. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  11498. 0x30, 0x18, 0x30, 0x16,
  11499. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11500. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11501. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  11502. 0x30, 0x16, 0x30, 0x14,
  11503. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11504. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11505. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  11506. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  11507. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11508. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  11509. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  11510. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  11511. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  11512. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  11513. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11514. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  11515. 0x30, 0x17, 0x30, 0x15,
  11516. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  11517. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11518. ret = wc_InitRsaKey(keyPub, NULL);
  11519. if (ret != 0)
  11520. return -7690;
  11521. /* Parameter Validation testing. */
  11522. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  11523. if (ret != BAD_FUNC_ARG) {
  11524. ret = -7691;
  11525. goto done;
  11526. }
  11527. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  11528. if (ret != BAD_FUNC_ARG) {
  11529. ret = -7692;
  11530. goto done;
  11531. }
  11532. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  11533. if (ret != BAD_FUNC_ARG) {
  11534. ret = -7693;
  11535. goto done;
  11536. }
  11537. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  11538. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11539. if (ret != 0) {
  11540. #else
  11541. if (ret != ASN_GETINT_E) {
  11542. #endif
  11543. ret = -7694;
  11544. goto done;
  11545. }
  11546. wc_FreeRsaKey(keyPub);
  11547. ret = wc_InitRsaKey(keyPub, NULL);
  11548. if (ret != 0)
  11549. return -7695;
  11550. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  11551. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11552. if (ret != 0) {
  11553. #else
  11554. if (ret != ASN_GETINT_E) {
  11555. #endif
  11556. ret = -7696;
  11557. goto done;
  11558. }
  11559. wc_FreeRsaKey(keyPub);
  11560. ret = wc_InitRsaKey(keyPub, NULL);
  11561. if (ret != 0)
  11562. return -7697;
  11563. /* Use API. */
  11564. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  11565. if (ret != 0) {
  11566. ret = -7698;
  11567. goto done;
  11568. }
  11569. wc_FreeRsaKey(keyPub);
  11570. ret = wc_InitRsaKey(keyPub, NULL);
  11571. if (ret != 0)
  11572. return -7699;
  11573. /* Parameter Validation testing. */
  11574. inSz = sizeof(good);
  11575. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  11576. if (ret != BAD_FUNC_ARG) {
  11577. ret = -7700;
  11578. goto done;
  11579. }
  11580. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  11581. if (ret != BAD_FUNC_ARG) {
  11582. ret = -7701;
  11583. goto done;
  11584. }
  11585. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  11586. if (ret != BAD_FUNC_ARG) {
  11587. ret = -7702;
  11588. goto done;
  11589. }
  11590. /* Use good data and offset to bad data. */
  11591. inOutIdx = 2;
  11592. inSz = sizeof(good) - inOutIdx;
  11593. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11594. if (ret != ASN_PARSE_E) {
  11595. ret = -7703;
  11596. goto done;
  11597. }
  11598. inOutIdx = 2;
  11599. inSz = sizeof(goodAlgId) - inOutIdx;
  11600. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11601. if (ret != ASN_PARSE_E) {
  11602. ret = -7704;
  11603. goto done;
  11604. }
  11605. inOutIdx = 2;
  11606. inSz = sizeof(goodAlgId);
  11607. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11608. #ifndef WOLFSSL_NO_DECODE_EXTRA
  11609. if (ret != ASN_PARSE_E)
  11610. #else
  11611. if (ret != ASN_RSA_KEY_E)
  11612. #endif
  11613. {
  11614. ret = -7705;
  11615. goto done;
  11616. }
  11617. /* Try different bad data. */
  11618. inSz = sizeof(badAlgIdNull);
  11619. inOutIdx = 0;
  11620. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  11621. if (ret != ASN_EXPECT_0_E) {
  11622. ret = -7706;
  11623. goto done;
  11624. }
  11625. inSz = sizeof(badNotBitString);
  11626. inOutIdx = 0;
  11627. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  11628. if (ret != ASN_BITSTR_E) {
  11629. ret = -7707;
  11630. goto done;
  11631. }
  11632. inSz = sizeof(badBitStringLen);
  11633. inOutIdx = 0;
  11634. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  11635. if (ret != ASN_PARSE_E) {
  11636. ret = -7708;
  11637. goto done;
  11638. }
  11639. inSz = sizeof(badNoSeq);
  11640. inOutIdx = 0;
  11641. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  11642. if (ret != ASN_PARSE_E) {
  11643. ret = -7709;
  11644. goto done;
  11645. }
  11646. inSz = sizeof(badNoObj);
  11647. inOutIdx = 0;
  11648. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  11649. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  11650. ret = -7710;
  11651. goto done;
  11652. }
  11653. inSz = sizeof(badIntN);
  11654. inOutIdx = 0;
  11655. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  11656. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  11657. ret = -7711;
  11658. goto done;
  11659. }
  11660. inSz = sizeof(badNotIntE);
  11661. inOutIdx = 0;
  11662. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  11663. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  11664. ret = -7712;
  11665. goto done;
  11666. }
  11667. /* TODO: Shouldn't pass as the sequence length is too small. */
  11668. inSz = sizeof(badLength);
  11669. inOutIdx = 0;
  11670. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  11671. #ifndef WOLFSSL_ASN_TEMPLATE
  11672. if (ret != 0)
  11673. #else
  11674. if (ret != ASN_PARSE_E)
  11675. #endif
  11676. {
  11677. ret = -7713;
  11678. goto done;
  11679. }
  11680. /* TODO: Shouldn't ignore object id's data. */
  11681. wc_FreeRsaKey(keyPub);
  11682. ret = wc_InitRsaKey(keyPub, NULL);
  11683. if (ret != 0)
  11684. return -7714;
  11685. inSz = sizeof(badBitStrNoZero);
  11686. inOutIdx = 0;
  11687. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  11688. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  11689. ret = -7715;
  11690. goto done;
  11691. }
  11692. wc_FreeRsaKey(keyPub);
  11693. ret = wc_InitRsaKey(keyPub, NULL);
  11694. if (ret != 0)
  11695. return -7716;
  11696. /* Valid data cases. */
  11697. inSz = sizeof(good);
  11698. inOutIdx = 0;
  11699. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11700. if (ret != 0) {
  11701. ret = -7717;
  11702. goto done;
  11703. }
  11704. if (inOutIdx != inSz) {
  11705. ret = -7718;
  11706. goto done;
  11707. }
  11708. wc_FreeRsaKey(keyPub);
  11709. ret = wc_InitRsaKey(keyPub, NULL);
  11710. if (ret != 0)
  11711. return -7719;
  11712. inSz = sizeof(goodAlgId);
  11713. inOutIdx = 0;
  11714. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11715. if (ret != 0) {
  11716. ret = -7720;
  11717. goto done;
  11718. }
  11719. if (inOutIdx != inSz) {
  11720. ret = -7721;
  11721. goto done;
  11722. }
  11723. wc_FreeRsaKey(keyPub);
  11724. ret = wc_InitRsaKey(keyPub, NULL);
  11725. if (ret != 0)
  11726. return -7722;
  11727. inSz = sizeof(goodAlgIdNull);
  11728. inOutIdx = 0;
  11729. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  11730. if (ret != 0) {
  11731. ret = -7723;
  11732. goto done;
  11733. }
  11734. if (inOutIdx != inSz) {
  11735. ret = -7724;
  11736. goto done;
  11737. }
  11738. done:
  11739. wc_FreeRsaKey(keyPub);
  11740. return ret;
  11741. }
  11742. #endif
  11743. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  11744. /* Need to create known good signatures to test with this. */
  11745. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  11746. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  11747. {
  11748. byte digest[WC_MAX_DIGEST_SIZE];
  11749. int ret = 0;
  11750. const char inStr[] = TEST_STRING;
  11751. word32 inLen = (word32)TEST_STRING_SZ;
  11752. word32 outSz;
  11753. word32 plainSz;
  11754. word32 digestSz;
  11755. int i, j;
  11756. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11757. int k, l;
  11758. #endif
  11759. int len;
  11760. byte* plain;
  11761. int mgf[] = {
  11762. #ifndef NO_SHA
  11763. WC_MGF1SHA1,
  11764. #endif
  11765. #ifdef WOLFSSL_SHA224
  11766. WC_MGF1SHA224,
  11767. #endif
  11768. WC_MGF1SHA256,
  11769. #ifdef WOLFSSL_SHA384
  11770. WC_MGF1SHA384,
  11771. #endif
  11772. #ifdef WOLFSSL_SHA512
  11773. WC_MGF1SHA512
  11774. #endif
  11775. };
  11776. enum wc_HashType hash[] = {
  11777. #ifndef NO_SHA
  11778. WC_HASH_TYPE_SHA,
  11779. #endif
  11780. #ifdef WOLFSSL_SHA224
  11781. WC_HASH_TYPE_SHA224,
  11782. #endif
  11783. WC_HASH_TYPE_SHA256,
  11784. #ifdef WOLFSSL_SHA384
  11785. WC_HASH_TYPE_SHA384,
  11786. #endif
  11787. #ifdef WOLFSSL_SHA512
  11788. WC_HASH_TYPE_SHA512,
  11789. #endif
  11790. };
  11791. DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  11792. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11793. DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  11794. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11795. if (in == NULL || out == NULL || sig == NULL)
  11796. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  11797. #endif
  11798. XMEMCPY(in, inStr, inLen);
  11799. /* Test all combinations of hash and MGF. */
  11800. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  11801. /* Calculate hash of message. */
  11802. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  11803. if (ret != 0)
  11804. ERROR_OUT(-7730, exit_rsa_pss);
  11805. digestSz = wc_HashGetDigestSize(hash[j]);
  11806. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  11807. outSz = RSA_TEST_BYTES;
  11808. do {
  11809. #if defined(WOLFSSL_ASYNC_CRYPT)
  11810. ret = wc_AsyncWait(ret, &key->asyncDev,
  11811. WC_ASYNC_FLAG_CALL_AGAIN);
  11812. #endif
  11813. if (ret >= 0) {
  11814. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  11815. hash[j], mgf[i], -1, key, rng);
  11816. }
  11817. } while (ret == WC_PENDING_E);
  11818. if (ret <= 0)
  11819. ERROR_OUT(-7731, exit_rsa_pss);
  11820. outSz = ret;
  11821. XMEMCPY(sig, out, outSz);
  11822. plain = NULL;
  11823. TEST_SLEEP();
  11824. do {
  11825. #if defined(WOLFSSL_ASYNC_CRYPT)
  11826. ret = wc_AsyncWait(ret, &key->asyncDev,
  11827. WC_ASYNC_FLAG_CALL_AGAIN);
  11828. #endif
  11829. if (ret >= 0) {
  11830. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  11831. mgf[i], -1, key);
  11832. }
  11833. } while (ret == WC_PENDING_E);
  11834. if (ret <= 0)
  11835. ERROR_OUT(-7732, exit_rsa_pss);
  11836. plainSz = ret;
  11837. TEST_SLEEP();
  11838. #if defined(HAVE_SELFTEST) && \
  11839. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11840. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11841. hash[j], -1);
  11842. #else
  11843. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  11844. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  11845. #endif
  11846. if (ret != 0)
  11847. ERROR_OUT(-7733, exit_rsa_pss);
  11848. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11849. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  11850. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  11851. if (i == k && j == l)
  11852. continue;
  11853. XMEMCPY(sig, out, outSz);
  11854. do {
  11855. #if defined(WOLFSSL_ASYNC_CRYPT)
  11856. ret = wc_AsyncWait(ret, &key->asyncDev,
  11857. WC_ASYNC_FLAG_CALL_AGAIN);
  11858. #endif
  11859. if (ret >= 0) {
  11860. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  11861. (byte**)&plain, hash[l], mgf[k], -1, key);
  11862. }
  11863. } while (ret == WC_PENDING_E);
  11864. if (ret >= 0)
  11865. ERROR_OUT(-7734, exit_rsa_pss);
  11866. }
  11867. }
  11868. #endif
  11869. }
  11870. }
  11871. /* Test that a salt length of zero works. */
  11872. digestSz = wc_HashGetDigestSize(hash[0]);
  11873. outSz = RSA_TEST_BYTES;
  11874. do {
  11875. #if defined(WOLFSSL_ASYNC_CRYPT)
  11876. ret = wc_AsyncWait(ret, &key->asyncDev,
  11877. WC_ASYNC_FLAG_CALL_AGAIN);
  11878. #endif
  11879. if (ret >= 0) {
  11880. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11881. mgf[0], 0, key, rng);
  11882. }
  11883. } while (ret == WC_PENDING_E);
  11884. if (ret <= 0)
  11885. ERROR_OUT(-7735, exit_rsa_pss);
  11886. outSz = ret;
  11887. TEST_SLEEP();
  11888. do {
  11889. #if defined(WOLFSSL_ASYNC_CRYPT)
  11890. ret = wc_AsyncWait(ret, &key->asyncDev,
  11891. WC_ASYNC_FLAG_CALL_AGAIN);
  11892. #endif
  11893. if (ret >= 0) {
  11894. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  11895. 0, key);
  11896. }
  11897. } while (ret == WC_PENDING_E);
  11898. if (ret <= 0)
  11899. ERROR_OUT(-7736, exit_rsa_pss);
  11900. plainSz = ret;
  11901. TEST_SLEEP();
  11902. do {
  11903. #if defined(WOLFSSL_ASYNC_CRYPT)
  11904. ret = wc_AsyncWait(ret, &key->asyncDev,
  11905. WC_ASYNC_FLAG_CALL_AGAIN);
  11906. #endif
  11907. if (ret >= 0) {
  11908. #if defined(HAVE_SELFTEST) && \
  11909. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11910. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11911. hash[0], 0);
  11912. #else
  11913. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  11914. hash[0], 0, 0, HEAP_HINT);
  11915. #endif
  11916. }
  11917. } while (ret == WC_PENDING_E);
  11918. if (ret != 0)
  11919. ERROR_OUT(-7737, exit_rsa_pss);
  11920. XMEMCPY(sig, out, outSz);
  11921. plain = NULL;
  11922. do {
  11923. #if defined(WOLFSSL_ASYNC_CRYPT)
  11924. ret = wc_AsyncWait(ret, &key->asyncDev,
  11925. WC_ASYNC_FLAG_CALL_AGAIN);
  11926. #endif
  11927. if (ret >= 0) {
  11928. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11929. 0, key);
  11930. }
  11931. } while (ret == WC_PENDING_E);
  11932. if (ret <= 0)
  11933. ERROR_OUT(-7738, exit_rsa_pss);
  11934. plainSz = ret;
  11935. TEST_SLEEP();
  11936. #if defined(HAVE_SELFTEST) && \
  11937. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11938. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11939. 0);
  11940. #else
  11941. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  11942. 0, 0, HEAP_HINT);
  11943. #endif
  11944. if (ret != 0)
  11945. ERROR_OUT(-7739, exit_rsa_pss);
  11946. /* Test bad salt lengths in various APIs. */
  11947. digestSz = wc_HashGetDigestSize(hash[0]);
  11948. outSz = RSA_TEST_BYTES;
  11949. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11950. len = -2;
  11951. #else
  11952. len = -3;
  11953. #endif
  11954. do {
  11955. #if defined(WOLFSSL_ASYNC_CRYPT)
  11956. ret = wc_AsyncWait(ret, &key->asyncDev,
  11957. WC_ASYNC_FLAG_CALL_AGAIN);
  11958. #endif
  11959. if (ret >= 0) {
  11960. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11961. mgf[0], len, key, rng);
  11962. }
  11963. } while (ret == WC_PENDING_E);
  11964. if (ret != PSS_SALTLEN_E)
  11965. ERROR_OUT(-7740, exit_rsa_pss);
  11966. do {
  11967. #if defined(WOLFSSL_ASYNC_CRYPT)
  11968. ret = wc_AsyncWait(ret, &key->asyncDev,
  11969. WC_ASYNC_FLAG_CALL_AGAIN);
  11970. #endif
  11971. if (ret >= 0) {
  11972. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11973. mgf[0], digestSz + 1, key, rng);
  11974. }
  11975. } while (ret == WC_PENDING_E);
  11976. if (ret != PSS_SALTLEN_E)
  11977. ERROR_OUT(-7741, exit_rsa_pss);
  11978. TEST_SLEEP();
  11979. do {
  11980. #if defined(WOLFSSL_ASYNC_CRYPT)
  11981. ret = wc_AsyncWait(ret, &key->asyncDev,
  11982. WC_ASYNC_FLAG_CALL_AGAIN);
  11983. #endif
  11984. if (ret >= 0) {
  11985. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  11986. mgf[0], -2, key);
  11987. }
  11988. } while (ret == WC_PENDING_E);
  11989. if (ret != PSS_SALTLEN_E)
  11990. ERROR_OUT(-7742, exit_rsa_pss);
  11991. TEST_SLEEP();
  11992. do {
  11993. #if defined(WOLFSSL_ASYNC_CRYPT)
  11994. ret = wc_AsyncWait(ret, &key->asyncDev,
  11995. WC_ASYNC_FLAG_CALL_AGAIN);
  11996. #endif
  11997. if (ret >= 0) {
  11998. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11999. digestSz + 1, key);
  12000. }
  12001. } while (ret == WC_PENDING_E);
  12002. if (ret != PSS_SALTLEN_E)
  12003. ERROR_OUT(-7743, exit_rsa_pss);
  12004. TEST_SLEEP();
  12005. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  12006. len = -2;
  12007. #else
  12008. len = -3;
  12009. #endif
  12010. #if defined(HAVE_SELFTEST) && \
  12011. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12012. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  12013. len);
  12014. #else
  12015. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12016. len, 0, HEAP_HINT);
  12017. #endif
  12018. if (ret != PSS_SALTLEN_E)
  12019. ERROR_OUT(-7744, exit_rsa_pss);
  12020. #ifndef WOLFSSL_PSS_LONG_SALT
  12021. len = digestSz + 1;
  12022. #else
  12023. len = plainSz - digestSz - 1;
  12024. #endif
  12025. #if defined(HAVE_SELFTEST) && \
  12026. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12027. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  12028. len);
  12029. #else
  12030. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12031. len, 0, HEAP_HINT);
  12032. #endif
  12033. if (ret != PSS_SALTLEN_E)
  12034. ERROR_OUT(-7745, exit_rsa_pss);
  12035. ret = 0;
  12036. exit_rsa_pss:
  12037. FREE_VAR(sig, HEAP_HINT);
  12038. FREE_VAR(in, HEAP_HINT);
  12039. FREE_VAR(out, HEAP_HINT);
  12040. return ret;
  12041. }
  12042. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  12043. #endif
  12044. #ifdef WC_RSA_NO_PADDING
  12045. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  12046. {
  12047. WC_RNG rng;
  12048. byte* tmp = NULL;
  12049. size_t bytes;
  12050. int ret;
  12051. word32 inLen = 0;
  12052. word32 idx = 0;
  12053. word32 outSz = RSA_TEST_BYTES;
  12054. word32 plainSz = RSA_TEST_BYTES;
  12055. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12056. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  12057. !defined(NO_FILESYSTEM)
  12058. XFILE file;
  12059. #endif
  12060. DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  12061. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12062. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12063. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  12064. if (key == NULL || out == NULL || plain == NULL)
  12065. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  12066. #endif
  12067. /* initialize stack structures */
  12068. XMEMSET(&rng, 0, sizeof(rng));
  12069. XMEMSET(key, 0, sizeof(RsaKey));
  12070. #ifdef USE_CERT_BUFFERS_1024
  12071. bytes = (size_t)sizeof_client_key_der_1024;
  12072. if (bytes < (size_t)sizeof_client_cert_der_1024)
  12073. bytes = (size_t)sizeof_client_cert_der_1024;
  12074. #elif defined(USE_CERT_BUFFERS_2048)
  12075. bytes = (size_t)sizeof_client_key_der_2048;
  12076. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12077. bytes = (size_t)sizeof_client_cert_der_2048;
  12078. #else
  12079. bytes = FOURK_BUF;
  12080. #endif
  12081. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12082. if (tmp == NULL
  12083. #ifdef WOLFSSL_ASYNC_CRYPT
  12084. || out == NULL || plain == NULL
  12085. #endif
  12086. ) {
  12087. ERROR_OUT(-7800, exit_rsa_nopadding);
  12088. }
  12089. #ifdef USE_CERT_BUFFERS_1024
  12090. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  12091. #elif defined(USE_CERT_BUFFERS_2048)
  12092. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12093. #elif defined(USE_CERT_BUFFERS_3072)
  12094. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12095. #elif defined(USE_CERT_BUFFERS_4096)
  12096. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12097. #elif !defined(NO_FILESYSTEM)
  12098. file = XFOPEN(clientKey, "rb");
  12099. if (!file) {
  12100. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  12101. ERROR_OUT(-7801, exit_rsa_nopadding);
  12102. }
  12103. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12104. XFCLOSE(file);
  12105. #else
  12106. /* No key to use. */
  12107. ERROR_OUT(-7802, exit_rsa_nopadding);
  12108. #endif /* USE_CERT_BUFFERS */
  12109. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  12110. if (ret != 0) {
  12111. ERROR_OUT(-7803, exit_rsa_nopadding);
  12112. }
  12113. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12114. if (ret != 0) {
  12115. ERROR_OUT(-7804, exit_rsa_nopadding);
  12116. }
  12117. /* after loading in key use tmp as the test buffer */
  12118. #ifndef HAVE_FIPS
  12119. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12120. #else
  12121. ret = wc_InitRng(&rng);
  12122. #endif
  12123. if (ret != 0) {
  12124. ERROR_OUT(-7805, exit_rsa_nopadding);
  12125. }
  12126. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12127. inLen = wc_RsaEncryptSize(key);
  12128. outSz = inLen;
  12129. plainSz = inLen;
  12130. XMEMSET(tmp, 7, inLen);
  12131. do {
  12132. #if defined(WOLFSSL_ASYNC_CRYPT)
  12133. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12134. #endif
  12135. if (ret >= 0) {
  12136. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  12137. RSA_PRIVATE_ENCRYPT, &rng);
  12138. }
  12139. } while (ret == WC_PENDING_E);
  12140. if (ret <= 0) {
  12141. ERROR_OUT(-7806, exit_rsa_nopadding);
  12142. }
  12143. /* encrypted result should not be the same as input */
  12144. if (XMEMCMP(out, tmp, inLen) == 0) {
  12145. ERROR_OUT(-7807, exit_rsa_nopadding);
  12146. }
  12147. TEST_SLEEP();
  12148. /* decrypt with public key and compare result */
  12149. do {
  12150. #if defined(WOLFSSL_ASYNC_CRYPT)
  12151. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12152. #endif
  12153. if (ret >= 0) {
  12154. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  12155. RSA_PUBLIC_DECRYPT, &rng);
  12156. }
  12157. } while (ret == WC_PENDING_E);
  12158. if (ret <= 0) {
  12159. ERROR_OUT(-7808, exit_rsa_nopadding);
  12160. }
  12161. if (XMEMCMP(plain, tmp, inLen) != 0) {
  12162. ERROR_OUT(-7809, exit_rsa_nopadding);
  12163. }
  12164. TEST_SLEEP();
  12165. #endif
  12166. #ifdef WC_RSA_BLINDING
  12167. ret = wc_RsaSetRNG(NULL, &rng);
  12168. if (ret != BAD_FUNC_ARG) {
  12169. ERROR_OUT(-7810, exit_rsa_nopadding);
  12170. }
  12171. ret = wc_RsaSetRNG(key, &rng);
  12172. if (ret < 0) {
  12173. ERROR_OUT(-7811, exit_rsa_nopadding);
  12174. }
  12175. #endif
  12176. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  12177. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12178. do {
  12179. #if defined(WOLFSSL_ASYNC_CRYPT)
  12180. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12181. #endif
  12182. if (ret >= 0) {
  12183. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  12184. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  12185. }
  12186. } while (ret == WC_PENDING_E);
  12187. if (ret < 0) {
  12188. ERROR_OUT(-7812, exit_rsa_nopadding);
  12189. }
  12190. TEST_SLEEP();
  12191. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  12192. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12193. do {
  12194. #if defined(WOLFSSL_ASYNC_CRYPT)
  12195. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12196. #endif
  12197. if (ret >= 0) {
  12198. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  12199. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  12200. }
  12201. } while (ret == WC_PENDING_E);
  12202. if (ret < 0) {
  12203. ERROR_OUT(-7813, exit_rsa_nopadding);
  12204. }
  12205. if (XMEMCMP(plain, tmp, inLen) != 0) {
  12206. ERROR_OUT(-7814, exit_rsa_nopadding);
  12207. }
  12208. TEST_SLEEP();
  12209. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12210. /* test some bad arguments */
  12211. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  12212. &rng);
  12213. if (ret != BAD_FUNC_ARG) {
  12214. ERROR_OUT(-7815, exit_rsa_nopadding);
  12215. }
  12216. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  12217. &rng);
  12218. if (ret != BAD_FUNC_ARG) {
  12219. ERROR_OUT(-7816, exit_rsa_nopadding);
  12220. }
  12221. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  12222. &rng);
  12223. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  12224. ERROR_OUT(-7817, exit_rsa_nopadding);
  12225. }
  12226. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  12227. RSA_PUBLIC_DECRYPT, &rng);
  12228. if (ret != BAD_FUNC_ARG) {
  12229. ERROR_OUT(-7818, exit_rsa_nopadding);
  12230. }
  12231. /* if making it to this point of code without hitting an ERROR_OUT then
  12232. * all tests have passed */
  12233. ret = 0;
  12234. exit_rsa_nopadding:
  12235. wc_FreeRsaKey(key);
  12236. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12237. FREE_VAR(key, HEAP_HINT);
  12238. FREE_VAR(out, HEAP_HINT);
  12239. FREE_VAR(plain, HEAP_HINT);
  12240. wc_FreeRng(&rng);
  12241. return ret;
  12242. }
  12243. #endif /* WC_RSA_NO_PADDING */
  12244. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  12245. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  12246. {
  12247. byte* tmp = NULL;
  12248. size_t bytes;
  12249. int ret;
  12250. word32 inLen = 0;
  12251. #ifndef NO_ASN
  12252. word32 idx = 0;
  12253. #endif
  12254. word32 outSz = RSA_TEST_BYTES;
  12255. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12256. word32 plainSz = RSA_TEST_BYTES;
  12257. #endif
  12258. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  12259. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  12260. XFILE file;
  12261. #endif
  12262. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12263. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12264. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12265. #endif
  12266. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  12267. if (out == NULL
  12268. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12269. || plain == NULL
  12270. #endif
  12271. ) {
  12272. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  12273. }
  12274. #endif
  12275. #if defined(USE_CERT_BUFFERS_2048)
  12276. bytes = (size_t)sizeof_client_key_der_2048;
  12277. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12278. bytes = (size_t)sizeof_client_cert_der_2048;
  12279. #else
  12280. bytes = FOURK_BUF;
  12281. #endif
  12282. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12283. if (tmp == NULL
  12284. #ifdef WOLFSSL_ASYNC_CRYPT
  12285. || out == NULL || plain == NULL
  12286. #endif
  12287. ) {
  12288. ERROR_OUT(-7800, exit_rsa_even_mod);
  12289. }
  12290. #if defined(USE_CERT_BUFFERS_2048)
  12291. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12292. #elif defined(USE_CERT_BUFFERS_3072)
  12293. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12294. #elif defined(USE_CERT_BUFFERS_4096)
  12295. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12296. #elif !defined(NO_FILESYSTEM)
  12297. file = XFOPEN(clientKey, "rb");
  12298. if (!file) {
  12299. err_sys("can't open ./certs/client-key.der, "
  12300. "Please run from wolfSSL home dir", -40);
  12301. ERROR_OUT(-7801, exit_rsa_even_mod);
  12302. }
  12303. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12304. XFCLOSE(file);
  12305. #else
  12306. /* No key to use. */
  12307. ERROR_OUT(-7802, exit_rsa_even_mod);
  12308. #endif /* USE_CERT_BUFFERS */
  12309. #ifndef NO_ASN
  12310. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12311. if (ret != 0) {
  12312. ERROR_OUT(-7804, exit_rsa_even_mod);
  12313. }
  12314. #else
  12315. #ifdef USE_CERT_BUFFERS_2048
  12316. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  12317. if (ret != 0) {
  12318. ERROR_OUT(-7804, exit_rsa_even_mod);
  12319. }
  12320. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  12321. if (ret != 0) {
  12322. ERROR_OUT(-7804, exit_rsa_even_mod);
  12323. }
  12324. #ifndef NO_SIG_WRAPPER
  12325. modLen = 2048;
  12326. #endif
  12327. #else
  12328. #error Not supported yet!
  12329. #endif
  12330. #endif
  12331. key->n.dp[0] &= (mp_digit)-2;
  12332. if (ret != 0) {
  12333. ERROR_OUT(-7804, exit_rsa_even_mod);
  12334. }
  12335. /* after loading in key use tmp as the test buffer */
  12336. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  12337. !defined(WOLFSSL_SP_ARM64_ASM)
  12338. /* The ARM64_ASM code that was FIPS validated did not return these expected
  12339. * failure codes. These tests cases were added after the assembly was
  12340. * in-lined in the module and validated, these tests will be available in
  12341. * the 140-3 module */
  12342. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  12343. inLen = 32;
  12344. outSz = wc_RsaEncryptSize(key);
  12345. XMEMSET(tmp, 7, plainSz);
  12346. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  12347. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12348. ERROR_OUT(-7806, exit_rsa_even_mod);
  12349. }
  12350. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  12351. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12352. ERROR_OUT(-7808, exit_rsa_even_mod);
  12353. }
  12354. #endif
  12355. #ifdef WC_RSA_BLINDING
  12356. ret = wc_RsaSetRNG(key, rng);
  12357. if (ret < 0) {
  12358. ERROR_OUT(-7811, exit_rsa_even_mod);
  12359. }
  12360. #endif
  12361. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  12362. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  12363. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  12364. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12365. ERROR_OUT(-7812, exit_rsa_even_mod);
  12366. }
  12367. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  12368. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12369. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  12370. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12371. ERROR_OUT(-7813, exit_rsa_even_mod);
  12372. }
  12373. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12374. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  12375. /* if making it to this point of code without hitting an ERROR_OUT then
  12376. * all tests have passed */
  12377. ret = 0;
  12378. exit_rsa_even_mod:
  12379. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12380. FREE_VAR(out, HEAP_HINT);
  12381. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12382. FREE_VAR(plain, HEAP_HINT);
  12383. #endif
  12384. (void)out;
  12385. (void)outSz;
  12386. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12387. (void)plain;
  12388. (void)plainSz;
  12389. #endif
  12390. (void)inLen;
  12391. (void)rng;
  12392. return ret;
  12393. }
  12394. #endif /* WOLFSSL_HAVE_SP_RSA */
  12395. #ifdef WOLFSSL_CERT_GEN
  12396. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  12397. {
  12398. #ifdef WOLFSSL_SMALL_STACK
  12399. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12400. #ifdef WOLFSSL_TEST_CERT
  12401. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12402. #endif
  12403. #else
  12404. RsaKey caKey[1];
  12405. #ifdef WOLFSSL_TEST_CERT
  12406. DecodedCert decode[1];
  12407. #endif
  12408. #endif
  12409. byte* der = NULL;
  12410. int ret;
  12411. Cert* myCert = NULL;
  12412. int certSz;
  12413. size_t bytes3;
  12414. word32 idx3 = 0;
  12415. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12416. XFILE file3;
  12417. #endif
  12418. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  12419. struct tm beforeTime;
  12420. struct tm afterTime;
  12421. #endif
  12422. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  12423. (void)keypub;
  12424. #ifdef WOLFSSL_SMALL_STACK
  12425. if (caKey == NULL)
  12426. ERROR_OUT(MEMORY_E, exit_rsa);
  12427. #ifdef WOLFSSL_TEST_CERT
  12428. if (decode == NULL)
  12429. ERROR_OUT(MEMORY_E, exit_rsa);
  12430. #endif
  12431. #endif
  12432. XMEMSET(caKey, 0, sizeof *caKey);
  12433. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12434. if (der == NULL) {
  12435. ERROR_OUT(-7820, exit_rsa);
  12436. }
  12437. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12438. if (myCert == NULL) {
  12439. ERROR_OUT(-7821, exit_rsa);
  12440. }
  12441. /* self signed */
  12442. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  12443. ERROR_OUT(-7822, exit_rsa);
  12444. }
  12445. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12446. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  12447. myCert->serialSz = (int)sizeof(mySerial);
  12448. myCert->isCA = 1;
  12449. #ifndef NO_SHA256
  12450. myCert->sigType = CTC_SHA256wRSA;
  12451. #else
  12452. myCert->sigType = CTC_SHAwRSA;
  12453. #endif
  12454. #ifdef WOLFSSL_CERT_EXT
  12455. /* add Policies */
  12456. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12457. CTC_MAX_CERTPOL_SZ);
  12458. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  12459. CTC_MAX_CERTPOL_SZ);
  12460. myCert->certPoliciesNb = 2;
  12461. /* add SKID from the Public Key */
  12462. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12463. ERROR_OUT(-7823, exit_rsa);
  12464. }
  12465. /* add AKID from the Public Key */
  12466. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12467. ERROR_OUT(-7824, exit_rsa);
  12468. }
  12469. /* add Key Usage */
  12470. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  12471. ERROR_OUT(-7825, exit_rsa);
  12472. }
  12473. #ifdef WOLFSSL_EKU_OID
  12474. {
  12475. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  12476. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  12477. HEAP_HINT) != 0) {
  12478. ERROR_OUT(-7826, exit_rsa);
  12479. }
  12480. }
  12481. #endif /* WOLFSSL_EKU_OID */
  12482. #endif /* WOLFSSL_CERT_EXT */
  12483. ret = 0;
  12484. do {
  12485. #if defined(WOLFSSL_ASYNC_CRYPT)
  12486. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12487. #endif
  12488. if (ret >= 0) {
  12489. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  12490. }
  12491. } while (ret == WC_PENDING_E);
  12492. if (ret < 0) {
  12493. ERROR_OUT(-7827, exit_rsa);
  12494. }
  12495. certSz = ret;
  12496. #ifdef WOLFSSL_TEST_CERT
  12497. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12498. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12499. if (ret != 0) {
  12500. FreeDecodedCert(decode);
  12501. ERROR_OUT(-7828, exit_rsa);
  12502. }
  12503. FreeDecodedCert(decode);
  12504. #endif
  12505. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  12506. CERT_TYPE, -5578);
  12507. if (ret != 0) {
  12508. goto exit_rsa;
  12509. }
  12510. /* Setup Certificate */
  12511. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  12512. ERROR_OUT(-7829, exit_rsa);
  12513. }
  12514. #ifdef WOLFSSL_ALT_NAMES
  12515. /* Get CA Cert for testing */
  12516. #ifdef USE_CERT_BUFFERS_1024
  12517. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  12518. bytes3 = sizeof_ca_cert_der_1024;
  12519. #elif defined(USE_CERT_BUFFERS_2048)
  12520. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  12521. bytes3 = sizeof_ca_cert_der_2048;
  12522. #else
  12523. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  12524. if (!file3) {
  12525. ERROR_OUT(-7830, exit_rsa);
  12526. }
  12527. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12528. XFCLOSE(file3);
  12529. #endif /* USE_CERT_BUFFERS */
  12530. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  12531. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  12532. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  12533. if (ret != 0) {
  12534. ERROR_OUT(-7831, exit_rsa);
  12535. }
  12536. #endif
  12537. /* get alt names from der */
  12538. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  12539. if (ret != 0) {
  12540. ERROR_OUT(-7832, exit_rsa);
  12541. }
  12542. /* get dates from der */
  12543. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  12544. if (ret != 0) {
  12545. ERROR_OUT(-7833, exit_rsa);
  12546. }
  12547. #ifndef NO_ASN_TIME
  12548. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  12549. if (ret < 0) {
  12550. ERROR_OUT(-7834, exit_rsa);
  12551. }
  12552. #endif
  12553. #endif /* WOLFSSL_ALT_NAMES */
  12554. /* Get CA Key */
  12555. #ifdef USE_CERT_BUFFERS_1024
  12556. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12557. bytes3 = sizeof_ca_key_der_1024;
  12558. #elif defined(USE_CERT_BUFFERS_2048)
  12559. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12560. bytes3 = sizeof_ca_key_der_2048;
  12561. #else
  12562. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12563. if (!file3) {
  12564. ERROR_OUT(-7835, exit_rsa);
  12565. }
  12566. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12567. XFCLOSE(file3);
  12568. #endif /* USE_CERT_BUFFERS */
  12569. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12570. if (ret != 0) {
  12571. ERROR_OUT(-7836, exit_rsa);
  12572. }
  12573. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12574. if (ret != 0) {
  12575. ERROR_OUT(-7837, exit_rsa);
  12576. }
  12577. #ifndef NO_SHA256
  12578. myCert->sigType = CTC_SHA256wRSA;
  12579. #else
  12580. myCert->sigType = CTC_SHAwRSA;
  12581. #endif
  12582. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12583. #ifdef WOLFSSL_CERT_EXT
  12584. /* add Policies */
  12585. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12586. CTC_MAX_CERTPOL_SZ);
  12587. myCert->certPoliciesNb =1;
  12588. /* add SKID from the Public Key */
  12589. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  12590. ERROR_OUT(-7838, exit_rsa);
  12591. }
  12592. /* add AKID from the CA certificate */
  12593. #if defined(USE_CERT_BUFFERS_2048)
  12594. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12595. sizeof_ca_cert_der_2048);
  12596. #elif defined(USE_CERT_BUFFERS_1024)
  12597. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12598. sizeof_ca_cert_der_1024);
  12599. #else
  12600. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12601. #endif
  12602. if (ret != 0) {
  12603. ERROR_OUT(-7839, exit_rsa);
  12604. }
  12605. /* add Key Usage */
  12606. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  12607. ERROR_OUT(-7840, exit_rsa);
  12608. }
  12609. #endif /* WOLFSSL_CERT_EXT */
  12610. #if defined(USE_CERT_BUFFERS_2048)
  12611. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12612. sizeof_ca_cert_der_2048);
  12613. #elif defined(USE_CERT_BUFFERS_1024)
  12614. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12615. sizeof_ca_cert_der_1024);
  12616. #else
  12617. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12618. #endif
  12619. if (ret < 0) {
  12620. ERROR_OUT(-7841, exit_rsa);
  12621. }
  12622. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  12623. if (certSz < 0) {
  12624. ERROR_OUT(-7842, exit_rsa);
  12625. }
  12626. ret = 0;
  12627. do {
  12628. #if defined(WOLFSSL_ASYNC_CRYPT)
  12629. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12630. #endif
  12631. if (ret >= 0) {
  12632. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  12633. caKey, NULL, rng);
  12634. }
  12635. } while (ret == WC_PENDING_E);
  12636. if (ret < 0) {
  12637. ERROR_OUT(-7843, exit_rsa);
  12638. }
  12639. certSz = ret;
  12640. #ifdef WOLFSSL_TEST_CERT
  12641. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12642. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12643. if (ret != 0) {
  12644. FreeDecodedCert(decode);
  12645. ERROR_OUT(-7844, exit_rsa);
  12646. }
  12647. FreeDecodedCert(decode);
  12648. #endif
  12649. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  12650. CERT_TYPE, -5598);
  12651. if (ret != 0) {
  12652. goto exit_rsa;
  12653. }
  12654. exit_rsa:
  12655. #ifdef WOLFSSL_SMALL_STACK
  12656. if (caKey != NULL) {
  12657. wc_FreeRsaKey(caKey);
  12658. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12659. }
  12660. #ifdef WOLFSSL_TEST_CERT
  12661. if (decode != NULL)
  12662. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12663. #endif
  12664. #else
  12665. wc_FreeRsaKey(caKey);
  12666. #endif
  12667. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12668. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12669. return ret;
  12670. }
  12671. #endif
  12672. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  12673. /* Make Cert / Sign example for ECC cert and RSA CA */
  12674. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  12675. {
  12676. #ifdef WOLFSSL_SMALL_STACK
  12677. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12678. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12679. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12680. #ifdef WOLFSSL_TEST_CERT
  12681. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12682. #endif
  12683. #else
  12684. RsaKey caKey[1];
  12685. ecc_key caEccKey[1];
  12686. ecc_key caEccKeyPub[1];
  12687. #ifdef WOLFSSL_TEST_CERT
  12688. DecodedCert decode[1];
  12689. #endif
  12690. #endif
  12691. byte* der = NULL;
  12692. Cert* myCert = NULL;
  12693. int certSz;
  12694. size_t bytes3;
  12695. word32 idx3 = 0;
  12696. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  12697. || !defined(USE_CERT_BUFFERS_256)
  12698. XFILE file3;
  12699. #endif
  12700. int ret;
  12701. #ifdef WOLFSSL_SMALL_STACK
  12702. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  12703. #ifdef WOLFSSL_TEST_CERT
  12704. || (decode == NULL)
  12705. #endif
  12706. )
  12707. ERROR_OUT(MEMORY_E, exit_rsa);
  12708. #endif
  12709. XMEMSET(caKey, 0, sizeof *caKey);
  12710. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  12711. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  12712. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12713. if (der == NULL) {
  12714. ERROR_OUT(-7850, exit_rsa);
  12715. }
  12716. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12717. if (myCert == NULL) {
  12718. ERROR_OUT(-7851, exit_rsa);
  12719. }
  12720. /* Get CA Key */
  12721. #ifdef USE_CERT_BUFFERS_1024
  12722. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12723. bytes3 = sizeof_ca_key_der_1024;
  12724. #elif defined(USE_CERT_BUFFERS_2048)
  12725. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12726. bytes3 = sizeof_ca_key_der_2048;
  12727. #else
  12728. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12729. if (!file3) {
  12730. ERROR_OUT(-7852, exit_rsa);
  12731. }
  12732. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12733. XFCLOSE(file3);
  12734. #endif /* USE_CERT_BUFFERS */
  12735. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12736. if (ret != 0) {
  12737. ERROR_OUT(-7853, exit_rsa);
  12738. }
  12739. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12740. if (ret != 0) {
  12741. ERROR_OUT(-7854, exit_rsa);
  12742. }
  12743. /* Get Cert Key */
  12744. #ifdef USE_CERT_BUFFERS_256
  12745. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  12746. bytes3 = sizeof_ecc_key_pub_der_256;
  12747. #else
  12748. file3 = XFOPEN(eccKeyPubFile, "rb");
  12749. if (!file3) {
  12750. ERROR_OUT(-7855, exit_rsa);
  12751. }
  12752. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12753. XFCLOSE(file3);
  12754. #endif
  12755. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  12756. if (ret != 0) {
  12757. ERROR_OUT(-7856, exit_rsa);
  12758. }
  12759. idx3 = 0;
  12760. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  12761. if (ret != 0) {
  12762. ERROR_OUT(-7857, exit_rsa);
  12763. }
  12764. /* Setup Certificate */
  12765. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  12766. ERROR_OUT(-7858, exit_rsa);
  12767. }
  12768. #ifndef NO_SHA256
  12769. myCert->sigType = CTC_SHA256wRSA;
  12770. #else
  12771. myCert->sigType = CTC_SHAwRSA;
  12772. #endif
  12773. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12774. #ifdef WOLFSSL_CERT_EXT
  12775. /* add Policies */
  12776. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  12777. CTC_MAX_CERTPOL_SZ);
  12778. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  12779. CTC_MAX_CERTPOL_SZ);
  12780. myCert->certPoliciesNb = 2;
  12781. /* add SKID from the Public Key */
  12782. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  12783. ERROR_OUT(-7859, exit_rsa);
  12784. }
  12785. /* add AKID from the CA certificate */
  12786. #if defined(USE_CERT_BUFFERS_2048)
  12787. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12788. sizeof_ca_cert_der_2048);
  12789. #elif defined(USE_CERT_BUFFERS_1024)
  12790. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12791. sizeof_ca_cert_der_1024);
  12792. #else
  12793. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12794. #endif
  12795. if (ret != 0) {
  12796. ERROR_OUT(-7860, exit_rsa);
  12797. }
  12798. /* add Key Usage */
  12799. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  12800. ERROR_OUT(-7861, exit_rsa);
  12801. }
  12802. #endif /* WOLFSSL_CERT_EXT */
  12803. #if defined(USE_CERT_BUFFERS_2048)
  12804. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12805. sizeof_ca_cert_der_2048);
  12806. #elif defined(USE_CERT_BUFFERS_1024)
  12807. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12808. sizeof_ca_cert_der_1024);
  12809. #else
  12810. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12811. #endif
  12812. if (ret < 0) {
  12813. ERROR_OUT(-7862, exit_rsa);
  12814. }
  12815. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  12816. if (certSz < 0) {
  12817. ERROR_OUT(-7863, exit_rsa);
  12818. }
  12819. ret = 0;
  12820. do {
  12821. #if defined(WOLFSSL_ASYNC_CRYPT)
  12822. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12823. #endif
  12824. if (ret >= 0) {
  12825. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  12826. FOURK_BUF, caKey, NULL, rng);
  12827. }
  12828. } while (ret == WC_PENDING_E);
  12829. if (ret < 0) {
  12830. ERROR_OUT(-7864, exit_rsa);
  12831. }
  12832. certSz = ret;
  12833. #ifdef WOLFSSL_TEST_CERT
  12834. InitDecodedCert(decode, der, certSz, 0);
  12835. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12836. if (ret != 0) {
  12837. FreeDecodedCert(decode);
  12838. ERROR_OUT(-7865, exit_rsa);
  12839. }
  12840. FreeDecodedCert(decode);
  12841. #endif
  12842. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  12843. CERT_TYPE, -5616);
  12844. if (ret != 0) {
  12845. goto exit_rsa;
  12846. }
  12847. exit_rsa:
  12848. #ifdef WOLFSSL_SMALL_STACK
  12849. if (caKey != NULL) {
  12850. wc_FreeRsaKey(caKey);
  12851. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12852. }
  12853. if (caEccKey != NULL) {
  12854. wc_ecc_free(caEccKey);
  12855. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12856. }
  12857. if (caEccKeyPub != NULL) {
  12858. wc_ecc_free(caEccKeyPub);
  12859. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12860. }
  12861. #ifdef WOLFSSL_TEST_CERT
  12862. if (decode != NULL)
  12863. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12864. #endif
  12865. #else
  12866. wc_FreeRsaKey(caKey);
  12867. wc_ecc_free(caEccKey);
  12868. wc_ecc_free(caEccKeyPub);
  12869. #endif
  12870. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12871. myCert = NULL;
  12872. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12873. der = NULL;
  12874. if (ret >= 0)
  12875. ret = 0;
  12876. return ret;
  12877. }
  12878. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  12879. #ifdef WOLFSSL_KEY_GEN
  12880. static int rsa_keygen_test(WC_RNG* rng)
  12881. {
  12882. #ifdef WOLFSSL_SMALL_STACK
  12883. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12884. #else
  12885. RsaKey genKey[1];
  12886. #endif
  12887. int ret;
  12888. byte* der = NULL;
  12889. #ifndef WOLFSSL_CRYPTOCELL
  12890. word32 idx = 0;
  12891. #endif
  12892. int derSz = 0;
  12893. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  12894. int keySz = 1024;
  12895. #else
  12896. int keySz = 2048;
  12897. #endif
  12898. #ifdef WOLFSSL_SMALL_STACK
  12899. if (! genKey)
  12900. ERROR_OUT(MEMORY_E, exit_rsa);
  12901. #endif
  12902. XMEMSET(genKey, 0, sizeof *genKey);
  12903. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  12904. if (ret != 0) {
  12905. ERROR_OUT(-7870, exit_rsa);
  12906. }
  12907. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  12908. #if defined(WOLFSSL_ASYNC_CRYPT)
  12909. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  12910. #endif
  12911. if (ret != 0) {
  12912. ERROR_OUT(-7871, exit_rsa);
  12913. }
  12914. TEST_SLEEP();
  12915. /* If not using old FIPS, or not using FAST or USER RSA... */
  12916. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  12917. (!defined(HAVE_FIPS) || \
  12918. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  12919. !defined(HAVE_SELFTEST) && !defined(HAVE_INTEL_QA) \
  12920. && !defined(WOLFSSL_NO_RSA_KEY_CHECK)
  12921. ret = wc_CheckRsaKey(genKey);
  12922. if (ret != 0) {
  12923. ERROR_OUT(-7872, exit_rsa);
  12924. }
  12925. #endif
  12926. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12927. if (der == NULL) {
  12928. ERROR_OUT(-7873, exit_rsa);
  12929. }
  12930. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  12931. if (derSz < 0) {
  12932. ERROR_OUT(-7874, exit_rsa);
  12933. }
  12934. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  12935. PRIVATEKEY_TYPE, -5555);
  12936. if (ret != 0) {
  12937. goto exit_rsa;
  12938. }
  12939. wc_FreeRsaKey(genKey);
  12940. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  12941. if (ret != 0) {
  12942. ERROR_OUT(-7875, exit_rsa);
  12943. }
  12944. #ifndef WOLFSSL_CRYPTOCELL
  12945. idx = 0;
  12946. /* The private key part of the key gen pairs from cryptocell can't be exported */
  12947. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  12948. if (ret != 0) {
  12949. ERROR_OUT(-7876, exit_rsa);
  12950. }
  12951. #endif /* WOLFSSL_CRYPTOCELL */
  12952. exit_rsa:
  12953. #ifdef WOLFSSL_SMALL_STACK
  12954. if (genKey) {
  12955. wc_FreeRsaKey(genKey);
  12956. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12957. }
  12958. #else
  12959. wc_FreeRsaKey(genKey);
  12960. #endif
  12961. if (der != NULL) {
  12962. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12963. der = NULL;
  12964. }
  12965. return ret;
  12966. }
  12967. #endif
  12968. #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_NTRU)
  12969. static int rsa_ntru_test(RsaKey* caKey, WC_RNG* rng, byte* tmp)
  12970. {
  12971. int ret;
  12972. Cert myCert;
  12973. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12974. XFILE caFile;
  12975. #endif
  12976. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  12977. XFILE ntruPrivFile;
  12978. #endif
  12979. int certSz;
  12980. word32 idx3 = 0;
  12981. #ifdef WOLFSSL_TEST_CERT
  12982. DecodedCert decode;
  12983. #endif
  12984. byte public_key[557]; /* sized for EES401EP2 */
  12985. word16 public_key_len; /* no. of octets in public key */
  12986. byte private_key[607]; /* sized for EES401EP2 */
  12987. word16 private_key_len; /* no. of octets in private key */
  12988. DRBG_HANDLE drbg;
  12989. static uint8_t const pers_str[] = {
  12990. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  12991. };
  12992. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  12993. sizeof(pers_str), GetEntropy, &drbg);
  12994. if (rc != DRBG_OK) {
  12995. ERROR_OUT(-7946, exit_rsa);
  12996. }
  12997. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  12998. &public_key_len, NULL,
  12999. &private_key_len, NULL);
  13000. if (rc != NTRU_OK) {
  13001. ERROR_OUT(-7947, exit_rsa);
  13002. }
  13003. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13004. &public_key_len, public_key,
  13005. &private_key_len, private_key);
  13006. if (rc != NTRU_OK) {
  13007. ERROR_OUT(-7948, exit_rsa);
  13008. }
  13009. rc = ntru_crypto_drbg_uninstantiate(drbg);
  13010. if (rc != NTRU_OK) {
  13011. ERROR_OUT(-7949, exit_rsa);
  13012. }
  13013. #ifdef USE_CERT_BUFFERS_1024
  13014. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13015. bytes = sizeof_ca_key_der_1024;
  13016. #elif defined(USE_CERT_BUFFERS_2048)
  13017. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13018. bytes = sizeof_ca_key_der_2048;
  13019. #else
  13020. caFile = XFOPEN(rsaCaKeyFile, "rb");
  13021. if (!caFile) {
  13022. ERROR_OUT(-7950, exit_rsa);
  13023. }
  13024. bytes = XFREAD(tmp, 1, FOURK_BUF, caFile);
  13025. XFCLOSE(caFile);
  13026. #endif /* USE_CERT_BUFFERS */
  13027. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13028. if (ret != 0) {
  13029. ERROR_OUT(-7951, exit_rsa);
  13030. }
  13031. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes);
  13032. if (ret != 0) {
  13033. ERROR_OUT(-7952, exit_rsa);
  13034. }
  13035. if (wc_InitCert_ex(&myCert, HEAP_HINT, devId)) {
  13036. ERROR_OUT(-7953, exit_rsa);
  13037. }
  13038. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  13039. myCert.daysValid = 1000;
  13040. #ifdef WOLFSSL_CERT_EXT
  13041. /* add SKID from the Public Key */
  13042. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  13043. public_key_len) != 0) {
  13044. ERROR_OUT(-7954, exit_rsa);
  13045. }
  13046. /* add AKID from the CA certificate */
  13047. #if defined(USE_CERT_BUFFERS_2048)
  13048. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  13049. sizeof_ca_cert_der_2048);
  13050. #elif defined(USE_CERT_BUFFERS_1024)
  13051. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  13052. sizeof_ca_cert_der_1024);
  13053. #else
  13054. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  13055. #endif
  13056. if (ret != 0) {
  13057. ERROR_OUT(-7955, exit_rsa);
  13058. }
  13059. /* add Key Usage */
  13060. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  13061. ERROR_OUT(-7956, exit_rsa);
  13062. }
  13063. #endif /* WOLFSSL_CERT_EXT */
  13064. #if defined(USE_CERT_BUFFERS_2048)
  13065. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  13066. sizeof_ca_cert_der_2048);
  13067. #elif defined(USE_CERT_BUFFERS_1024)
  13068. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  13069. sizeof_ca_cert_der_1024);
  13070. #else
  13071. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  13072. #endif
  13073. if (ret < 0) {
  13074. ERROR_OUT(-7957, exit_rsa);
  13075. }
  13076. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13077. if (der == NULL) {
  13078. ERROR_OUT(-7958, exit_rsa);
  13079. }
  13080. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  13081. public_key_len, rng);
  13082. if (certSz < 0) {
  13083. ERROR_OUT(-7959, exit_rsa);
  13084. }
  13085. ret = 0;
  13086. do {
  13087. #if defined(WOLFSSL_ASYNC_CRYPT)
  13088. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13089. #endif
  13090. if (ret >= 0) {
  13091. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  13092. caKey, NULL, rng);
  13093. }
  13094. } while (ret == WC_PENDING_E);
  13095. wc_FreeRsaKey(caKey);
  13096. if (ret < 0) {
  13097. ERROR_OUT(-7960, exit_rsa);
  13098. }
  13099. certSz = ret;
  13100. #ifdef WOLFSSL_TEST_CERT
  13101. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  13102. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  13103. if (ret != 0) {
  13104. FreeDecodedCert(&decode);
  13105. ERROR_OUT(-7961, exit_rsa);
  13106. }
  13107. FreeDecodedCert(&decode);
  13108. #endif
  13109. ret = SaveDerAndPem(der, certSz, "./ntru-cert.der", "./ntru-cert.pem",
  13110. CERT_TYPE, -5637);
  13111. if (ret != 0) {
  13112. goto exit_rsa;
  13113. }
  13114. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13115. ntruPrivFile = XFOPEN("./ntru-key.raw", "wb");
  13116. if (!ntruPrivFile) {
  13117. ERROR_OUT(-7962, exit_rsa);
  13118. }
  13119. ret = (int)XFWRITE(private_key, 1, private_key_len, ntruPrivFile);
  13120. XFCLOSE(ntruPrivFile);
  13121. if (ret != private_key_len) {
  13122. ERROR_OUT(-7963, exit_rsa);
  13123. }
  13124. #endif
  13125. exit_rsa:
  13126. if (der != NULL) {
  13127. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13128. der = NULL;
  13129. }
  13130. if (ret >= 0)
  13131. ret = 0;
  13132. else
  13133. return ret;
  13134. }
  13135. #endif
  13136. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13137. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  13138. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  13139. (!defined(HAVE_FIPS) || \
  13140. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13141. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  13142. {
  13143. int ret = 0;
  13144. word32 idx = 0;
  13145. const char inStr[] = TEST_STRING;
  13146. const word32 inLen = (word32)TEST_STRING_SZ;
  13147. const word32 outSz = RSA_TEST_BYTES;
  13148. const word32 plainSz = RSA_TEST_BYTES;
  13149. byte* res = NULL;
  13150. DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  13151. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13152. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13153. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  13154. if (in == NULL || out == NULL || plain == NULL)
  13155. ERROR_OUT(MEMORY_E, exit_rsa);
  13156. #endif
  13157. XMEMCPY(in, inStr, inLen);
  13158. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  13159. if (in == NULL || out == NULL || plain == NULL)
  13160. ERROR_OUT(MEMORY_E, exit_rsa);
  13161. #endif
  13162. #ifndef NO_SHA
  13163. do {
  13164. #if defined(WOLFSSL_ASYNC_CRYPT)
  13165. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13166. #endif
  13167. if (ret >= 0) {
  13168. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13169. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13170. }
  13171. } while (ret == WC_PENDING_E);
  13172. if (ret < 0) {
  13173. ERROR_OUT(-7918, exit_rsa);
  13174. }
  13175. TEST_SLEEP();
  13176. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13177. idx = (word32)ret;
  13178. do {
  13179. #if defined(WOLFSSL_ASYNC_CRYPT)
  13180. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13181. #endif
  13182. if (ret >= 0) {
  13183. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13184. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13185. }
  13186. } while (ret == WC_PENDING_E);
  13187. if (ret < 0) {
  13188. ERROR_OUT(-7919, exit_rsa);
  13189. }
  13190. if (XMEMCMP(plain, in, inLen)) {
  13191. ERROR_OUT(-7920, exit_rsa);
  13192. }
  13193. TEST_SLEEP();
  13194. #endif /* NO_SHA */
  13195. #endif
  13196. #ifndef NO_SHA256
  13197. XMEMSET(plain, 0, plainSz);
  13198. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13199. do {
  13200. #if defined(WOLFSSL_ASYNC_CRYPT)
  13201. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13202. #endif
  13203. if (ret >= 0) {
  13204. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13205. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13206. }
  13207. } while (ret == WC_PENDING_E);
  13208. if (ret < 0) {
  13209. ERROR_OUT(-7921, exit_rsa);
  13210. }
  13211. TEST_SLEEP();
  13212. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13213. idx = (word32)ret;
  13214. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13215. do {
  13216. #if defined(WOLFSSL_ASYNC_CRYPT)
  13217. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13218. #endif
  13219. if (ret >= 0) {
  13220. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13221. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13222. }
  13223. } while (ret == WC_PENDING_E);
  13224. if (ret < 0) {
  13225. ERROR_OUT(-7922, exit_rsa);
  13226. }
  13227. if (XMEMCMP(plain, in, inLen)) {
  13228. ERROR_OUT(-7923, exit_rsa);
  13229. }
  13230. TEST_SLEEP();
  13231. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13232. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13233. do {
  13234. #if defined(WOLFSSL_ASYNC_CRYPT)
  13235. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13236. #endif
  13237. if (ret >= 0) {
  13238. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  13239. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13240. }
  13241. } while (ret == WC_PENDING_E);
  13242. if (ret < 0) {
  13243. ERROR_OUT(-7924, exit_rsa);
  13244. }
  13245. if (ret != (int)inLen) {
  13246. ERROR_OUT(-7925, exit_rsa);
  13247. }
  13248. if (XMEMCMP(res, in, inLen)) {
  13249. ERROR_OUT(-7926, exit_rsa);
  13250. }
  13251. TEST_SLEEP();
  13252. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13253. /* check fails if not using the same optional label */
  13254. XMEMSET(plain, 0, plainSz);
  13255. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13256. do {
  13257. #if defined(WOLFSSL_ASYNC_CRYPT)
  13258. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13259. #endif
  13260. if (ret >= 0) {
  13261. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13262. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13263. }
  13264. } while (ret == WC_PENDING_E);
  13265. if (ret < 0) {
  13266. ERROR_OUT(-7927, exit_rsa);
  13267. }
  13268. TEST_SLEEP();
  13269. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13270. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13271. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13272. !defined(WOLFSSL_CRYPTOCELL)
  13273. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  13274. idx = (word32)ret;
  13275. do {
  13276. #if defined(WOLFSSL_ASYNC_CRYPT)
  13277. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13278. #endif
  13279. if (ret >= 0) {
  13280. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13281. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13282. }
  13283. } while (ret == WC_PENDING_E);
  13284. if (ret > 0) { /* in this case decrypt should fail */
  13285. ERROR_OUT(-7928, exit_rsa);
  13286. }
  13287. ret = 0;
  13288. TEST_SLEEP();
  13289. #endif /* !HAVE_CAVIUM */
  13290. /* check using optional label with encrypt/decrypt */
  13291. XMEMSET(plain, 0, plainSz);
  13292. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13293. do {
  13294. #if defined(WOLFSSL_ASYNC_CRYPT)
  13295. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13296. #endif
  13297. if (ret >= 0) {
  13298. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13299. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13300. }
  13301. } while (ret == WC_PENDING_E);
  13302. if (ret < 0) {
  13303. ERROR_OUT(-7929, exit_rsa);
  13304. }
  13305. TEST_SLEEP();
  13306. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13307. idx = (word32)ret;
  13308. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13309. do {
  13310. #if defined(WOLFSSL_ASYNC_CRYPT)
  13311. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13312. #endif
  13313. if (ret >= 0) {
  13314. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13315. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13316. }
  13317. } while (ret == WC_PENDING_E);
  13318. if (ret < 0) {
  13319. ERROR_OUT(-7930, exit_rsa);
  13320. }
  13321. if (XMEMCMP(plain, in, inLen)) {
  13322. ERROR_OUT(-7931, exit_rsa);
  13323. }
  13324. TEST_SLEEP();
  13325. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13326. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13327. #ifndef NO_SHA
  13328. /* check fail using mismatch hash algorithms */
  13329. XMEMSET(plain, 0, plainSz);
  13330. do {
  13331. #if defined(WOLFSSL_ASYNC_CRYPT)
  13332. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13333. #endif
  13334. if (ret >= 0) {
  13335. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13336. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  13337. }
  13338. } while (ret == WC_PENDING_E);
  13339. if (ret < 0) {
  13340. ERROR_OUT(-7932, exit_rsa);
  13341. }
  13342. TEST_SLEEP();
  13343. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13344. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13345. !defined(WOLFSSL_CRYPTOCELL)
  13346. idx = (word32)ret;
  13347. do {
  13348. #if defined(WOLFSSL_ASYNC_CRYPT)
  13349. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13350. #endif
  13351. if (ret >= 0) {
  13352. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13353. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  13354. in, inLen);
  13355. }
  13356. } while (ret == WC_PENDING_E);
  13357. if (ret > 0) { /* should fail */
  13358. ERROR_OUT(-7933, exit_rsa);
  13359. }
  13360. ret = 0;
  13361. TEST_SLEEP();
  13362. #endif /* !HAVE_CAVIUM */
  13363. #endif /* NO_SHA */
  13364. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13365. #endif /* NO_SHA256 */
  13366. #ifdef WOLFSSL_SHA512
  13367. /* Check valid RSA key size is used while using hash length of SHA512
  13368. If key size is less than (hash length * 2) + 2 then is invalid use
  13369. and test, since OAEP padding requires this.
  13370. BAD_FUNC_ARG is returned when this case is not met */
  13371. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  13372. XMEMSET(plain, 0, plainSz);
  13373. do {
  13374. #if defined(WOLFSSL_ASYNC_CRYPT)
  13375. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13376. #endif
  13377. if (ret >= 0) {
  13378. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13379. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13380. }
  13381. } while (ret == WC_PENDING_E);
  13382. if (ret < 0) {
  13383. ERROR_OUT(-7934, exit_rsa);
  13384. }
  13385. TEST_SLEEP();
  13386. idx = ret;
  13387. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13388. do {
  13389. #if defined(WOLFSSL_ASYNC_CRYPT)
  13390. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13391. #endif
  13392. if (ret >= 0) {
  13393. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13394. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13395. }
  13396. } while (ret == WC_PENDING_E);
  13397. if (ret < 0) {
  13398. ERROR_OUT(-7935, exit_rsa);
  13399. }
  13400. if (XMEMCMP(plain, in, inLen)) {
  13401. ERROR_OUT(-7936, exit_rsa);
  13402. }
  13403. TEST_SLEEP();
  13404. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13405. }
  13406. #endif /* WOLFSSL_SHA512 */
  13407. /* check using pkcsv15 padding with _ex API */
  13408. XMEMSET(plain, 0, plainSz);
  13409. do {
  13410. #if defined(WOLFSSL_ASYNC_CRYPT)
  13411. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13412. #endif
  13413. if (ret >= 0) {
  13414. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13415. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13416. }
  13417. } while (ret == WC_PENDING_E);
  13418. if (ret < 0) {
  13419. ERROR_OUT(-7937, exit_rsa);
  13420. }
  13421. TEST_SLEEP();
  13422. idx = (word32)ret;
  13423. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13424. do {
  13425. #if defined(WOLFSSL_ASYNC_CRYPT)
  13426. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13427. #endif
  13428. if (ret >= 0) {
  13429. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13430. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13431. }
  13432. } while (ret == WC_PENDING_E);
  13433. if (ret < 0) {
  13434. ERROR_OUT(-7938, exit_rsa);
  13435. }
  13436. if (XMEMCMP(plain, in, inLen)) {
  13437. ERROR_OUT(-7939, exit_rsa);
  13438. }
  13439. TEST_SLEEP();
  13440. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13441. exit_rsa:
  13442. FREE_VAR(in, HEAP_HINT);
  13443. FREE_VAR(out, HEAP_HINT);
  13444. FREE_VAR(plain, HEAP_HINT);
  13445. (void)idx;
  13446. (void)inStr;
  13447. (void)res;
  13448. if (ret >= 0)
  13449. ret = 0;
  13450. return ret;
  13451. }
  13452. #endif
  13453. #endif
  13454. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  13455. {
  13456. int ret;
  13457. byte* tmp = NULL;
  13458. byte* der = NULL;
  13459. size_t bytes;
  13460. WC_RNG rng;
  13461. #ifdef WOLFSSL_SMALL_STACK
  13462. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13463. #else
  13464. RsaKey key[1];
  13465. #endif
  13466. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13467. #ifdef WOLFSSL_SMALL_STACK
  13468. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13469. #else
  13470. RsaKey keypub[1];
  13471. #endif
  13472. #endif
  13473. #if defined(HAVE_NTRU)
  13474. #ifdef WOLFSSL_SMALL_STACK
  13475. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13476. #else
  13477. RsaKey caKey[1];
  13478. #endif
  13479. #endif
  13480. word32 idx = 0;
  13481. const char inStr[] = TEST_STRING;
  13482. const word32 inLen = (word32)TEST_STRING_SZ;
  13483. const word32 outSz = RSA_TEST_BYTES;
  13484. const word32 plainSz = RSA_TEST_BYTES;
  13485. byte* res = NULL;
  13486. #ifndef NO_SIG_WRAPPER
  13487. int modLen;
  13488. #endif
  13489. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13490. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13491. !defined(NO_FILESYSTEM)
  13492. XFILE file;
  13493. #ifdef WOLFSSL_TEST_CERT
  13494. XFILE file2;
  13495. #endif
  13496. #endif
  13497. #ifdef WOLFSSL_TEST_CERT
  13498. #ifdef WOLFSSL_SMALL_STACK
  13499. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13500. #else
  13501. DecodedCert cert[1];
  13502. #endif
  13503. #ifndef NO_ASN_TIME
  13504. struct tm timearg;
  13505. const byte* date;
  13506. byte dateFormat;
  13507. int dateLength;
  13508. #endif
  13509. #endif
  13510. DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  13511. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13512. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13513. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  13514. if (in == NULL || out == NULL || plain == NULL)
  13515. ERROR_OUT(MEMORY_E, exit_rsa);
  13516. #endif
  13517. XMEMCPY(in, inStr, inLen);
  13518. #ifdef WOLFSSL_SMALL_STACK
  13519. if (key == NULL)
  13520. ERROR_OUT(MEMORY_E, exit_rsa);
  13521. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13522. if (keypub == NULL)
  13523. ERROR_OUT(MEMORY_E, exit_rsa);
  13524. #endif
  13525. #if defined(HAVE_NTRU)
  13526. if (caKey == NULL)
  13527. ERROR_OUT(MEMORY_E, exit_rsa);
  13528. #endif
  13529. #ifdef WOLFSSL_TEST_CERT
  13530. if (cert == NULL)
  13531. ERROR_OUT(MEMORY_E, exit_rsa);
  13532. #endif
  13533. #endif /* WOLFSSL_SMALL_STACK */
  13534. /* initialize stack structures */
  13535. XMEMSET(&rng, 0, sizeof(rng));
  13536. XMEMSET(key, 0, sizeof *key);
  13537. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13538. XMEMSET(keypub, 0, sizeof *keypub);
  13539. #endif
  13540. #if defined(HAVE_NTRU)
  13541. XMEMSET(caKey, 0, sizeof *caKey);
  13542. #endif
  13543. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  13544. ret = rsa_decode_test(key);
  13545. if (ret != 0)
  13546. ERROR_OUT(ret, exit_rsa);
  13547. #endif
  13548. #ifdef USE_CERT_BUFFERS_1024
  13549. bytes = (size_t)sizeof_client_key_der_1024;
  13550. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13551. bytes = (size_t)sizeof_client_cert_der_1024;
  13552. #elif defined(USE_CERT_BUFFERS_2048)
  13553. bytes = (size_t)sizeof_client_key_der_2048;
  13554. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13555. bytes = (size_t)sizeof_client_cert_der_2048;
  13556. #elif defined(USE_CERT_BUFFERS_3072)
  13557. bytes = (size_t)sizeof_client_key_der_3072;
  13558. if (bytes < (size_t)sizeof_client_cert_der_3072)
  13559. bytes = (size_t)sizeof_client_cert_der_3072;
  13560. #elif defined(USE_CERT_BUFFERS_4096)
  13561. bytes = (size_t)sizeof_client_key_der_4096;
  13562. if (bytes < (size_t)sizeof_client_cert_der_4096)
  13563. bytes = (size_t)sizeof_client_cert_der_4096;
  13564. #else
  13565. bytes = FOURK_BUF;
  13566. #endif
  13567. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13568. if (tmp == NULL)
  13569. ERROR_OUT(-7900, exit_rsa);
  13570. #ifdef USE_CERT_BUFFERS_1024
  13571. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13572. #elif defined(USE_CERT_BUFFERS_2048)
  13573. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13574. #elif defined(USE_CERT_BUFFERS_3072)
  13575. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13576. #elif defined(USE_CERT_BUFFERS_4096)
  13577. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13578. #elif !defined(NO_FILESYSTEM)
  13579. file = XFOPEN(clientKey, "rb");
  13580. if (!file) {
  13581. err_sys("can't open ./certs/client-key.der, "
  13582. "Please run from wolfSSL home dir", -40);
  13583. ERROR_OUT(-7901, exit_rsa);
  13584. }
  13585. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13586. XFCLOSE(file);
  13587. #else
  13588. /* No key to use. */
  13589. ERROR_OUT(-7902, exit_rsa);
  13590. #endif /* USE_CERT_BUFFERS */
  13591. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13592. if (ret != 0) {
  13593. ERROR_OUT(-7903, exit_rsa);
  13594. }
  13595. #ifndef NO_ASN
  13596. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13597. if (ret != 0) {
  13598. ERROR_OUT(-7904, exit_rsa);
  13599. }
  13600. #ifndef NO_SIG_WRAPPER
  13601. modLen = wc_RsaEncryptSize(key);
  13602. #endif
  13603. #else
  13604. #ifdef USE_CERT_BUFFERS_2048
  13605. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13606. if (ret != 0) {
  13607. ERROR_OUT(-7905, exit_rsa);
  13608. }
  13609. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13610. if (ret != 0) {
  13611. ERROR_OUT(-7906, exit_rsa);
  13612. }
  13613. #ifndef NO_SIG_WRAPPER
  13614. modLen = 2048;
  13615. #endif
  13616. #else
  13617. #error Not supported yet!
  13618. #endif
  13619. #endif
  13620. #ifndef WC_NO_RNG
  13621. #ifndef HAVE_FIPS
  13622. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13623. #else
  13624. ret = wc_InitRng(&rng);
  13625. #endif
  13626. if (ret != 0) {
  13627. ERROR_OUT(-7907, exit_rsa);
  13628. }
  13629. #endif
  13630. #ifndef NO_SIG_WRAPPER
  13631. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  13632. if (ret != 0)
  13633. goto exit_rsa;
  13634. #endif
  13635. #ifdef WC_RSA_NONBLOCK
  13636. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  13637. if (ret != 0)
  13638. goto exit_rsa;
  13639. #endif
  13640. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13641. !defined(WC_NO_RNG)
  13642. do {
  13643. #if defined(WOLFSSL_ASYNC_CRYPT)
  13644. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13645. #endif
  13646. if (ret >= 0) {
  13647. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  13648. }
  13649. } while (ret == WC_PENDING_E);
  13650. if (ret < 0) {
  13651. ERROR_OUT(-7908, exit_rsa);
  13652. }
  13653. TEST_SLEEP();
  13654. #ifdef WC_RSA_BLINDING
  13655. {
  13656. int tmpret = ret;
  13657. ret = wc_RsaSetRNG(key, &rng);
  13658. if (ret < 0) {
  13659. ERROR_OUT(-7909, exit_rsa);
  13660. }
  13661. ret = tmpret;
  13662. }
  13663. #endif
  13664. idx = (word32)ret; /* save off encrypted length */
  13665. do {
  13666. #if defined(WOLFSSL_ASYNC_CRYPT)
  13667. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13668. #endif
  13669. if (ret >= 0) {
  13670. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  13671. }
  13672. } while (ret == WC_PENDING_E);
  13673. if (ret < 0) {
  13674. ERROR_OUT(-7910, exit_rsa);
  13675. }
  13676. if (XMEMCMP(plain, in, inLen)) {
  13677. ERROR_OUT(-7911, exit_rsa);
  13678. }
  13679. TEST_SLEEP();
  13680. do {
  13681. #if defined(WOLFSSL_ASYNC_CRYPT)
  13682. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13683. #endif
  13684. if (ret >= 0) {
  13685. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  13686. }
  13687. } while (ret == WC_PENDING_E);
  13688. if (ret < 0) {
  13689. ERROR_OUT(-7912, exit_rsa);
  13690. }
  13691. if (ret != (int)inLen) {
  13692. ERROR_OUT(-7913, exit_rsa);
  13693. }
  13694. if (XMEMCMP(res, in, inLen)) {
  13695. ERROR_OUT(-7914, exit_rsa);
  13696. }
  13697. TEST_SLEEP();
  13698. do {
  13699. #if defined(WOLFSSL_ASYNC_CRYPT)
  13700. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13701. #endif
  13702. if (ret >= 0) {
  13703. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  13704. }
  13705. } while (ret == WC_PENDING_E);
  13706. if (ret < 0) {
  13707. ERROR_OUT(-7915, exit_rsa);
  13708. }
  13709. TEST_SLEEP();
  13710. #elif defined(WOLFSSL_PUBLIC_MP)
  13711. {
  13712. static byte signature_2048[] = {
  13713. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  13714. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  13715. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  13716. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  13717. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  13718. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  13719. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  13720. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  13721. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  13722. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  13723. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  13724. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  13725. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  13726. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  13727. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  13728. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  13729. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  13730. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  13731. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  13732. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  13733. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  13734. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  13735. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  13736. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  13737. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  13738. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  13739. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  13740. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  13741. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  13742. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  13743. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  13744. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  13745. };
  13746. ret = sizeof(signature_2048);
  13747. XMEMCPY(out, signature_2048, ret);
  13748. }
  13749. #endif
  13750. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  13751. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  13752. defined(WOLFSSL_PUBLIC_MP))
  13753. idx = (word32)ret;
  13754. XMEMSET(plain, 0, plainSz);
  13755. do {
  13756. #if defined(WOLFSSL_ASYNC_CRYPT)
  13757. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13758. #endif
  13759. if (ret >= 0) {
  13760. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  13761. #if defined(WOLFSSL_CRYPTOCELL)
  13762. /*
  13763. Cryptocell requires the input data and signature byte array to verify.
  13764. first argument must be the input data
  13765. second argument must be the length of input data
  13766. third argument must be the signature byte array or the output from
  13767. wc_RsaSSL_Sign()
  13768. fourth argument must be the length of the signature byte array
  13769. */
  13770. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  13771. #else
  13772. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  13773. #endif /* WOLFSSL_CRYPTOCELL */
  13774. #else
  13775. byte* dec = NULL;
  13776. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  13777. if (ret > 0) {
  13778. XMEMCPY(plain, dec, ret);
  13779. }
  13780. #endif
  13781. }
  13782. } while (ret == WC_PENDING_E);
  13783. if (ret < 0) {
  13784. ERROR_OUT(-7916, exit_rsa);
  13785. }
  13786. if (XMEMCMP(plain, in, (size_t)ret)) {
  13787. ERROR_OUT(-7917, exit_rsa);
  13788. }
  13789. TEST_SLEEP();
  13790. #endif
  13791. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13792. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  13793. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  13794. (!defined(HAVE_FIPS) || \
  13795. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13796. ret = rsa_oaep_padding_test(key, &rng);
  13797. if (ret != 0)
  13798. return ret;
  13799. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  13800. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  13801. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13802. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  13803. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13804. ret = rsa_export_key_test(key);
  13805. if (ret != 0)
  13806. return ret;
  13807. #endif
  13808. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13809. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13810. ret = rsa_flatten_test(key);
  13811. if (ret != 0)
  13812. return ret;
  13813. #endif
  13814. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  13815. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13816. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  13817. (void)clientCert;
  13818. #endif
  13819. #ifdef WOLFSSL_TEST_CERT
  13820. #if defined(WOLFSSL_MDK_ARM)
  13821. #define sizeof(s) XSTRLEN((char *)(s))
  13822. #endif
  13823. #ifdef USE_CERT_BUFFERS_1024
  13824. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  13825. bytes = (size_t)sizeof_client_cert_der_1024;
  13826. #elif defined(USE_CERT_BUFFERS_2048)
  13827. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  13828. bytes = (size_t)sizeof_client_cert_der_2048;
  13829. #elif defined(USE_CERT_BUFFERS_3072)
  13830. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  13831. bytes = (size_t)sizeof_client_cert_der_3072;
  13832. #elif defined(USE_CERT_BUFFERS_4096)
  13833. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  13834. bytes = (size_t)sizeof_client_cert_der_4096;
  13835. #elif !defined(NO_FILESYSTEM)
  13836. file2 = XFOPEN(clientCert, "rb");
  13837. if (!file2) {
  13838. ERROR_OUT(-7940, exit_rsa);
  13839. }
  13840. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  13841. XFCLOSE(file2);
  13842. #else
  13843. /* No certificate to use. */
  13844. ERROR_OUT(-7941, exit_rsa);
  13845. #endif
  13846. #ifdef sizeof
  13847. #undef sizeof
  13848. #endif
  13849. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  13850. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  13851. if (ret != 0) {
  13852. FreeDecodedCert(cert);
  13853. ERROR_OUT(-7942, exit_rsa);
  13854. }
  13855. #ifndef NO_ASN_TIME
  13856. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  13857. &dateFormat, &dateLength);
  13858. if (ret != 0) {
  13859. FreeDecodedCert(cert);
  13860. ERROR_OUT(-7943, exit_rsa);
  13861. }
  13862. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  13863. if (ret != 0) {
  13864. FreeDecodedCert(cert);
  13865. ERROR_OUT(-7944, exit_rsa);
  13866. }
  13867. #endif
  13868. FreeDecodedCert(cert);
  13869. #endif /* WOLFSSL_TEST_CERT */
  13870. #ifdef WOLFSSL_CERT_EXT
  13871. #ifdef USE_CERT_BUFFERS_1024
  13872. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  13873. bytes = sizeof_client_keypub_der_1024;
  13874. #elif defined(USE_CERT_BUFFERS_2048)
  13875. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  13876. bytes = sizeof_client_keypub_der_2048;
  13877. #elif defined(USE_CERT_BUFFERS_3072)
  13878. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  13879. bytes = sizeof_client_keypub_der_3072;
  13880. #elif defined(USE_CERT_BUFFERS_4096)
  13881. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  13882. bytes = sizeof_client_keypub_der_4096;
  13883. #else
  13884. file = XFOPEN(clientKeyPub, "rb");
  13885. if (!file) {
  13886. err_sys("can't open ./certs/client-keyPub.der, "
  13887. "Please run from wolfSSL home dir", -40);
  13888. ERROR_OUT(-7945, exit_rsa);
  13889. }
  13890. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13891. XFCLOSE(file);
  13892. #endif /* USE_CERT_BUFFERS */
  13893. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  13894. if (ret != 0) {
  13895. ERROR_OUT(-7946, exit_rsa);
  13896. }
  13897. idx = 0;
  13898. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  13899. if (ret != 0) {
  13900. ERROR_OUT(-7947, exit_rsa);
  13901. }
  13902. #endif /* WOLFSSL_CERT_EXT */
  13903. #ifdef WOLFSSL_KEY_GEN
  13904. ret = rsa_keygen_test(&rng);
  13905. if (ret != 0)
  13906. goto exit_rsa;
  13907. #endif
  13908. #ifdef WOLFSSL_CERT_GEN
  13909. /* Make Cert / Sign example for RSA cert and RSA CA */
  13910. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  13911. if (ret != 0)
  13912. goto exit_rsa;
  13913. #if !defined(NO_RSA) && defined(HAVE_ECC)
  13914. ret = rsa_ecc_certgen_test(&rng, tmp);
  13915. if (ret != 0)
  13916. goto exit_rsa;
  13917. #endif
  13918. #ifdef HAVE_NTRU
  13919. ret = rsa_ntru_test(caKey, &rng, tmp);
  13920. if (ret != 0)
  13921. goto exit_rsa;
  13922. #endif /* HAVE_NTRU */
  13923. #ifdef WOLFSSL_CERT_REQ
  13924. {
  13925. Cert *req;
  13926. int derSz;
  13927. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13928. if (! req)
  13929. ERROR_OUT(MEMORY_E, exit_rsa);
  13930. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  13931. if (der == NULL) {
  13932. ERROR_OUT(-7964, exit_rsa);
  13933. }
  13934. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  13935. ERROR_OUT(-7965, exit_rsa);
  13936. }
  13937. req->version = 0;
  13938. req->isCA = 1;
  13939. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  13940. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  13941. #ifndef NO_SHA256
  13942. req->sigType = CTC_SHA256wRSA;
  13943. #else
  13944. req->sigType = CTC_SHAwRSA;
  13945. #endif
  13946. #ifdef WOLFSSL_CERT_EXT
  13947. /* add SKID from the Public Key */
  13948. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  13949. ERROR_OUT(-7966, exit_rsa);
  13950. }
  13951. /* add Key Usage */
  13952. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  13953. ERROR_OUT(-7967, exit_rsa);
  13954. }
  13955. /* add Extended Key Usage */
  13956. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  13957. "emailProtection,timeStamping,OCSPSigning") != 0) {
  13958. ERROR_OUT(-7968, exit_rsa);
  13959. }
  13960. #ifdef WOLFSSL_EKU_OID
  13961. {
  13962. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13963. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  13964. HEAP_HINT) != 0) {
  13965. ERROR_OUT(-7969, exit_rsa);
  13966. }
  13967. }
  13968. #endif /* WOLFSSL_EKU_OID */
  13969. #endif /* WOLFSSL_CERT_EXT */
  13970. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13971. if (derSz < 0) {
  13972. ERROR_OUT(-7970, exit_rsa);
  13973. }
  13974. #ifdef WOLFSSL_CERT_EXT
  13975. /* Try again with "any" flag set, will override all others */
  13976. if (wc_SetExtKeyUsage(req, "any") != 0) {
  13977. ERROR_OUT(-7971, exit_rsa);
  13978. }
  13979. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13980. if (derSz < 0) {
  13981. ERROR_OUT(-7972, exit_rsa);
  13982. }
  13983. #endif /* WOLFSSL_CERT_EXT */
  13984. ret = 0;
  13985. do {
  13986. #if defined(WOLFSSL_ASYNC_CRYPT)
  13987. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13988. #endif
  13989. if (ret >= 0) {
  13990. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  13991. key, NULL, &rng);
  13992. }
  13993. } while (ret == WC_PENDING_E);
  13994. if (ret < 0) {
  13995. ERROR_OUT(-7973, exit_rsa);
  13996. }
  13997. derSz = ret;
  13998. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  13999. CERTREQ_TYPE, -5650);
  14000. if (ret != 0) {
  14001. goto exit_rsa;
  14002. }
  14003. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14004. if (derSz < 0) {
  14005. ERROR_OUT(-7974, exit_rsa);
  14006. }
  14007. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14008. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14009. der = NULL;
  14010. }
  14011. #endif /* WOLFSSL_CERT_REQ */
  14012. #endif /* WOLFSSL_CERT_GEN */
  14013. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14014. /* Need to create known good signatures to test with this. */
  14015. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  14016. ret = rsa_pss_test(&rng, key);
  14017. if (ret != 0)
  14018. goto exit_rsa;
  14019. #endif
  14020. #endif
  14021. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14022. #ifdef WOLFSSL_SMALL_STACK
  14023. /* New key to be loaded in rsa_even_mod_test(). */
  14024. if (key != NULL)
  14025. #endif
  14026. wc_FreeRsaKey(key);
  14027. /* New key to be loaded in rsa_even_mod_test(). */
  14028. ret = rsa_even_mod_test(&rng, key);
  14029. #endif
  14030. exit_rsa:
  14031. #ifdef WOLFSSL_SMALL_STACK
  14032. if (key != NULL) {
  14033. wc_FreeRsaKey(key);
  14034. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14035. }
  14036. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14037. if (keypub != NULL) {
  14038. wc_FreeRsaKey(keypub);
  14039. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14040. }
  14041. #endif
  14042. #if defined(HAVE_NTRU)
  14043. if (caKey != NULL) {
  14044. wc_FreeRsaKey(caKey);
  14045. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14046. }
  14047. #endif
  14048. #ifdef WOLFSSL_TEST_CERT
  14049. if (cert != NULL)
  14050. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14051. #endif
  14052. #else
  14053. wc_FreeRsaKey(key);
  14054. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14055. wc_FreeRsaKey(keypub);
  14056. #endif
  14057. #if defined(HAVE_NTRU)
  14058. wc_FreeRsaKey(caKey);
  14059. #endif
  14060. #endif /* WOLFSSL_SMALL_STACK */
  14061. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14062. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14063. wc_FreeRng(&rng);
  14064. FREE_VAR(in, HEAP_HINT);
  14065. FREE_VAR(out, HEAP_HINT);
  14066. FREE_VAR(plain, HEAP_HINT);
  14067. (void)res;
  14068. (void)bytes;
  14069. (void)idx;
  14070. (void)in;
  14071. (void)out;
  14072. (void)plain;
  14073. (void)idx;
  14074. (void)inStr;
  14075. (void)inLen;
  14076. (void)outSz;
  14077. (void)plainSz;
  14078. /* ret can be greater then 0 with certgen but all negative values should
  14079. * be returned and treated as an error */
  14080. if (ret >= 0) {
  14081. return 0;
  14082. }
  14083. else {
  14084. return ret;
  14085. }
  14086. }
  14087. #endif /* !NO_RSA */
  14088. #ifndef NO_DH
  14089. static int dh_fips_generate_test(WC_RNG *rng)
  14090. {
  14091. int ret = 0;
  14092. #ifdef WOLFSSL_SMALL_STACK
  14093. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  14094. #else
  14095. DhKey key[1];
  14096. #endif
  14097. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  14098. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  14099. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  14100. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  14101. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  14102. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  14103. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  14104. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  14105. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  14106. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  14107. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  14108. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  14109. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  14110. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  14111. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  14112. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  14113. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  14114. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  14115. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  14116. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  14117. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  14118. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  14119. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  14120. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  14121. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  14122. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  14123. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  14124. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  14125. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  14126. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  14127. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  14128. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  14129. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  14130. };
  14131. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  14132. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  14133. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  14134. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  14135. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  14136. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  14137. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  14138. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  14139. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  14140. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  14141. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  14142. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  14143. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  14144. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  14145. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  14146. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  14147. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  14148. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  14149. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  14150. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  14151. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  14152. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  14153. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  14154. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  14155. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  14156. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  14157. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  14158. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  14159. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  14160. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  14161. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  14162. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  14163. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  14164. };
  14165. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  14166. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  14167. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  14168. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  14169. 0x40, 0x52, 0xed, 0x41
  14170. };
  14171. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  14172. 0x00,
  14173. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  14174. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  14175. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  14176. 0x40, 0x52, 0xed, 0x41
  14177. };
  14178. byte priv[256];
  14179. byte pub[256];
  14180. word32 privSz = sizeof(priv);
  14181. word32 pubSz = sizeof(pub);
  14182. #ifdef WOLFSSL_SMALL_STACK
  14183. if (key == NULL)
  14184. ERROR_OUT(MEMORY_E, exit_gen_test);
  14185. #endif
  14186. /* Parameter Validation testing. */
  14187. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  14188. if (ret != BAD_FUNC_ARG)
  14189. ERROR_OUT(-7980, exit_gen_test);
  14190. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  14191. if (ret != BAD_FUNC_ARG)
  14192. ERROR_OUT(-7981, exit_gen_test);
  14193. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  14194. if (ret != BAD_FUNC_ARG)
  14195. ERROR_OUT(-7982, exit_gen_test);
  14196. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  14197. if (ret != BAD_FUNC_ARG)
  14198. ERROR_OUT(-7983, exit_gen_test);
  14199. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  14200. if (ret != BAD_FUNC_ARG)
  14201. ERROR_OUT(-7984, exit_gen_test);
  14202. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  14203. if (ret != BAD_FUNC_ARG)
  14204. ERROR_OUT(-7985, exit_gen_test);
  14205. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14206. if (ret != 0)
  14207. ERROR_OUT(-7986, exit_gen_test);
  14208. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  14209. if (ret != 0) {
  14210. ERROR_OUT(-7987, exit_gen_test);
  14211. }
  14212. wc_FreeDhKey(key);
  14213. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14214. if (ret != 0)
  14215. ERROR_OUT(-7988, exit_gen_test);
  14216. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  14217. if (ret != 0) {
  14218. ERROR_OUT(-7989, exit_gen_test);
  14219. }
  14220. /* Use API. */
  14221. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14222. #if defined(WOLFSSL_ASYNC_CRYPT)
  14223. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14224. #endif
  14225. if (ret != 0) {
  14226. ERROR_OUT(-7990, exit_gen_test);
  14227. }
  14228. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  14229. if (ret != 0) {
  14230. ERROR_OUT(-7991, exit_gen_test);
  14231. }
  14232. wc_FreeDhKey(key);
  14233. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14234. if (ret != 0)
  14235. ERROR_OUT(-7992, exit_gen_test);
  14236. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  14237. if (ret != 0) {
  14238. ERROR_OUT(-7993, exit_gen_test);
  14239. }
  14240. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  14241. if (ret != 0) {
  14242. ERROR_OUT(-7994, exit_gen_test);
  14243. }
  14244. #ifndef HAVE_SELFTEST
  14245. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14246. if (ret != 0) {
  14247. ERROR_OUT(-7995, exit_gen_test);
  14248. }
  14249. /* Taint the public key so the check fails. */
  14250. pub[0]++;
  14251. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14252. if (ret != MP_CMP_E) {
  14253. ERROR_OUT(-7996, exit_gen_test);
  14254. }
  14255. #ifdef WOLFSSL_KEY_GEN
  14256. wc_FreeDhKey(key);
  14257. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14258. if (ret != 0)
  14259. ERROR_OUT(-7997, exit_gen_test);
  14260. ret = wc_DhGenerateParams(rng, 2048, key);
  14261. if (ret != 0) {
  14262. ERROR_OUT(-7998, exit_gen_test);
  14263. }
  14264. privSz = sizeof(priv);
  14265. pubSz = sizeof(pub);
  14266. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14267. #if defined(WOLFSSL_ASYNC_CRYPT)
  14268. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14269. #endif
  14270. if (ret != 0) {
  14271. ERROR_OUT(-7999, exit_gen_test);
  14272. }
  14273. #endif /* WOLFSSL_KEY_GEN */
  14274. #endif /* HAVE_SELFTEST */
  14275. ret = 0;
  14276. exit_gen_test:
  14277. #ifdef WOLFSSL_SMALL_STACK
  14278. if (key) {
  14279. wc_FreeDhKey(key);
  14280. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14281. }
  14282. #else
  14283. wc_FreeDhKey(key);
  14284. #endif
  14285. return ret;
  14286. }
  14287. static int dh_generate_test(WC_RNG *rng)
  14288. {
  14289. int ret = 0;
  14290. #ifdef WOLFSSL_SMALL_STACK
  14291. DhKey *smallKey = (DhKey*)XMALLOC(sizeof(DhKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14292. #else
  14293. DhKey smallKey[1];
  14294. #endif
  14295. byte p[2] = { 0, 5 };
  14296. byte g[2] = { 0, 2 };
  14297. #if !defined(WOLFSSL_SP_MATH)
  14298. #ifdef WOLFSSL_DH_CONST
  14299. /* the table for constant DH lookup will round to the lowest byte size 21 */
  14300. byte priv[21];
  14301. byte pub[21];
  14302. #else
  14303. byte priv[2];
  14304. byte pub[2];
  14305. #endif
  14306. word32 privSz = sizeof(priv);
  14307. word32 pubSz = sizeof(pub);
  14308. #endif
  14309. #ifdef WOLFSSL_SMALL_STACK
  14310. if (smallKey == NULL) {
  14311. ERROR_OUT(-8010, exit_gen_test);
  14312. }
  14313. #endif
  14314. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  14315. if (ret != 0)
  14316. return -8010;
  14317. /* Parameter Validation testing. */
  14318. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  14319. if (ret != BAD_FUNC_ARG)
  14320. return -8011;
  14321. wc_FreeDhKey(NULL);
  14322. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  14323. if (ret != BAD_FUNC_ARG) {
  14324. ERROR_OUT(-8012, exit_gen_test);
  14325. }
  14326. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  14327. if (ret != BAD_FUNC_ARG) {
  14328. ERROR_OUT(-8013, exit_gen_test);
  14329. }
  14330. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  14331. if (ret != BAD_FUNC_ARG) {
  14332. ERROR_OUT(-8014, exit_gen_test);
  14333. }
  14334. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  14335. if (ret != BAD_FUNC_ARG) {
  14336. ERROR_OUT(-8015, exit_gen_test);
  14337. }
  14338. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  14339. if (ret != BAD_FUNC_ARG) {
  14340. ERROR_OUT(-8016, exit_gen_test);
  14341. }
  14342. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  14343. if (ret != 0) {
  14344. ERROR_OUT(-8017, exit_gen_test);
  14345. }
  14346. #if !defined(WOLFSSL_SP_MATH)
  14347. /* Use API. */
  14348. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  14349. #if defined(WOLFSSL_ASYNC_CRYPT)
  14350. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  14351. #endif
  14352. if (ret != 0) {
  14353. ret = -8018;
  14354. }
  14355. #else
  14356. (void)rng;
  14357. ret = 0;
  14358. #endif
  14359. exit_gen_test:
  14360. wc_FreeDhKey(smallKey);
  14361. #ifdef WOLFSSL_SMALL_STACK
  14362. if (smallKey != NULL) {
  14363. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14364. }
  14365. #endif
  14366. return ret;
  14367. }
  14368. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14369. typedef struct dh_pubvalue_test {
  14370. const byte* data;
  14371. word32 len;
  14372. } dh_pubvalue_test;
  14373. static int dh_test_check_pubvalue(void)
  14374. {
  14375. int ret;
  14376. word32 i;
  14377. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  14378. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  14379. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  14380. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  14381. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  14382. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  14383. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  14384. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  14385. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  14386. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  14387. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  14388. const dh_pubvalue_test dh_pubval_fail[] = {
  14389. { prime, sizeof(prime) },
  14390. { pubValZero, sizeof(pubValZero) },
  14391. { pubValZeroLong, sizeof(pubValZeroLong) },
  14392. { pubValOne, sizeof(pubValOne) },
  14393. { pubValOneLong, sizeof(pubValOneLong) },
  14394. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  14395. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  14396. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  14397. { pubValTooBig0, sizeof(pubValTooBig0) },
  14398. { pubValTooBig1, sizeof(pubValTooBig1) },
  14399. { pubValTooLong, sizeof(pubValTooLong) },
  14400. };
  14401. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  14402. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  14403. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  14404. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  14405. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  14406. const dh_pubvalue_test dh_pubval_pass[] = {
  14407. { pubValTwo, sizeof(pubValTwo) },
  14408. { pubValTwoLong, sizeof(pubValTwoLong) },
  14409. { pubValGood, sizeof(pubValGood) },
  14410. { pubValGoodLen, sizeof(pubValGoodLen) },
  14411. { pubValGoodLong, sizeof(pubValGoodLong) },
  14412. };
  14413. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  14414. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  14415. dh_pubval_fail[i].len);
  14416. if (ret != MP_VAL)
  14417. return -8020 - (int)i;
  14418. }
  14419. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  14420. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  14421. dh_pubval_pass[i].len);
  14422. if (ret != 0)
  14423. return -8030 - (int)i;
  14424. }
  14425. return 0;
  14426. }
  14427. #endif
  14428. #if defined(HAVE_FFDHE)
  14429. #if defined(HAVE_FFDHE_4096)
  14430. #define MAX_DH_PRIV_SZ 39
  14431. #define MAX_DH_KEY_SZ 512
  14432. #elif defined(HAVE_FFDHE_3072)
  14433. #define MAX_DH_PRIV_SZ 34
  14434. #define MAX_DH_KEY_SZ 384
  14435. #else
  14436. #define MAX_DH_PRIV_SZ 29
  14437. #define MAX_DH_KEY_SZ 256
  14438. #endif
  14439. #ifndef WC_NO_RNG
  14440. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  14441. {
  14442. int ret;
  14443. word32 privSz, pubSz, privSz2, pubSz2;
  14444. #ifdef WOLFSSL_SMALL_STACK
  14445. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14446. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14447. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14448. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14449. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14450. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14451. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14452. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14453. #else
  14454. byte priv[MAX_DH_PRIV_SZ];
  14455. byte pub[MAX_DH_KEY_SZ];
  14456. byte priv2[MAX_DH_PRIV_SZ];
  14457. byte pub2[MAX_DH_KEY_SZ];
  14458. byte agree[MAX_DH_KEY_SZ];
  14459. byte agree2[MAX_DH_KEY_SZ];
  14460. DhKey key[1];
  14461. DhKey key2[1];
  14462. #endif
  14463. word32 agreeSz = MAX_DH_KEY_SZ;
  14464. word32 agreeSz2 = MAX_DH_KEY_SZ;
  14465. #ifdef WOLFSSL_SMALL_STACK
  14466. if ((priv == NULL) ||
  14467. (pub == NULL) ||
  14468. (priv2 == NULL) ||
  14469. (pub2 == NULL) ||
  14470. (agree == NULL) ||
  14471. (agree2 == NULL) ||
  14472. (key == NULL) ||
  14473. (key2 == NULL))
  14474. ERROR_OUT(-8050, done);
  14475. #endif
  14476. pubSz = MAX_DH_KEY_SZ;
  14477. pubSz2 = MAX_DH_KEY_SZ;
  14478. privSz = MAX_DH_PRIV_SZ;
  14479. privSz2 = MAX_DH_PRIV_SZ;
  14480. XMEMSET(key, 0, sizeof(*key));
  14481. XMEMSET(key2, 0, sizeof(*key2));
  14482. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14483. if (ret != 0) {
  14484. ERROR_OUT(-8051, done);
  14485. }
  14486. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14487. if (ret != 0) {
  14488. ERROR_OUT(-8052, done);
  14489. }
  14490. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  14491. if (ret != 0) {
  14492. ERROR_OUT(-8053, done);
  14493. }
  14494. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  14495. params->g_len);
  14496. if (ret != 0) {
  14497. ERROR_OUT(-8054, done);
  14498. }
  14499. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14500. #if defined(WOLFSSL_ASYNC_CRYPT)
  14501. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14502. #endif
  14503. if (ret != 0) {
  14504. ERROR_OUT(-8055, done);
  14505. }
  14506. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  14507. #if defined(WOLFSSL_ASYNC_CRYPT)
  14508. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14509. #endif
  14510. if (ret != 0) {
  14511. ERROR_OUT(-8056, done);
  14512. }
  14513. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14514. #if defined(WOLFSSL_ASYNC_CRYPT)
  14515. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14516. #endif
  14517. if (ret != 0) {
  14518. ERROR_OUT(-8057, done);
  14519. }
  14520. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14521. #if defined(WOLFSSL_ASYNC_CRYPT)
  14522. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14523. #endif
  14524. if (ret != 0) {
  14525. ERROR_OUT(-8058, done);
  14526. }
  14527. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14528. ERROR_OUT(-8059, done);
  14529. }
  14530. #if defined(WOLFSSL_HAVE_SP_DH) && defined(USE_FAST_MATH)
  14531. /* Make p even */
  14532. key->p.dp[0] &= (mp_digit)-2;
  14533. if (ret != 0) {
  14534. ERROR_OUT(-8058, done);
  14535. }
  14536. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14537. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14538. ERROR_OUT(-8058, done);
  14539. }
  14540. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14541. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14542. ERROR_OUT(-8057, done);
  14543. }
  14544. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14545. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14546. ERROR_OUT(-8057, done);
  14547. }
  14548. /* Getting here means success - set ret to 0. */
  14549. ret = 0;
  14550. #endif
  14551. done:
  14552. #ifdef WOLFSSL_SMALL_STACK
  14553. if (priv)
  14554. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14555. if (pub)
  14556. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14557. if (priv2)
  14558. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14559. if (pub2)
  14560. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14561. if (agree)
  14562. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14563. if (agree2)
  14564. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14565. if (key) {
  14566. wc_FreeDhKey(key);
  14567. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14568. }
  14569. if (key2) {
  14570. wc_FreeDhKey(key2);
  14571. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14572. }
  14573. #else
  14574. wc_FreeDhKey(key);
  14575. wc_FreeDhKey(key2);
  14576. #endif
  14577. return ret;
  14578. }
  14579. #endif /* !WC_NO_RNG */
  14580. #endif /* HAVE_FFDHE */
  14581. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  14582. {
  14583. int ret;
  14584. word32 bytes;
  14585. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  14586. #ifndef WC_NO_RNG
  14587. WC_RNG rng;
  14588. #endif
  14589. int keyInit = 0;
  14590. #define DH_TEST_TMP_SIZE 1024
  14591. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14592. #define DH_TEST_BUF_SIZE 256
  14593. #else
  14594. #define DH_TEST_BUF_SIZE 512
  14595. #endif
  14596. #ifndef WC_NO_RNG
  14597. word32 agreeSz = DH_TEST_BUF_SIZE;
  14598. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  14599. #endif
  14600. #ifdef WOLFSSL_SMALL_STACK
  14601. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14602. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14603. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14604. #else
  14605. DhKey key[1];
  14606. DhKey key2[1];
  14607. byte tmp[DH_TEST_TMP_SIZE];
  14608. #endif
  14609. #ifndef WC_NO_RNG
  14610. #ifdef WOLFSSL_SMALL_STACK
  14611. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14612. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14613. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14614. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14615. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14616. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14617. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  14618. agree == NULL || agree2 == NULL) {
  14619. ERROR_OUT(-8100, done);
  14620. }
  14621. #else
  14622. byte priv[DH_TEST_BUF_SIZE];
  14623. byte pub[DH_TEST_BUF_SIZE];
  14624. byte priv2[DH_TEST_BUF_SIZE];
  14625. byte pub2[DH_TEST_BUF_SIZE];
  14626. byte agree[DH_TEST_BUF_SIZE];
  14627. byte agree2[DH_TEST_BUF_SIZE];
  14628. #endif
  14629. #endif /* !WC_NO_RNG */
  14630. #ifdef WOLFSSL_SMALL_STACK
  14631. if (key == NULL || key2 == NULL || tmp == NULL) {
  14632. ERROR_OUT(-8100, done);
  14633. }
  14634. #endif
  14635. #ifdef USE_CERT_BUFFERS_1024
  14636. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  14637. bytes = (size_t)sizeof_dh_key_der_1024;
  14638. #elif defined(USE_CERT_BUFFERS_2048)
  14639. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  14640. bytes = (size_t)sizeof_dh_key_der_2048;
  14641. #elif defined(USE_CERT_BUFFERS_3072)
  14642. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  14643. bytes = (size_t)sizeof_dh_key_der_3072;
  14644. #elif defined(USE_CERT_BUFFERS_4096)
  14645. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  14646. bytes = (size_t)sizeof_dh_key_der_4096;
  14647. #elif defined(NO_ASN)
  14648. /* don't use file, no DER parsing */
  14649. #elif !defined(NO_FILESYSTEM)
  14650. {
  14651. XFILE file = XFOPEN(dhParamsFile, "rb");
  14652. if (! file)
  14653. ERROR_OUT(-8101, done);
  14654. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14655. XFCLOSE(file);
  14656. }
  14657. #else
  14658. /* No DH key to use. */
  14659. ERROR_OUT(-8102, done);
  14660. #endif /* USE_CERT_BUFFERS */
  14661. (void)idx;
  14662. (void)tmp;
  14663. (void)bytes;
  14664. pubSz = DH_TEST_BUF_SIZE;
  14665. pubSz2 = DH_TEST_BUF_SIZE;
  14666. privSz = DH_TEST_BUF_SIZE;
  14667. privSz2 = DH_TEST_BUF_SIZE;
  14668. #ifndef WC_NO_RNG
  14669. XMEMSET(&rng, 0, sizeof(rng));
  14670. #endif
  14671. /* Use API for coverage. */
  14672. ret = wc_InitDhKey(key);
  14673. if (ret != 0) {
  14674. ERROR_OUT(-8103, done);
  14675. }
  14676. wc_FreeDhKey(key);
  14677. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14678. if (ret != 0) {
  14679. ERROR_OUT(-8104, done);
  14680. }
  14681. keyInit = 1;
  14682. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14683. if (ret != 0) {
  14684. ERROR_OUT(-8105, done);
  14685. }
  14686. #ifdef NO_ASN
  14687. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14688. if (ret != 0) {
  14689. ERROR_OUT(-8106, done);
  14690. }
  14691. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14692. if (ret != 0) {
  14693. ERROR_OUT(-8107, done);
  14694. }
  14695. #else
  14696. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14697. if (ret != 0) {
  14698. ERROR_OUT(-8108, done);
  14699. }
  14700. idx = 0;
  14701. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  14702. if (ret != 0) {
  14703. ERROR_OUT(-8109, done);
  14704. }
  14705. #endif
  14706. #ifndef WC_NO_RNG
  14707. #ifndef HAVE_FIPS
  14708. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14709. #else
  14710. ret = wc_InitRng(&rng);
  14711. #endif
  14712. if (ret != 0) {
  14713. ERROR_OUT(-8110, done);
  14714. }
  14715. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  14716. #if defined(WOLFSSL_ASYNC_CRYPT)
  14717. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14718. #endif
  14719. if (ret != 0) {
  14720. ERROR_OUT(-8111, done);
  14721. }
  14722. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  14723. #if defined(WOLFSSL_ASYNC_CRYPT)
  14724. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14725. #endif
  14726. if (ret != 0) {
  14727. ERROR_OUT(-8112, done);
  14728. }
  14729. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14730. #if defined(WOLFSSL_ASYNC_CRYPT)
  14731. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14732. #endif
  14733. if (ret != 0) {
  14734. ERROR_OUT(-8113, done);
  14735. }
  14736. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14737. #if defined(WOLFSSL_ASYNC_CRYPT)
  14738. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14739. #endif
  14740. if (ret != 0) {
  14741. ERROR_OUT(-8114, done);
  14742. }
  14743. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14744. ERROR_OUT(-8115, done);
  14745. }
  14746. #endif /* !WC_NO_RNG */
  14747. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14748. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  14749. ERROR_OUT(-8116, done);
  14750. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  14751. ERROR_OUT(-8117, done);
  14752. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  14753. ERROR_OUT(-8118, done);
  14754. {
  14755. word32 pSz, qSz, gSz;
  14756. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  14757. ERROR_OUT(-8119, done);
  14758. }
  14759. #endif
  14760. /* Test DH key import / export */
  14761. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  14762. (!defined(HAVE_FIPS) || \
  14763. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  14764. wc_FreeDhKey(key);
  14765. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14766. if (ret != 0) {
  14767. ERROR_OUT(-8120, done);
  14768. }
  14769. #ifndef NO_ASN
  14770. {
  14771. /* DH Private - Key Export / Import */
  14772. #ifdef WOLFSSL_SMALL_STACK
  14773. byte *tmp2;
  14774. #else
  14775. byte tmp2[DH_TEST_TMP_SIZE];
  14776. #endif
  14777. XFILE file = XFOPEN(dhKeyFile, "rb");
  14778. if (!file)
  14779. ERROR_OUT(-8130, done);
  14780. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14781. XFCLOSE(file);
  14782. #ifdef WOLFSSL_SMALL_STACK
  14783. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14784. if (tmp2 == NULL)
  14785. ERROR_OUT(-8131, done);
  14786. #endif
  14787. idx = 0;
  14788. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  14789. /* Import DH Private key as DER */
  14790. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14791. if (ret == 0) {
  14792. /* Export as DER */
  14793. idx = DH_TEST_TMP_SIZE;
  14794. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  14795. }
  14796. /* Verify export matches original */
  14797. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  14798. ERROR_OUT(-8132, done);
  14799. }
  14800. /* DH Public Key - Export / Import */
  14801. file = XFOPEN(dhKeyPubFile, "rb");
  14802. if (!file)
  14803. ERROR_OUT(-8133, done);
  14804. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14805. XFCLOSE(file);
  14806. /* for HAVE_WOLF_BIGINT prevent leak */
  14807. wc_FreeDhKey(key);
  14808. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14809. idx = 0;
  14810. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  14811. /* Import DH Public key as DER */
  14812. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14813. if (ret == 0) {
  14814. /* Export as DER */
  14815. idx = DH_TEST_TMP_SIZE;
  14816. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  14817. }
  14818. /* Verify export matches original */
  14819. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  14820. ERROR_OUT(-8134, done);
  14821. }
  14822. #ifdef WOLFSSL_SMALL_STACK
  14823. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14824. #endif
  14825. }
  14826. #else
  14827. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14828. if (ret != 0) {
  14829. ERROR_OUT(-8121, done);
  14830. }
  14831. #endif /* !NO_ASN */
  14832. privSz = DH_TEST_BUF_SIZE;
  14833. pubSz = DH_TEST_BUF_SIZE;
  14834. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  14835. if (ret != 0) {
  14836. ERROR_OUT(-8122, done);
  14837. }
  14838. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  14839. if (ret != 0) {
  14840. ERROR_OUT(-8125, done);
  14841. }
  14842. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  14843. #ifndef WC_NO_RNG
  14844. ret = dh_generate_test(&rng);
  14845. if (ret != 0)
  14846. ERROR_OUT(-8123, done);
  14847. ret = dh_fips_generate_test(&rng);
  14848. if (ret != 0)
  14849. ERROR_OUT(-8124, done);
  14850. #endif /* !WC_NO_RNG */
  14851. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14852. ret = dh_test_check_pubvalue();
  14853. if (ret != 0)
  14854. ERROR_OUT(-8125, done);
  14855. #endif
  14856. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  14857. !defined(WOLFSSL_SP_ARM64_ASM)
  14858. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  14859. * this will be available for testing in the 140-3 module */
  14860. #ifndef WC_NO_RNG
  14861. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  14862. #ifdef HAVE_FFDHE_2048
  14863. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  14864. if (ret != 0)
  14865. ERROR_OUT(-8126, done);
  14866. #endif
  14867. #ifdef HAVE_FFDHE_3072
  14868. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  14869. if (ret != 0)
  14870. ERROR_OUT(-8127, done);
  14871. #endif
  14872. #ifdef HAVE_FFDHE_4096
  14873. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  14874. if (ret != 0)
  14875. ERROR_OUT(-8128, done);
  14876. #endif
  14877. #endif /* !WC_NO_RNG */
  14878. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  14879. wc_FreeDhKey(key);
  14880. keyInit = 0;
  14881. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  14882. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  14883. /* Test Check Key */
  14884. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  14885. NULL, 0, 0, &rng);
  14886. if (ret != 0)
  14887. ERROR_OUT(-8129, done);
  14888. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  14889. #endif
  14890. done:
  14891. #ifndef WC_NO_RNG
  14892. wc_FreeRng(&rng);
  14893. #endif
  14894. #ifdef WOLFSSL_SMALL_STACK
  14895. if (key) {
  14896. if (keyInit)
  14897. wc_FreeDhKey(key);
  14898. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14899. }
  14900. if (key2) {
  14901. wc_FreeDhKey(key2);
  14902. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14903. }
  14904. if (tmp)
  14905. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14906. if (priv)
  14907. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14908. if (pub)
  14909. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14910. if (priv2)
  14911. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14912. if (pub2)
  14913. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14914. if (agree)
  14915. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14916. if (agree2)
  14917. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14918. #else
  14919. if (keyInit)
  14920. wc_FreeDhKey(key);
  14921. wc_FreeDhKey(key2);
  14922. #endif
  14923. (void)privSz;
  14924. (void)pubSz;
  14925. (void)pubSz2;
  14926. (void)privSz2;
  14927. return ret;
  14928. #undef DH_TEST_BUF_SIZE
  14929. #undef DH_TEST_TMP_SIZE
  14930. }
  14931. #endif /* NO_DH */
  14932. #ifndef NO_DSA
  14933. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  14934. {
  14935. int ret = 0, answer;
  14936. word32 bytes;
  14937. word32 idx = 0;
  14938. WC_RNG rng;
  14939. wc_Sha sha;
  14940. byte hash[WC_SHA_DIGEST_SIZE];
  14941. byte signature[40];
  14942. #ifdef WOLFSSL_KEY_GEN
  14943. byte* der = 0;
  14944. #endif
  14945. #define DSA_TEST_TMP_SIZE 1024
  14946. #ifdef WOLFSSL_SMALL_STACK
  14947. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14948. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14949. #ifdef WOLFSSL_KEY_GEN
  14950. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14951. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14952. #endif
  14953. if ((tmp == NULL) ||
  14954. (key == NULL)
  14955. #ifdef WOLFSSL_KEY_GEN
  14956. || (derIn == NULL)
  14957. || (genKey == NULL)
  14958. #endif
  14959. ) {
  14960. ret = -8216;
  14961. goto out;
  14962. }
  14963. #else
  14964. byte tmp[1024];
  14965. DsaKey key[1];
  14966. #ifdef WOLFSSL_KEY_GEN
  14967. DsaKey derIn[1];
  14968. DsaKey genKey[1];
  14969. #endif
  14970. #endif
  14971. #ifdef USE_CERT_BUFFERS_1024
  14972. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  14973. bytes = sizeof_dsa_key_der_1024;
  14974. #elif defined(USE_CERT_BUFFERS_2048)
  14975. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  14976. bytes = sizeof_dsa_key_der_2048;
  14977. #else
  14978. {
  14979. XFILE file = XFOPEN(dsaKey, "rb");
  14980. if (!file)
  14981. ERROR_OUT(-8200, out);
  14982. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  14983. XFCLOSE(file);
  14984. }
  14985. #endif /* USE_CERT_BUFFERS */
  14986. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  14987. if (ret != 0)
  14988. ERROR_OUT(-8201, out);
  14989. wc_ShaUpdate(&sha, tmp, bytes);
  14990. wc_ShaFinal(&sha, hash);
  14991. wc_ShaFree(&sha);
  14992. ret = wc_InitDsaKey(key);
  14993. if (ret != 0)
  14994. ERROR_OUT(-8202, out);
  14995. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  14996. if (ret != 0)
  14997. ERROR_OUT(-8203, out);
  14998. #ifndef HAVE_FIPS
  14999. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15000. #else
  15001. ret = wc_InitRng(&rng);
  15002. #endif
  15003. if (ret != 0)
  15004. ERROR_OUT(-8204, out);
  15005. ret = wc_DsaSign(hash, signature, key, &rng);
  15006. if (ret != 0)
  15007. ERROR_OUT(-8205, out);
  15008. ret = wc_DsaVerify(hash, signature, key, &answer);
  15009. if (ret != 0)
  15010. ERROR_OUT(-8206, out);
  15011. if (answer != 1)
  15012. ERROR_OUT(-8207, out);
  15013. wc_FreeDsaKey(key);
  15014. #ifdef WOLFSSL_KEY_GEN
  15015. {
  15016. int derSz = 0;
  15017. ret = wc_InitDsaKey(genKey);
  15018. if (ret != 0)
  15019. ERROR_OUT(-8208, out);
  15020. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  15021. if (ret != 0) {
  15022. wc_FreeDsaKey(genKey);
  15023. ERROR_OUT(-8209, out);
  15024. }
  15025. ret = wc_MakeDsaKey(&rng, genKey);
  15026. if (ret != 0) {
  15027. wc_FreeDsaKey(genKey);
  15028. ERROR_OUT(-8210, out);
  15029. }
  15030. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15031. if (der == NULL) {
  15032. wc_FreeDsaKey(genKey);
  15033. ERROR_OUT(-8211, out);
  15034. }
  15035. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  15036. if (derSz < 0) {
  15037. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15038. ERROR_OUT(-8212, out);
  15039. }
  15040. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  15041. DSA_PRIVATEKEY_TYPE, -5814);
  15042. if (ret != 0) {
  15043. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15044. wc_FreeDsaKey(genKey);
  15045. goto out;
  15046. }
  15047. ret = wc_InitDsaKey(derIn);
  15048. if (ret != 0) {
  15049. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15050. wc_FreeDsaKey(genKey);
  15051. ERROR_OUT(-8213, out);
  15052. }
  15053. idx = 0;
  15054. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  15055. if (ret != 0) {
  15056. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15057. wc_FreeDsaKey(derIn);
  15058. wc_FreeDsaKey(genKey);
  15059. ERROR_OUT(-8214, out);
  15060. }
  15061. }
  15062. #endif /* WOLFSSL_KEY_GEN */
  15063. out:
  15064. #ifdef WOLFSSL_SMALL_STACK
  15065. if (key) {
  15066. #endif
  15067. if (wc_InitDsaKey_h(key, NULL) != 0)
  15068. ret = -8215;
  15069. #ifdef WOLFSSL_SMALL_STACK
  15070. }
  15071. #endif
  15072. #ifdef WOLFSSL_KEY_GEN
  15073. if (der)
  15074. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15075. #endif
  15076. #ifdef WOLFSSL_SMALL_STACK
  15077. if (tmp)
  15078. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15079. if (key)
  15080. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15081. #ifdef WOLFSSL_KEY_GEN
  15082. if (derIn) {
  15083. wc_FreeDsaKey(derIn);
  15084. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15085. }
  15086. if (genKey) {
  15087. wc_FreeDsaKey(genKey);
  15088. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15089. }
  15090. #endif
  15091. #else /* !WOLFSSL_SMALL_STACK */
  15092. #ifdef WOLFSSL_KEY_GEN
  15093. wc_FreeDsaKey(derIn);
  15094. wc_FreeDsaKey(genKey);
  15095. #endif
  15096. #endif
  15097. wc_FreeRng(&rng);
  15098. return ret;
  15099. }
  15100. #endif /* NO_DSA */
  15101. #ifdef WOLFCRYPT_HAVE_SRP
  15102. static int generate_random_salt(byte *buf, word32 size)
  15103. {
  15104. int ret = -8220;
  15105. WC_RNG rng;
  15106. if(NULL == buf || !size)
  15107. return -8221;
  15108. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  15109. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  15110. wc_FreeRng(&rng);
  15111. }
  15112. return ret;
  15113. }
  15114. static int srp_test_digest(SrpType dgstType)
  15115. {
  15116. int r;
  15117. byte clientPubKey[192]; /* A */
  15118. byte serverPubKey[192]; /* B */
  15119. word32 clientPubKeySz = 192;
  15120. word32 serverPubKeySz = 192;
  15121. byte username[] = "user";
  15122. word32 usernameSz = 4;
  15123. byte password[] = "password";
  15124. word32 passwordSz = 8;
  15125. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  15126. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  15127. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  15128. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  15129. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  15130. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  15131. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  15132. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  15133. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  15134. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  15135. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  15136. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  15137. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  15138. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  15139. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  15140. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  15141. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  15142. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  15143. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  15144. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  15145. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  15146. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  15147. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  15148. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  15149. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  15150. };
  15151. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15152. 0x02
  15153. };
  15154. byte salt[10];
  15155. byte verifier[192];
  15156. word32 v_size = sizeof(verifier);
  15157. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  15158. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  15159. #ifdef WOLFSSL_SMALL_STACK
  15160. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15161. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15162. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  15163. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  15164. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  15165. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  15166. if ((cli == NULL) ||
  15167. (srv == NULL) ||
  15168. (clientProof == NULL) ||
  15169. (serverProof == NULL)) {
  15170. r = -8222;
  15171. goto out;
  15172. }
  15173. #else
  15174. Srp cli[1], srv[1];
  15175. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  15176. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  15177. #endif
  15178. /* set as 0's so if second init on srv not called SrpTerm is not on
  15179. * garbage values */
  15180. XMEMSET(srv, 0, sizeof *srv);
  15181. XMEMSET(cli, 0, sizeof *cli);
  15182. /* generating random salt */
  15183. r = generate_random_salt(salt, sizeof(salt));
  15184. /* client knows username and password. */
  15185. /* server knows N, g, salt and verifier. */
  15186. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  15187. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  15188. /* loading N, g and salt in advance to generate the verifier. */
  15189. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  15190. g, sizeof(g),
  15191. salt, sizeof(salt));
  15192. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  15193. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  15194. /* client sends username to server */
  15195. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  15196. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  15197. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  15198. g, sizeof(g),
  15199. salt, sizeof(salt));
  15200. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  15201. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  15202. /* server sends N, g, salt and B to client */
  15203. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  15204. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  15205. serverPubKey, serverPubKeySz);
  15206. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  15207. /* client sends A and M1 to server */
  15208. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  15209. serverPubKey, serverPubKeySz);
  15210. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  15211. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  15212. /* server sends M2 to client */
  15213. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  15214. wc_SrpTerm(cli);
  15215. wc_SrpTerm(srv);
  15216. #ifdef WOLFSSL_SMALL_STACK
  15217. out:
  15218. if (cli)
  15219. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15220. if (srv)
  15221. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15222. if (clientProof)
  15223. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15224. if (serverProof)
  15225. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15226. #endif
  15227. return r;
  15228. }
  15229. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  15230. {
  15231. int ret;
  15232. #ifndef NO_SHA
  15233. ret = srp_test_digest(SRP_TYPE_SHA);
  15234. if (ret != 0)
  15235. return ret;
  15236. #endif
  15237. #ifndef NO_SHA256
  15238. ret = srp_test_digest(SRP_TYPE_SHA256);
  15239. if (ret != 0)
  15240. return ret;
  15241. #endif
  15242. #ifdef WOLFSSL_SHA384
  15243. ret = srp_test_digest(SRP_TYPE_SHA384);
  15244. if (ret != 0)
  15245. return ret;
  15246. #endif
  15247. #ifdef WOLFSSL_SHA512
  15248. ret = srp_test_digest(SRP_TYPE_SHA512);
  15249. if (ret != 0)
  15250. return ret;
  15251. #endif
  15252. return ret;
  15253. }
  15254. #endif /* WOLFCRYPT_HAVE_SRP */
  15255. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  15256. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  15257. static int openssl_aes_test(void)
  15258. {
  15259. #ifdef HAVE_AES_CBC
  15260. #ifdef WOLFSSL_AES_128
  15261. {
  15262. /* EVP_CipherUpdate test */
  15263. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  15264. {
  15265. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15266. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15267. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15268. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15269. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15270. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15271. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15272. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15273. };
  15274. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15275. "0123456789abcdef "; /* align */
  15276. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15277. "1234567890abcdef "; /* align */
  15278. byte cipher[AES_BLOCK_SIZE * 4];
  15279. byte plain [AES_BLOCK_SIZE * 4];
  15280. #ifdef WOLFSSL_SMALL_STACK
  15281. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15282. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15283. #else
  15284. EVP_CIPHER_CTX en[1];
  15285. EVP_CIPHER_CTX de[1];
  15286. #endif
  15287. int outlen ;
  15288. int total = 0;
  15289. int i;
  15290. #ifdef WOLFSSL_SMALL_STACK
  15291. if ((en == NULL) || (de == NULL))
  15292. return MEMORY_E;
  15293. #endif
  15294. EVP_CIPHER_CTX_init(en);
  15295. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15296. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15297. return -8400;
  15298. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  15299. (byte*)cbcPlain, 9) == 0)
  15300. return -8401;
  15301. if (outlen != 0)
  15302. return -8402;
  15303. total += outlen;
  15304. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  15305. (byte*)&cbcPlain[9] , 9) == 0)
  15306. return -8403;
  15307. if (outlen != 16)
  15308. return -8404;
  15309. total += outlen;
  15310. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  15311. return -8405;
  15312. if (outlen != 16)
  15313. return -8406;
  15314. total += outlen;
  15315. if (total != 32)
  15316. return 3408;
  15317. total = 0;
  15318. EVP_CIPHER_CTX_init(de);
  15319. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15320. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15321. return -8407;
  15322. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  15323. return -8408;
  15324. if (outlen != 0)
  15325. return -8409;
  15326. total += outlen;
  15327. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  15328. (byte*)&cipher[6], 12) == 0)
  15329. return -8410;
  15330. if (outlen != 0)
  15331. total += outlen;
  15332. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  15333. (byte*)&cipher[6+12], 14) == 0)
  15334. return -8411;
  15335. if (outlen != 16)
  15336. return -8412;
  15337. total += outlen;
  15338. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  15339. return -8413;
  15340. if (outlen != 2)
  15341. return -8414;
  15342. total += outlen;
  15343. if (total != 18)
  15344. return 3427;
  15345. if (XMEMCMP(plain, cbcPlain, 18))
  15346. return -8415;
  15347. /* test with encrypting/decrypting more than 16 bytes at once */
  15348. total = 0;
  15349. EVP_CIPHER_CTX_init(en);
  15350. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15351. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15352. return -8416;
  15353. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  15354. (byte*)cbcPlain, 17) == 0)
  15355. return -8417;
  15356. if (outlen != 16)
  15357. return -8418;
  15358. total += outlen;
  15359. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  15360. (byte*)&cbcPlain[17] , 1) == 0)
  15361. return -8419;
  15362. if (outlen != 0)
  15363. return -8420;
  15364. total += outlen;
  15365. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  15366. return -8421;
  15367. if (outlen != 16)
  15368. return -8422;
  15369. total += outlen;
  15370. if (total != 32)
  15371. return -8423;
  15372. total = 0;
  15373. EVP_CIPHER_CTX_init(de);
  15374. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15375. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15376. return -8424;
  15377. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  15378. return -8425;
  15379. if (outlen != 16)
  15380. return -8426;
  15381. total += outlen;
  15382. /* final call on non block size should fail */
  15383. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  15384. return -8427;
  15385. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  15386. (byte*)&cipher[17], 1) == 0)
  15387. return -8428;
  15388. if (outlen != 0)
  15389. total += outlen;
  15390. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  15391. (byte*)&cipher[17+1], 14) == 0)
  15392. return -8429;
  15393. if (outlen != 0)
  15394. return -8430;
  15395. total += outlen;
  15396. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  15397. return -8431;
  15398. if (outlen != 2)
  15399. return -8432;
  15400. total += outlen;
  15401. if (total != 18)
  15402. return -8433;
  15403. if (XMEMCMP(plain, cbcPlain, 18))
  15404. return -8434;
  15405. /* test byte by byte decrypt */
  15406. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  15407. plain[i] = i;
  15408. }
  15409. total = 0;
  15410. EVP_CIPHER_CTX_init(en);
  15411. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15412. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15413. return -8435;
  15414. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  15415. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  15416. return -8436;
  15417. if (outlen != AES_BLOCK_SIZE * 3)
  15418. return -8437;
  15419. total += outlen;
  15420. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  15421. return -8438;
  15422. if (outlen != AES_BLOCK_SIZE)
  15423. return -8439;
  15424. total += outlen;
  15425. if (total != sizeof(plain))
  15426. return -8440;
  15427. total = 0;
  15428. EVP_CIPHER_CTX_init(de);
  15429. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15430. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15431. return -8441;
  15432. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  15433. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  15434. (byte*)cipher + i, 1) == 0)
  15435. return -8442;
  15436. if (outlen > 0) {
  15437. int j;
  15438. total += outlen;
  15439. for (j = 0; j < total; j++) {
  15440. if (plain[j] != j) {
  15441. return -8443;
  15442. }
  15443. }
  15444. }
  15445. }
  15446. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  15447. return -8444;
  15448. total += outlen;
  15449. if (total != AES_BLOCK_SIZE * 3) {
  15450. return -8445;
  15451. }
  15452. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  15453. if (plain[i] != i) {
  15454. return -8446;
  15455. }
  15456. }
  15457. #ifdef WOLFSSL_SMALL_STACK
  15458. wolfSSL_EVP_CIPHER_CTX_free(en);
  15459. wolfSSL_EVP_CIPHER_CTX_free(de);
  15460. #endif
  15461. }
  15462. /* set buffers to be exact size to catch potential over read/write */
  15463. {
  15464. /* EVP_CipherUpdate test */
  15465. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  15466. {
  15467. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15468. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15469. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15470. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15471. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15472. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15473. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15474. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15475. };
  15476. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15477. "0123456789abcdef "; /* align */
  15478. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15479. "1234567890abcdef "; /* align */
  15480. #define EVP_TEST_BUF_SZ 18
  15481. #define EVP_TEST_BUF_PAD 32
  15482. byte cipher[EVP_TEST_BUF_SZ];
  15483. byte plain [EVP_TEST_BUF_SZ];
  15484. byte padded[EVP_TEST_BUF_PAD];
  15485. #ifdef WOLFSSL_SMALL_STACK
  15486. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15487. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15488. #else
  15489. EVP_CIPHER_CTX en[1];
  15490. EVP_CIPHER_CTX de[1];
  15491. #endif
  15492. int outlen ;
  15493. int total = 0;
  15494. #ifdef WOLFSSL_SMALL_STACK
  15495. if ((en == NULL) || (de == NULL))
  15496. return MEMORY_E;
  15497. #endif
  15498. EVP_CIPHER_CTX_init(en);
  15499. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15500. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15501. return -8447;
  15502. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  15503. return -8448;
  15504. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  15505. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  15506. return -8449;
  15507. if (outlen != 16)
  15508. return -8450;
  15509. total += outlen;
  15510. /* should fail here */
  15511. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  15512. return -8451;
  15513. /* turn padding back on and do successful encrypt */
  15514. total = 0;
  15515. EVP_CIPHER_CTX_init(en);
  15516. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15517. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15518. return -8452;
  15519. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  15520. return -8453;
  15521. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  15522. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  15523. return -8454;
  15524. if (outlen != 16)
  15525. return -8455;
  15526. total += outlen;
  15527. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  15528. return -8456;
  15529. total += outlen;
  15530. if (total != 32)
  15531. return -8457;
  15532. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  15533. /* test out of bounds read on buffers w/o padding during decryption */
  15534. total = 0;
  15535. EVP_CIPHER_CTX_init(de);
  15536. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15537. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15538. return -8458;
  15539. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  15540. return -8459;
  15541. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  15542. EVP_TEST_BUF_SZ) == 0)
  15543. return -8460;
  15544. if (outlen != 16)
  15545. return -8461;
  15546. total += outlen;
  15547. /* should fail since not using padding */
  15548. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  15549. return -8462;
  15550. total = 0;
  15551. EVP_CIPHER_CTX_init(de);
  15552. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15553. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15554. return -8463;
  15555. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  15556. return -8464;
  15557. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  15558. EVP_TEST_BUF_PAD) == 0)
  15559. return -8465;
  15560. if (outlen != 16)
  15561. return -8466;
  15562. total += outlen;
  15563. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  15564. return -8467;
  15565. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  15566. return -8468;
  15567. #ifdef WOLFSSL_SMALL_STACK
  15568. wolfSSL_EVP_CIPHER_CTX_free(en);
  15569. wolfSSL_EVP_CIPHER_CTX_free(de);
  15570. #endif
  15571. }
  15572. { /* evp_cipher test: EVP_aes_128_cbc */
  15573. #ifdef WOLFSSL_SMALL_STACK
  15574. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15575. #else
  15576. EVP_CIPHER_CTX ctx[1];
  15577. #endif
  15578. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  15579. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15580. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15581. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15582. };
  15583. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15584. {
  15585. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15586. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  15587. };
  15588. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15589. "0123456789abcdef "; /* align */
  15590. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15591. "1234567890abcdef "; /* align */
  15592. byte cipher[AES_BLOCK_SIZE * 4];
  15593. byte plain [AES_BLOCK_SIZE * 4];
  15594. #ifdef WOLFSSL_SMALL_STACK
  15595. if (ctx == NULL)
  15596. return MEMORY_E;
  15597. #endif
  15598. EVP_CIPHER_CTX_init(ctx);
  15599. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  15600. return -8469;
  15601. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15602. return -8470;
  15603. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15604. return -8471;
  15605. EVP_CIPHER_CTX_init(ctx);
  15606. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  15607. return -8472;
  15608. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15609. return -8473;
  15610. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15611. return -8474;
  15612. #ifdef WOLFSSL_SMALL_STACK
  15613. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15614. #endif
  15615. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15616. #endif /* WOLFSSL_AES_128 */
  15617. #endif /* HAVE_AES_CBC */
  15618. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15619. { /* evp_cipher test: EVP_aes_256_ecb*/
  15620. #ifdef WOLFSSL_SMALL_STACK
  15621. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15622. #else
  15623. EVP_CIPHER_CTX ctx[1];
  15624. #endif
  15625. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15626. {
  15627. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15628. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15629. };
  15630. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15631. {
  15632. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15633. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15634. };
  15635. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15636. {
  15637. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15638. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15639. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15640. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15641. };
  15642. byte cipher[AES_BLOCK_SIZE * 4];
  15643. byte plain [AES_BLOCK_SIZE * 4];
  15644. #ifdef WOLFSSL_SMALL_STACK
  15645. if (ctx == NULL)
  15646. return MEMORY_E;
  15647. #endif
  15648. EVP_CIPHER_CTX_init(ctx);
  15649. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  15650. return -8475;
  15651. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15652. return -8476;
  15653. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15654. return -8477;
  15655. EVP_CIPHER_CTX_init(ctx);
  15656. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  15657. return -8478;
  15658. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15659. return -8479;
  15660. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15661. return -8480;
  15662. #ifdef WOLFSSL_SMALL_STACK
  15663. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15664. #endif
  15665. } /* end evp_cipher test */
  15666. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  15667. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  15668. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  15669. {
  15670. /* Test: AES_encrypt/decrypt/set Key */
  15671. #ifdef WOLFSSL_SMALL_STACK
  15672. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15673. #ifdef HAVE_AES_DECRYPT
  15674. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15675. #endif
  15676. #else
  15677. AES_KEY enc[1];
  15678. #ifdef HAVE_AES_DECRYPT
  15679. AES_KEY dec[1];
  15680. #endif
  15681. #endif
  15682. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15683. {
  15684. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15685. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15686. };
  15687. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15688. {
  15689. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15690. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15691. };
  15692. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15693. {
  15694. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15695. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15696. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15697. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15698. };
  15699. byte plain[sizeof(msg)];
  15700. byte cipher[sizeof(msg)];
  15701. #ifdef WOLFSSL_SMALL_STACK
  15702. if (enc == NULL)
  15703. return MEMORY_E;
  15704. #ifdef HAVE_AES_DECRYPT
  15705. if (dec == NULL)
  15706. return MEMORY_E;
  15707. #endif
  15708. #endif
  15709. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  15710. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  15711. AES_encrypt(msg, cipher, enc);
  15712. #ifdef HAVE_AES_DECRYPT
  15713. AES_decrypt(cipher, plain, dec);
  15714. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15715. return -8481;
  15716. #endif /* HAVE_AES_DECRYPT */
  15717. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15718. return -8482;
  15719. #ifdef WOLFSSL_SMALL_STACK
  15720. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15721. #ifdef HAVE_AES_DECRYPT
  15722. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15723. #endif
  15724. #endif
  15725. }
  15726. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  15727. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  15728. #ifdef WOLFSSL_AES_COUNTER
  15729. {
  15730. byte plainBuff [64];
  15731. byte cipherBuff[64];
  15732. #ifdef WOLFSSL_AES_128
  15733. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  15734. {
  15735. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15736. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15737. };
  15738. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  15739. {
  15740. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15741. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15742. };
  15743. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  15744. {
  15745. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15746. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15747. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15748. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15749. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15750. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15751. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15752. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15753. };
  15754. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  15755. {
  15756. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  15757. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  15758. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  15759. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  15760. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  15761. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  15762. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  15763. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  15764. };
  15765. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  15766. {
  15767. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  15768. 0xc2
  15769. };
  15770. #endif
  15771. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15772. * NIST Special Publication 800-38A */
  15773. #ifdef WOLFSSL_AES_192
  15774. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  15775. {
  15776. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  15777. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  15778. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  15779. };
  15780. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  15781. {
  15782. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15783. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15784. };
  15785. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  15786. {
  15787. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15788. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15789. };
  15790. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  15791. {
  15792. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  15793. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  15794. };
  15795. #endif /* WOLFSSL_AES_192 */
  15796. #ifdef WOLFSSL_AES_256
  15797. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15798. * NIST Special Publication 800-38A */
  15799. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  15800. {
  15801. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15802. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15803. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15804. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15805. };
  15806. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  15807. {
  15808. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15809. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15810. };
  15811. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  15812. {
  15813. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15814. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15815. };
  15816. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  15817. {
  15818. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  15819. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  15820. };
  15821. #endif /* WOLFSSL_AES_256 */
  15822. #ifdef WOLFSSL_SMALL_STACK
  15823. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15824. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15825. #else
  15826. EVP_CIPHER_CTX en[1];
  15827. EVP_CIPHER_CTX de[1];
  15828. #endif
  15829. #ifdef WOLFSSL_AES_128
  15830. #ifndef WOLFSSL_SMALL_STACK
  15831. EVP_CIPHER_CTX *p_en;
  15832. EVP_CIPHER_CTX *p_de;
  15833. #endif
  15834. #ifdef WOLFSSL_SMALL_STACK
  15835. if ((en == NULL) || (de == NULL))
  15836. return MEMORY_E;
  15837. #endif
  15838. EVP_CIPHER_CTX_init(en);
  15839. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15840. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15841. return -8483;
  15842. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  15843. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15844. return -8484;
  15845. EVP_CIPHER_CTX_init(de);
  15846. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15847. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15848. return -8485;
  15849. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15850. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15851. return -8486;
  15852. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15853. return -8487;
  15854. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15855. return -8488;
  15856. #ifndef WOLFSSL_SMALL_STACK
  15857. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  15858. if (p_en == NULL)
  15859. return -8489;
  15860. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  15861. if (p_de == NULL)
  15862. return -8490;
  15863. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  15864. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15865. return -8491;
  15866. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  15867. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15868. return -8492;
  15869. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  15870. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15871. return -8493;
  15872. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  15873. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15874. return -8494;
  15875. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  15876. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  15877. #endif /* !WOLFSSL_SMALL_STACK */
  15878. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15879. return -8495;
  15880. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15881. return -8496;
  15882. EVP_CIPHER_CTX_init(en);
  15883. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15884. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15885. return -8497;
  15886. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15887. return -8498;
  15888. EVP_CIPHER_CTX_init(de);
  15889. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15890. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15891. return -8499;
  15892. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15893. return -8500;
  15894. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15895. return -8501;
  15896. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  15897. return -8502;
  15898. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15899. return -8503;
  15900. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15901. return -8504;
  15902. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15903. return -8505;
  15904. if (XMEMCMP(cipherBuff, oddCipher, 9))
  15905. return -8506;
  15906. #endif /* WOLFSSL_AES_128 */
  15907. #ifdef WOLFSSL_AES_192
  15908. EVP_CIPHER_CTX_init(en);
  15909. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  15910. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15911. return -8507;
  15912. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  15913. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15914. return -8508;
  15915. EVP_CIPHER_CTX_init(de);
  15916. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  15917. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15918. return -8509;
  15919. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15920. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15921. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15922. return -8510;
  15923. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  15924. return -8511;
  15925. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  15926. return -8512;
  15927. #endif /* WOLFSSL_AES_192 */
  15928. #ifdef WOLFSSL_AES_256
  15929. EVP_CIPHER_CTX_init(en);
  15930. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  15931. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15932. return -8513;
  15933. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  15934. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15935. return -8514;
  15936. EVP_CIPHER_CTX_init(de);
  15937. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  15938. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15939. return -8515;
  15940. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15941. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15942. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15943. return -8516;
  15944. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  15945. return -8517;
  15946. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  15947. return -8518;
  15948. #ifdef WOLFSSL_SMALL_STACK
  15949. wolfSSL_EVP_CIPHER_CTX_free(en);
  15950. wolfSSL_EVP_CIPHER_CTX_free(de);
  15951. #endif
  15952. #endif /* WOLFSSL_AES_256 */
  15953. }
  15954. #endif /* HAVE_AES_COUNTER */
  15955. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  15956. {
  15957. #ifdef WOLFSSL_SMALL_STACK
  15958. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15959. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15960. #else
  15961. AES_KEY enc[1];
  15962. AES_KEY dec[1];
  15963. #endif
  15964. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  15965. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  15966. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  15967. };
  15968. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15969. {
  15970. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15971. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15972. };
  15973. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  15974. {
  15975. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  15976. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  15977. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  15978. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  15979. };
  15980. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15981. {
  15982. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15983. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15984. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15985. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  15986. };
  15987. byte cipher[AES_BLOCK_SIZE * 2];
  15988. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  15989. int num = 0;
  15990. #ifdef WOLFSSL_SMALL_STACK
  15991. if ((enc == NULL) || (dec == NULL))
  15992. return MEMORY_E;
  15993. #endif
  15994. XMEMCPY(iv, setIv, sizeof(setIv));
  15995. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  15996. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  15997. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  15998. &num, AES_ENCRYPT);
  15999. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16000. return -8519;
  16001. if (num != 15) /* should have used 15 of the 16 bytes */
  16002. return -8520;
  16003. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16004. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16005. &num, AES_ENCRYPT);
  16006. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  16007. return -8521;
  16008. if (num != 0)
  16009. return -8522;
  16010. #ifdef WOLFSSL_SMALL_STACK
  16011. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16012. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16013. #endif
  16014. }
  16015. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  16016. return 0;
  16017. }
  16018. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  16019. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  16020. {
  16021. int ret;
  16022. EVP_MD_CTX md_ctx;
  16023. testVector a, b, c, d, e, f;
  16024. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  16025. a.inLen = 0;
  16026. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  16027. (void)a;
  16028. (void)b;
  16029. (void)c;
  16030. (void)d;
  16031. (void)e;
  16032. (void)f;
  16033. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  16034. {
  16035. byte* p;
  16036. p = (byte*)CRYPTO_malloc(10, "", 0);
  16037. if (p == NULL) {
  16038. return -8600;
  16039. }
  16040. XMEMSET(p, 0, 10);
  16041. CRYPTO_free(p, "", 0);
  16042. }
  16043. #ifndef NO_MD5
  16044. a.input = "1234567890123456789012345678901234567890123456789012345678"
  16045. "9012345678901234567890";
  16046. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  16047. "\x7a";
  16048. a.inLen = XSTRLEN(a.input);
  16049. a.outLen = WC_MD5_DIGEST_SIZE;
  16050. EVP_MD_CTX_init(&md_ctx);
  16051. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  16052. if (ret == WOLFSSL_SUCCESS) {
  16053. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  16054. }
  16055. if (ret == WOLFSSL_SUCCESS) {
  16056. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16057. }
  16058. EVP_MD_CTX_cleanup(&md_ctx);
  16059. if (ret != WOLFSSL_SUCCESS ||
  16060. XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  16061. return -8601;
  16062. }
  16063. #endif /* NO_MD5 */
  16064. #ifndef NO_SHA
  16065. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  16066. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  16067. "aaaaaaaaaa";
  16068. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  16069. "\x53\x99\x5E\x26\xA0";
  16070. b.inLen = XSTRLEN(b.input);
  16071. b.outLen = WC_SHA_DIGEST_SIZE;
  16072. EVP_MD_CTX_init(&md_ctx);
  16073. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  16074. if (ret == WOLFSSL_SUCCESS) {
  16075. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  16076. if (ret == WOLFSSL_SUCCESS)
  16077. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16078. }
  16079. EVP_MD_CTX_cleanup(&md_ctx);
  16080. if (ret != WOLFSSL_SUCCESS ||
  16081. XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0) {
  16082. return -8602;
  16083. }
  16084. #endif /* NO_SHA */
  16085. #ifdef WOLFSSL_SHA224
  16086. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16087. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16088. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  16089. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  16090. e.inLen = XSTRLEN(e.input);
  16091. e.outLen = WC_SHA224_DIGEST_SIZE;
  16092. EVP_MD_CTX_init(&md_ctx);
  16093. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  16094. if (ret == WOLFSSL_SUCCESS) {
  16095. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16096. if (ret == WOLFSSL_SUCCESS)
  16097. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16098. }
  16099. EVP_MD_CTX_cleanup(&md_ctx);
  16100. if (ret != WOLFSSL_SUCCESS ||
  16101. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  16102. return -8603;
  16103. }
  16104. #endif /* WOLFSSL_SHA224 */
  16105. #ifndef NO_SHA256
  16106. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  16107. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  16108. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  16109. "\x06\xC1";
  16110. d.inLen = XSTRLEN(d.input);
  16111. d.outLen = WC_SHA256_DIGEST_SIZE;
  16112. EVP_MD_CTX_init(&md_ctx);
  16113. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  16114. if (ret == WOLFSSL_SUCCESS) {
  16115. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  16116. if (ret == WOLFSSL_SUCCESS)
  16117. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16118. }
  16119. EVP_MD_CTX_cleanup(&md_ctx);
  16120. if (ret != WOLFSSL_SUCCESS ||
  16121. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  16122. return -8604;
  16123. }
  16124. #endif /* !NO_SHA256 */
  16125. #ifdef WOLFSSL_SHA384
  16126. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16127. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16128. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  16129. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  16130. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  16131. "\x74\x60\x39";
  16132. e.inLen = XSTRLEN(e.input);
  16133. e.outLen = WC_SHA384_DIGEST_SIZE;
  16134. EVP_MD_CTX_init(&md_ctx);
  16135. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  16136. if (ret == WOLFSSL_SUCCESS) {
  16137. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16138. if (ret == WOLFSSL_SUCCESS)
  16139. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16140. }
  16141. EVP_MD_CTX_cleanup(&md_ctx);
  16142. if (ret != WOLFSSL_SUCCESS ||
  16143. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  16144. return -8605;
  16145. }
  16146. #endif /* WOLFSSL_SHA384 */
  16147. #ifdef WOLFSSL_SHA512
  16148. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16149. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16150. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  16151. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  16152. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  16153. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  16154. "\x87\x4b\xe9\x09";
  16155. f.inLen = XSTRLEN(f.input);
  16156. f.outLen = WC_SHA512_DIGEST_SIZE;
  16157. EVP_MD_CTX_init(&md_ctx);
  16158. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  16159. if (ret == WOLFSSL_SUCCESS) {
  16160. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16161. if (ret == WOLFSSL_SUCCESS)
  16162. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16163. }
  16164. EVP_MD_CTX_cleanup(&md_ctx);
  16165. if (ret != WOLFSSL_SUCCESS ||
  16166. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  16167. return -8606;
  16168. }
  16169. #endif /* WOLFSSL_SHA512 */
  16170. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16171. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  16172. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16173. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16174. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  16175. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  16176. f.inLen = XSTRLEN(f.input);
  16177. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  16178. EVP_MD_CTX_init(&md_ctx);
  16179. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  16180. if (ret == WOLFSSL_SUCCESS) {
  16181. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16182. if (ret == WOLFSSL_SUCCESS)
  16183. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16184. }
  16185. EVP_MD_CTX_cleanup(&md_ctx);
  16186. if (ret != WOLFSSL_SUCCESS ||
  16187. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  16188. return -8722;
  16189. }
  16190. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  16191. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  16192. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16193. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  16194. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16195. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16196. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  16197. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  16198. "\x56\x3a";
  16199. f.inLen = XSTRLEN(f.input);
  16200. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  16201. EVP_MD_CTX_init(&md_ctx);
  16202. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  16203. if (ret == WOLFSSL_SUCCESS) {
  16204. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16205. if (ret == WOLFSSL_SUCCESS)
  16206. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16207. }
  16208. EVP_MD_CTX_cleanup(&md_ctx);
  16209. if (ret != WOLFSSL_SUCCESS ||
  16210. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  16211. return -8723;
  16212. }
  16213. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  16214. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  16215. #ifdef WOLFSSL_SHA3
  16216. #ifndef WOLFSSL_NOSHA3_224
  16217. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16218. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16219. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  16220. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  16221. e.inLen = XSTRLEN(e.input);
  16222. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  16223. EVP_MD_CTX_init(&md_ctx);
  16224. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  16225. if (ret == WOLFSSL_SUCCESS) {
  16226. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16227. if (ret == WOLFSSL_SUCCESS)
  16228. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16229. }
  16230. EVP_MD_CTX_cleanup(&md_ctx);
  16231. if (ret != WOLFSSL_SUCCESS ||
  16232. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  16233. return -8607;
  16234. }
  16235. #endif /* WOLFSSL_NOSHA3_224 */
  16236. #ifndef WOLFSSL_NOSHA3_256
  16237. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16238. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16239. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  16240. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  16241. "\x1d\x18";
  16242. d.inLen = XSTRLEN(d.input);
  16243. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  16244. EVP_MD_CTX_init(&md_ctx);
  16245. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  16246. if (ret == WOLFSSL_SUCCESS) {
  16247. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  16248. if (ret == WOLFSSL_SUCCESS)
  16249. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16250. }
  16251. EVP_MD_CTX_cleanup(&md_ctx);
  16252. if (ret != WOLFSSL_SUCCESS ||
  16253. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  16254. return -8608;
  16255. }
  16256. #endif /* WOLFSSL_NOSHA3_256 */
  16257. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16258. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16259. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  16260. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  16261. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  16262. "\xaa\x7f\xc7";
  16263. e.inLen = XSTRLEN(e.input);
  16264. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  16265. EVP_MD_CTX_init(&md_ctx);
  16266. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  16267. if (ret == WOLFSSL_SUCCESS) {
  16268. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16269. if (ret == WOLFSSL_SUCCESS)
  16270. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16271. }
  16272. EVP_MD_CTX_cleanup(&md_ctx);
  16273. if (ret != WOLFSSL_SUCCESS ||
  16274. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  16275. return -8609;
  16276. }
  16277. #ifndef WOLFSSL_NOSHA3_512
  16278. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16279. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16280. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  16281. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  16282. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  16283. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  16284. "\xf2\x1d\xd1\x85";
  16285. f.inLen = XSTRLEN(f.input);
  16286. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  16287. EVP_MD_CTX_init(&md_ctx);
  16288. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  16289. if (ret == WOLFSSL_SUCCESS) {
  16290. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16291. if (ret == WOLFSSL_SUCCESS)
  16292. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16293. }
  16294. EVP_MD_CTX_cleanup(&md_ctx);
  16295. if (ret != WOLFSSL_SUCCESS ||
  16296. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  16297. return -8610;
  16298. }
  16299. #endif /* WOLFSSL_NOSHA3_512 */
  16300. #endif /* WOLFSSL_SHA3 */
  16301. #ifndef WC_NO_RNG
  16302. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  16303. return -8611;
  16304. #endif
  16305. #ifndef NO_MD5
  16306. c.input = "what do ya want for nothing?";
  16307. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  16308. "\x76";
  16309. c.inLen = XSTRLEN(c.input);
  16310. c.outLen = WC_MD5_DIGEST_SIZE;
  16311. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  16312. hash, 0) == NULL ||
  16313. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  16314. {
  16315. return -8612;
  16316. }
  16317. #endif /* NO_MD5 */
  16318. #ifndef NO_DES3
  16319. { /* des test */
  16320. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  16321. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16322. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16323. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16324. };
  16325. byte plain[24];
  16326. byte cipher[24];
  16327. const_DES_cblock key = {
  16328. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  16329. };
  16330. DES_cblock iv = {
  16331. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  16332. };
  16333. DES_key_schedule sched;
  16334. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  16335. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  16336. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  16337. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  16338. };
  16339. DES_key_sched(&key, &sched);
  16340. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  16341. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  16342. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  16343. return -8613;
  16344. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  16345. return -8614;
  16346. /* test changing iv */
  16347. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  16348. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  16349. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  16350. return -8615;
  16351. } /* end des test */
  16352. #endif /* NO_DES3 */
  16353. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16354. if (openssl_aes_test() != 0) {
  16355. return -8616;
  16356. }
  16357. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  16358. { /* evp_cipher test: EVP_aes_128_cbc */
  16359. #ifdef WOLFSSL_SMALL_STACK
  16360. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16361. #else
  16362. EVP_CIPHER_CTX ctx[1];
  16363. #endif
  16364. int idx, cipherSz, plainSz;
  16365. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16366. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16367. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16368. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16369. };
  16370. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  16371. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16372. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  16373. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  16374. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  16375. };
  16376. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  16377. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16378. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  16379. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  16380. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  16381. };
  16382. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16383. "0123456789abcdef "; /* align */
  16384. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16385. "1234567890abcdef "; /* align */
  16386. byte cipher[AES_BLOCK_SIZE * 4];
  16387. byte plain [AES_BLOCK_SIZE * 4];
  16388. #ifdef WOLFSSL_SMALL_STACK
  16389. if (ctx == NULL)
  16390. return MEMORY_E;
  16391. #endif
  16392. cipherSz = 0;
  16393. EVP_CIPHER_CTX_init(ctx);
  16394. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  16395. if (ret == WOLFSSL_SUCCESS) {
  16396. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  16397. if (ret == WOLFSSL_SUCCESS)
  16398. cipherSz += idx;
  16399. }
  16400. if (ret == WOLFSSL_SUCCESS) {
  16401. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  16402. if (ret == WOLFSSL_SUCCESS)
  16403. cipherSz += idx;
  16404. }
  16405. EVP_CIPHER_CTX_cleanup(ctx);
  16406. if (ret != WOLFSSL_SUCCESS)
  16407. return -8617;
  16408. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  16409. return -8618;
  16410. /* check partial decrypt (not enough padding for full block) */
  16411. plainSz = 0;
  16412. EVP_CIPHER_CTX_init(ctx);
  16413. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  16414. if (ret == WOLFSSL_SUCCESS) {
  16415. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  16416. if (ret == WOLFSSL_SUCCESS)
  16417. plainSz += idx;
  16418. }
  16419. if (ret == WOLFSSL_SUCCESS) {
  16420. /* this test should fail... not enough padding for full block */
  16421. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  16422. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  16423. ret = WOLFSSL_SUCCESS;
  16424. else
  16425. ret = -8619;
  16426. }
  16427. else
  16428. ret = -8620;
  16429. EVP_CIPHER_CTX_cleanup(ctx);
  16430. if (ret != WOLFSSL_SUCCESS)
  16431. return ret;
  16432. plainSz = 0;
  16433. EVP_CIPHER_CTX_init(ctx);
  16434. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  16435. if (ret == WOLFSSL_SUCCESS) {
  16436. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  16437. if (ret == WOLFSSL_SUCCESS)
  16438. plainSz += idx;
  16439. }
  16440. if (ret == WOLFSSL_SUCCESS) {
  16441. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  16442. if (ret == WOLFSSL_SUCCESS)
  16443. plainSz += idx;
  16444. }
  16445. EVP_CIPHER_CTX_cleanup(ctx);
  16446. if (ret != WOLFSSL_SUCCESS)
  16447. return -8621;
  16448. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  16449. return -8622;
  16450. cipherSz = 0;
  16451. EVP_CIPHER_CTX_init(ctx);
  16452. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  16453. if (ret == WOLFSSL_SUCCESS) {
  16454. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  16455. if (ret == WOLFSSL_SUCCESS)
  16456. cipherSz += idx;
  16457. }
  16458. if (ret == WOLFSSL_SUCCESS) {
  16459. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  16460. if (ret == WOLFSSL_SUCCESS)
  16461. cipherSz += idx;
  16462. }
  16463. EVP_CIPHER_CTX_cleanup(ctx);
  16464. if (ret != WOLFSSL_SUCCESS)
  16465. return -8623;
  16466. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  16467. return -8624;
  16468. #ifdef WOLFSSL_SMALL_STACK
  16469. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16470. #endif
  16471. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16472. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  16473. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16474. { /* evp_cipher test: EVP_aes_256_ecb*/
  16475. #ifdef WOLFSSL_SMALL_STACK
  16476. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16477. #else
  16478. EVP_CIPHER_CTX ctx[1];
  16479. #endif
  16480. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  16481. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16482. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16483. };
  16484. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  16485. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16486. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16487. };
  16488. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  16489. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16490. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16491. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16492. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16493. };
  16494. byte cipher[AES_BLOCK_SIZE * 4];
  16495. byte plain [AES_BLOCK_SIZE * 4];
  16496. #ifdef WOLFSSL_SMALL_STACK
  16497. if (ctx == NULL)
  16498. return MEMORY_E;
  16499. #endif
  16500. EVP_CIPHER_CTX_init(ctx);
  16501. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  16502. if (ret == WOLFSSL_SUCCESS)
  16503. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  16504. EVP_CIPHER_CTX_cleanup(ctx);
  16505. if (ret != 16)
  16506. return -8625;
  16507. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16508. return -8626;
  16509. EVP_CIPHER_CTX_init(ctx);
  16510. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  16511. if (ret == WOLFSSL_SUCCESS)
  16512. ret = EVP_Cipher(ctx, plain, cipher, 16);
  16513. EVP_CIPHER_CTX_cleanup(ctx);
  16514. if (ret != 16)
  16515. return -8627;
  16516. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16517. return -8628;
  16518. #ifdef WOLFSSL_SMALL_STACK
  16519. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16520. #endif
  16521. } /* end evp_cipher test */
  16522. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  16523. #define OPENSSL_TEST_ERROR (-10000)
  16524. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16525. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16526. {
  16527. /* Test: AES_encrypt/decrypt/set Key */
  16528. #ifdef WOLFSSL_SMALL_STACK
  16529. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16530. #ifdef HAVE_AES_DECRYPT
  16531. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16532. #endif
  16533. #else
  16534. AES_KEY enc[1];
  16535. #ifdef HAVE_AES_DECRYPT
  16536. AES_KEY dec[1];
  16537. #endif
  16538. #endif
  16539. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16540. {
  16541. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16542. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16543. };
  16544. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16545. {
  16546. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16547. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16548. };
  16549. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16550. {
  16551. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16552. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16553. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16554. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16555. };
  16556. byte plain[sizeof(msg)];
  16557. byte cipher[sizeof(msg)];
  16558. printf("openSSL extra test\n") ;
  16559. #ifdef WOLFSSL_SMALL_STACK
  16560. if (enc == NULL)
  16561. return MEMORY_E;
  16562. #ifdef HAVE_AES_DECRYPT
  16563. if (dec == NULL)
  16564. return MEMORY_E;
  16565. #endif
  16566. #endif
  16567. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16568. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16569. AES_encrypt(msg, cipher, enc);
  16570. #ifdef HAVE_AES_DECRYPT
  16571. AES_decrypt(cipher, plain, dec);
  16572. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16573. return OPENSSL_TEST_ERROR-60;
  16574. #endif /* HAVE_AES_DECRYPT */
  16575. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16576. return OPENSSL_TEST_ERROR-61;
  16577. #ifdef WOLFSSL_SMALL_STACK
  16578. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16579. #ifdef HAVE_AES_DECRYPT
  16580. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16581. #endif
  16582. #endif
  16583. }
  16584. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16585. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16586. #ifdef WOLFSSL_AES_COUNTER
  16587. {
  16588. byte plainBuff [64];
  16589. byte cipherBuff[64];
  16590. #ifdef WOLFSSL_AES_128
  16591. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16592. {
  16593. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16594. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16595. };
  16596. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16597. {
  16598. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16599. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16600. };
  16601. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16602. {
  16603. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16604. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16605. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16606. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16607. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16608. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16609. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16610. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16611. };
  16612. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16613. {
  16614. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16615. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16616. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16617. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16618. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16619. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16620. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16621. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16622. };
  16623. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16624. {
  16625. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16626. 0xc2
  16627. };
  16628. #endif /* WOLFSSL_AES_128 */
  16629. #ifdef WOLFSSL_AES_192
  16630. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16631. * NIST Special Publication 800-38A */
  16632. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16633. {
  16634. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16635. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16636. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16637. };
  16638. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16639. {
  16640. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16641. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16642. };
  16643. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16644. {
  16645. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16646. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16647. };
  16648. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16649. {
  16650. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16651. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16652. };
  16653. #endif /* WOLFSSL_AES_192 */
  16654. #ifdef WOLFSSL_AES_256
  16655. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16656. * NIST Special Publication 800-38A */
  16657. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16658. {
  16659. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16660. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16661. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16662. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16663. };
  16664. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16665. {
  16666. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16667. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16668. };
  16669. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16670. {
  16671. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16672. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16673. };
  16674. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16675. {
  16676. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16677. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16678. };
  16679. #endif /* WOLFSSL_AES_256 */
  16680. #ifdef WOLFSSL_SMALL_STACK
  16681. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16682. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16683. if ((en == NULL) || (de == NULL))
  16684. return MEMORY_E;
  16685. #else
  16686. EVP_CIPHER_CTX en[1];
  16687. EVP_CIPHER_CTX de[1];
  16688. #endif
  16689. #ifdef WOLFSSL_AES_128
  16690. #ifndef WOLFSSL_SMALL_STACK
  16691. EVP_CIPHER_CTX *p_en;
  16692. EVP_CIPHER_CTX *p_de;
  16693. #endif
  16694. EVP_CIPHER_CTX_init(en);
  16695. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16696. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16697. return -8629;
  16698. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16699. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16700. return -8630;
  16701. EVP_CIPHER_CTX_init(de);
  16702. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16703. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16704. return -8631;
  16705. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16706. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16707. return -8632;
  16708. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16709. return -8633;
  16710. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16711. return -8634;
  16712. #ifndef WOLFSSL_SMALL_STACK
  16713. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16714. if(p_en == NULL)return -8635;
  16715. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16716. if(p_de == NULL)return -8636;
  16717. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16718. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16719. return -8637;
  16720. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16721. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16722. return -8638;
  16723. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16724. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16725. return -8639;
  16726. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16727. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16728. return -8640;
  16729. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16730. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16731. #endif /* !WOLFSSL_SMALL_STACK */
  16732. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16733. return -8641;
  16734. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16735. return -8642;
  16736. EVP_CIPHER_CTX_init(en);
  16737. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16738. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16739. return -8643;
  16740. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16741. return -8644;
  16742. EVP_CIPHER_CTX_init(de);
  16743. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16744. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16745. return -8645;
  16746. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16747. return -8646;
  16748. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16749. return -8647;
  16750. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16751. return -8648;
  16752. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16753. return -8649;
  16754. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16755. return -8650;
  16756. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16757. return -8651;
  16758. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16759. return -8652;
  16760. #endif /* WOLFSSL_AES_128 */
  16761. #ifdef WOLFSSL_AES_192
  16762. EVP_CIPHER_CTX_init(en);
  16763. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16764. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16765. return -8653;
  16766. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16767. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16768. return -8654;
  16769. EVP_CIPHER_CTX_init(de);
  16770. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16771. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16772. return -8655;
  16773. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16774. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16775. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16776. return -8656;
  16777. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16778. return -8657;
  16779. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16780. return -8658;
  16781. #endif /* WOLFSSL_AES_192 */
  16782. #ifdef WOLFSSL_AES_256
  16783. EVP_CIPHER_CTX_init(en);
  16784. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16785. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16786. return -8659;
  16787. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16788. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16789. return -8660;
  16790. EVP_CIPHER_CTX_init(de);
  16791. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16792. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16793. return -8661;
  16794. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16795. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16796. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16797. return -8662;
  16798. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16799. return -8663;
  16800. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16801. return -8664;
  16802. #endif /* WOLFSSL_AES_256 */
  16803. #ifdef WOLFSSL_SMALL_STACK
  16804. wolfSSL_EVP_CIPHER_CTX_free(en);
  16805. wolfSSL_EVP_CIPHER_CTX_free(de);
  16806. #endif
  16807. }
  16808. #endif /* HAVE_AES_COUNTER */
  16809. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  16810. {
  16811. /* EVP_CipherUpdate test */
  16812. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16813. {
  16814. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16815. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16816. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16817. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16818. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16819. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16820. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16821. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16822. };
  16823. byte key[] = "0123456789abcdef "; /* align */
  16824. byte iv[] = "1234567890abcdef "; /* align */
  16825. byte cipher[AES_BLOCK_SIZE * 4];
  16826. byte plain [AES_BLOCK_SIZE * 4];
  16827. #ifdef WOLFSSL_SMALL_STACK
  16828. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16829. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16830. #else
  16831. EVP_CIPHER_CTX en[1];
  16832. EVP_CIPHER_CTX de[1];
  16833. #endif
  16834. int outlen ;
  16835. int total = 0;
  16836. #ifdef WOLFSSL_SMALL_STACK
  16837. if ((en == NULL) || (de == NULL))
  16838. return MEMORY_E;
  16839. #endif
  16840. EVP_CIPHER_CTX_init(en);
  16841. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16842. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16843. return -8665;
  16844. /* openSSL compatibility, if(inlen == 0)return 1; */
  16845. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16846. (byte*)cbcPlain, 0) != 1)
  16847. return -8666;
  16848. EVP_CIPHER_CTX_init(en);
  16849. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16850. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16851. return -8667;
  16852. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16853. (byte*)cbcPlain, 9) == 0)
  16854. return -8668;
  16855. if(outlen != 0)
  16856. return -8669;
  16857. total += outlen;
  16858. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16859. (byte*)&cbcPlain[9] , 9) == 0)
  16860. return -8670;
  16861. if(outlen != 16)
  16862. return -8671;
  16863. total += outlen;
  16864. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16865. return -8672;
  16866. if(outlen != 16)
  16867. return -8673;
  16868. total += outlen;
  16869. if(total != 32)
  16870. return -8674;
  16871. total = 0;
  16872. EVP_CIPHER_CTX_init(de);
  16873. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16874. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16875. return -8675;
  16876. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16877. return -8676;
  16878. if(outlen != 0)
  16879. return -8677;
  16880. total += outlen;
  16881. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16882. (byte*)&cipher[6], 12) == 0)
  16883. return -8678;
  16884. if(outlen != 0)
  16885. total += outlen;
  16886. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16887. (byte*)&cipher[6+12], 14) == 0)
  16888. return -8679;
  16889. if(outlen != 16)
  16890. return -8680;
  16891. total += outlen;
  16892. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16893. return -8681;
  16894. if(outlen != 2)
  16895. return -8682;
  16896. total += outlen;
  16897. if(total != 18)
  16898. return -8683;
  16899. if (XMEMCMP(plain, cbcPlain, 18))
  16900. return -8684;
  16901. total = 0;
  16902. EVP_CIPHER_CTX_init(en);
  16903. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  16904. (unsigned char*)key, (unsigned char*)iv) == 0)
  16905. return -8685;
  16906. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  16907. return -8686;
  16908. if(outlen != 0)
  16909. return -8687;
  16910. total += outlen;
  16911. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  16912. return -8688;
  16913. if(outlen != 16)
  16914. return -8689;
  16915. total += outlen;
  16916. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16917. return -8690;
  16918. if(outlen != 16)
  16919. return -8691;
  16920. total += outlen;
  16921. if(total != 32)
  16922. return 3438;
  16923. total = 0;
  16924. EVP_CIPHER_CTX_init(de);
  16925. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  16926. (unsigned char*)key, (unsigned char*)iv) == 0)
  16927. return -8692;
  16928. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16929. return -8693;
  16930. if(outlen != 0)
  16931. return -8694;
  16932. total += outlen;
  16933. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  16934. return -8695;
  16935. if(outlen != 0)
  16936. total += outlen;
  16937. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  16938. return -8696;
  16939. if(outlen != 16)
  16940. return -8697;
  16941. total += outlen;
  16942. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  16943. return -8698;
  16944. if(outlen != 2)
  16945. return -8699;
  16946. total += outlen;
  16947. if(total != 18)
  16948. return 3447;
  16949. if (XMEMCMP(plain, cbcPlain, 18))
  16950. return -8700;
  16951. if (EVP_CIPHER_key_length(NULL) != 0)
  16952. return -8701;
  16953. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  16954. return -8702;
  16955. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  16956. return -8703;
  16957. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  16958. return -8704;
  16959. EVP_CIPHER_CTX_init(en);
  16960. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  16961. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16962. return -8705;
  16963. EVP_CIPHER_CTX_init(en);
  16964. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16965. (unsigned char*)key, (unsigned char*)iv) == 0)
  16966. return -8706;
  16967. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16968. return -8707;
  16969. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16970. return -8708;
  16971. EVP_CIPHER_CTX_init(de);
  16972. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  16973. (unsigned char*)key, (unsigned char*)iv) == 0)
  16974. return -8709;
  16975. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16976. return -8710;
  16977. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16978. return -8711;
  16979. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  16980. return -8712;
  16981. EVP_CIPHER_CTX_init(en);
  16982. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16983. (unsigned char*)key, (unsigned char*)iv);
  16984. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  16985. return -8713;
  16986. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  16987. return -8714;
  16988. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  16989. return -8715;
  16990. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  16991. return -8716;
  16992. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  16993. return -8717;
  16994. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  16995. EVP_CIPHER_CTX_set_flags(en, 42);
  16996. if (en->flags != 42)
  16997. return -8718;
  16998. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  16999. return -8719;
  17000. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  17001. return -8720;
  17002. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  17003. return -8721;
  17004. #ifdef WOLFSSL_SMALL_STACK
  17005. wolfSSL_EVP_CIPHER_CTX_free(en);
  17006. wolfSSL_EVP_CIPHER_CTX_free(de);
  17007. #endif
  17008. }
  17009. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17010. #endif /* ifndef NO_AES */
  17011. return 0;
  17012. }
  17013. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  17014. {
  17015. int ret = 0;
  17016. #if !defined(NO_SHA256) && !defined(NO_SHA)
  17017. WOLFSSL_EVP_MD_CTX* ctx;
  17018. WOLFSSL_EVP_MD_CTX* ctx2;
  17019. ctx = EVP_MD_CTX_create();
  17020. ctx2 = EVP_MD_CTX_create();
  17021. ret = EVP_DigestInit(ctx, EVP_sha256());
  17022. if (ret != SSL_SUCCESS) {
  17023. ret = -8800;
  17024. goto openSSL_evpMD_test_done;
  17025. }
  17026. ret = EVP_MD_CTX_copy(ctx2, ctx);
  17027. if (ret != SSL_SUCCESS) {
  17028. ret = -8801;
  17029. goto openSSL_evpMD_test_done;
  17030. }
  17031. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  17032. ret = -8802;
  17033. goto openSSL_evpMD_test_done;
  17034. }
  17035. ret = EVP_DigestInit(ctx, EVP_sha1());
  17036. if (ret != SSL_SUCCESS) {
  17037. ret = -8803;
  17038. goto openSSL_evpMD_test_done;
  17039. }
  17040. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  17041. ret = -8804;
  17042. goto openSSL_evpMD_test_done;
  17043. }
  17044. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  17045. if (ret != SSL_SUCCESS) {
  17046. ret = -8805;
  17047. goto openSSL_evpMD_test_done;
  17048. }
  17049. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  17050. ret = -8806;
  17051. goto openSSL_evpMD_test_done;
  17052. }
  17053. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  17054. ret = -8807;
  17055. goto openSSL_evpMD_test_done;
  17056. }
  17057. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  17058. ret = -8808;
  17059. goto openSSL_evpMD_test_done;
  17060. }
  17061. if (EVP_add_digest(NULL) != 0) {
  17062. ret = -8809;
  17063. goto openSSL_evpMD_test_done;
  17064. }
  17065. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  17066. ret = -8810;
  17067. goto openSSL_evpMD_test_done;
  17068. }
  17069. ret = 0; /* got to success state without jumping to end with a fail */
  17070. openSSL_evpMD_test_done:
  17071. EVP_MD_CTX_destroy(ctx);
  17072. EVP_MD_CTX_destroy(ctx2);
  17073. #endif /* NO_SHA256 */
  17074. return ret;
  17075. }
  17076. #ifdef DEBUG_SIGN
  17077. static void show(const char *title, const char *p, unsigned int s) {
  17078. char* i;
  17079. printf("%s: ", title);
  17080. for (i = p;
  17081. i < p + s;
  17082. printf("%c", *i), i++);
  17083. printf("\n");
  17084. }
  17085. #else
  17086. #define show(a,b,c)
  17087. #endif
  17088. #define FOURK_BUFF 4096
  17089. #define ERR_BASE_PKEY -5000
  17090. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  17091. {
  17092. int ret = 0;
  17093. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  17094. byte* prvTmp;
  17095. byte* pubTmp;
  17096. int prvBytes;
  17097. int pubBytes;
  17098. RSA *prvRsa = NULL;
  17099. RSA *pubRsa = NULL;
  17100. EVP_PKEY *prvPkey = NULL;
  17101. EVP_PKEY *pubPkey = NULL;
  17102. EVP_PKEY_CTX *enc = NULL;
  17103. EVP_PKEY_CTX *dec = NULL;
  17104. byte in[] = TEST_STRING;
  17105. byte out[256];
  17106. size_t outlen;
  17107. size_t keySz;
  17108. byte plain[256];
  17109. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  17110. XFILE keyFile;
  17111. XFILE keypubFile;
  17112. char cliKey[] = "./certs/client-key.der";
  17113. char cliKeypub[] = "./certs/client-keyPub.der";
  17114. #endif
  17115. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17116. if (prvTmp == NULL)
  17117. return ERR_BASE_PKEY-1;
  17118. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17119. if (pubTmp == NULL) {
  17120. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17121. return ERR_BASE_PKEY-2;
  17122. }
  17123. #ifdef USE_CERT_BUFFERS_1024
  17124. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  17125. prvBytes = sizeof_client_key_der_1024;
  17126. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17127. pubBytes = sizeof_client_keypub_der_1024;
  17128. #elif defined(USE_CERT_BUFFERS_2048)
  17129. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  17130. prvBytes = sizeof_client_key_der_2048;
  17131. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17132. pubBytes = sizeof_client_keypub_der_2048;
  17133. #else
  17134. keyFile = XFOPEN(cliKey, "rb");
  17135. if (!keyFile) {
  17136. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17137. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17138. err_sys("can't open ./certs/client-key.der, "
  17139. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  17140. return ERR_BASE_PKEY-3;
  17141. }
  17142. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  17143. XFCLOSE(keyFile);
  17144. keypubFile = XFOPEN(cliKeypub, "rb");
  17145. if (!keypubFile) {
  17146. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17147. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17148. err_sys("can't open ./certs/client-cert.der, "
  17149. "Please run from wolfSSL home dir", -4);
  17150. return ERR_BASE_PKEY-4;
  17151. }
  17152. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  17153. XFCLOSE(keypubFile);
  17154. #endif /* USE_CERT_BUFFERS */
  17155. prvRsa = wolfSSL_RSA_new();
  17156. pubRsa = wolfSSL_RSA_new();
  17157. if((prvRsa == NULL) || (pubRsa == NULL)){
  17158. printf("error with RSA_new\n");
  17159. ret = ERR_BASE_PKEY-10;
  17160. goto openssl_pkey0_test_done;
  17161. }
  17162. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  17163. if(ret != SSL_SUCCESS){
  17164. printf("error with RSA_LoadDer_ex\n");
  17165. ret = ERR_BASE_PKEY-11;
  17166. goto openssl_pkey0_test_done;
  17167. }
  17168. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  17169. if(ret != SSL_SUCCESS){
  17170. printf("error with RSA_LoadDer_ex\n");
  17171. ret = ERR_BASE_PKEY-12;
  17172. goto openssl_pkey0_test_done;
  17173. }
  17174. keySz = (size_t)RSA_size(pubRsa);
  17175. prvPkey = wolfSSL_EVP_PKEY_new();
  17176. pubPkey = wolfSSL_EVP_PKEY_new();
  17177. if((prvPkey == NULL) || (pubPkey == NULL)){
  17178. printf("error with PKEY_new\n");
  17179. ret = ERR_BASE_PKEY-13;
  17180. goto openssl_pkey0_test_done;
  17181. }
  17182. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  17183. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  17184. if(ret != 2){
  17185. printf("error with PKEY_set1_RSA\n");
  17186. ret = ERR_BASE_PKEY-14;
  17187. goto openssl_pkey0_test_done;
  17188. }
  17189. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  17190. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  17191. if((dec == NULL)||(enc==NULL)){
  17192. printf("error with EVP_PKEY_CTX_new\n");
  17193. ret = ERR_BASE_PKEY-15;
  17194. goto openssl_pkey0_test_done;
  17195. }
  17196. ret = EVP_PKEY_decrypt_init(dec);
  17197. if (ret != 1) {
  17198. printf("error with decrypt init\n");
  17199. ret = ERR_BASE_PKEY-16;
  17200. goto openssl_pkey0_test_done;
  17201. }
  17202. ret = EVP_PKEY_encrypt_init(enc);
  17203. if (ret != 1) {
  17204. printf("error with encrypt init\n");
  17205. ret = ERR_BASE_PKEY-17;
  17206. goto openssl_pkey0_test_done;
  17207. }
  17208. XMEMSET(out, 0, sizeof(out));
  17209. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  17210. if (ret != 1) {
  17211. printf("error encrypting msg\n");
  17212. ret = ERR_BASE_PKEY-18;
  17213. goto openssl_pkey0_test_done;
  17214. }
  17215. show("encrypted msg", out, outlen);
  17216. XMEMSET(plain, 0, sizeof(plain));
  17217. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  17218. if (ret != 1) {
  17219. printf("error decrypting msg\n");
  17220. ret = ERR_BASE_PKEY-19;
  17221. goto openssl_pkey0_test_done;
  17222. }
  17223. show("decrypted msg", plain, outlen);
  17224. /* RSA_PKCS1_OAEP_PADDING test */
  17225. ret = EVP_PKEY_decrypt_init(dec);
  17226. if (ret != 1) {
  17227. printf("error with decrypt init\n");
  17228. ret = ERR_BASE_PKEY-30;
  17229. goto openssl_pkey0_test_done;
  17230. }
  17231. ret = EVP_PKEY_encrypt_init(enc);
  17232. if (ret != 1) {
  17233. printf("error with encrypt init\n");
  17234. ret = ERR_BASE_PKEY-31;
  17235. goto openssl_pkey0_test_done;
  17236. }
  17237. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  17238. printf("first set rsa padding error\n");
  17239. ret = ERR_BASE_PKEY-32;
  17240. goto openssl_pkey0_test_done;
  17241. }
  17242. #ifndef HAVE_FIPS
  17243. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  17244. printf("second set rsa padding error\n");
  17245. ret = ERR_BASE_PKEY-33;
  17246. goto openssl_pkey0_test_done;
  17247. }
  17248. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  17249. printf("third set rsa padding error\n");
  17250. ret = ERR_BASE_PKEY-34;
  17251. goto openssl_pkey0_test_done;
  17252. }
  17253. #endif
  17254. XMEMSET(out, 0, sizeof(out));
  17255. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  17256. if (ret != 1) {
  17257. printf("error encrypting msg\n");
  17258. ret = ERR_BASE_PKEY-35;
  17259. goto openssl_pkey0_test_done;
  17260. }
  17261. show("encrypted msg", out, outlen);
  17262. XMEMSET(plain, 0, sizeof(plain));
  17263. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  17264. if (ret != 1) {
  17265. printf("error decrypting msg\n");
  17266. ret = ERR_BASE_PKEY-36;
  17267. goto openssl_pkey0_test_done;
  17268. }
  17269. show("decrypted msg", plain, outlen);
  17270. ret = 0; /* made it to this point without error then set success */
  17271. openssl_pkey0_test_done:
  17272. wolfSSL_RSA_free(prvRsa);
  17273. wolfSSL_RSA_free(pubRsa);
  17274. EVP_PKEY_free(pubPkey);
  17275. EVP_PKEY_free(prvPkey);
  17276. EVP_PKEY_CTX_free(dec);
  17277. EVP_PKEY_CTX_free(enc);
  17278. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17279. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17280. #endif /* NO_RSA */
  17281. return ret;
  17282. }
  17283. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  17284. {
  17285. int ret = 0;
  17286. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  17287. !defined(NO_SHA)
  17288. EVP_PKEY_CTX* dec = NULL;
  17289. EVP_PKEY_CTX* enc = NULL;
  17290. EVP_PKEY* pubKey = NULL;
  17291. EVP_PKEY* prvKey = NULL;
  17292. X509* x509 = NULL;
  17293. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  17294. const unsigned char* clikey;
  17295. long cliKeySz;
  17296. size_t outlen;
  17297. int keyLenBits = 2048;
  17298. #ifdef WOLFSSL_SMALL_STACK
  17299. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17300. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17301. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17302. if ((tmp == NULL) ||
  17303. (cipher == NULL) ||
  17304. (plain == NULL)) {
  17305. ret = -9015;
  17306. goto openssl_pkey1_test_done;
  17307. }
  17308. #else
  17309. unsigned char tmp[FOURK_BUF];
  17310. unsigned char cipher[RSA_TEST_BYTES];
  17311. unsigned char plain[RSA_TEST_BYTES];
  17312. #endif
  17313. #if defined(USE_CERT_BUFFERS_1024)
  17314. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17315. cliKeySz = (long)sizeof_client_key_der_1024;
  17316. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  17317. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  17318. keyLenBits = 1024;
  17319. #elif defined(USE_CERT_BUFFERS_2048)
  17320. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17321. cliKeySz = (long)sizeof_client_key_der_2048;
  17322. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  17323. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  17324. #elif defined(USE_CERT_BUFFERS_3072)
  17325. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  17326. cliKeySz = (long)sizeof_client_key_der_3072;
  17327. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  17328. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  17329. keyLenBits = 3072;
  17330. #elif defined(USE_CERT_BUFFERS_4096)
  17331. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  17332. cliKeySz = (long)sizeof_client_key_der_4096;
  17333. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  17334. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  17335. keyLenBits = 4096;
  17336. #else
  17337. XFILE f;
  17338. f = XFOPEN(clientKey, "rb");
  17339. if (!f) {
  17340. err_sys("can't open ./certs/client-key.der, "
  17341. "Please run from wolfSSL home dir", -41);
  17342. ret = -9000;
  17343. goto openssl_pkey1_test_done;
  17344. }
  17345. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  17346. XFCLOSE(f);
  17347. /* using existing wolfSSL api to get public and private key */
  17348. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  17349. #endif /* USE_CERT_BUFFERS */
  17350. clikey = tmp;
  17351. if ((prvKey = EVP_PKEY_new()) == NULL) {
  17352. ret = -9001;
  17353. goto openssl_pkey1_test_done;
  17354. }
  17355. EVP_PKEY_free(prvKey);
  17356. prvKey = NULL;
  17357. if (x509 == NULL) {
  17358. ret = -9002;
  17359. goto openssl_pkey1_test_done;
  17360. }
  17361. pubKey = X509_get_pubkey(x509);
  17362. if (pubKey == NULL) {
  17363. ret = -9003;
  17364. goto openssl_pkey1_test_done;
  17365. }
  17366. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  17367. if (prvKey == NULL) {
  17368. ret = -9004;
  17369. goto openssl_pkey1_test_done;
  17370. }
  17371. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  17372. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  17373. ret = -9005;
  17374. goto openssl_pkey1_test_done;
  17375. }
  17376. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  17377. ret = -9006;
  17378. goto openssl_pkey1_test_done;
  17379. }
  17380. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  17381. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  17382. if (dec == NULL || enc == NULL) {
  17383. ret = -9007;
  17384. goto openssl_pkey1_test_done;
  17385. }
  17386. if (EVP_PKEY_decrypt_init(dec) != 1) {
  17387. ret = -9008;
  17388. goto openssl_pkey1_test_done;
  17389. }
  17390. if (EVP_PKEY_encrypt_init(enc) != 1) {
  17391. ret = -9009;
  17392. goto openssl_pkey1_test_done;
  17393. }
  17394. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  17395. ret = -9010;
  17396. goto openssl_pkey1_test_done;
  17397. }
  17398. #ifndef HAVE_FIPS
  17399. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  17400. ret = -9011;
  17401. goto openssl_pkey1_test_done;
  17402. }
  17403. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  17404. ret = -9012;
  17405. goto openssl_pkey1_test_done;
  17406. }
  17407. #endif
  17408. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  17409. outlen = keyLenBits/8;
  17410. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  17411. ret = -9013;
  17412. goto openssl_pkey1_test_done;
  17413. }
  17414. XMEMSET(plain, 0, RSA_TEST_BYTES);
  17415. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  17416. ret = -9014;
  17417. goto openssl_pkey1_test_done;
  17418. }
  17419. openssl_pkey1_test_done:
  17420. if (pubKey != NULL) {
  17421. EVP_PKEY_free(pubKey);
  17422. }
  17423. if (prvKey != NULL) {
  17424. EVP_PKEY_free(prvKey);
  17425. }
  17426. if (dec != NULL) {
  17427. EVP_PKEY_CTX_free(dec);
  17428. }
  17429. if (enc != NULL) {
  17430. EVP_PKEY_CTX_free(enc);
  17431. }
  17432. if (x509 != NULL) {
  17433. X509_free(x509);
  17434. }
  17435. #ifdef WOLFSSL_SMALL_STACK
  17436. if (tmp != NULL)
  17437. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17438. if (cipher != NULL)
  17439. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17440. if (plain != NULL)
  17441. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17442. #endif
  17443. #endif
  17444. return ret;
  17445. }
  17446. #define ERR_BASE_EVPSIG -5100
  17447. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  17448. {
  17449. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  17450. byte* prvTmp;
  17451. byte* pubTmp;
  17452. int prvBytes;
  17453. int pubBytes;
  17454. RSA *prvRsa;
  17455. RSA *pubRsa;
  17456. EVP_PKEY *prvPkey;
  17457. EVP_PKEY *pubPkey;
  17458. EVP_MD_CTX* sign;
  17459. EVP_MD_CTX* verf;
  17460. char msg[] = "see spot run";
  17461. unsigned char sig[256];
  17462. unsigned int sigSz;
  17463. const void* pt;
  17464. unsigned int count;
  17465. int ret, ret1, ret2;
  17466. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  17467. XFILE keyFile;
  17468. XFILE keypubFile;
  17469. char cliKey[] = "./certs/client-key.der";
  17470. char cliKeypub[] = "./certs/client-keyPub.der";
  17471. #endif
  17472. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17473. if (prvTmp == NULL)
  17474. return ERR_BASE_EVPSIG-1;
  17475. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17476. if (pubTmp == NULL) {
  17477. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17478. return ERR_BASE_EVPSIG-2;
  17479. }
  17480. #ifdef USE_CERT_BUFFERS_1024
  17481. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  17482. prvBytes = sizeof_client_key_der_1024;
  17483. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17484. pubBytes = sizeof_client_keypub_der_1024;
  17485. #elif defined(USE_CERT_BUFFERS_2048)
  17486. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  17487. prvBytes = sizeof_client_key_der_2048;
  17488. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17489. pubBytes = sizeof_client_keypub_der_2048;
  17490. #else
  17491. keyFile = XFOPEN(cliKey, "rb");
  17492. if (!keyFile) {
  17493. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17494. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17495. err_sys("can't open ./certs/client-key.der, "
  17496. "Please run from wolfSSL home dir", -40);
  17497. return ERR_BASE_EVPSIG-3;
  17498. }
  17499. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  17500. XFCLOSE(keyFile);
  17501. keypubFile = XFOPEN(cliKeypub, "rb");
  17502. if (!keypubFile) {
  17503. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17504. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17505. err_sys("can't open ./certs/client-cert.der, "
  17506. "Please run from wolfSSL home dir", -41);
  17507. return ERR_BASE_EVPSIG-4;
  17508. }
  17509. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  17510. XFCLOSE(keypubFile);
  17511. #endif /* USE_CERT_BUFFERS */
  17512. prvRsa = wolfSSL_RSA_new();
  17513. pubRsa = wolfSSL_RSA_new();
  17514. if((prvRsa == NULL) || (pubRsa == NULL)){
  17515. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17516. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17517. err_sys("ERROR with RSA_new", -9100);
  17518. return ERR_BASE_EVPSIG-5;
  17519. }
  17520. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  17521. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  17522. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  17523. printf("error with RSA_LoadDer_ex\n");
  17524. return ERR_BASE_EVPSIG-6;
  17525. }
  17526. prvPkey = wolfSSL_EVP_PKEY_new();
  17527. pubPkey = wolfSSL_EVP_PKEY_new();
  17528. if((prvPkey == NULL) || (pubPkey == NULL)){
  17529. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17530. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17531. printf("error with KEY_new\n");
  17532. return ERR_BASE_EVPSIG-7;
  17533. }
  17534. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  17535. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  17536. if((ret1 != 1) || (ret2 != 1)){
  17537. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17538. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17539. printf("error with EVP_PKEY_set1_RSA\n");
  17540. return ERR_BASE_EVPSIG-8;
  17541. }
  17542. /****************** sign and verify *******************/
  17543. sign = EVP_MD_CTX_create();
  17544. verf = EVP_MD_CTX_create();
  17545. if((sign == NULL)||(verf == NULL)){
  17546. printf("error with EVP_MD_CTX_create\n");
  17547. EVP_MD_CTX_destroy(sign);
  17548. EVP_MD_CTX_destroy(verf);
  17549. return ERR_BASE_EVPSIG-10;
  17550. }
  17551. ret = EVP_SignInit(sign, EVP_sha1());
  17552. if (ret != SSL_SUCCESS){
  17553. printf("error with EVP_SignInit\n");
  17554. EVP_MD_CTX_destroy(sign);
  17555. EVP_MD_CTX_destroy(verf);
  17556. return ERR_BASE_EVPSIG-11;
  17557. }
  17558. count = sizeof(msg);
  17559. show("message = ", (char *)msg, count);
  17560. /* sign */
  17561. XMEMSET(sig, 0, sizeof(sig));
  17562. pt = (const void*)msg;
  17563. ret1 = EVP_SignUpdate(sign, pt, count);
  17564. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  17565. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  17566. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17567. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17568. EVP_MD_CTX_destroy(sign);
  17569. EVP_MD_CTX_destroy(verf);
  17570. printf("error with EVP_MD_CTX_create\n");
  17571. return ERR_BASE_EVPSIG-12;
  17572. }
  17573. show("signature = ", (char *)sig, sigSz);
  17574. /* verify */
  17575. pt = (const void*)msg;
  17576. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  17577. ret2 = EVP_VerifyUpdate(verf, pt, count);
  17578. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  17579. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17580. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17581. EVP_MD_CTX_destroy(sign);
  17582. EVP_MD_CTX_destroy(verf);
  17583. printf("error with EVP_Verify\n");
  17584. return ERR_BASE_EVPSIG-13;
  17585. }
  17586. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  17587. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17588. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17589. EVP_MD_CTX_destroy(sign);
  17590. EVP_MD_CTX_destroy(verf);
  17591. printf("error with EVP_VerifyFinal\n");
  17592. return ERR_BASE_EVPSIG-14;
  17593. }
  17594. /* expect fail without update */
  17595. EVP_VerifyInit(verf, EVP_sha1());
  17596. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  17597. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17598. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17599. EVP_MD_CTX_destroy(sign);
  17600. EVP_MD_CTX_destroy(verf);
  17601. printf("EVP_VerifyInit without update not detected\n");
  17602. return ERR_BASE_EVPSIG-15;
  17603. }
  17604. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17605. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17606. EVP_MD_CTX_destroy(sign);
  17607. EVP_MD_CTX_destroy(verf);
  17608. wolfSSL_RSA_free(prvRsa);
  17609. wolfSSL_RSA_free(pubRsa);
  17610. EVP_PKEY_free(pubPkey);
  17611. EVP_PKEY_free(prvPkey);
  17612. #endif /* NO_RSA */
  17613. return 0;
  17614. }
  17615. #endif /* OPENSSL_EXTRA */
  17616. #ifndef NO_PWDBASED
  17617. #ifdef HAVE_SCRYPT
  17618. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  17619. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  17620. {
  17621. #ifdef HAVE_FIPS
  17622. /* RFC 7914 test vector keys are too short for FIPS. */
  17623. #else
  17624. int ret;
  17625. byte derived[64];
  17626. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  17627. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  17628. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  17629. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  17630. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  17631. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  17632. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  17633. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  17634. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  17635. };
  17636. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17637. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  17638. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  17639. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  17640. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  17641. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  17642. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  17643. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  17644. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  17645. };
  17646. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17647. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17648. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  17649. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  17650. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  17651. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  17652. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  17653. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  17654. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  17655. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  17656. };
  17657. #endif
  17658. #ifdef SCRYPT_TEST_ALL
  17659. /* Test case is very slow.
  17660. * Use for confirmation after code change or new platform.
  17661. */
  17662. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17663. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  17664. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  17665. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  17666. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  17667. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  17668. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  17669. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  17670. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  17671. };
  17672. #endif
  17673. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  17674. if (ret != 0)
  17675. return -9200;
  17676. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  17677. return -9201;
  17678. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  17679. sizeof(verify2));
  17680. if (ret != 0)
  17681. return -9202;
  17682. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17683. return -9203;
  17684. /* Don't run these test on embedded, since they use large mallocs */
  17685. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17686. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17687. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  17688. if (ret != 0)
  17689. return -9204;
  17690. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  17691. return -9205;
  17692. #ifdef SCRYPT_TEST_ALL
  17693. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17694. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  17695. if (ret != 0)
  17696. return -9206;
  17697. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  17698. return -9207;
  17699. #endif
  17700. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  17701. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  17702. 8, 16, sizeof(verify2));
  17703. if (ret != 0)
  17704. return -9208;
  17705. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17706. return -9209;
  17707. #endif /* !HAVE_FIPS */
  17708. return 0;
  17709. }
  17710. #endif
  17711. #ifdef HAVE_PKCS12
  17712. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  17713. {
  17714. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  17715. 0x00, 0x00 };
  17716. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  17717. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  17718. 0x00, 0x67, 0x00, 0x00 };
  17719. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  17720. byte derived[64];
  17721. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17722. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  17723. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  17724. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  17725. };
  17726. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17727. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  17728. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  17729. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  17730. };
  17731. int id = 1;
  17732. int kLen = 24;
  17733. int iterations = 1;
  17734. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  17735. iterations, kLen, WC_SHA256, id);
  17736. if (ret < 0)
  17737. return -9300;
  17738. if (XMEMCMP(derived, verify, kLen) != 0)
  17739. return -9301;
  17740. iterations = 1000;
  17741. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  17742. iterations, kLen, WC_SHA256, id);
  17743. if (ret < 0)
  17744. return -9302;
  17745. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  17746. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  17747. if (ret < 0)
  17748. return -9303;
  17749. if (XMEMCMP(derived, verify2, 24) != 0)
  17750. return -9304;
  17751. return 0;
  17752. }
  17753. #endif /* HAVE_PKCS12 */
  17754. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17755. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  17756. {
  17757. char passwd[] = "passwordpassword";
  17758. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17759. int iterations = 2048;
  17760. int kLen = 24;
  17761. byte derived[64];
  17762. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17763. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  17764. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  17765. };
  17766. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  17767. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  17768. if (ret != 0)
  17769. return ret;
  17770. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17771. return -9400;
  17772. return 0;
  17773. }
  17774. #endif /* HAVE_PBKDF2 && !NO_SHA256 */
  17775. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17776. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  17777. {
  17778. char passwd[] = "password";
  17779. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17780. int iterations = 1000;
  17781. int kLen = 16;
  17782. byte derived[16];
  17783. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17784. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  17785. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  17786. };
  17787. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  17788. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  17789. HEAP_HINT);
  17790. if (ret != 0)
  17791. return ret;
  17792. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17793. return -9500;
  17794. return 0;
  17795. }
  17796. #endif /* HAVE_PBKDF2 && !NO_SHA */
  17797. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  17798. {
  17799. int ret = 0;
  17800. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17801. ret = pbkdf1_test();
  17802. if (ret != 0)
  17803. return ret;
  17804. #endif
  17805. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17806. ret = pbkdf2_test();
  17807. if (ret != 0)
  17808. return ret;
  17809. #endif
  17810. #ifdef HAVE_PKCS12
  17811. ret = pkcs12_test();
  17812. if (ret != 0)
  17813. return ret;
  17814. #endif
  17815. #ifdef HAVE_SCRYPT
  17816. ret = scrypt_test();
  17817. if (ret != 0)
  17818. return ret;
  17819. #endif
  17820. return ret;
  17821. }
  17822. #endif /* NO_PWDBASED */
  17823. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  17824. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void)
  17825. {
  17826. int ret = 0;
  17827. #if !defined(NO_SHA) || !defined(NO_SHA256)
  17828. int L = 42;
  17829. byte okm1[42];
  17830. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17831. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17832. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  17833. #ifndef HAVE_FIPS
  17834. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17835. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  17836. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  17837. 0xf8, 0xf9 };
  17838. #endif
  17839. #ifndef NO_SHA
  17840. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  17841. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  17842. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  17843. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  17844. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  17845. 0x49, 0x18 };
  17846. #ifndef HAVE_FIPS
  17847. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  17848. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  17849. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  17850. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  17851. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  17852. 0xf8, 0x96 };
  17853. #endif
  17854. #endif /* !NO_SHA */
  17855. #ifndef NO_SHA256
  17856. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  17857. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  17858. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  17859. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  17860. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  17861. 0x96, 0xc8 };
  17862. #ifndef HAVE_FIPS
  17863. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  17864. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  17865. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  17866. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  17867. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  17868. 0x58, 0x65 };
  17869. #endif
  17870. #endif /* !NO_SHA256 */
  17871. #ifndef NO_SHA
  17872. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17873. if (ret != 0)
  17874. return -9700;
  17875. if (XMEMCMP(okm1, res1, L) != 0)
  17876. return -9701;
  17877. #ifndef HAVE_FIPS
  17878. /* fips can't have key size under 14 bytes, salt is key too */
  17879. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  17880. if (ret != 0)
  17881. return -9702;
  17882. if (XMEMCMP(okm1, res2, L) != 0)
  17883. return -9703;
  17884. #endif /* HAVE_FIPS */
  17885. #endif /* !NO_SHA */
  17886. #ifndef NO_SHA256
  17887. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17888. if (ret != 0)
  17889. return -9704;
  17890. if (XMEMCMP(okm1, res3, L) != 0)
  17891. return -9705;
  17892. #ifndef HAVE_FIPS
  17893. /* fips can't have key size under 14 bytes, salt is key too */
  17894. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  17895. if (ret != 0)
  17896. return -9706;
  17897. if (XMEMCMP(okm1, res4, L) != 0)
  17898. return -9707;
  17899. #endif /* HAVE_FIPS */
  17900. #endif /* !NO_SHA256 */
  17901. #endif /* !NO_SHA || !NO_SHA256 */
  17902. return ret;
  17903. }
  17904. #endif /* HAVE_HKDF */
  17905. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  17906. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  17907. {
  17908. int ret;
  17909. byte kek[128];
  17910. #ifndef NO_SHA
  17911. /* SHA-1, COUNT = 0
  17912. * shared secret length: 192
  17913. * SharedInfo length: 0
  17914. * key data length: 128
  17915. */
  17916. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  17917. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  17918. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  17919. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  17920. };
  17921. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17922. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  17923. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  17924. };
  17925. #endif
  17926. #ifndef NO_SHA256
  17927. /* SHA-256, COUNT = 3
  17928. * shared secret length: 192
  17929. * SharedInfo length: 0
  17930. * key data length: 128
  17931. */
  17932. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  17933. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  17934. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  17935. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  17936. };
  17937. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17938. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  17939. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  17940. };
  17941. #endif
  17942. #ifdef WOLFSSL_SHA512
  17943. /* SHA-512, COUNT = 0
  17944. * shared secret length: 192
  17945. * SharedInfo length: 0
  17946. * key data length: 128
  17947. */
  17948. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  17949. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  17950. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  17951. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  17952. };
  17953. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17954. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  17955. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  17956. };
  17957. /* SHA-512, COUNT = 0
  17958. * shared secret length: 521
  17959. * SharedInfo length: 128
  17960. * key data length: 1024
  17961. */
  17962. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  17963. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  17964. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  17965. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  17966. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  17967. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  17968. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  17969. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  17970. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  17971. 0x5a, 0x2d
  17972. };
  17973. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  17974. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  17975. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  17976. };
  17977. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17978. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  17979. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  17980. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  17981. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  17982. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  17983. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  17984. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  17985. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  17986. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  17987. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  17988. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  17989. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  17990. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  17991. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  17992. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  17993. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  17994. };
  17995. #endif
  17996. #ifndef NO_SHA
  17997. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  17998. kek, sizeof(verify));
  17999. if (ret != 0)
  18000. return -9800;
  18001. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  18002. return -9801;
  18003. #endif
  18004. #ifndef NO_SHA256
  18005. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  18006. kek, sizeof(verify2));
  18007. if (ret != 0)
  18008. return -9802;
  18009. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  18010. return -9803;
  18011. #endif
  18012. #ifdef WOLFSSL_SHA512
  18013. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  18014. kek, sizeof(verify3));
  18015. if (ret != 0)
  18016. return -9804;
  18017. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  18018. return -9805;
  18019. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  18020. sizeof(info4), kek, sizeof(verify4));
  18021. if (ret != 0)
  18022. return -9806;
  18023. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  18024. return -9807;
  18025. #endif
  18026. return 0;
  18027. }
  18028. #endif /* HAVE_X963_KDF */
  18029. #ifdef HAVE_ECC
  18030. /* size to use for ECC key gen tests */
  18031. #ifndef ECC_KEYGEN_SIZE
  18032. #ifndef NO_ECC256
  18033. #define ECC_KEYGEN_SIZE 32
  18034. #elif defined(HAVE_ECC384)
  18035. #define ECC_KEYGEN_SIZE 48
  18036. #elif defined(HAVE_ECC224)
  18037. #define ECC_KEYGEN_SIZE 28
  18038. #elif defined(HAVE_ECC521)
  18039. #define ECC_KEYGEN_SIZE 66
  18040. #else
  18041. #error No ECC keygen size defined for test
  18042. #endif
  18043. #endif
  18044. #ifdef BENCH_EMBEDDED
  18045. #define ECC_SHARED_SIZE 128
  18046. #else
  18047. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  18048. #endif
  18049. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  18050. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  18051. #ifndef NO_ECC_VECTOR_TEST
  18052. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  18053. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  18054. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  18055. #define HAVE_ECC_VECTOR_TEST
  18056. #endif
  18057. #endif
  18058. #ifdef HAVE_ECC_VECTOR_TEST
  18059. typedef struct eccVector {
  18060. const char* msg; /* SHA-1 Encoded Message */
  18061. const char* Qx;
  18062. const char* Qy;
  18063. const char* d; /* Private Key */
  18064. const char* R;
  18065. const char* S;
  18066. const char* curveName;
  18067. word32 msgLen;
  18068. word32 keySize;
  18069. #ifndef NO_ASN
  18070. const byte* r;
  18071. word32 rSz;
  18072. const byte* s;
  18073. word32 sSz;
  18074. #endif
  18075. } eccVector;
  18076. static int ecc_test_vector_item(const eccVector* vector)
  18077. {
  18078. int ret = 0, verify = 0;
  18079. word32 sigSz;
  18080. #ifdef WOLFSSL_SMALL_STACK
  18081. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18082. #else
  18083. ecc_key userA[1];
  18084. #endif
  18085. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  18086. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  18087. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  18088. DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  18089. DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  18090. DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  18091. #endif
  18092. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  18093. if (sig == NULL)
  18094. ERROR_OUT(MEMORY_E, done);
  18095. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  18096. if (sigRaw == NULL || r == NULL || s == NULL)
  18097. ERROR_OUT(MEMORY_E, done);
  18098. #endif
  18099. #endif
  18100. #ifdef WOLFSSL_SMALL_STACK
  18101. if (userA == NULL)
  18102. ERROR_OUT(MEMORY_E, done);
  18103. #endif
  18104. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18105. if (ret != 0)
  18106. goto done;
  18107. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  18108. vector->d, vector->curveName);
  18109. if (ret != 0)
  18110. goto done;
  18111. XMEMSET(sig, 0, ECC_SIG_SIZE);
  18112. sigSz = ECC_SIG_SIZE;
  18113. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  18114. if (ret != 0)
  18115. goto done;
  18116. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  18117. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  18118. sigRawSz = ECC_SIG_SIZE;
  18119. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  18120. sigRaw, &sigRawSz);
  18121. if (ret != 0)
  18122. goto done;
  18123. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  18124. ret = -9810;
  18125. goto done;
  18126. }
  18127. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  18128. if (ret != 0)
  18129. goto done;
  18130. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  18131. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  18132. ret = -9811;
  18133. goto done;
  18134. }
  18135. #endif
  18136. do {
  18137. #if defined(WOLFSSL_ASYNC_CRYPT)
  18138. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18139. #endif
  18140. if (ret == 0)
  18141. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  18142. vector->msgLen, &verify, userA);
  18143. } while (ret == WC_PENDING_E);
  18144. if (ret != 0)
  18145. goto done;
  18146. TEST_SLEEP();
  18147. if (verify != 1)
  18148. ret = -9812;
  18149. done:
  18150. #ifdef WOLFSSL_SMALL_STACK
  18151. if (userA != NULL) {
  18152. wc_ecc_free(userA);
  18153. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18154. }
  18155. #else
  18156. wc_ecc_free(userA);
  18157. #endif
  18158. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  18159. FREE_VAR(sigRaw, HEAP_HINT);
  18160. FREE_VAR(r, HEAP_HINT);
  18161. FREE_VAR(s, HEAP_HINT);
  18162. #endif
  18163. FREE_VAR(sig, HEAP_HINT);
  18164. return ret;
  18165. }
  18166. static int ecc_test_vector(int keySize)
  18167. {
  18168. int ret;
  18169. eccVector vec;
  18170. XMEMSET(&vec, 0, sizeof(vec));
  18171. vec.keySize = (word32)keySize;
  18172. switch(keySize) {
  18173. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  18174. case 14:
  18175. return 0;
  18176. #endif /* HAVE_ECC112 */
  18177. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  18178. case 16:
  18179. return 0;
  18180. #endif /* HAVE_ECC128 */
  18181. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  18182. case 20:
  18183. return 0;
  18184. #endif /* HAVE_ECC160 */
  18185. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  18186. case 24:
  18187. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  18188. #if 1
  18189. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  18190. vec.msgLen = 20;
  18191. #else
  18192. /* This is the raw message prior to SHA-1 */
  18193. vec.msg =
  18194. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  18195. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  18196. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  18197. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  18198. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  18199. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  18200. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  18201. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  18202. vec.msgLen = 128;
  18203. #endif
  18204. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  18205. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  18206. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  18207. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  18208. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  18209. vec.curveName = "SECP192R1";
  18210. #ifndef NO_ASN
  18211. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  18212. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  18213. vec.rSz = 24;
  18214. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  18215. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  18216. vec.sSz = 24;
  18217. #endif
  18218. break;
  18219. #endif /* HAVE_ECC192 */
  18220. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  18221. case 28:
  18222. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  18223. #if 1
  18224. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  18225. vec.msgLen = 20;
  18226. #else
  18227. /* This is the raw message prior to SHA-1 */
  18228. vec.msg =
  18229. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  18230. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  18231. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  18232. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  18233. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  18234. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  18235. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  18236. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  18237. vec.msgLen = 128;
  18238. #endif
  18239. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  18240. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  18241. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  18242. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  18243. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  18244. vec.curveName = "SECP224R1";
  18245. #ifndef NO_ASN
  18246. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  18247. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  18248. "\xbc\x16\x71\xa7";
  18249. vec.rSz = 28;
  18250. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  18251. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  18252. "\x6a\xf3\xad\x5b";
  18253. vec.sSz = 28;
  18254. #endif
  18255. break;
  18256. #endif /* HAVE_ECC224 */
  18257. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  18258. case 30:
  18259. return 0;
  18260. #endif /* HAVE_ECC239 */
  18261. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  18262. case 32:
  18263. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  18264. #if 1
  18265. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  18266. vec.msgLen = 20;
  18267. #else
  18268. /* This is the raw message prior to SHA-1 */
  18269. vec.msg =
  18270. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  18271. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  18272. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  18273. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  18274. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  18275. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  18276. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  18277. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  18278. vec.msgLen = 128;
  18279. #endif
  18280. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  18281. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  18282. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  18283. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  18284. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  18285. #ifndef NO_ASN
  18286. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  18287. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  18288. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  18289. vec.rSz = 32;
  18290. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  18291. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  18292. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  18293. vec.sSz = 32;
  18294. #endif
  18295. vec.curveName = "SECP256R1";
  18296. break;
  18297. #endif /* !NO_ECC256 */
  18298. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  18299. case 40:
  18300. return 0;
  18301. #endif /* HAVE_ECC320 */
  18302. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  18303. case 48:
  18304. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  18305. #if 1
  18306. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  18307. vec.msgLen = 20;
  18308. #else
  18309. /* This is the raw message prior to SHA-1 */
  18310. vec.msg =
  18311. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  18312. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  18313. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  18314. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  18315. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  18316. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  18317. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  18318. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  18319. vec.msgLen = 128;
  18320. #endif
  18321. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  18322. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  18323. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  18324. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  18325. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  18326. vec.curveName = "SECP384R1";
  18327. #ifndef NO_ASN
  18328. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  18329. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  18330. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  18331. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  18332. vec.rSz = 48;
  18333. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  18334. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  18335. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  18336. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  18337. vec.sSz = 48;
  18338. #endif
  18339. break;
  18340. #endif /* HAVE_ECC384 */
  18341. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  18342. case 64:
  18343. return 0;
  18344. #endif /* HAVE_ECC512 */
  18345. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  18346. case 66:
  18347. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  18348. #if 1
  18349. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  18350. vec.msgLen = 20;
  18351. #else
  18352. /* This is the raw message prior to SHA-1 */
  18353. vec.msg =
  18354. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  18355. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  18356. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  18357. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  18358. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  18359. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  18360. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  18361. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  18362. vec.msgLen = 128;
  18363. #endif
  18364. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  18365. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  18366. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  18367. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  18368. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  18369. vec.curveName = "SECP521R1";
  18370. #ifndef NO_ASN
  18371. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  18372. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  18373. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  18374. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  18375. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  18376. "\xa3\xbb\x15\x21\xbe";
  18377. vec.rSz = 65;
  18378. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  18379. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  18380. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  18381. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  18382. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  18383. "\x3d\x22\xf2\x48\x0c";
  18384. vec.sSz = 65;
  18385. #endif
  18386. break;
  18387. #endif /* HAVE_ECC521 */
  18388. default:
  18389. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  18390. }; /* Switch */
  18391. ret = ecc_test_vector_item(&vec);
  18392. if (ret < 0) {
  18393. return ret;
  18394. }
  18395. return 0;
  18396. }
  18397. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_DETERMINISTIC_K)
  18398. static int ecc_test_deterministic_k(WC_RNG* rng)
  18399. {
  18400. int ret;
  18401. ecc_key key;
  18402. byte sig[72];
  18403. word32 sigSz;
  18404. unsigned char msg[] = "sample";
  18405. unsigned char hash[32];
  18406. const char* dIUT =
  18407. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  18408. const char* QIUTx =
  18409. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  18410. const char* QIUTy =
  18411. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  18412. const byte expSig[] = {
  18413. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  18414. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  18415. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  18416. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  18417. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  18418. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  18419. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  18420. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  18421. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  18422. };
  18423. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  18424. if (ret != 0) {
  18425. return ret;
  18426. }
  18427. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  18428. if (ret != 0) {
  18429. goto done;
  18430. }
  18431. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  18432. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  18433. if (ret != 0) {
  18434. goto done;
  18435. }
  18436. ret = wc_ecc_set_deterministic(&key, 1);
  18437. if (ret != 0) {
  18438. goto done;
  18439. }
  18440. sigSz = sizeof(sig);
  18441. do {
  18442. #if defined(WOLFSSL_ASYNC_CRYPT)
  18443. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18444. #endif
  18445. if (ret == 0)
  18446. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  18447. } while (ret == WC_PENDING_E);
  18448. if (ret != 0) {
  18449. goto done;
  18450. }
  18451. TEST_SLEEP();
  18452. if (sigSz != sizeof(expSig)) {
  18453. ret = -9830;
  18454. goto done;
  18455. }
  18456. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  18457. ret = -9831;
  18458. goto done;
  18459. }
  18460. sigSz = sizeof(sig);
  18461. do {
  18462. #if defined(WOLFSSL_ASYNC_CRYPT)
  18463. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18464. #endif
  18465. if (ret == 0)
  18466. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  18467. } while (ret == WC_PENDING_E);
  18468. if (ret != 0) {
  18469. goto done;
  18470. }
  18471. TEST_SLEEP();
  18472. done:
  18473. wc_ecc_free(&key);
  18474. return ret;
  18475. }
  18476. #endif
  18477. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  18478. static int ecc_test_sign_vectors(WC_RNG* rng)
  18479. {
  18480. int ret;
  18481. ecc_key key;
  18482. byte sig[72];
  18483. word32 sigSz;
  18484. unsigned char hash[32] = "test wolfSSL deterministic sign";
  18485. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  18486. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  18487. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  18488. const byte k[1] = { 0x02 };
  18489. const byte expSig[71] = {
  18490. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  18491. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  18492. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  18493. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  18494. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  18495. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  18496. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  18497. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  18498. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  18499. };
  18500. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  18501. if (ret != 0) {
  18502. return ret;
  18503. }
  18504. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  18505. if (ret != 0) {
  18506. goto done;
  18507. }
  18508. ret = wc_ecc_sign_set_k(k, sizeof(k), &key);
  18509. if (ret != 0) {
  18510. goto done;
  18511. }
  18512. sigSz = sizeof(sig);
  18513. do {
  18514. #if defined(WOLFSSL_ASYNC_CRYPT)
  18515. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18516. #endif
  18517. if (ret == 0)
  18518. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  18519. } while (ret == WC_PENDING_E);
  18520. if (ret != 0) {
  18521. goto done;
  18522. }
  18523. TEST_SLEEP();
  18524. if (sigSz != sizeof(expSig)) {
  18525. ret = -9830;
  18526. goto done;
  18527. }
  18528. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  18529. ret = -9831;
  18530. goto done;
  18531. }
  18532. sigSz = sizeof(sig);
  18533. do {
  18534. #if defined(WOLFSSL_ASYNC_CRYPT)
  18535. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18536. #endif
  18537. if (ret == 0)
  18538. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  18539. } while (ret == WC_PENDING_E);
  18540. if (ret != 0) {
  18541. goto done;
  18542. }
  18543. TEST_SLEEP();
  18544. done:
  18545. wc_ecc_free(&key);
  18546. return ret;
  18547. }
  18548. #endif
  18549. #ifdef HAVE_ECC_CDH
  18550. static int ecc_test_cdh_vectors(WC_RNG* rng)
  18551. {
  18552. int ret;
  18553. #ifdef WOLFSSL_SMALL_STACK
  18554. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18555. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18556. #else
  18557. ecc_key pub_key[1], priv_key[1];
  18558. #endif
  18559. byte sharedA[32] = {0}, sharedB[32] = {0};
  18560. word32 x, z;
  18561. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  18562. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  18563. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  18564. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  18565. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  18566. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  18567. #ifdef WOLFSSL_SMALL_STACK
  18568. if ((pub_key == NULL) ||
  18569. (priv_key == NULL)) {
  18570. ret = MEMORY_E;
  18571. goto done;
  18572. }
  18573. #endif
  18574. XMEMSET(pub_key, 0, sizeof *pub_key);
  18575. XMEMSET(priv_key, 0, sizeof *priv_key);
  18576. /* setup private and public keys */
  18577. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  18578. if (ret != 0)
  18579. goto done;
  18580. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  18581. if (ret != 0)
  18582. goto done;
  18583. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  18584. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  18585. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  18586. if (ret != 0)
  18587. goto done;
  18588. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  18589. if (ret != 0)
  18590. goto done;
  18591. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18592. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18593. !defined(HAVE_SELFTEST)
  18594. ret = wc_ecc_set_rng(priv_key, rng);
  18595. if (ret != 0)
  18596. goto done;
  18597. #else
  18598. (void)rng;
  18599. #endif
  18600. /* compute ECC Cofactor shared secret */
  18601. x = sizeof(sharedA);
  18602. do {
  18603. #if defined(WOLFSSL_ASYNC_CRYPT)
  18604. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18605. #endif
  18606. if (ret == 0)
  18607. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  18608. } while (ret == WC_PENDING_E);
  18609. if (ret != 0) {
  18610. goto done;
  18611. }
  18612. TEST_SLEEP();
  18613. /* read in expected Z */
  18614. z = sizeof(sharedB);
  18615. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  18616. if (ret != 0)
  18617. goto done;
  18618. /* compare results */
  18619. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  18620. ERROR_OUT(-9840, done);
  18621. }
  18622. done:
  18623. #ifdef WOLFSSL_SMALL_STACK
  18624. if (priv_key) {
  18625. wc_ecc_free(priv_key);
  18626. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18627. }
  18628. if (pub_key) {
  18629. wc_ecc_free(pub_key);
  18630. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18631. }
  18632. #else
  18633. wc_ecc_free(priv_key);
  18634. wc_ecc_free(pub_key);
  18635. #endif
  18636. return ret;
  18637. }
  18638. #endif /* HAVE_ECC_CDH */
  18639. #endif /* HAVE_ECC_VECTOR_TEST */
  18640. #ifdef HAVE_ECC_KEY_IMPORT
  18641. /* returns 0 on success */
  18642. static int ecc_test_make_pub(WC_RNG* rng)
  18643. {
  18644. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18645. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18646. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18647. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18648. #endif
  18649. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18650. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18651. #else
  18652. ecc_key key[1];
  18653. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  18654. ecc_key pub[1];
  18655. #endif
  18656. byte exportBuf[ECC_BUFSIZE];
  18657. byte tmp[ECC_BUFSIZE];
  18658. #endif
  18659. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  18660. #if defined(HAVE_ECC_KEY_EXPORT)
  18661. word32 x;
  18662. #endif
  18663. word32 tmpSz;
  18664. int ret = 0;
  18665. ecc_point* pubPoint = NULL;
  18666. #ifdef HAVE_ECC_VERIFY
  18667. int verify = 0;
  18668. #endif
  18669. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18670. if ((key == NULL) ||
  18671. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18672. (pub == NULL) ||
  18673. #endif
  18674. (exportBuf == NULL) ||
  18675. (tmp == NULL))
  18676. ERROR_OUT(MEMORY_E, done);
  18677. #endif
  18678. (void)msg;
  18679. (void)verify;
  18680. (void)exportBuf;
  18681. (void)rng;
  18682. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18683. #ifndef NO_ECC256
  18684. #ifdef USE_CERT_BUFFERS_256
  18685. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  18686. tmpSz = (size_t)sizeof_ecc_key_der_256;
  18687. #else
  18688. {
  18689. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  18690. if (!file) {
  18691. ERROR_OUT(-9850, done);
  18692. }
  18693. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  18694. XFCLOSE(file);
  18695. }
  18696. #endif /* USE_CERT_BUFFERS_256 */
  18697. /* import private only then test with */
  18698. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  18699. if (ret == 0) {
  18700. ERROR_OUT(-9851, done);
  18701. }
  18702. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  18703. if (ret == 0) {
  18704. ERROR_OUT(-9852, done);
  18705. }
  18706. x = 0;
  18707. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  18708. if (ret != 0) {
  18709. ERROR_OUT(-9853, done);
  18710. }
  18711. #ifdef HAVE_ECC_KEY_EXPORT
  18712. x = ECC_BUFSIZE;
  18713. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18714. if (ret != 0) {
  18715. ERROR_OUT(-9854, done);
  18716. }
  18717. /* make private only key */
  18718. wc_ecc_free(key);
  18719. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18720. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18721. if (ret != 0) {
  18722. ERROR_OUT(-9855, done);
  18723. }
  18724. x = ECC_BUFSIZE;
  18725. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18726. if (ret == 0) {
  18727. ERROR_OUT(-9856, done);
  18728. }
  18729. #endif /* HAVE_ECC_KEY_EXPORT */
  18730. ret = wc_ecc_make_pub(NULL, NULL);
  18731. if (ret == 0) {
  18732. ERROR_OUT(-9857, done);
  18733. }
  18734. TEST_SLEEP();
  18735. #ifndef WOLFSSL_NO_MALLOC
  18736. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  18737. if (pubPoint == NULL) {
  18738. ERROR_OUT(-9858, done);
  18739. }
  18740. ret = wc_ecc_make_pub(key, pubPoint);
  18741. if (ret != 0) {
  18742. ERROR_OUT(-9859, done);
  18743. }
  18744. TEST_SLEEP();
  18745. #ifdef HAVE_ECC_KEY_EXPORT
  18746. /* export should still fail, is private only key */
  18747. x = ECC_BUFSIZE;
  18748. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18749. if (ret == 0) {
  18750. ERROR_OUT(-9860, done);
  18751. }
  18752. #endif /* HAVE_ECC_KEY_EXPORT */
  18753. #endif /* !WOLFSSL_NO_MALLOC */
  18754. #endif /* !NO_ECC256 */
  18755. /* create a new key since above test for loading key is not supported */
  18756. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  18757. defined(WOLFSSL_QNX_CAAM)
  18758. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  18759. if (ret != 0) {
  18760. ERROR_OUT(-9861, done);
  18761. }
  18762. #endif
  18763. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  18764. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG)))
  18765. tmpSz = ECC_BUFSIZE;
  18766. ret = 0;
  18767. do {
  18768. #if defined(WOLFSSL_ASYNC_CRYPT)
  18769. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18770. #endif
  18771. if (ret == 0)
  18772. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp, &tmpSz, rng, key);
  18773. } while (ret == WC_PENDING_E);
  18774. if (ret != 0) {
  18775. ERROR_OUT(-9862, done);
  18776. }
  18777. TEST_SLEEP();
  18778. #ifdef HAVE_ECC_VERIFY
  18779. /* try verify with private only key */
  18780. ret = 0;
  18781. do {
  18782. #if defined(WOLFSSL_ASYNC_CRYPT)
  18783. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18784. #endif
  18785. if (ret == 0)
  18786. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, (word32)XSTRLEN((const char* )msg), &verify, key);
  18787. } while (ret == WC_PENDING_E);
  18788. if (ret != 0) {
  18789. ERROR_OUT(-9863, done);
  18790. }
  18791. if (verify != 1) {
  18792. ERROR_OUT(-9864, done);
  18793. }
  18794. TEST_SLEEP();
  18795. #ifdef HAVE_ECC_KEY_EXPORT
  18796. /* exporting the public part should now work */
  18797. x = ECC_BUFSIZE;
  18798. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18799. if (ret != 0) {
  18800. ERROR_OUT(-9865, done);
  18801. }
  18802. #endif /* HAVE_ECC_KEY_EXPORT */
  18803. #endif /* HAVE_ECC_VERIFY */
  18804. #endif /* HAVE_ECC_SIGN */
  18805. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  18806. /* now test private only key with creating a shared secret */
  18807. x = ECC_BUFSIZE;
  18808. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18809. if (ret != 0) {
  18810. ERROR_OUT(-9866, done);
  18811. }
  18812. #ifndef WOLFSSL_QNX_CAAM
  18813. /* make private only key */
  18814. wc_ecc_free(key);
  18815. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18816. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18817. if (ret != 0) {
  18818. ERROR_OUT(-9867, done);
  18819. }
  18820. /* check that public export fails with private only key */
  18821. x = ECC_BUFSIZE;
  18822. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18823. if (ret == 0) {
  18824. ERROR_OUT(-9868, done);
  18825. }
  18826. #endif /* WOLFSSL_QNX_CAAM */
  18827. /* make public key for shared secret */
  18828. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  18829. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  18830. #if defined(WOLFSSL_ASYNC_CRYPT)
  18831. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  18832. #endif
  18833. if (ret != 0) {
  18834. ERROR_OUT(-9869, done);
  18835. }
  18836. TEST_SLEEP();
  18837. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18838. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18839. !defined(HAVE_SELFTEST)
  18840. ret = wc_ecc_set_rng(key, rng);
  18841. if (ret != 0)
  18842. goto done;
  18843. #endif
  18844. x = ECC_BUFSIZE;
  18845. do {
  18846. #if defined(WOLFSSL_ASYNC_CRYPT)
  18847. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18848. #endif
  18849. if (ret == 0) {
  18850. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  18851. }
  18852. } while (ret == WC_PENDING_E);
  18853. wc_ecc_free(pub);
  18854. if (ret != 0) {
  18855. ERROR_OUT(-9870, done);
  18856. }
  18857. TEST_SLEEP();
  18858. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  18859. ret = 0;
  18860. done:
  18861. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  18862. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18863. if (key != NULL) {
  18864. wc_ecc_free(key);
  18865. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18866. }
  18867. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18868. if (pub != NULL)
  18869. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18870. #endif
  18871. if (exportBuf != NULL)
  18872. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18873. if (tmp != NULL)
  18874. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18875. #else
  18876. wc_ecc_free(key);
  18877. #endif
  18878. return ret;
  18879. }
  18880. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  18881. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  18882. {
  18883. int ret;
  18884. #ifdef WOLFSSL_SMALL_STACK
  18885. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18886. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18887. #else
  18888. ecc_key eccKey[1];
  18889. byte tmpBuf[ECC_BUFSIZE];
  18890. #endif
  18891. word32 tmpSz;
  18892. word32 idx;
  18893. #ifdef WOLFSSL_SMALL_STACK
  18894. if ((eccKey == NULL) || (tmpBuf == NULL))
  18895. ERROR_OUT(MEMORY_E, done);
  18896. #endif
  18897. ret = wc_ecc_init(eccKey);
  18898. if (ret != 0) {
  18899. goto done;
  18900. }
  18901. ret = wc_ecc_make_key(rng, keySize, eccKey);
  18902. #if defined(WOLFSSL_ASYNC_CRYPT)
  18903. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18904. #endif
  18905. if (ret != 0) {
  18906. goto done;
  18907. }
  18908. tmpSz = ECC_BUFSIZE;
  18909. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  18910. wc_ecc_free(eccKey);
  18911. if (ret < 0) {
  18912. goto done;
  18913. }
  18914. tmpSz = ret;
  18915. ret = wc_ecc_init(eccKey);
  18916. if (ret != 0) {
  18917. goto done;
  18918. }
  18919. idx = 0;
  18920. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18921. if (ret != 0) {
  18922. goto done;
  18923. }
  18924. wc_ecc_free(eccKey);
  18925. ret = wc_ecc_init(eccKey);
  18926. if (ret != 0) {
  18927. goto done;
  18928. }
  18929. idx = 0;
  18930. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18931. if (ret != 0) {
  18932. goto done;
  18933. }
  18934. ret = 0;
  18935. done:
  18936. #ifdef WOLFSSL_SMALL_STACK
  18937. if (eccKey != NULL) {
  18938. wc_ecc_free(eccKey);
  18939. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18940. }
  18941. if (tmpBuf != NULL)
  18942. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18943. #else
  18944. wc_ecc_free(eccKey);
  18945. #endif
  18946. return ret;
  18947. }
  18948. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18949. #endif /* HAVE_ECC_KEY_IMPORT */
  18950. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  18951. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  18952. {
  18953. int ret = 0;
  18954. int derSz;
  18955. #ifdef HAVE_PKCS8
  18956. word32 pkcs8Sz;
  18957. #endif
  18958. #ifdef WOLFSSL_SMALL_STACK
  18959. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18960. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18961. #else
  18962. byte der[ECC_BUFSIZE];
  18963. ecc_key userA[1];
  18964. #endif
  18965. #ifdef WOLFSSL_SMALL_STACK
  18966. if ((der == NULL) || (userA == NULL))
  18967. ERROR_OUT(MEMORY_E, done);
  18968. #endif
  18969. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18970. if (ret != 0)
  18971. goto done;
  18972. ret = wc_ecc_make_key(rng, keySize, userA);
  18973. #if defined(WOLFSSL_ASYNC_CRYPT)
  18974. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18975. #endif
  18976. if (ret != 0)
  18977. goto done;
  18978. TEST_SLEEP();
  18979. ret = wc_ecc_check_key(userA);
  18980. if (ret != 0)
  18981. goto done;
  18982. TEST_SLEEP();
  18983. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  18984. if (derSz < 0) {
  18985. ERROR_OUT(derSz, done);
  18986. }
  18987. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  18988. ECC_PRIVATEKEY_TYPE, -8347);
  18989. if (ret != 0) {
  18990. goto done;
  18991. }
  18992. /* test export of public key */
  18993. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  18994. if (derSz < 0) {
  18995. ERROR_OUT(derSz, done);
  18996. }
  18997. if (derSz == 0) {
  18998. ERROR_OUT(-9890, done);
  18999. }
  19000. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  19001. if (ret != 0) {
  19002. goto done;
  19003. }
  19004. #ifdef HAVE_PKCS8
  19005. /* test export of PKCS#8 unencrypted private key */
  19006. pkcs8Sz = FOURK_BUF;
  19007. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  19008. if (derSz < 0) {
  19009. ERROR_OUT(derSz, done);
  19010. }
  19011. if (derSz == 0) {
  19012. ERROR_OUT(-9891, done);
  19013. }
  19014. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  19015. if (ret != 0) {
  19016. goto done;
  19017. }
  19018. #endif /* HAVE_PKCS8 */
  19019. done:
  19020. #ifdef WOLFSSL_SMALL_STACK
  19021. if (der != NULL)
  19022. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19023. if (userA != NULL) {
  19024. wc_ecc_free(userA);
  19025. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19026. }
  19027. #else
  19028. wc_ecc_free(userA);
  19029. #endif
  19030. return ret;
  19031. }
  19032. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  19033. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  19034. int curve_id, const ecc_set_type* dp)
  19035. {
  19036. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  19037. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19038. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  19039. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  19040. #endif
  19041. #ifdef HAVE_ECC_KEY_EXPORT
  19042. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  19043. DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  19044. #endif
  19045. word32 x = 0;
  19046. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  19047. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19048. word32 y;
  19049. #endif
  19050. #ifdef HAVE_ECC_SIGN
  19051. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  19052. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  19053. int i;
  19054. #ifdef HAVE_ECC_VERIFY
  19055. int verify;
  19056. #endif /* HAVE_ECC_VERIFY */
  19057. #endif /* HAVE_ECC_SIGN */
  19058. int ret;
  19059. #ifdef WOLFSSL_SMALL_STACK
  19060. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19061. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19062. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19063. #else
  19064. ecc_key userA[1];
  19065. ecc_key userB[1];
  19066. ecc_key pubKey[1];
  19067. #endif
  19068. #ifndef WC_NO_RNG
  19069. int curveSize;
  19070. #endif
  19071. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  19072. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  19073. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19074. if (sharedA == NULL || sharedB == NULL)
  19075. ERROR_OUT(-9900, done);
  19076. #endif
  19077. #ifdef HAVE_ECC_KEY_EXPORT
  19078. if (exportBuf == NULL)
  19079. ERROR_OUT(-9901, done);
  19080. #endif
  19081. #ifdef HAVE_ECC_SIGN
  19082. if (sig == NULL || digest == NULL)
  19083. ERROR_OUT(-9902, done);
  19084. #endif
  19085. #endif /* WOLFSSL_SMALL_STACK */
  19086. (void)testVerifyCount;
  19087. (void)dp;
  19088. (void)x;
  19089. #ifdef WOLFSSL_SMALL_STACK
  19090. if ((userA == NULL) ||
  19091. (userB == NULL) ||
  19092. (pubKey == NULL))
  19093. ERROR_OUT(-9903, done);
  19094. #endif
  19095. XMEMSET(userA, 0, sizeof *userA);
  19096. XMEMSET(userB, 0, sizeof *userB);
  19097. XMEMSET(pubKey, 0, sizeof *pubKey);
  19098. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  19099. if (ret != 0)
  19100. ERROR_OUT(-9904, done);
  19101. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  19102. if (ret != 0)
  19103. ERROR_OUT(-9905, done);
  19104. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  19105. if (ret != 0)
  19106. ERROR_OUT(-9906, done);
  19107. #ifdef WOLFSSL_CUSTOM_CURVES
  19108. if (dp != NULL) {
  19109. ret = wc_ecc_set_custom_curve(userA, dp);
  19110. if (ret != 0)
  19111. ERROR_OUT(-9907, done);
  19112. ret = wc_ecc_set_custom_curve(userB, dp);
  19113. if (ret != 0)
  19114. ERROR_OUT(-9908, done);
  19115. }
  19116. #endif
  19117. #ifndef WC_NO_RNG
  19118. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  19119. #if defined(WOLFSSL_ASYNC_CRYPT)
  19120. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  19121. #endif
  19122. if (ret != 0)
  19123. ERROR_OUT(-9910, done);
  19124. TEST_SLEEP();
  19125. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  19126. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  19127. if (curveSize != userA->dp->size)
  19128. ERROR_OUT(-9911, done);
  19129. }
  19130. ret = wc_ecc_check_key(userA);
  19131. if (ret != 0)
  19132. ERROR_OUT(-9912, done);
  19133. TEST_SLEEP();
  19134. /* ATECC508/608 configuration may not support more than one ECDH key */
  19135. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19136. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  19137. #if defined(WOLFSSL_ASYNC_CRYPT)
  19138. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  19139. #endif
  19140. if (ret != 0)
  19141. ERROR_OUT(-9914, done);
  19142. TEST_SLEEP();
  19143. /* only perform the below tests if the key size matches */
  19144. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  19145. ERROR_OUT(ECC_CURVE_OID_E, done);
  19146. #ifdef HAVE_ECC_DHE
  19147. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19148. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19149. !defined(HAVE_SELFTEST)
  19150. ret = wc_ecc_set_rng(userA, rng);
  19151. if (ret != 0)
  19152. ERROR_OUT(-9915, done);
  19153. ret = wc_ecc_set_rng(userB, rng);
  19154. if (ret != 0)
  19155. ERROR_OUT(-9916, done);
  19156. #endif
  19157. x = ECC_SHARED_SIZE;
  19158. do {
  19159. #if defined(WOLFSSL_ASYNC_CRYPT)
  19160. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19161. #endif
  19162. if (ret == 0)
  19163. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  19164. } while (ret == WC_PENDING_E);
  19165. if (ret != 0) {
  19166. ERROR_OUT(-9917, done);
  19167. }
  19168. TEST_SLEEP();
  19169. y = ECC_SHARED_SIZE;
  19170. do {
  19171. #if defined(WOLFSSL_ASYNC_CRYPT)
  19172. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19173. #endif
  19174. if (ret == 0)
  19175. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  19176. } while (ret == WC_PENDING_E);
  19177. if (ret != 0)
  19178. ERROR_OUT(-9918, done);
  19179. if (y != x)
  19180. ERROR_OUT(-9919, done);
  19181. if (XMEMCMP(sharedA, sharedB, x))
  19182. ERROR_OUT(-9920, done);
  19183. TEST_SLEEP();
  19184. #endif /* HAVE_ECC_DHE */
  19185. #ifdef HAVE_ECC_CDH
  19186. /* add cofactor flag */
  19187. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  19188. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  19189. x = ECC_SHARED_SIZE;
  19190. do {
  19191. #if defined(WOLFSSL_ASYNC_CRYPT)
  19192. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19193. #endif
  19194. if (ret == 0)
  19195. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  19196. } while (ret == WC_PENDING_E);
  19197. if (ret != 0)
  19198. ERROR_OUT(-9921, done);
  19199. TEST_SLEEP();
  19200. y = ECC_SHARED_SIZE;
  19201. do {
  19202. #if defined(WOLFSSL_ASYNC_CRYPT)
  19203. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19204. #endif
  19205. if (ret == 0)
  19206. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  19207. } while (ret == WC_PENDING_E);
  19208. if (ret != 0)
  19209. ERROR_OUT(-9922, done);
  19210. if (y != x)
  19211. ERROR_OUT(-9923, done);
  19212. if (XMEMCMP(sharedA, sharedB, x))
  19213. ERROR_OUT(-9924, done);
  19214. TEST_SLEEP();
  19215. /* remove cofactor flag */
  19216. wc_ecc_set_flags(userA, 0);
  19217. wc_ecc_set_flags(userB, 0);
  19218. #endif /* HAVE_ECC_CDH */
  19219. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  19220. #ifdef HAVE_ECC_KEY_EXPORT
  19221. x = ECC_KEY_EXPORT_BUF_SIZE;
  19222. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  19223. if (ret != 0)
  19224. ERROR_OUT(-9925, done);
  19225. #ifdef HAVE_ECC_KEY_IMPORT
  19226. #ifdef WOLFSSL_CUSTOM_CURVES
  19227. if (dp != NULL) {
  19228. ret = wc_ecc_set_custom_curve(pubKey, dp);
  19229. if (ret != 0)
  19230. ERROR_OUT(-9926, done);
  19231. }
  19232. #endif
  19233. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  19234. if (ret != 0)
  19235. ERROR_OUT(-9927, done);
  19236. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19237. #ifdef HAVE_ECC_DHE
  19238. y = ECC_SHARED_SIZE;
  19239. do {
  19240. #if defined(WOLFSSL_ASYNC_CRYPT)
  19241. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19242. #endif
  19243. if (ret == 0)
  19244. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  19245. } while (ret == WC_PENDING_E);
  19246. if (ret != 0)
  19247. ERROR_OUT(-9928, done);
  19248. if (XMEMCMP(sharedA, sharedB, y))
  19249. ERROR_OUT(-9929, done);
  19250. TEST_SLEEP();
  19251. #endif /* HAVE_ECC_DHE */
  19252. #ifdef HAVE_COMP_KEY
  19253. /* try compressed export / import too */
  19254. x = ECC_KEY_EXPORT_BUF_SIZE;
  19255. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  19256. if (ret != 0)
  19257. ERROR_OUT(-9930, done);
  19258. wc_ecc_free(pubKey);
  19259. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  19260. if (ret != 0)
  19261. ERROR_OUT(-9931, done);
  19262. #ifdef WOLFSSL_CUSTOM_CURVES
  19263. if (dp != NULL) {
  19264. ret = wc_ecc_set_custom_curve(pubKey, dp);
  19265. if (ret != 0)
  19266. ERROR_OUT(-9932, done);
  19267. }
  19268. #endif
  19269. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  19270. if (ret != 0)
  19271. ERROR_OUT(-9933, done);
  19272. #ifdef HAVE_ECC_DHE
  19273. y = ECC_SHARED_SIZE;
  19274. do {
  19275. #if defined(WOLFSSL_ASYNC_CRYPT)
  19276. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19277. #endif
  19278. if (ret == 0)
  19279. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  19280. } while (ret == WC_PENDING_E);
  19281. if (ret != 0)
  19282. ERROR_OUT(-9934, done);
  19283. if (XMEMCMP(sharedA, sharedB, y))
  19284. ERROR_OUT(-9935, done);
  19285. TEST_SLEEP();
  19286. #endif /* HAVE_ECC_DHE */
  19287. #endif /* HAVE_COMP_KEY */
  19288. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  19289. #endif /* !WC_NO_RNG */
  19290. #endif /* HAVE_ECC_KEY_IMPORT */
  19291. #endif /* HAVE_ECC_KEY_EXPORT */
  19292. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))
  19293. #ifdef HAVE_ECC_SIGN
  19294. /* ECC w/out Shamir has issue with all 0 digest */
  19295. /* WC_BIGINT doesn't have 0 len well on hardware */
  19296. /* Cryptocell has issues with all 0 digest */
  19297. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  19298. !defined(WOLFSSL_CRYPTOCELL)
  19299. /* test DSA sign hash with zeros */
  19300. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  19301. digest[i] = 0;
  19302. }
  19303. x = ECC_SIG_SIZE;
  19304. do {
  19305. #if defined(WOLFSSL_ASYNC_CRYPT)
  19306. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19307. #endif
  19308. if (ret == 0)
  19309. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  19310. userA);
  19311. } while (ret == WC_PENDING_E);
  19312. if (ret != 0)
  19313. ERROR_OUT(-9936, done);
  19314. TEST_SLEEP();
  19315. #ifdef HAVE_ECC_VERIFY
  19316. for (i=0; i<testVerifyCount; i++) {
  19317. verify = 0;
  19318. do {
  19319. #if defined(WOLFSSL_ASYNC_CRYPT)
  19320. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19321. #endif
  19322. if (ret == 0)
  19323. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  19324. &verify, userA);
  19325. } while (ret == WC_PENDING_E);
  19326. if (ret != 0)
  19327. ERROR_OUT(-9937, done);
  19328. if (verify != 1)
  19329. ERROR_OUT(-9938, done);
  19330. TEST_SLEEP();
  19331. }
  19332. #endif /* HAVE_ECC_VERIFY */
  19333. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  19334. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  19335. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  19336. digest[i] = (byte)i;
  19337. }
  19338. x = ECC_SIG_SIZE;
  19339. do {
  19340. #if defined(WOLFSSL_ASYNC_CRYPT)
  19341. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19342. #endif
  19343. if (ret == 0)
  19344. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  19345. userA);
  19346. } while (ret == WC_PENDING_E);
  19347. if (ret != 0)
  19348. ERROR_OUT(-9939, done);
  19349. TEST_SLEEP();
  19350. #ifdef HAVE_ECC_VERIFY
  19351. for (i=0; i<testVerifyCount; i++) {
  19352. verify = 0;
  19353. do {
  19354. #if defined(WOLFSSL_ASYNC_CRYPT)
  19355. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19356. #endif
  19357. if (ret == 0)
  19358. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  19359. &verify, userA);
  19360. } while (ret == WC_PENDING_E);
  19361. if (ret != 0)
  19362. ERROR_OUT(-9940, done);
  19363. if (verify != 1)
  19364. ERROR_OUT(-9941, done);
  19365. TEST_SLEEP();
  19366. }
  19367. #endif /* HAVE_ECC_VERIFY */
  19368. #endif /* HAVE_ECC_SIGN */
  19369. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  19370. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  19371. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A)
  19372. x = ECC_KEY_EXPORT_BUF_SIZE;
  19373. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  19374. if (ret != 0)
  19375. ERROR_OUT(-9942, done);
  19376. #elif defined(HAVE_ECC_KEY_EXPORT)
  19377. (void)exportBuf;
  19378. #endif /* HAVE_ECC_KEY_EXPORT */
  19379. done:
  19380. #ifdef WOLFSSL_SMALL_STACK
  19381. if (userA != NULL) {
  19382. wc_ecc_free(userA);
  19383. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19384. }
  19385. if (userB != NULL) {
  19386. wc_ecc_free(userB);
  19387. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19388. }
  19389. if (pubKey != NULL) {
  19390. wc_ecc_free(pubKey);
  19391. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19392. }
  19393. #else
  19394. wc_ecc_free(pubKey);
  19395. wc_ecc_free(userB);
  19396. wc_ecc_free(userA);
  19397. #endif
  19398. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  19399. FREE_VAR(sharedA, HEAP_HINT);
  19400. FREE_VAR(sharedB, HEAP_HINT);
  19401. #endif
  19402. #ifdef HAVE_ECC_KEY_EXPORT
  19403. FREE_VAR(exportBuf, HEAP_HINT);
  19404. #endif
  19405. #ifdef HAVE_ECC_SIGN
  19406. FREE_VAR(sig, HEAP_HINT);
  19407. FREE_VAR(digest, HEAP_HINT);
  19408. #endif
  19409. (void)keySize;
  19410. (void)curve_id;
  19411. (void)rng;
  19412. return ret;
  19413. }
  19414. #undef ECC_TEST_VERIFY_COUNT
  19415. #define ECC_TEST_VERIFY_COUNT 2
  19416. static int ecc_test_curve(WC_RNG* rng, int keySize)
  19417. {
  19418. int ret;
  19419. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  19420. ECC_CURVE_DEF, NULL);
  19421. if (ret < 0) {
  19422. if (ret == ECC_CURVE_OID_E) {
  19423. /* ignore error for curves not found */
  19424. /* some curve sizes are only available with:
  19425. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  19426. and HAVE_ECC_KOBLITZ */
  19427. }
  19428. else {
  19429. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  19430. return ret;
  19431. }
  19432. }
  19433. #ifdef HAVE_ECC_VECTOR_TEST
  19434. ret = ecc_test_vector(keySize);
  19435. if (ret < 0) {
  19436. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  19437. return ret;
  19438. }
  19439. #endif
  19440. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19441. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  19442. ret = ecc_test_key_decode(rng, keySize);
  19443. if (ret < 0) {
  19444. if (ret == ECC_CURVE_OID_E) {
  19445. /* ignore error for curves not found */
  19446. }
  19447. else {
  19448. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  19449. return ret;
  19450. }
  19451. }
  19452. #endif
  19453. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  19454. ret = ecc_test_key_gen(rng, keySize);
  19455. if (ret < 0) {
  19456. if (ret == ECC_CURVE_OID_E) {
  19457. /* ignore error for curves not found */
  19458. }
  19459. else {
  19460. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  19461. return ret;
  19462. }
  19463. }
  19464. #endif
  19465. return 0;
  19466. }
  19467. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  19468. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19469. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19470. !defined(WOLFSSL_NO_MALLOC)
  19471. static int ecc_point_test(void)
  19472. {
  19473. int ret;
  19474. ecc_point* point;
  19475. ecc_point* point2;
  19476. #ifdef HAVE_COMP_KEY
  19477. ecc_point* point3;
  19478. ecc_point* point4;
  19479. #endif
  19480. word32 outLen;
  19481. byte out[65];
  19482. byte der[] = { 0x04, /* = Uncompressed */
  19483. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19484. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19485. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19486. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19487. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19488. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19489. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19490. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  19491. #ifdef HAVE_COMP_KEY
  19492. byte derComp0[] = { 0x02, /* = Compressed, y even */
  19493. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19494. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19495. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19496. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  19497. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  19498. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19499. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19500. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  19501. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  19502. #endif
  19503. byte altDer[] = { 0x04, /* = Uncompressed */
  19504. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19505. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19506. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19507. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19508. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19509. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19510. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19511. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  19512. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  19513. /* if curve P256 is not enabled then test should not fail */
  19514. if (curve_idx == ECC_CURVE_INVALID)
  19515. return 0;
  19516. outLen = sizeof(out);
  19517. point = wc_ecc_new_point();
  19518. if (point == NULL)
  19519. return -10000;
  19520. point2 = wc_ecc_new_point();
  19521. if (point2 == NULL) {
  19522. wc_ecc_del_point(point);
  19523. return -10001;
  19524. }
  19525. #ifdef HAVE_COMP_KEY
  19526. point3 = wc_ecc_new_point();
  19527. if (point3 == NULL) {
  19528. wc_ecc_del_point(point2);
  19529. wc_ecc_del_point(point);
  19530. return -10002;
  19531. }
  19532. point4 = wc_ecc_new_point();
  19533. if (point4 == NULL) {
  19534. wc_ecc_del_point(point3);
  19535. wc_ecc_del_point(point2);
  19536. wc_ecc_del_point(point);
  19537. return -10003;
  19538. }
  19539. #endif
  19540. /* Parameter Validation testing. */
  19541. wc_ecc_del_point(NULL);
  19542. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  19543. if (ret != ECC_BAD_ARG_E) {
  19544. ret = -10004;
  19545. goto done;
  19546. }
  19547. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  19548. if (ret != ECC_BAD_ARG_E) {
  19549. ret = -10005;
  19550. goto done;
  19551. }
  19552. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  19553. if (ret != ECC_BAD_ARG_E) {
  19554. ret = -10006;
  19555. goto done;
  19556. }
  19557. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  19558. if (ret != ECC_BAD_ARG_E) {
  19559. ret = -10007;
  19560. goto done;
  19561. }
  19562. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  19563. if (ret != ECC_BAD_ARG_E) {
  19564. ret = -10008;
  19565. goto done;
  19566. }
  19567. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  19568. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  19569. ret = -10009;
  19570. goto done;
  19571. }
  19572. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  19573. if (ret != ECC_BAD_ARG_E) {
  19574. ret = -10010;
  19575. goto done;
  19576. }
  19577. outLen = 0;
  19578. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  19579. if (ret != BUFFER_E) {
  19580. ret = -10011;
  19581. goto done;
  19582. }
  19583. ret = wc_ecc_copy_point(NULL, NULL);
  19584. if (ret != ECC_BAD_ARG_E) {
  19585. ret = -10012;
  19586. goto done;
  19587. }
  19588. ret = wc_ecc_copy_point(NULL, point2);
  19589. if (ret != ECC_BAD_ARG_E) {
  19590. ret = -10013;
  19591. goto done;
  19592. }
  19593. ret = wc_ecc_copy_point(point, NULL);
  19594. if (ret != ECC_BAD_ARG_E) {
  19595. ret = -10014;
  19596. goto done;
  19597. }
  19598. ret = wc_ecc_cmp_point(NULL, NULL);
  19599. if (ret != BAD_FUNC_ARG) {
  19600. ret = -10015;
  19601. goto done;
  19602. }
  19603. ret = wc_ecc_cmp_point(NULL, point2);
  19604. if (ret != BAD_FUNC_ARG) {
  19605. ret = -10016;
  19606. goto done;
  19607. }
  19608. ret = wc_ecc_cmp_point(point, NULL);
  19609. if (ret != BAD_FUNC_ARG) {
  19610. ret = -10017;
  19611. goto done;
  19612. }
  19613. /* Use API. */
  19614. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  19615. if (ret != 0) {
  19616. ret = -10018;
  19617. goto done;
  19618. }
  19619. outLen = sizeof(out);
  19620. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  19621. if (ret != 0) {
  19622. ret = -10019;
  19623. goto done;
  19624. }
  19625. if (outLen != sizeof(der)) {
  19626. ret = -10020;
  19627. goto done;
  19628. }
  19629. if (XMEMCMP(out, der, outLen) != 0) {
  19630. ret = -10021;
  19631. goto done;
  19632. }
  19633. ret = wc_ecc_copy_point(point2, point);
  19634. if (ret != MP_OKAY) {
  19635. ret = -10022;
  19636. goto done;
  19637. }
  19638. ret = wc_ecc_cmp_point(point2, point);
  19639. if (ret != MP_EQ) {
  19640. ret = -10023;
  19641. goto done;
  19642. }
  19643. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  19644. if (ret != 0) {
  19645. ret = -10024;
  19646. goto done;
  19647. }
  19648. ret = wc_ecc_cmp_point(point2, point);
  19649. if (ret != MP_GT) {
  19650. ret = -10025;
  19651. goto done;
  19652. }
  19653. #ifdef HAVE_COMP_KEY
  19654. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  19655. if (ret != 0) {
  19656. ret = -10026;
  19657. goto done;
  19658. }
  19659. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  19660. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  19661. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  19662. if (ret != 0) {
  19663. ret = -10027;
  19664. goto done;
  19665. }
  19666. #endif
  19667. ret = wc_ecc_cmp_point(point3, point4);
  19668. if (ret != MP_EQ) {
  19669. ret = -10028;
  19670. goto done;
  19671. }
  19672. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  19673. if (ret != 0) {
  19674. ret = -10029;
  19675. goto done;
  19676. }
  19677. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  19678. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  19679. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  19680. if (ret != 0) {
  19681. ret = -10030;
  19682. goto done;
  19683. }
  19684. #endif
  19685. ret = wc_ecc_cmp_point(point3, point4);
  19686. if (ret != MP_EQ) {
  19687. ret = -10031;
  19688. goto done;
  19689. }
  19690. #endif
  19691. done:
  19692. #ifdef HAVE_COMP_KEY
  19693. wc_ecc_del_point(point4);
  19694. wc_ecc_del_point(point3);
  19695. #endif
  19696. wc_ecc_del_point(point2);
  19697. wc_ecc_del_point(point);
  19698. return ret;
  19699. }
  19700. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  19701. #ifndef NO_SIG_WRAPPER
  19702. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  19703. {
  19704. int ret;
  19705. word32 sigSz;
  19706. int size;
  19707. byte out[ECC_MAX_SIG_SIZE];
  19708. byte in[] = TEST_STRING;
  19709. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  19710. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  19711. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  19712. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  19713. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  19714. };
  19715. word32 inLen = (word32)XSTRLEN((char*)in);
  19716. size = wc_ecc_sig_size(key);
  19717. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  19718. if (ret != size)
  19719. return -10040;
  19720. sigSz = (word32)ret;
  19721. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  19722. inLen, out, &sigSz, key, sizeof(*key), rng);
  19723. if (ret != 0)
  19724. return -10041;
  19725. TEST_SLEEP();
  19726. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  19727. inLen, out, sigSz, key, sizeof(*key));
  19728. if (ret != 0)
  19729. return -10042;
  19730. TEST_SLEEP();
  19731. sigSz = (word32)sizeof(out);
  19732. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19733. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  19734. if (ret != 0)
  19735. return -10043;
  19736. TEST_SLEEP();
  19737. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19738. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  19739. if (ret != 0)
  19740. return -10044;
  19741. TEST_SLEEP();
  19742. return 0;
  19743. }
  19744. #endif
  19745. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19746. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19747. !defined(WOLFSSL_QNX_CAAM)
  19748. static int ecc_exp_imp_test(ecc_key* key)
  19749. {
  19750. int ret;
  19751. int curve_id;
  19752. #ifdef WOLFSSL_SMALL_STACK
  19753. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  19754. #else
  19755. ecc_key keyImp[1];
  19756. #endif
  19757. byte priv[32];
  19758. word32 privLen;
  19759. byte pub[65];
  19760. word32 pubLen, pubLenX, pubLenY;
  19761. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  19762. "ce46cbc947616d0cbaa82323818a793d";
  19763. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  19764. "922f8b30ea6e8811742ac7238fe87308";
  19765. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  19766. "2e7a493ec1153b3a95feb8a4873f8d08";
  19767. #ifdef WOLFSSL_SMALL_STACK
  19768. if (keyImp == NULL)
  19769. ERROR_OUT(-10050, done);
  19770. #endif
  19771. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19772. privLen = sizeof(priv);
  19773. ret = wc_ecc_export_private_only(key, priv, &privLen);
  19774. if (ret != 0) {
  19775. ret = -10051;
  19776. goto done;
  19777. }
  19778. pubLen = sizeof(pub);
  19779. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  19780. if (ret != 0) {
  19781. ret = -10052;
  19782. goto done;
  19783. }
  19784. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  19785. if (ret != 0) {
  19786. ret = -10053;
  19787. goto done;
  19788. }
  19789. wc_ecc_free(keyImp);
  19790. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19791. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  19792. if (ret != 0) {
  19793. ret = -10054;
  19794. goto done;
  19795. }
  19796. wc_ecc_free(keyImp);
  19797. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19798. curve_id = wc_ecc_get_curve_id(key->idx);
  19799. if (curve_id < 0) {
  19800. ret = -10055;
  19801. goto done;
  19802. }
  19803. /* test import private only */
  19804. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  19805. curve_id);
  19806. if (ret != 0) {
  19807. ret = -10056;
  19808. goto done;
  19809. }
  19810. wc_ecc_free(keyImp);
  19811. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19812. /* test export public raw */
  19813. pubLenX = pubLenY = 32;
  19814. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  19815. if (ret != 0) {
  19816. ret = -10057;
  19817. goto done;
  19818. }
  19819. #ifndef HAVE_SELFTEST
  19820. /* test import of public */
  19821. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  19822. if (ret != 0) {
  19823. ret = -10058;
  19824. goto done;
  19825. }
  19826. #endif
  19827. wc_ecc_free(keyImp);
  19828. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19829. /* test export private and public raw */
  19830. pubLenX = pubLenY = privLen = 32;
  19831. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  19832. priv, &privLen);
  19833. if (ret != 0) {
  19834. ret = -10059;
  19835. goto done;
  19836. }
  19837. #ifndef HAVE_SELFTEST
  19838. /* test import of private and public */
  19839. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  19840. if (ret != 0) {
  19841. ret = -10060;
  19842. goto done;
  19843. }
  19844. #endif
  19845. done:
  19846. #ifdef WOLFSSL_SMALL_STACK
  19847. if (keyImp != NULL) {
  19848. wc_ecc_free(keyImp);
  19849. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19850. }
  19851. #else
  19852. wc_ecc_free(keyImp);
  19853. #endif
  19854. return ret;
  19855. }
  19856. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  19857. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19858. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_QNX_CAAM)
  19859. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19860. static int ecc_mulmod_test(ecc_key* key1)
  19861. {
  19862. int ret;
  19863. #ifdef WOLFSSL_SMALL_STACK
  19864. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19865. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19866. #else
  19867. ecc_key key2[1];
  19868. ecc_key key3[1];
  19869. #endif
  19870. #ifdef WOLFSSL_SMALL_STACK
  19871. if ((key2 == NULL) || (key3 == NULL))
  19872. ERROR_OUT(MEMORY_E, done);
  19873. #endif
  19874. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  19875. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  19876. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  19877. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  19878. * private key in key2.
  19879. */
  19880. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  19881. ECC_SECP256R1);
  19882. if (ret != 0)
  19883. goto done;
  19884. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  19885. * in key3.
  19886. */
  19887. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  19888. key1->dp->prime, ECC_SECP256R1);
  19889. if (ret != 0)
  19890. goto done;
  19891. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  19892. 1);
  19893. if (ret != 0) {
  19894. ret = -10070;
  19895. goto done;
  19896. }
  19897. done:
  19898. #ifdef WOLFSSL_SMALL_STACK
  19899. if (key2 != NULL) {
  19900. wc_ecc_free(key2);
  19901. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19902. }
  19903. if (key3 != NULL) {
  19904. wc_ecc_free(key3);
  19905. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19906. }
  19907. #else
  19908. wc_ecc_free(key3);
  19909. wc_ecc_free(key2);
  19910. #endif
  19911. return ret;
  19912. }
  19913. #endif
  19914. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  19915. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  19916. {
  19917. int ret;
  19918. byte out[128];
  19919. word32 outLen = sizeof(out);
  19920. /* Parameter Validation testing. */
  19921. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  19922. if (ret != BAD_FUNC_ARG)
  19923. return -10080;
  19924. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  19925. if (ret != BAD_FUNC_ARG)
  19926. return -10081;
  19927. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  19928. if (ret != BAD_FUNC_ARG)
  19929. return -10082;
  19930. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  19931. if (ret != BAD_FUNC_ARG)
  19932. return -10083;
  19933. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19934. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19935. !defined(HAVE_SELFTEST)
  19936. ret = wc_ecc_set_rng(key, rng);
  19937. if (ret != 0)
  19938. return -10084;
  19939. #else
  19940. (void)rng;
  19941. #endif
  19942. /* Use API. */
  19943. ret = 0;
  19944. do {
  19945. #if defined(WOLFSSL_ASYNC_CRYPT)
  19946. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19947. #endif
  19948. if (ret == 0)
  19949. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  19950. } while (ret == WC_PENDING_E);
  19951. if (ret != 0)
  19952. return -10085;
  19953. TEST_SLEEP();
  19954. return 0;
  19955. }
  19956. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  19957. #endif
  19958. static int ecc_def_curve_test(WC_RNG *rng)
  19959. {
  19960. int ret;
  19961. #ifdef WOLFSSL_SMALL_STACK
  19962. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19963. #else
  19964. ecc_key key[1];
  19965. #endif
  19966. #ifdef WC_NO_RNG
  19967. word32 idx = 0;
  19968. #endif
  19969. #ifdef WOLFSSL_SMALL_STACK
  19970. if (key == NULL)
  19971. ERROR_OUT(MEMORY_E, done);
  19972. #endif
  19973. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19974. /* Use API */
  19975. ret = wc_ecc_set_flags(NULL, 0);
  19976. if (ret != BAD_FUNC_ARG) {
  19977. ret = -10090;
  19978. goto done;
  19979. }
  19980. ret = wc_ecc_set_flags(key, 0);
  19981. if (ret != 0) {
  19982. ret = -10091;
  19983. goto done;
  19984. }
  19985. #ifndef WC_NO_RNG
  19986. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19987. #if defined(WOLFSSL_ASYNC_CRYPT)
  19988. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19989. #endif
  19990. #else
  19991. /* use test ECC key */
  19992. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  19993. (word32)sizeof_ecc_key_der_256);
  19994. (void)rng;
  19995. #endif
  19996. if (ret != 0) {
  19997. ret = -10092;
  19998. goto done;
  19999. }
  20000. TEST_SLEEP();
  20001. #ifndef NO_SIG_WRAPPER
  20002. ret = ecc_sig_test(rng, key);
  20003. if (ret < 0)
  20004. goto done;
  20005. #endif
  20006. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  20007. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20008. !defined(WOLFSSL_QNX_CAAM)
  20009. ret = ecc_exp_imp_test(key);
  20010. if (ret < 0)
  20011. goto done;
  20012. #endif
  20013. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20014. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_QNX_CAAM)
  20015. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  20016. ret = ecc_mulmod_test(key);
  20017. if (ret < 0)
  20018. goto done;
  20019. #endif
  20020. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20021. ret = ecc_ssh_test(key, rng);
  20022. if (ret < 0)
  20023. goto done;
  20024. #endif
  20025. #endif /* WOLFSSL_ATECC508A */
  20026. done:
  20027. #ifdef WOLFSSL_SMALL_STACK
  20028. if (key != NULL) {
  20029. wc_ecc_free(key);
  20030. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20031. }
  20032. #else
  20033. wc_ecc_free(key);
  20034. #endif
  20035. return ret;
  20036. }
  20037. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  20038. #ifdef WOLFSSL_CERT_EXT
  20039. static int ecc_decode_test(void)
  20040. {
  20041. int ret;
  20042. word32 inSz;
  20043. word32 inOutIdx;
  20044. #ifdef WOLFSSL_SMALL_STACK
  20045. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20046. #else
  20047. ecc_key key[1];
  20048. #endif
  20049. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  20050. /* This is ecc_clikeypub_der_256. */
  20051. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  20052. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  20053. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  20054. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  20055. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  20056. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  20057. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  20058. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  20059. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  20060. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  20061. 0xb4 };
  20062. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  20063. 0x00, 0x04, 0x01, 0x01 };
  20064. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  20065. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  20066. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  20067. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  20068. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  20069. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  20070. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  20071. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  20072. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  20073. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  20074. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  20075. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  20076. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  20077. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  20078. 0x03, 0x03, 0x04, 0x01, 0x01 };
  20079. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  20080. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  20081. 0x03, 0x03, 0x00, 0x04, 0x01 };
  20082. #ifdef WOLFSSL_SMALL_STACK
  20083. if (key == NULL)
  20084. ERROR_OUT(MEMORY_E, done);
  20085. #endif
  20086. XMEMSET(key, 0, sizeof *key);
  20087. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20088. inSz = sizeof(good);
  20089. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  20090. if (ret != BAD_FUNC_ARG) {
  20091. ret = -10100;
  20092. goto done;
  20093. }
  20094. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  20095. if (ret != BAD_FUNC_ARG) {
  20096. ret = -10101;
  20097. goto done;
  20098. }
  20099. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  20100. if (ret != BAD_FUNC_ARG) {
  20101. ret = -10102;
  20102. goto done;
  20103. }
  20104. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  20105. if (ret != BAD_FUNC_ARG) {
  20106. ret = -10103;
  20107. goto done;
  20108. }
  20109. /* Change offset to produce bad input data. */
  20110. inOutIdx = 2;
  20111. inSz = sizeof(good) - inOutIdx;
  20112. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  20113. if (ret != ASN_PARSE_E) {
  20114. ret = -10104;
  20115. goto done;
  20116. }
  20117. inOutIdx = 4;
  20118. inSz = sizeof(good) - inOutIdx;
  20119. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  20120. if (ret != ASN_PARSE_E) {
  20121. ret = -10105;
  20122. goto done;
  20123. }
  20124. /* Bad data. */
  20125. inSz = sizeof(badNoObjId);
  20126. inOutIdx = 0;
  20127. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  20128. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  20129. ret = -10106;
  20130. goto done;
  20131. }
  20132. inSz = sizeof(badOneObjId);
  20133. inOutIdx = 0;
  20134. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  20135. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  20136. ret = -10107;
  20137. goto done;
  20138. }
  20139. inSz = sizeof(badObjId1Len);
  20140. inOutIdx = 0;
  20141. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  20142. if (ret != ASN_PARSE_E) {
  20143. ret = -10108;
  20144. goto done;
  20145. }
  20146. inSz = sizeof(badObj2d1Len);
  20147. inOutIdx = 0;
  20148. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  20149. if (ret != ASN_PARSE_E) {
  20150. ret = -10109;
  20151. goto done;
  20152. }
  20153. inSz = sizeof(badNotBitStr);
  20154. inOutIdx = 0;
  20155. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  20156. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  20157. ret = -10110;
  20158. goto done;
  20159. }
  20160. inSz = sizeof(badBitStrLen);
  20161. inOutIdx = 0;
  20162. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  20163. if (ret != ASN_PARSE_E) {
  20164. ret = -10111;
  20165. goto done;
  20166. }
  20167. inSz = sizeof(badNoBitStrZero);
  20168. inOutIdx = 0;
  20169. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  20170. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  20171. ret = -10112;
  20172. goto done;
  20173. }
  20174. inSz = sizeof(badPoint);
  20175. inOutIdx = 0;
  20176. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  20177. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  20178. ret = -10113;
  20179. goto done;
  20180. }
  20181. inSz = sizeof(good);
  20182. inOutIdx = 0;
  20183. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  20184. if (ret != 0) {
  20185. ret = -10114;
  20186. goto done;
  20187. }
  20188. done:
  20189. #ifdef WOLFSSL_SMALL_STACK
  20190. if (key != NULL) {
  20191. wc_ecc_free(key);
  20192. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20193. }
  20194. #else
  20195. wc_ecc_free(key);
  20196. #endif
  20197. return ret;
  20198. }
  20199. #endif /* WOLFSSL_CERT_EXT */
  20200. #ifdef WOLFSSL_CUSTOM_CURVES
  20201. static const byte eccKeyExplicitCurve[] = {
  20202. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  20203. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  20204. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  20205. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  20206. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  20207. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  20208. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  20209. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  20210. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  20211. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  20212. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  20213. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  20214. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  20215. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  20216. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  20217. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  20218. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  20219. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  20220. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  20221. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  20222. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  20223. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  20224. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  20225. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  20226. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  20227. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  20228. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  20229. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  20230. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  20231. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  20232. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  20233. };
  20234. static int ecc_test_custom_curves(WC_RNG* rng)
  20235. {
  20236. int ret;
  20237. word32 inOutIdx;
  20238. #ifdef WOLFSSL_SMALL_STACK
  20239. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20240. #else
  20241. ecc_key key[1];
  20242. #endif
  20243. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  20244. #ifndef WOLFSSL_ECC_CURVE_STATIC
  20245. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  20246. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  20247. };
  20248. #define ecc_oid_brainpoolp256r1_sz \
  20249. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  20250. #else
  20251. #define ecc_oid_brainpoolp256r1 { \
  20252. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  20253. }
  20254. #define ecc_oid_brainpoolp256r1_sz 9
  20255. #endif
  20256. #define ecc_oid_brainpoolp256r1_sum 104
  20257. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  20258. 32, /* size/bytes */
  20259. ECC_CURVE_CUSTOM, /* ID */
  20260. "BRAINPOOLP256R1", /* curve name */
  20261. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  20262. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  20263. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  20264. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  20265. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  20266. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  20267. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  20268. ecc_oid_brainpoolp256r1_sz,
  20269. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  20270. 1, /* cofactor */
  20271. };
  20272. #ifdef WOLFSSL_SMALL_STACK
  20273. if (! key) {
  20274. ret = MEMORY_E;
  20275. goto done;
  20276. }
  20277. #endif
  20278. XMEMSET(key, 0, sizeof *key);
  20279. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  20280. &ecc_dp_brainpool256r1);
  20281. if (ret != 0) {
  20282. printf("ECC test for custom curve failed! %d\n", ret);
  20283. goto done;
  20284. }
  20285. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  20286. {
  20287. int curve_id;
  20288. #ifdef HAVE_ECC_BRAINPOOL
  20289. curve_id = ECC_BRAINPOOLP256R1;
  20290. #else
  20291. curve_id = ECC_SECP256K1;
  20292. #endif
  20293. /* Test and demonstrate use of non-SECP curve */
  20294. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  20295. if (ret < 0) {
  20296. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  20297. goto done;
  20298. }
  20299. }
  20300. #endif
  20301. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20302. if (ret != 0) {
  20303. ret = -10120;
  20304. goto done;
  20305. }
  20306. inOutIdx = 0;
  20307. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  20308. sizeof(eccKeyExplicitCurve));
  20309. if (ret != 0)
  20310. ret = -10121;
  20311. done:
  20312. #ifdef WOLFSSL_SMALL_STACK
  20313. if (key) {
  20314. wc_ecc_free(key);
  20315. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20316. }
  20317. #else
  20318. wc_ecc_free(key);
  20319. #endif
  20320. return ret;
  20321. }
  20322. #endif /* WOLFSSL_CUSTOM_CURVES */
  20323. #ifdef WOLFSSL_CERT_GEN
  20324. /* Make Cert / Sign example for ECC cert and ECC CA */
  20325. static int ecc_test_cert_gen(WC_RNG* rng)
  20326. {
  20327. int ret;
  20328. #ifdef WOLFSSL_SMALL_STACK
  20329. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20330. #ifdef WOLFSSL_TEST_CERT
  20331. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20332. #endif
  20333. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20334. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20335. #else
  20336. Cert myCert[1];
  20337. #ifdef WOLFSSL_TEST_CERT
  20338. DecodedCert decode[1];
  20339. #endif
  20340. ecc_key caEccKey[1];
  20341. ecc_key certPubKey[1];
  20342. #endif
  20343. int certSz;
  20344. size_t bytes;
  20345. word32 idx = 0;
  20346. #ifndef USE_CERT_BUFFERS_256
  20347. XFILE file;
  20348. #endif
  20349. #ifdef WOLFSSL_SMALL_STACK
  20350. byte* der = NULL;
  20351. #else
  20352. byte der[FOURK_BUF];
  20353. #endif
  20354. #ifdef WOLFSSL_SMALL_STACK
  20355. if ((myCert == NULL)
  20356. #ifdef WOLFSSL_TEST_CERT
  20357. || (decode == NULL)
  20358. #endif
  20359. || (caEccKey == NULL) || (certPubKey == NULL))
  20360. ERROR_OUT(MEMORY_E, exit);
  20361. #endif
  20362. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  20363. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  20364. #ifdef WOLFSSL_SMALL_STACK
  20365. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20366. if (der == NULL) {
  20367. ERROR_OUT(-10130, exit);
  20368. }
  20369. #endif
  20370. /* Get cert private key */
  20371. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  20372. /* Get Cert Key 384 */
  20373. #ifdef USE_CERT_BUFFERS_256
  20374. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  20375. bytes = sizeof_ca_ecc_key_der_384;
  20376. #else
  20377. file = XFOPEN(eccCaKey384File, "rb");
  20378. if (!file) {
  20379. ERROR_OUT(-10131, exit);
  20380. }
  20381. bytes = XFREAD(der, 1, FOURK_BUF, file);
  20382. XFCLOSE(file);
  20383. (void)eccCaKeyFile;
  20384. #endif /* USE_CERT_BUFFERS_256 */
  20385. #else
  20386. #ifdef USE_CERT_BUFFERS_256
  20387. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  20388. bytes = sizeof_ca_ecc_key_der_256;
  20389. #else
  20390. file = XFOPEN(eccCaKeyFile, "rb");
  20391. if (!file) {
  20392. ERROR_OUT(-10132, exit);
  20393. }
  20394. bytes = XFREAD(der, 1, FOURK_BUF, file);
  20395. XFCLOSE(file);
  20396. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  20397. (void)eccCaKey384File;
  20398. #endif
  20399. #endif /* USE_CERT_BUFFERS_256 */
  20400. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  20401. /* Get CA Key */
  20402. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  20403. if (ret != 0) {
  20404. ERROR_OUT(-10133, exit);
  20405. }
  20406. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  20407. if (ret != 0) {
  20408. ERROR_OUT(-10134, exit);
  20409. }
  20410. /* Make a public key */
  20411. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  20412. if (ret != 0) {
  20413. ERROR_OUT(-10135, exit);
  20414. }
  20415. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  20416. #if defined(WOLFSSL_ASYNC_CRYPT)
  20417. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  20418. #endif
  20419. if (ret != 0) {
  20420. ERROR_OUT(-10136, exit);
  20421. }
  20422. TEST_SLEEP();
  20423. /* Setup Certificate */
  20424. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  20425. ERROR_OUT(-10137, exit);
  20426. }
  20427. #ifndef NO_SHA256
  20428. myCert->sigType = CTC_SHA256wECDSA;
  20429. #else
  20430. myCert->sigType = CTC_SHAwECDSA;
  20431. #endif
  20432. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  20433. #ifdef WOLFSSL_CERT_EXT
  20434. /* add Policies */
  20435. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  20436. CTC_MAX_CERTPOL_SZ);
  20437. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  20438. CTC_MAX_CERTPOL_SZ);
  20439. myCert->certPoliciesNb = 2;
  20440. /* add SKID from the Public Key */
  20441. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  20442. ERROR_OUT(-10138, exit);
  20443. }
  20444. /* add AKID from the Public Key */
  20445. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  20446. ERROR_OUT(-10139, exit);
  20447. }
  20448. /* add Key Usage */
  20449. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  20450. ERROR_OUT(-10140, exit);
  20451. }
  20452. #endif /* WOLFSSL_CERT_EXT */
  20453. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  20454. #if defined(USE_CERT_BUFFERS_256)
  20455. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  20456. sizeof_ca_ecc_cert_der_384);
  20457. #else
  20458. ret = wc_SetIssuer(myCert, eccCaCert384File);
  20459. (void)eccCaCertFile;
  20460. #endif
  20461. #else
  20462. #if defined(USE_CERT_BUFFERS_256)
  20463. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  20464. sizeof_ca_ecc_cert_der_256);
  20465. #else
  20466. ret = wc_SetIssuer(myCert, eccCaCertFile);
  20467. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  20468. (void)eccCaCert384File;
  20469. #endif
  20470. #endif
  20471. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  20472. if (ret < 0) {
  20473. ERROR_OUT(-10141, exit);
  20474. }
  20475. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  20476. if (certSz < 0) {
  20477. ERROR_OUT(-10142, exit);
  20478. }
  20479. ret = 0;
  20480. do {
  20481. #if defined(WOLFSSL_ASYNC_CRYPT)
  20482. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20483. #endif
  20484. if (ret >= 0) {
  20485. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  20486. FOURK_BUF, NULL, caEccKey, rng);
  20487. }
  20488. } while (ret == WC_PENDING_E);
  20489. if (ret < 0) {
  20490. ERROR_OUT(-10143, exit);
  20491. }
  20492. certSz = ret;
  20493. TEST_SLEEP();
  20494. #ifdef WOLFSSL_TEST_CERT
  20495. InitDecodedCert(decode, der, certSz, 0);
  20496. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  20497. if (ret != 0) {
  20498. FreeDecodedCert(decode);
  20499. ERROR_OUT(-10144, exit);
  20500. }
  20501. FreeDecodedCert(decode);
  20502. #endif
  20503. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  20504. CERT_TYPE, -6735);
  20505. if (ret != 0) {
  20506. goto exit;
  20507. }
  20508. exit:
  20509. #ifdef WOLFSSL_SMALL_STACK
  20510. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20511. #endif
  20512. #ifdef WOLFSSL_SMALL_STACK
  20513. if (myCert != NULL)
  20514. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20515. #ifdef WOLFSSL_TEST_CERT
  20516. if (decode != NULL)
  20517. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20518. #endif
  20519. if (caEccKey != NULL) {
  20520. wc_ecc_free(caEccKey);
  20521. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20522. }
  20523. if (certPubKey != NULL) {
  20524. wc_ecc_free(certPubKey);
  20525. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20526. }
  20527. #else
  20528. wc_ecc_free(certPubKey);
  20529. wc_ecc_free(caEccKey);
  20530. #endif
  20531. return ret;
  20532. }
  20533. #endif /* WOLFSSL_CERT_GEN */
  20534. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  20535. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  20536. static int ecc_test_allocator(WC_RNG* rng)
  20537. {
  20538. int ret = 0;
  20539. ecc_key* key;
  20540. #ifdef WC_NO_RNG
  20541. word32 idx = 0;
  20542. #endif
  20543. key = wc_ecc_key_new(HEAP_HINT);
  20544. if (key == NULL) {
  20545. ERROR_OUT(-10150, exit);
  20546. }
  20547. #ifndef WC_NO_RNG
  20548. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20549. if (ret != 0) {
  20550. ERROR_OUT(-10151, exit);
  20551. }
  20552. #else
  20553. /* use test ECC key */
  20554. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  20555. (word32)sizeof_ecc_key_der_256);
  20556. (void)rng;
  20557. #endif
  20558. exit:
  20559. wc_ecc_key_free(key);
  20560. return ret;
  20561. }
  20562. #endif
  20563. /* ECC Non-blocking tests for Sign and Verify */
  20564. /* Requires SP math and supports P384 or P256 */
  20565. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  20566. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  20567. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  20568. /* Test Data - Random */
  20569. static const uint8_t kMsg[] = {
  20570. 0x69, 0xbc, 0x9f, 0xce, 0x68, 0x17, 0xc2, 0x10, 0xea, 0xfc, 0x10, 0x65, 0x67, 0x52, 0xed, 0x78,
  20571. 0x6e, 0xb8, 0x83, 0x9c, 0x9a, 0xb4, 0x56, 0x0d, 0xc1, 0x0d, 0x1f, 0x78, 0x6e, 0x75, 0xd7, 0xbe,
  20572. 0x92, 0x6b, 0x12, 0xf6, 0x76, 0x60, 0x8e, 0xb1, 0xf4, 0x19, 0x0c, 0x81, 0xe7, 0x54, 0x5e, 0xbc,
  20573. 0xe0, 0xae, 0xc2, 0x7d, 0x1b, 0xc4, 0x6e, 0xec, 0xb1, 0x99, 0x6c, 0xbf, 0x0e, 0x38, 0xa8, 0x01,
  20574. 0xa6, 0x9a, 0x48, 0x12, 0xe4, 0xc9, 0x3b, 0xf0, 0x63, 0x46, 0x15, 0xb4, 0x61, 0xa8, 0x1a, 0x60,
  20575. 0x71, 0x87, 0x98, 0xd7, 0x6f, 0x98, 0x7b, 0x2d, 0xb9, 0x19, 0x1b, 0x21, 0x9c, 0x70, 0x58, 0xe8,
  20576. 0x0d, 0x0f, 0xe9, 0x2d, 0x9a, 0x9a, 0xf1, 0x55, 0xa0, 0x4c, 0xd3, 0x07, 0xbd, 0x97, 0x48, 0xec,
  20577. 0x88, 0x0a, 0xaf, 0xb3, 0x80, 0x78, 0xa4, 0x59, 0x43, 0x57, 0xd3, 0xa7, 0x01, 0x66, 0x0e, 0xfc
  20578. };
  20579. /* ECC Private Key "d" */
  20580. static const uint8_t kPrivKey[] = {
  20581. #ifdef HAVE_ECC384
  20582. /* SECP384R1 */
  20583. /* d */
  20584. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  20585. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  20586. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  20587. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  20588. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  20589. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  20590. #else
  20591. /* SECP256R1 */
  20592. /* d */
  20593. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  20594. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  20595. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  20596. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  20597. #endif
  20598. };
  20599. /* ECC public key Qx/Qy */
  20600. static const uint8_t kPubKey[] = {
  20601. #ifdef HAVE_ECC384
  20602. /* SECP384R1 */
  20603. /* Qx */
  20604. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  20605. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  20606. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  20607. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  20608. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  20609. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  20610. /* Qy */
  20611. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  20612. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  20613. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  20614. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  20615. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  20616. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  20617. #else
  20618. /* SECP256R1 */
  20619. /* Qx */
  20620. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  20621. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  20622. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  20623. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  20624. /* Qy */
  20625. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  20626. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  20627. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  20628. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  20629. #endif
  20630. };
  20631. /* ECC Curve */
  20632. #ifdef HAVE_ECC384
  20633. /* SECP384R1 */
  20634. #define ECC_CURVE_SZ 48
  20635. #define ECC_CURVE_ID ECC_SECP384R1
  20636. #else
  20637. /* SECP256R1 */
  20638. #define ECC_CURVE_SZ 32
  20639. #define ECC_CURVE_ID ECC_SECP256R1
  20640. #endif
  20641. /* Hash Algorithm */
  20642. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  20643. #define HASH_DIGEST_SZ WC_SHA3_384_DIGEST_SIZE
  20644. #define HASH_SHA_VER 3
  20645. #define CRYPTO_HASH_FN crypto_sha3_384
  20646. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  20647. #define HASH_DIGEST_SZ WC_SHA384_DIGEST_SIZE
  20648. #define HASH_SHA_VER 2
  20649. #define CRYPTO_HASH_FN crypto_sha2_384
  20650. #elif !defined(NO_SHA256)
  20651. #define HASH_DIGEST_SZ WC_SHA256_DIGEST_SIZE
  20652. #define HASH_SHA_VER 2
  20653. #define CRYPTO_HASH_FN crypto_sha2_256
  20654. #else
  20655. #error test configuration not supported
  20656. #endif
  20657. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  20658. /* helper to perform hashing block by block */
  20659. static int crypto_sha3_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  20660. uint32_t hashSz, uint32_t blkSz)
  20661. {
  20662. int ret;
  20663. uint32_t i = 0, chunk;
  20664. wc_Sha3 sha3;
  20665. /* validate arguments */
  20666. if ((buf == NULL && len > 0) || hash == NULL ||
  20667. hashSz < WC_SHA3_384_DIGEST_SIZE || blkSz == 0)
  20668. {
  20669. return BAD_FUNC_ARG;
  20670. }
  20671. /* Init Sha3_384 structure */
  20672. ret = wc_InitSha3_384(&sha3, NULL, INVALID_DEVID);
  20673. if (ret != 0) {
  20674. return ret;
  20675. }
  20676. while (i < len) {
  20677. chunk = blkSz;
  20678. if ((chunk + i) > len)
  20679. chunk = len - i;
  20680. /* Perform chunked update */
  20681. ret = wc_Sha3_384_Update(&sha3, (buf + i), chunk);
  20682. if (ret != 0) {
  20683. break;
  20684. }
  20685. i += chunk;
  20686. }
  20687. if (ret == 0) {
  20688. /* Get final digest result */
  20689. ret = wc_Sha3_384_Final(&sha3, hash);
  20690. }
  20691. return ret;
  20692. }
  20693. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  20694. /* helper to perform hashing block by block */
  20695. static int crypto_sha2_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  20696. uint32_t hashSz, uint32_t blkSz)
  20697. {
  20698. int ret;
  20699. uint32_t i = 0, chunk;
  20700. wc_Sha384 sha384;
  20701. /* validate arguments */
  20702. if ((buf == NULL && len > 0) || hash == NULL ||
  20703. hashSz < WC_SHA384_DIGEST_SIZE || blkSz == 0)
  20704. {
  20705. return BAD_FUNC_ARG;
  20706. }
  20707. /* Init Sha384 structure */
  20708. ret = wc_InitSha384(&sha384);
  20709. if (ret != 0) {
  20710. return ret;
  20711. }
  20712. while (i < len) {
  20713. chunk = blkSz;
  20714. if ((chunk + i) > len)
  20715. chunk = len - i;
  20716. /* Perform chunked update */
  20717. ret = wc_Sha384Update(&sha384, (buf + i), chunk);
  20718. if (ret != 0) {
  20719. break;
  20720. }
  20721. i += chunk;
  20722. }
  20723. if (ret == 0) {
  20724. /* Get final digest result */
  20725. ret = wc_Sha384Final(&sha384, hash);
  20726. }
  20727. return ret;
  20728. }
  20729. #elif !defined(NO_SHA256)
  20730. /* helper to perform hashing block by block */
  20731. static int crypto_sha2_256(const uint8_t *buf, uint32_t len, uint8_t *hash,
  20732. uint32_t hashSz, uint32_t blkSz)
  20733. {
  20734. int ret;
  20735. uint32_t i = 0, chunk;
  20736. wc_Sha256 sha256;
  20737. /* validate arguments */
  20738. if ((buf == NULL && len > 0) || hash == NULL ||
  20739. hashSz < WC_SHA256_DIGEST_SIZE || blkSz == 0)
  20740. {
  20741. return BAD_FUNC_ARG;
  20742. }
  20743. /* Init Sha256 structure */
  20744. ret = wc_InitSha256(&sha256);
  20745. if (ret != 0) {
  20746. return ret;
  20747. }
  20748. while (i < len) {
  20749. chunk = blkSz;
  20750. if ((chunk + i) > len)
  20751. chunk = len - i;
  20752. /* Perform chunked update */
  20753. ret = wc_Sha256Update(&sha256, (buf + i), chunk);
  20754. if (ret != 0) {
  20755. break;
  20756. }
  20757. i += chunk;
  20758. }
  20759. if (ret == 0) {
  20760. /* Get final digest result */
  20761. ret = wc_Sha256Final(&sha256, hash);
  20762. }
  20763. return ret;
  20764. }
  20765. #endif
  20766. /* perform verify of signature and hash using public key */
  20767. /* key is public Qx + public Qy */
  20768. /* sig is r + s */
  20769. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  20770. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  20771. uint32_t curveSz, int curveId)
  20772. {
  20773. int ret, verify_res = 0, count = 0;
  20774. mp_int r, s;
  20775. ecc_key ecc;
  20776. ecc_nb_ctx_t nb_ctx;
  20777. /* validate arguments */
  20778. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  20779. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  20780. {
  20781. return BAD_FUNC_ARG;
  20782. }
  20783. /* Setup the ECC key */
  20784. ret = wc_ecc_init(&ecc);
  20785. if (ret < 0) {
  20786. return ret;
  20787. }
  20788. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20789. if (ret != MP_OKAY) {
  20790. wc_ecc_free(&ecc);
  20791. return ret;
  20792. }
  20793. /* Setup the signature r/s variables */
  20794. ret = mp_init(&r);
  20795. if (ret != MP_OKAY) {
  20796. wc_ecc_free(&ecc);
  20797. return ret;
  20798. }
  20799. ret = mp_init(&s);
  20800. if (ret != MP_OKAY) {
  20801. mp_clear(&r);
  20802. wc_ecc_free(&ecc);
  20803. return ret;
  20804. }
  20805. /* Import public key x/y */
  20806. ret = wc_ecc_import_unsigned(
  20807. &ecc,
  20808. (byte*)key, /* Public "x" Coordinate */
  20809. (byte*)(key + curveSz), /* Public "y" Coordinate */
  20810. NULL, /* Private "d" (optional) */
  20811. curveId /* ECC Curve Id */
  20812. );
  20813. /* Make sure it was a public key imported */
  20814. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  20815. ret = ECC_BAD_ARG_E;
  20816. }
  20817. /* Import signature r/s */
  20818. if (ret == 0) {
  20819. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  20820. }
  20821. if (ret == 0) {
  20822. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  20823. }
  20824. /* Verify ECC Signature */
  20825. if (ret == 0) {
  20826. do {
  20827. ret = wc_ecc_verify_hash_ex(
  20828. &r, &s, /* r/s as mp_int */
  20829. hash, hashSz, /* computed hash digest */
  20830. &verify_res, /* verification result 1=success */
  20831. &ecc
  20832. );
  20833. count++;
  20834. /* This is where real-time work could be called */
  20835. } while (ret == FP_WOULDBLOCK);
  20836. #ifdef DEBUG_WOLFSSL
  20837. printf("ECC non-block verify: %d times\n", count);
  20838. #endif
  20839. }
  20840. /* check verify result */
  20841. if (ret == 0 && verify_res == 0) {
  20842. ret = SIG_VERIFY_E;
  20843. }
  20844. mp_clear(&r);
  20845. mp_clear(&s);
  20846. wc_ecc_free(&ecc);
  20847. (void)count;
  20848. return ret;
  20849. }
  20850. /* perform signature operation against hash using private key */
  20851. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  20852. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  20853. uint32_t curveSz, int curveId, WC_RNG* rng)
  20854. {
  20855. int ret, count = 0;
  20856. mp_int r, s;
  20857. ecc_key ecc;
  20858. ecc_nb_ctx_t nb_ctx;
  20859. /* validate arguments */
  20860. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  20861. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  20862. {
  20863. return BAD_FUNC_ARG;
  20864. }
  20865. /* Initialize signature result */
  20866. memset(sig, 0, curveSz*2);
  20867. /* Setup the ECC key */
  20868. ret = wc_ecc_init(&ecc);
  20869. if (ret < 0) {
  20870. return ret;
  20871. }
  20872. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20873. if (ret != MP_OKAY) {
  20874. wc_ecc_free(&ecc);
  20875. return ret;
  20876. }
  20877. /* Setup the signature r/s variables */
  20878. ret = mp_init(&r);
  20879. if (ret != MP_OKAY) {
  20880. wc_ecc_free(&ecc);
  20881. return ret;
  20882. }
  20883. ret = mp_init(&s);
  20884. if (ret != MP_OKAY) {
  20885. mp_clear(&r);
  20886. wc_ecc_free(&ecc);
  20887. return ret;
  20888. }
  20889. /* Import private key "k" */
  20890. ret = wc_ecc_import_private_key_ex(
  20891. key, keySz, /* private key "d" */
  20892. NULL, 0, /* public (optional) */
  20893. &ecc,
  20894. curveId /* ECC Curve Id */
  20895. );
  20896. if (ret == 0) {
  20897. do {
  20898. /* Verify ECC Signature */
  20899. ret = wc_ecc_sign_hash_ex(
  20900. hash, hashSz, /* computed hash digest */
  20901. rng, &ecc, /* random and key context */
  20902. &r, &s /* r/s as mp_int */
  20903. );
  20904. count++;
  20905. /* This is where real-time work could be called */
  20906. } while (ret == FP_WOULDBLOCK);
  20907. #ifdef DEBUG_WOLFSSL
  20908. printf("ECC non-block sign: %d times\n", count);
  20909. #endif
  20910. }
  20911. if (ret == 0) {
  20912. /* export r/s */
  20913. mp_to_unsigned_bin_len(&r, sig, curveSz);
  20914. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  20915. }
  20916. mp_clear(&r);
  20917. mp_clear(&s);
  20918. wc_ecc_free(&ecc);
  20919. (void)count;
  20920. return ret;
  20921. }
  20922. static int ecc_test_nonblock(WC_RNG* rng)
  20923. {
  20924. int ret;
  20925. uint8_t hash[HASH_DIGEST_SZ];
  20926. uint8_t sig[ECC_CURVE_SZ*2];
  20927. uint32_t sigSz = sizeof(sig);
  20928. ret = CRYPTO_HASH_FN(
  20929. kMsg, sizeof(kMsg), /* input message */
  20930. hash, sizeof(hash), /* hash digest result */
  20931. 32 /* configurable block / chunk size */
  20932. );
  20933. if (ret == 0) {
  20934. /* Sign hash using private key */
  20935. /* Note: result of an ECC sign varies for each call even with same
  20936. private key and hash. This is because a new random public key is
  20937. used for each operation. */
  20938. ret = crypto_ecc_sign(
  20939. kPrivKey, sizeof(kPrivKey), /* private key */
  20940. hash, sizeof(hash), /* computed hash digest */
  20941. sig, &sigSz, /* signature r/s */
  20942. ECC_CURVE_SZ, /* curve size in bytes */
  20943. ECC_CURVE_ID, /* curve id */
  20944. rng
  20945. );
  20946. }
  20947. if (ret == 0) {
  20948. /* Verify generated signature is valid */
  20949. ret = crypto_ecc_verify(
  20950. kPubKey, sizeof(kPubKey), /* public key point x/y */
  20951. hash, sizeof(hash), /* computed hash digest */
  20952. sig, sigSz, /* signature r/s */
  20953. ECC_CURVE_SZ, /* curve size in bytes */
  20954. ECC_CURVE_ID /* curve id */
  20955. );
  20956. }
  20957. return ret;
  20958. }
  20959. #endif /* WC_ECC_NONBLOCK && WOLFSSL_PUBLIC_MP && HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  20960. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  20961. {
  20962. int ret;
  20963. WC_RNG rng;
  20964. #ifdef WOLFSSL_CERT_EXT
  20965. ret = ecc_decode_test();
  20966. if (ret < 0)
  20967. return ret;
  20968. #endif
  20969. #ifndef HAVE_FIPS
  20970. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20971. #else
  20972. ret = wc_InitRng(&rng);
  20973. #endif
  20974. #ifndef WC_NO_RNG
  20975. if (ret != 0)
  20976. return -10300;
  20977. #else
  20978. (void)ret;
  20979. #endif
  20980. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  20981. ret = ecc_test_curve(&rng, 14);
  20982. if (ret < 0) {
  20983. goto done;
  20984. }
  20985. #endif /* HAVE_ECC112 */
  20986. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  20987. ret = ecc_test_curve(&rng, 16);
  20988. if (ret < 0) {
  20989. goto done;
  20990. }
  20991. #endif /* HAVE_ECC128 */
  20992. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  20993. ret = ecc_test_curve(&rng, 20);
  20994. if (ret < 0) {
  20995. goto done;
  20996. }
  20997. #endif /* HAVE_ECC160 */
  20998. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  20999. ret = ecc_test_curve(&rng, 24);
  21000. if (ret < 0) {
  21001. goto done;
  21002. }
  21003. #endif /* HAVE_ECC192 */
  21004. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  21005. ret = ecc_test_curve(&rng, 28);
  21006. if (ret < 0) {
  21007. goto done;
  21008. }
  21009. #endif /* HAVE_ECC224 */
  21010. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  21011. ret = ecc_test_curve(&rng, 30);
  21012. if (ret < 0) {
  21013. goto done;
  21014. }
  21015. #endif /* HAVE_ECC239 */
  21016. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21017. ret = ecc_test_curve(&rng, 32);
  21018. if (ret < 0) {
  21019. goto done;
  21020. }
  21021. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21022. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21023. !defined(WOLFSSL_NO_MALLOC)
  21024. ret = ecc_point_test();
  21025. if (ret < 0) {
  21026. goto done;
  21027. }
  21028. #endif
  21029. ret = ecc_def_curve_test(&rng);
  21030. if (ret < 0) {
  21031. goto done;
  21032. }
  21033. #endif /* !NO_ECC256 */
  21034. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  21035. ret = ecc_test_curve(&rng, 40);
  21036. if (ret < 0) {
  21037. goto done;
  21038. }
  21039. #endif /* HAVE_ECC320 */
  21040. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  21041. ret = ecc_test_curve(&rng, 48);
  21042. if (ret < 0) {
  21043. goto done;
  21044. }
  21045. #endif /* HAVE_ECC384 */
  21046. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  21047. ret = ecc_test_curve(&rng, 64);
  21048. if (ret < 0) {
  21049. goto done;
  21050. }
  21051. #endif /* HAVE_ECC512 */
  21052. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  21053. ret = ecc_test_curve(&rng, 66);
  21054. if (ret < 0) {
  21055. goto done;
  21056. }
  21057. #endif /* HAVE_ECC521 */
  21058. #if defined(WOLFSSL_CUSTOM_CURVES)
  21059. ret = ecc_test_custom_curves(&rng);
  21060. if (ret != 0) {
  21061. goto done;
  21062. }
  21063. #endif
  21064. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_DETERMINISTIC_K)
  21065. ret = ecc_test_deterministic_k(&rng);
  21066. if (ret != 0) {
  21067. printf("ecc_test_deterministic_k failed! %d\n", ret);
  21068. goto done;
  21069. }
  21070. #endif
  21071. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  21072. ret = ecc_test_sign_vectors(&rng);
  21073. if (ret != 0) {
  21074. printf("ecc_test_sign_vectors failed! %d\n", ret);
  21075. goto done;
  21076. }
  21077. #endif
  21078. #ifdef HAVE_ECC_CDH
  21079. ret = ecc_test_cdh_vectors(&rng);
  21080. if (ret != 0) {
  21081. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  21082. goto done;
  21083. }
  21084. #endif
  21085. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21086. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL)
  21087. ret = ecc_test_make_pub(&rng);
  21088. if (ret != 0) {
  21089. printf("ecc_test_make_pub failed!: %d\n", ret);
  21090. goto done;
  21091. }
  21092. #elif defined(HAVE_ECC_KEY_IMPORT)
  21093. (void)ecc_test_make_pub; /* for compiler warning */
  21094. #endif
  21095. #ifdef WOLFSSL_CERT_GEN
  21096. ret = ecc_test_cert_gen(&rng);
  21097. if (ret != 0) {
  21098. printf("ecc_test_cert_gen failed!: %d\n", ret);
  21099. goto done;
  21100. }
  21101. #endif
  21102. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  21103. ret = ecc_test_allocator(&rng);
  21104. if (ret != 0) {
  21105. printf("ecc_test_allocator failed!: %d\n", ret);
  21106. goto done;
  21107. }
  21108. #endif
  21109. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  21110. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  21111. ret = ecc_test_nonblock(&rng);
  21112. if (ret != 0) {
  21113. printf("ecc_test_nonblock failed!: %d\n", ret);
  21114. goto done;
  21115. }
  21116. #endif
  21117. done:
  21118. wc_FreeRng(&rng);
  21119. return ret;
  21120. }
  21121. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  21122. defined(WOLFSSL_AES_128)
  21123. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21124. static int ecc_encrypt_kat(WC_RNG *rng)
  21125. {
  21126. int ret = 0;
  21127. #ifdef WOLFSSL_ECIES_OLD
  21128. #ifdef WOLFSSL_SMALL_STACK
  21129. ecc_key* userA = NULL;
  21130. #else
  21131. ecc_key userA[1];
  21132. #endif
  21133. int userAInit = 0;
  21134. #endif
  21135. #ifdef WOLFSSL_SMALL_STACK
  21136. ecc_key* userB = NULL;
  21137. #else
  21138. ecc_key userB[1];
  21139. #endif
  21140. int userBInit = 0;
  21141. ecc_key* tmpKey;
  21142. byte plain[48];
  21143. word32 plainSz = sizeof(plain);
  21144. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  21145. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  21146. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  21147. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  21148. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  21149. };
  21150. #ifdef WOLFSSL_ECIES_OLD
  21151. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  21152. 0x04,
  21153. /* X */
  21154. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  21155. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  21156. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  21157. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  21158. /* X */
  21159. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  21160. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  21161. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  21162. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  21163. };
  21164. #endif
  21165. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  21166. #ifdef WOLFSSL_ECIES_OLD
  21167. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  21168. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  21169. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  21170. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  21171. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  21172. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  21173. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  21174. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  21175. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  21176. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  21177. #elif defined(WOLFSSL_ECIES_ISO18033)
  21178. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  21179. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  21180. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  21181. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  21182. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  21183. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  21184. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  21185. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  21186. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  21187. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  21188. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  21189. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  21190. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  21191. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  21192. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  21193. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  21194. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  21195. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  21196. 0x7f
  21197. #else
  21198. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  21199. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  21200. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  21201. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  21202. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  21203. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  21204. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  21205. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  21206. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  21207. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  21208. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  21209. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  21210. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  21211. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  21212. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  21213. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  21214. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  21215. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  21216. 0x55
  21217. #endif
  21218. };
  21219. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  21220. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21221. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  21222. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  21223. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  21224. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  21225. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  21226. };
  21227. #ifdef WOLFSSL_SMALL_STACK
  21228. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  21229. DYNAMIC_TYPE_TMP_BUFFER);
  21230. if (userB == NULL) {
  21231. ret = -10451;
  21232. }
  21233. #ifdef WOLFSSL_ECIES_OLD
  21234. if (ret == 0) {
  21235. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  21236. DYNAMIC_TYPE_TMP_BUFFER);
  21237. if (userA == NULL) {
  21238. ret = -10450;
  21239. }
  21240. }
  21241. #endif
  21242. #endif
  21243. if (ret == 0) {
  21244. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21245. if (ret != 0)
  21246. ret = -10453;
  21247. }
  21248. if (ret == 0) {
  21249. userBInit = 1;
  21250. #ifdef WOLFSSL_ECIES_OLD
  21251. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21252. if (ret != 0)
  21253. ret = -10452;
  21254. }
  21255. if (ret == 0) {
  21256. userAInit = 1;
  21257. tmpKey = userA;
  21258. #else
  21259. tmpKey = NULL;
  21260. #endif
  21261. }
  21262. if (ret == 0) {
  21263. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  21264. userB, ECC_SECP256R1);
  21265. if (ret != 0)
  21266. ret = -10454;
  21267. }
  21268. #ifdef WOLFSSL_ECIES_OLD
  21269. if (ret == 0) {
  21270. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  21271. ECC_SECP256R1);
  21272. if (ret != 0)
  21273. ret = -10455;
  21274. }
  21275. #endif
  21276. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21277. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21278. !defined(HAVE_SELFTEST)
  21279. if (ret == 0) {
  21280. ret = wc_ecc_set_rng(userB, rng);
  21281. if (ret != 0) {
  21282. ret = -10456;
  21283. }
  21284. }
  21285. #endif
  21286. if (ret == 0) {
  21287. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  21288. &plainSz, NULL);
  21289. if (ret != 0)
  21290. ret = -10457;
  21291. }
  21292. if (ret == 0) {
  21293. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  21294. ret = -10458;
  21295. }
  21296. }
  21297. if (userBInit)
  21298. wc_ecc_free(userB);
  21299. #ifdef WOLFSSL_ECIES_OLD
  21300. if (userAInit)
  21301. wc_ecc_free(userA);
  21302. #endif
  21303. #ifdef WOLFSSL_SMALL_STACK
  21304. if (userB != NULL) {
  21305. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21306. }
  21307. #ifdef WOLFSSL_ECIES_OLD
  21308. if (userA != NULL) {
  21309. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21310. }
  21311. #endif
  21312. #endif
  21313. return ret;
  21314. }
  21315. #endif
  21316. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  21317. {
  21318. WC_RNG rng;
  21319. int ret = 0;
  21320. #ifdef WOLFSSL_SMALL_STACK
  21321. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  21322. *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  21323. *tmpKey = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21324. #else
  21325. ecc_key userA[1], userB[1], tmpKey[1];
  21326. #endif
  21327. byte msg[48];
  21328. byte plain[48];
  21329. #ifdef WOLFSSL_ECIES_OLD
  21330. byte out[80];
  21331. #else
  21332. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  21333. #endif
  21334. word32 outSz = sizeof(out);
  21335. word32 plainSz = sizeof(plain);
  21336. int i;
  21337. ecEncCtx* cliCtx = NULL;
  21338. ecEncCtx* srvCtx = NULL;
  21339. byte cliSalt[EXCHANGE_SALT_SZ];
  21340. byte srvSalt[EXCHANGE_SALT_SZ];
  21341. const byte* tmpSalt;
  21342. byte msg2[48];
  21343. byte plain2[48];
  21344. #ifdef WOLFSSL_ECIES_OLD
  21345. byte out2[80];
  21346. #else
  21347. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  21348. #endif
  21349. word32 outSz2 = sizeof(out2);
  21350. word32 plainSz2 = sizeof(plain2);
  21351. #ifndef HAVE_FIPS
  21352. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21353. #else
  21354. ret = wc_InitRng(&rng);
  21355. #endif
  21356. if (ret != 0)
  21357. return -10400;
  21358. #ifdef WOLFSSL_SMALL_STACK
  21359. if ((userA == NULL) ||
  21360. (userB == NULL))
  21361. ERROR_OUT(MEMORY_E, done);
  21362. #endif
  21363. XMEMSET(userA, 0, sizeof *userA);
  21364. XMEMSET(userB, 0, sizeof *userB);
  21365. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21366. if (ret != 0)
  21367. goto done;
  21368. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21369. if (ret != 0)
  21370. goto done;
  21371. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  21372. if (ret != 0)
  21373. goto done;
  21374. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  21375. #if defined(WOLFSSL_ASYNC_CRYPT)
  21376. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21377. #endif
  21378. if (ret != 0){
  21379. ret = -10401; goto done;
  21380. }
  21381. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  21382. #if defined(WOLFSSL_ASYNC_CRYPT)
  21383. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21384. #endif
  21385. if (ret != 0){
  21386. ret = -10402; goto done;
  21387. }
  21388. /* set message to incrementing 0,1,2,etc... */
  21389. for (i = 0; i < (int)sizeof(msg); i++)
  21390. msg[i] = i;
  21391. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21392. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21393. !defined(HAVE_SELFTEST)
  21394. ret = wc_ecc_set_rng(userA, &rng);
  21395. if (ret != 0) {
  21396. ret = -10403; goto done;
  21397. }
  21398. ret = wc_ecc_set_rng(userB, &rng);
  21399. if (ret != 0) {
  21400. ret = -10404; goto done;
  21401. }
  21402. #endif
  21403. /* encrypt msg to B */
  21404. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  21405. if (ret != 0) {
  21406. ret = -10405; goto done;
  21407. }
  21408. #ifdef WOLFSSL_ECIES_OLD
  21409. tmpKey->dp = userA->dp;
  21410. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  21411. if (ret != 0) {
  21412. ret = -10413; goto done;
  21413. }
  21414. #endif
  21415. /* decrypt msg from A */
  21416. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  21417. if (ret != 0) {
  21418. ret = -10406; goto done;
  21419. }
  21420. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  21421. ret = -10407; goto done;
  21422. }
  21423. #ifndef WOLFSSL_ECIES_OLD
  21424. /* A decrypts msg (response) from B */
  21425. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  21426. if (ret != 0)
  21427. goto done;
  21428. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  21429. ret = -10415; goto done;
  21430. }
  21431. #endif
  21432. /* let's verify message exchange works, A is client, B is server */
  21433. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  21434. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  21435. if (cliCtx == NULL || srvCtx == NULL) {
  21436. ret = -10408; goto done;
  21437. }
  21438. /* get salt to send to peer */
  21439. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  21440. if (tmpSalt == NULL) {
  21441. ret = -10409; goto done;
  21442. }
  21443. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  21444. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  21445. if (tmpSalt == NULL) {
  21446. ret = -10410; goto done;
  21447. }
  21448. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  21449. /* in actual use, we'd get the peer's salt over the transport */
  21450. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  21451. if (ret != 0)
  21452. goto done;
  21453. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  21454. if (ret != 0)
  21455. goto done;
  21456. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  21457. if (ret != 0)
  21458. goto done;
  21459. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  21460. if (ret != 0)
  21461. goto done;
  21462. /* get encrypted msg (request) to send to B */
  21463. outSz = sizeof(out);
  21464. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  21465. if (ret != 0)
  21466. goto done;
  21467. #ifndef WOLFSSL_ECIES_OLD
  21468. wc_ecc_free(tmpKey);
  21469. #endif
  21470. /* B decrypts msg (request) from A */
  21471. plainSz = sizeof(plain);
  21472. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  21473. if (ret != 0)
  21474. goto done;
  21475. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  21476. ret = -10411; goto done;
  21477. }
  21478. /* msg2 (response) from B to A */
  21479. for (i = 0; i < (int)sizeof(msg2); i++)
  21480. msg2[i] = i + sizeof(msg2);
  21481. /* get encrypted msg (response) to send to B */
  21482. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  21483. &outSz2, srvCtx);
  21484. if (ret != 0)
  21485. goto done;
  21486. #ifdef WOLFSSL_ECIES_OLD
  21487. tmpKey->dp = userB->dp;
  21488. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  21489. if (ret != 0) {
  21490. ret = -10414; goto done;
  21491. }
  21492. #else
  21493. wc_ecc_free(tmpKey);
  21494. #endif
  21495. /* A decrypts msg (response) from B */
  21496. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  21497. cliCtx);
  21498. if (ret != 0)
  21499. goto done;
  21500. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  21501. ret = -10412; goto done;
  21502. }
  21503. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21504. ret = ecc_encrypt_kat(&rng);
  21505. #endif
  21506. done:
  21507. /* cleanup */
  21508. wc_ecc_ctx_free(srvCtx);
  21509. wc_ecc_ctx_free(cliCtx);
  21510. #ifdef WOLFSSL_SMALL_STACK
  21511. if (userA != NULL) {
  21512. wc_ecc_free(userA);
  21513. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21514. }
  21515. if (userB != NULL) {
  21516. wc_ecc_free(userB);
  21517. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21518. }
  21519. if (tmpKey != NULL) {
  21520. wc_ecc_free(tmpKey);
  21521. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21522. }
  21523. #else
  21524. wc_ecc_free(tmpKey);
  21525. wc_ecc_free(userB);
  21526. wc_ecc_free(userA);
  21527. #endif
  21528. wc_FreeRng(&rng);
  21529. return ret;
  21530. }
  21531. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  21532. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  21533. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  21534. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  21535. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  21536. {
  21537. size_t bytes;
  21538. #ifdef WOLFSSL_SMALL_STACK
  21539. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21540. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21541. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21542. #else
  21543. ecc_key cliKey[1];
  21544. ecc_key servKey[1];
  21545. ecc_key tmpKey[1];
  21546. #endif
  21547. WC_RNG rng;
  21548. word32 idx = 0;
  21549. int ret;
  21550. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  21551. byte in[] = "Everyone gets Friday off. ecc p";
  21552. word32 inLen = (word32)XSTRLEN((char*)in);
  21553. byte out[256];
  21554. byte plain[256];
  21555. int verify = 0;
  21556. word32 x;
  21557. #ifdef WOLFSSL_SMALL_STACK
  21558. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  21559. ERROR_OUT(MEMORY_E, done);
  21560. #endif
  21561. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  21562. if (ret != 0)
  21563. ERROR_OUT(-10420, done);
  21564. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  21565. if (ret != 0)
  21566. ERROR_OUT(-10421, done);
  21567. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  21568. if (ret != 0)
  21569. ERROR_OUT(-10421, done);
  21570. bytes = (size_t)sizeof_ecc_clikey_der_256;
  21571. /* place client key into ecc_key struct cliKey */
  21572. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  21573. (word32)bytes);
  21574. if (ret != 0)
  21575. ERROR_OUT(-10422, done);
  21576. idx = 0;
  21577. bytes = (size_t)sizeof_ecc_key_der_256;
  21578. /* place server key into ecc_key struct servKey */
  21579. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  21580. (word32)bytes);
  21581. if (ret != 0)
  21582. ERROR_OUT(-10423, done);
  21583. #ifndef WC_NO_RNG
  21584. #ifndef HAVE_FIPS
  21585. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21586. #else
  21587. ret = wc_InitRng(&rng);
  21588. #endif
  21589. if (ret != 0)
  21590. ERROR_OUT(-10424, done);
  21591. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21592. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21593. !defined(HAVE_SELFTEST)
  21594. ret = wc_ecc_set_rng(cliKey, &rng);
  21595. if (ret != 0) {
  21596. ERROR_OUT(-10425, done);
  21597. }
  21598. ret = wc_ecc_set_rng(servKey, &rng);
  21599. if (ret != 0) {
  21600. ERROR_OUT(-10425, done);
  21601. }
  21602. #endif
  21603. #endif /* !WC_NO_RNG */
  21604. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  21605. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21606. {
  21607. word32 y;
  21608. /* test encrypt and decrypt if they're available */
  21609. x = sizeof(out);
  21610. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  21611. if (ret < 0)
  21612. ERROR_OUT(-10426, done);
  21613. #ifdef WOLFSSL_ECIES_OLD
  21614. tmpKey->dp = cliKey->dp;
  21615. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  21616. if (ret != 0) {
  21617. ret = -10414; goto done;
  21618. }
  21619. #endif
  21620. y = sizeof(plain);
  21621. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  21622. if (ret < 0)
  21623. ERROR_OUT(-10427, done);
  21624. if (XMEMCMP(plain, in, inLen))
  21625. ERROR_OUT(-10428, done);
  21626. }
  21627. #endif
  21628. x = sizeof(out);
  21629. do {
  21630. #if defined(WOLFSSL_ASYNC_CRYPT)
  21631. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21632. #endif
  21633. if (ret == 0)
  21634. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  21635. } while (ret == WC_PENDING_E);
  21636. if (ret < 0)
  21637. ERROR_OUT(-10429, done);
  21638. TEST_SLEEP();
  21639. XMEMSET(plain, 0, sizeof(plain));
  21640. do {
  21641. #if defined(WOLFSSL_ASYNC_CRYPT)
  21642. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21643. #endif
  21644. if (ret == 0)
  21645. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  21646. cliKey);
  21647. } while (ret == WC_PENDING_E);
  21648. if (ret < 0)
  21649. ERROR_OUT(-10430, done);
  21650. if (verify != 1)
  21651. ERROR_OUT(-10431, done);
  21652. TEST_SLEEP();
  21653. #ifdef WOLFSSL_CERT_EXT
  21654. idx = 0;
  21655. bytes = sizeof_ecc_clikeypub_der_256;
  21656. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  21657. (word32) bytes);
  21658. if (ret != 0)
  21659. ERROR_OUT(-10432, done);
  21660. #endif
  21661. ret = 0;
  21662. done:
  21663. #ifdef WOLFSSL_SMALL_STACK
  21664. if (cliKey != NULL) {
  21665. wc_ecc_free(cliKey);
  21666. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21667. }
  21668. if (servKey != NULL) {
  21669. wc_ecc_free(servKey);
  21670. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21671. }
  21672. if (tmpKey != NULL) {
  21673. wc_ecc_free(tmpKey);
  21674. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21675. }
  21676. #else
  21677. wc_ecc_free(cliKey);
  21678. wc_ecc_free(servKey);
  21679. wc_ecc_free(tmpKey);
  21680. #endif
  21681. wc_FreeRng(&rng);
  21682. return ret;
  21683. }
  21684. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  21685. #endif /* HAVE_ECC */
  21686. #ifdef HAVE_CURVE25519
  21687. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  21688. defined(HAVE_CURVE25519_KEY_IMPORT)
  21689. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  21690. #define X25519_TEST_CNT 5
  21691. #else
  21692. #define X25519_TEST_CNT 1
  21693. #endif
  21694. static int curve25519_overflow_test(void)
  21695. {
  21696. /* secret key for party a */
  21697. byte sa[X25519_TEST_CNT][32] = {
  21698. {
  21699. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  21700. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  21701. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  21702. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  21703. },
  21704. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  21705. {
  21706. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  21707. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  21708. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  21709. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  21710. },
  21711. {
  21712. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  21713. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  21714. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  21715. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  21716. },
  21717. {
  21718. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  21719. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  21720. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  21721. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  21722. },
  21723. {
  21724. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  21725. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  21726. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  21727. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  21728. }
  21729. #endif
  21730. };
  21731. /* public key for party b */
  21732. byte pb[X25519_TEST_CNT][32] = {
  21733. {
  21734. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  21735. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  21736. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  21737. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  21738. },
  21739. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  21740. {
  21741. /* 0xff first byte in original - invalid! */
  21742. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  21743. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  21744. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  21745. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  21746. },
  21747. {
  21748. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  21749. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  21750. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  21751. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  21752. },
  21753. {
  21754. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  21755. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  21756. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  21757. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  21758. },
  21759. {
  21760. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  21761. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  21762. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  21763. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  21764. }
  21765. #endif
  21766. };
  21767. /* expected shared key */
  21768. byte ss[X25519_TEST_CNT][32] = {
  21769. {
  21770. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  21771. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  21772. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  21773. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  21774. },
  21775. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  21776. {
  21777. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  21778. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  21779. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  21780. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  21781. },
  21782. {
  21783. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21784. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21785. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21786. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  21787. },
  21788. {
  21789. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21790. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21792. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  21793. },
  21794. {
  21795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  21799. }
  21800. #endif
  21801. };
  21802. int ret = 0;
  21803. int i;
  21804. word32 y;
  21805. byte shared[32];
  21806. curve25519_key userA;
  21807. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  21808. for (i = 0; i < X25519_TEST_CNT; i++) {
  21809. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  21810. sizeof(pb[i]), &userA) != 0) {
  21811. ret = -10500 - i; break;
  21812. }
  21813. /* test against known test vector */
  21814. XMEMSET(shared, 0, sizeof(shared));
  21815. y = sizeof(shared);
  21816. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  21817. ret = -10510 - i; break;
  21818. }
  21819. if (XMEMCMP(ss[i], shared, y)) {
  21820. ret = -10520 - i; break;
  21821. }
  21822. }
  21823. wc_curve25519_free(&userA);
  21824. return ret;
  21825. }
  21826. /* Test the wc_curve25519_check_public API.
  21827. *
  21828. * returns 0 on success and -ve on failure.
  21829. */
  21830. static int curve25519_check_public_test(void)
  21831. {
  21832. /* Little-endian values that will fail */
  21833. byte fail_le[][CURVE25519_KEYSIZE] = {
  21834. {
  21835. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21836. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21837. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21839. },
  21840. {
  21841. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21842. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21843. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21844. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21845. },
  21846. {
  21847. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21848. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21849. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21850. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  21851. },
  21852. };
  21853. /* Big-endian values that will fail */
  21854. byte fail_be[][CURVE25519_KEYSIZE] = {
  21855. {
  21856. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21857. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21858. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21859. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21860. },
  21861. {
  21862. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21863. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21864. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21865. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21866. },
  21867. {
  21868. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21869. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21870. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21871. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21872. },
  21873. };
  21874. /* Good or valid public value */
  21875. byte good[CURVE25519_KEYSIZE] = {
  21876. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21877. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21878. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21879. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21880. };
  21881. int i;
  21882. /* Parameter checks */
  21883. /* NULL pointer */
  21884. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  21885. BAD_FUNC_ARG) {
  21886. return -10600;
  21887. }
  21888. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  21889. BAD_FUNC_ARG) {
  21890. return -10601;
  21891. }
  21892. /* Length of 0 treated differently to other invalid lengths for TLS */
  21893. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  21894. return -10602;
  21895. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  21896. return -10603;
  21897. /* Length not CURVE25519_KEYSIZE */
  21898. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  21899. if (i == CURVE25519_KEYSIZE)
  21900. continue;
  21901. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  21902. ECC_BAD_ARG_E) {
  21903. return -10604 - i;
  21904. }
  21905. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  21906. ECC_BAD_ARG_E) {
  21907. return -10614 - i;
  21908. }
  21909. }
  21910. /* Little-endian fail cases */
  21911. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  21912. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  21913. EC25519_LITTLE_ENDIAN) == 0) {
  21914. return -10624 - i;
  21915. }
  21916. }
  21917. /* Big-endian fail cases */
  21918. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  21919. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  21920. EC25519_BIG_ENDIAN) == 0) {
  21921. return -10634 - i;
  21922. }
  21923. }
  21924. /* Check a valid public value works! */
  21925. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  21926. EC25519_LITTLE_ENDIAN) != 0) {
  21927. return -10644;
  21928. }
  21929. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  21930. EC25519_BIG_ENDIAN) != 0) {
  21931. return -10645;
  21932. }
  21933. return 0;
  21934. }
  21935. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  21936. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  21937. defined(HAVE_CURVE25519_KEY_IMPORT)
  21938. static int curve255519_der_test(void)
  21939. {
  21940. int ret = 0;
  21941. /* certs/statickeys/x25519.der */
  21942. const byte kCurve25519PrivDer[] = {
  21943. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  21944. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  21945. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  21946. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  21947. };
  21948. /* certs/statickeys/x25519-pub.der */
  21949. const byte kCurve25519PubDer[] = {
  21950. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  21951. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  21952. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  21953. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  21954. };
  21955. curve25519_key key;
  21956. byte output[128];
  21957. word32 outputSz = 128;
  21958. word32 idx;
  21959. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  21960. return -10723;
  21961. }
  21962. /* Test decode / encode of Curve25519 private key only */
  21963. if (ret == 0) {
  21964. idx = 0;
  21965. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  21966. (word32)sizeof(kCurve25519PrivDer));
  21967. }
  21968. if (ret == 0) {
  21969. outputSz = (word32)sizeof(output);
  21970. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  21971. if (ret >= 0) {
  21972. outputSz = ret;
  21973. ret = 0;
  21974. }
  21975. else {
  21976. ret = -10724;
  21977. }
  21978. }
  21979. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  21980. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  21981. ret = -10725;
  21982. }
  21983. /* Test decode / encode of Curve25519 public key only */
  21984. if (ret == 0) {
  21985. idx = 0;
  21986. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  21987. (word32)sizeof(kCurve25519PubDer));
  21988. }
  21989. if (ret == 0) {
  21990. outputSz = (word32)sizeof(output);
  21991. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  21992. if (ret >= 0) {
  21993. outputSz = ret;
  21994. ret = 0;
  21995. }
  21996. else {
  21997. ret = -10726;
  21998. }
  21999. }
  22000. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  22001. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  22002. ret = -10727;
  22003. }
  22004. wc_curve25519_free(&key);
  22005. return ret;
  22006. }
  22007. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  22008. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  22009. {
  22010. WC_RNG rng;
  22011. int ret;
  22012. #ifdef HAVE_CURVE25519_SHARED_SECRET
  22013. byte sharedA[32];
  22014. byte sharedB[32];
  22015. word32 y;
  22016. #endif
  22017. #ifdef HAVE_CURVE25519_KEY_EXPORT
  22018. byte exportBuf[32];
  22019. #endif
  22020. word32 x = 0;
  22021. curve25519_key userA, userB, pubKey;
  22022. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  22023. defined(HAVE_CURVE25519_KEY_IMPORT)
  22024. /* test vectors from
  22025. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  22026. */
  22027. /* secret key for party a */
  22028. byte sa[] = {
  22029. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  22030. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  22031. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  22032. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  22033. };
  22034. /* public key for party a */
  22035. byte pa[] = {
  22036. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  22037. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  22038. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  22039. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  22040. };
  22041. /* secret key for party b */
  22042. byte sb[] = {
  22043. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  22044. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  22045. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  22046. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  22047. };
  22048. /* public key for party b */
  22049. byte pb[] = {
  22050. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  22051. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  22052. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  22053. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  22054. };
  22055. /* expected shared key */
  22056. byte ss[] = {
  22057. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  22058. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  22059. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  22060. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  22061. };
  22062. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  22063. (void)x;
  22064. #ifndef HAVE_FIPS
  22065. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22066. #else
  22067. ret = wc_InitRng(&rng);
  22068. #endif
  22069. if (ret != 0)
  22070. return -10700;
  22071. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  22072. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  22073. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  22074. /* make curve25519 keys */
  22075. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  22076. return -10701;
  22077. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  22078. return -10702;
  22079. #ifdef HAVE_CURVE25519_SHARED_SECRET
  22080. /* find shared secret key */
  22081. x = sizeof(sharedA);
  22082. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22083. return -10703;
  22084. y = sizeof(sharedB);
  22085. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22086. return -10704;
  22087. /* compare shared secret keys to test they are the same */
  22088. if (y != x)
  22089. return -10705;
  22090. if (XMEMCMP(sharedA, sharedB, x))
  22091. return -10706;
  22092. #endif
  22093. #ifdef HAVE_CURVE25519_KEY_EXPORT
  22094. /* export a public key and import it for another user */
  22095. x = sizeof(exportBuf);
  22096. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  22097. return -10707;
  22098. #ifdef HAVE_CURVE25519_KEY_IMPORT
  22099. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  22100. return -10708;
  22101. #endif
  22102. #endif
  22103. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  22104. defined(HAVE_CURVE25519_KEY_IMPORT)
  22105. /* test shared key after importing a public key */
  22106. XMEMSET(sharedB, 0, sizeof(sharedB));
  22107. y = sizeof(sharedB);
  22108. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  22109. return -10709;
  22110. if (XMEMCMP(sharedA, sharedB, y))
  22111. return -10710;
  22112. /* import RFC test vectors and compare shared key */
  22113. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22114. != 0)
  22115. return -10711;
  22116. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  22117. != 0)
  22118. return -10712;
  22119. /* test against known test vector */
  22120. XMEMSET(sharedB, 0, sizeof(sharedB));
  22121. y = sizeof(sharedB);
  22122. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  22123. return -10713;
  22124. if (XMEMCMP(ss, sharedB, y))
  22125. return -10714;
  22126. /* test swapping roles of keys and generating same shared key */
  22127. XMEMSET(sharedB, 0, sizeof(sharedB));
  22128. y = sizeof(sharedB);
  22129. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22130. return -10715;
  22131. if (XMEMCMP(ss, sharedB, y))
  22132. return -10716;
  22133. /* test with 1 generated key and 1 from known test vector */
  22134. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22135. != 0)
  22136. return -10717;
  22137. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  22138. return -10718;
  22139. x = sizeof(sharedA);
  22140. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22141. return -10719;
  22142. y = sizeof(sharedB);
  22143. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22144. return -10720;
  22145. /* compare shared secret keys to test they are the same */
  22146. if (y != x)
  22147. return -10721;
  22148. if (XMEMCMP(sharedA, sharedB, x))
  22149. return -10722;
  22150. ret = curve25519_overflow_test();
  22151. if (ret != 0)
  22152. return ret;
  22153. ret = curve25519_check_public_test();
  22154. if (ret != 0)
  22155. return ret;
  22156. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  22157. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  22158. defined(HAVE_CURVE25519_KEY_IMPORT)
  22159. ret = curve255519_der_test();
  22160. if (ret != 0)
  22161. return ret;
  22162. #endif
  22163. /* clean up keys when done */
  22164. wc_curve25519_free(&pubKey);
  22165. wc_curve25519_free(&userB);
  22166. wc_curve25519_free(&userA);
  22167. wc_FreeRng(&rng);
  22168. return 0;
  22169. }
  22170. #endif /* HAVE_CURVE25519 */
  22171. #ifdef HAVE_ED25519
  22172. #ifdef WOLFSSL_TEST_CERT
  22173. static int ed25519_test_cert(void)
  22174. {
  22175. DecodedCert cert[2];
  22176. DecodedCert* serverCert = NULL;
  22177. DecodedCert* caCert = NULL;
  22178. #ifdef HAVE_ED25519_VERIFY
  22179. ed25519_key key;
  22180. ed25519_key* pubKey = NULL;
  22181. int verify;
  22182. #endif /* HAVE_ED25519_VERIFY */
  22183. int ret;
  22184. byte* tmp;
  22185. size_t bytes;
  22186. XFILE file;
  22187. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22188. if (tmp == NULL) {
  22189. ERROR_OUT(-10730, done);
  22190. }
  22191. #ifdef USE_CERT_BUFFERS_256
  22192. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  22193. bytes = sizeof_ca_ed25519_cert;
  22194. #elif !defined(NO_FILESYSTEM)
  22195. file = XFOPEN(caEd25519Cert, "rb");
  22196. if (file == NULL) {
  22197. ERROR_OUT(-10731, done);
  22198. }
  22199. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22200. XFCLOSE(file);
  22201. #else
  22202. /* No certificate to use. */
  22203. ERROR_OUT(-10732, done);
  22204. #endif
  22205. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  22206. caCert = &cert[0];
  22207. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  22208. if (ret != 0) {
  22209. ERROR_OUT(-10733, done);
  22210. }
  22211. #ifdef USE_CERT_BUFFERS_256
  22212. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  22213. bytes = sizeof_server_ed25519_cert;
  22214. #elif !defined(NO_FILESYSTEM)
  22215. file = XFOPEN(serverEd25519Cert, "rb");
  22216. if (file == NULL) {
  22217. ERROR_OUT(-10734, done);
  22218. }
  22219. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22220. XFCLOSE(file);
  22221. #else
  22222. /* No certificate to use. */
  22223. ERROR_OUT(-10735, done);
  22224. #endif
  22225. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  22226. serverCert = &cert[1];
  22227. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  22228. if (ret != 0) {
  22229. ERROR_OUT(-10736, done);
  22230. }
  22231. #ifdef HAVE_ED25519_VERIFY
  22232. ret = wc_ed25519_init(&key);
  22233. if (ret < 0) {
  22234. ERROR_OUT(-10737, done);
  22235. }
  22236. pubKey = &key;
  22237. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  22238. pubKey);
  22239. if (ret < 0) {
  22240. ERROR_OUT(-10738, done);
  22241. }
  22242. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  22243. serverCert->source + serverCert->certBegin,
  22244. serverCert->sigIndex - serverCert->certBegin,
  22245. &verify, pubKey) < 0 || verify != 1) {
  22246. ERROR_OUT(-10739, done);
  22247. }
  22248. #endif /* HAVE_ED25519_VERIFY */
  22249. done:
  22250. if (tmp != NULL)
  22251. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22252. #ifdef HAVE_ED25519_VERIFY
  22253. wc_ed25519_free(pubKey);
  22254. #endif /* HAVE_ED25519_VERIFY */
  22255. if (caCert != NULL)
  22256. FreeDecodedCert(caCert);
  22257. if (serverCert != NULL)
  22258. FreeDecodedCert(serverCert);
  22259. return ret;
  22260. }
  22261. static int ed25519_test_make_cert(void)
  22262. {
  22263. WC_RNG rng;
  22264. Cert cert;
  22265. DecodedCert decode;
  22266. ed25519_key key;
  22267. ed25519_key* privKey = NULL;
  22268. int ret = 0;
  22269. byte* tmp = NULL;
  22270. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  22271. #ifndef HAVE_FIPS
  22272. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22273. #else
  22274. ret = wc_InitRng(&rng);
  22275. #endif
  22276. if (ret != 0)
  22277. return -10750;
  22278. wc_ed25519_init(&key);
  22279. privKey = &key;
  22280. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  22281. cert.daysValid = 365 * 2;
  22282. cert.selfSigned = 1;
  22283. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  22284. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  22285. cert.isCA = 0;
  22286. #ifdef WOLFSSL_CERT_EXT
  22287. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  22288. if (ret < 0) {
  22289. ERROR_OUT(-10751, done);
  22290. }
  22291. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  22292. if (ret < 0) {
  22293. ERROR_OUT(-10752, done);
  22294. }
  22295. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  22296. if (ret < 0) {
  22297. ERROR_OUT(-10753, done);
  22298. }
  22299. #endif
  22300. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22301. if (tmp == NULL) {
  22302. ERROR_OUT(-10754, done);
  22303. }
  22304. cert.sigType = CTC_ED25519;
  22305. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  22306. if (ret < 0) {
  22307. ERROR_OUT(-10755, done);
  22308. }
  22309. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  22310. ED25519_TYPE, privKey, &rng);
  22311. if (ret < 0) {
  22312. ERROR_OUT(-10756, done);
  22313. }
  22314. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  22315. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  22316. FreeDecodedCert(&decode);
  22317. if (ret != 0) {
  22318. ERROR_OUT(-10757, done);
  22319. }
  22320. done:
  22321. if (tmp != NULL)
  22322. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22323. wc_ed25519_free(privKey);
  22324. wc_FreeRng(&rng);
  22325. return ret;
  22326. }
  22327. #endif /* WOLFSSL_TEST_CERT */
  22328. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22329. defined(HAVE_ED25519_KEY_IMPORT)
  22330. static int ed25519ctx_test(void)
  22331. {
  22332. int ret;
  22333. byte out[ED25519_SIG_SIZE];
  22334. word32 outlen;
  22335. #ifdef HAVE_ED25519_VERIFY
  22336. int verify = 0;
  22337. #endif /* HAVE_ED25519_VERIFY */
  22338. ed25519_key key;
  22339. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  22340. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  22341. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  22342. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  22343. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  22344. };
  22345. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  22346. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  22347. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  22348. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  22349. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  22350. };
  22351. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  22352. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  22353. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  22354. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  22355. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  22356. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  22357. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  22358. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  22359. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  22360. };
  22361. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  22362. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  22363. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  22364. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  22365. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  22366. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  22367. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  22368. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  22369. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  22370. };
  22371. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  22372. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  22373. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  22374. };
  22375. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  22376. 0x66,0x6f,0x6f
  22377. };
  22378. outlen = sizeof(out);
  22379. XMEMSET(out, 0, sizeof(out));
  22380. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  22381. if (ret != 0)
  22382. return 10800;
  22383. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  22384. sizeof(pKeyCtx), &key);
  22385. if (ret == 0)
  22386. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  22387. contextCtx, sizeof(contextCtx));
  22388. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  22389. ret = -10801;
  22390. #if defined(HAVE_ED25519_VERIFY)
  22391. /* test verify on good msg */
  22392. if (ret == 0)
  22393. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  22394. &verify, &key, contextCtx, sizeof(contextCtx));
  22395. if (ret == 0 && verify != 1)
  22396. ret = -10802;
  22397. #endif
  22398. if (ret == 0)
  22399. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  22400. NULL, 0);
  22401. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  22402. ret = -10803;
  22403. #if defined(HAVE_ED25519_VERIFY)
  22404. /* test verify on good msg */
  22405. if (ret == 0)
  22406. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  22407. &verify, &key, NULL, 0);
  22408. if (ret == 0 && verify != 1)
  22409. ret = -10804;
  22410. #endif
  22411. wc_ed25519_free(&key);
  22412. return ret;
  22413. }
  22414. static int ed25519ph_test(void)
  22415. {
  22416. int ret = 0;
  22417. byte out[ED25519_SIG_SIZE];
  22418. word32 outlen;
  22419. #ifdef HAVE_ED25519_VERIFY
  22420. int verify = 0;
  22421. #endif /* HAVE_ED25519_VERIFY */
  22422. ed25519_key key;
  22423. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  22424. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  22425. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  22426. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  22427. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  22428. };
  22429. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  22430. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  22431. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  22432. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  22433. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  22434. };
  22435. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  22436. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  22437. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  22438. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  22439. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  22440. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  22441. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  22442. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  22443. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  22444. };
  22445. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  22446. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  22447. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  22448. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  22449. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  22450. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  22451. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  22452. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  22453. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  22454. };
  22455. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  22456. 0x61,0x62,0x63
  22457. };
  22458. /* SHA-512 hash of msgPh */
  22459. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  22460. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  22461. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  22462. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  22463. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  22464. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  22465. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  22466. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  22467. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  22468. };
  22469. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  22470. 0x66,0x6f,0x6f
  22471. };
  22472. outlen = sizeof(out);
  22473. XMEMSET(out, 0, sizeof(out));
  22474. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  22475. if (ret != 0)
  22476. return -10900;
  22477. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  22478. sizeof(pKeyPh), &key);
  22479. if (ret == 0)
  22480. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  22481. NULL, 0);
  22482. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  22483. ret = -10901;
  22484. #if defined(HAVE_ED25519_VERIFY)
  22485. /* test verify on good msg */
  22486. if (ret == 0)
  22487. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  22488. &verify, &key, NULL, 0);
  22489. if (ret == 0 && verify != 1)
  22490. ret = -10902;
  22491. #endif
  22492. if (ret == 0)
  22493. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  22494. contextPh2, sizeof(contextPh2));
  22495. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  22496. ret = -10903;
  22497. #if defined(HAVE_ED25519_VERIFY)
  22498. /* test verify on good msg */
  22499. if (ret == 0)
  22500. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  22501. &key, contextPh2, sizeof(contextPh2));
  22502. if (ret == 0 && verify != 1)
  22503. ret = -10904;
  22504. #endif
  22505. if (ret == 0)
  22506. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  22507. NULL, 0);
  22508. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  22509. ret = -10905;
  22510. #if defined(HAVE_ED25519_VERIFY)
  22511. if (ret == 0)
  22512. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  22513. &verify, &key, NULL, 0);
  22514. if (ret == 0 && verify != 1)
  22515. ret = -10906;
  22516. #endif
  22517. if (ret == 0)
  22518. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  22519. contextPh2, sizeof(contextPh2));
  22520. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  22521. ret = -10907;
  22522. #if defined(HAVE_ED25519_VERIFY)
  22523. if (ret == 0)
  22524. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22525. &key, contextPh2, sizeof(contextPh2));
  22526. if (ret == 0 && verify != 1)
  22527. ret = -10908;
  22528. #endif
  22529. wc_ed25519_free(&key);
  22530. return ret;
  22531. }
  22532. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  22533. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  22534. {
  22535. int ret;
  22536. WC_RNG rng;
  22537. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  22538. defined(HAVE_ED25519_KEY_IMPORT)
  22539. byte out[ED25519_SIG_SIZE];
  22540. byte exportPKey[ED25519_KEY_SIZE];
  22541. byte exportSKey[ED25519_KEY_SIZE];
  22542. word32 exportPSz;
  22543. word32 exportSSz;
  22544. int i;
  22545. word32 outlen;
  22546. #ifdef HAVE_ED25519_VERIFY
  22547. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  22548. int j;
  22549. #endif
  22550. int verify;
  22551. #endif /* HAVE_ED25519_VERIFY */
  22552. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  22553. word32 keySz, sigSz;
  22554. ed25519_key key;
  22555. ed25519_key key2;
  22556. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22557. defined(HAVE_ED25519_KEY_IMPORT)
  22558. /* test vectors from
  22559. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  22560. */
  22561. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  22562. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  22563. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  22564. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  22565. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  22566. };
  22567. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  22568. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  22569. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  22570. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  22571. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  22572. };
  22573. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  22574. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  22575. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  22576. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  22577. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  22578. };
  22579. /* uncompressed test */
  22580. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  22581. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  22582. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  22583. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  22584. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  22585. };
  22586. /* compressed prefix test */
  22587. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  22588. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  22589. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  22590. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  22591. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  22592. };
  22593. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  22594. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  22595. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  22596. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  22597. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  22598. };
  22599. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  22600. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  22601. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  22602. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  22603. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  22604. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  22605. };
  22606. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  22607. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  22608. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  22609. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  22610. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  22611. };
  22612. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  22613. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  22614. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  22615. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  22616. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  22617. };
  22618. /* uncompressed test */
  22619. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  22620. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  22621. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  22622. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  22623. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  22624. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  22625. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  22626. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  22627. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  22628. 0xd7
  22629. };
  22630. /* compressed prefix */
  22631. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  22632. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  22633. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  22634. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  22635. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  22636. };
  22637. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  22638. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  22639. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  22640. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  22641. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  22642. };
  22643. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  22644. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  22645. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  22646. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  22647. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  22648. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  22649. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  22650. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  22651. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  22652. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  22653. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  22654. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  22655. };
  22656. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  22657. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  22658. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  22659. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  22660. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  22661. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  22662. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  22663. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  22664. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  22665. };
  22666. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  22667. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  22668. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  22669. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  22670. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  22671. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  22672. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  22673. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  22674. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  22675. };
  22676. /* uncompressed test */
  22677. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  22678. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  22679. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  22680. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  22681. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  22682. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  22683. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  22684. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  22685. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  22686. };
  22687. /* compressed prefix */
  22688. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  22689. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  22690. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  22691. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  22692. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  22693. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  22694. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  22695. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  22696. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  22697. };
  22698. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  22699. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  22700. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  22701. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  22702. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  22703. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  22704. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  22705. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  22706. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  22707. };
  22708. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  22709. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  22710. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  22711. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  22712. /* test of a 1024 byte long message */
  22713. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  22714. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  22715. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  22716. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  22717. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  22718. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  22719. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  22720. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  22721. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  22722. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  22723. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  22724. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  22725. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  22726. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  22727. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  22728. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  22729. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  22730. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  22731. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  22732. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  22733. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  22734. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  22735. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  22736. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  22737. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  22738. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  22739. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  22740. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  22741. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  22742. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  22743. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  22744. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  22745. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  22746. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  22747. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  22748. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  22749. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  22750. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  22751. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  22752. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  22753. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  22754. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  22755. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  22756. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  22757. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  22758. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  22759. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  22760. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  22761. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  22762. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  22763. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  22764. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  22765. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  22766. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  22767. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  22768. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  22769. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  22770. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  22771. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  22772. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  22773. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  22774. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  22775. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  22776. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  22777. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  22778. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  22779. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  22780. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  22781. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  22782. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  22783. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  22784. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  22785. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  22786. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  22787. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  22788. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  22789. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  22790. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  22791. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  22792. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  22793. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  22794. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  22795. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  22796. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  22797. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  22798. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  22799. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  22800. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  22801. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  22802. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  22803. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  22804. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  22805. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  22806. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  22807. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  22808. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  22809. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  22810. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  22811. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  22812. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  22813. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  22814. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  22815. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  22816. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  22817. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  22818. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  22819. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  22820. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  22821. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  22822. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  22823. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  22824. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  22825. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  22826. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  22827. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  22828. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  22829. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  22830. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  22831. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  22832. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  22833. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  22834. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  22835. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  22836. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  22837. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  22838. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  22839. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  22840. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  22841. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  22842. };
  22843. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  22844. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  22845. sizeof(msg2),
  22846. sizeof(msg3),
  22847. 0 /*sizeof(msg1)*/,
  22848. 0 /*sizeof(msg1)*/,
  22849. sizeof(msg4)
  22850. };
  22851. #ifndef NO_ASN
  22852. static byte privateEd25519[] = {
  22853. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  22854. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  22855. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  22856. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  22857. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  22858. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  22859. };
  22860. static byte publicEd25519[] = {
  22861. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  22862. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  22863. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  22864. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  22865. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  22866. 0xf7,0x07,0x51,0x1a
  22867. };
  22868. static byte privPubEd25519[] = {
  22869. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  22870. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  22871. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  22872. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  22873. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  22874. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  22875. 0xa1,0x22,0x04,0x20,0xd7,0x5a,0x98,0x01,
  22876. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  22877. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  22878. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  22879. 0xf7,0x07,0x51,0x1a
  22880. };
  22881. word32 idx;
  22882. #endif /* NO_ASN */
  22883. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  22884. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  22885. ed25519_key key3;
  22886. #endif
  22887. /* create ed25519 keys */
  22888. #ifndef HAVE_FIPS
  22889. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22890. #else
  22891. ret = wc_InitRng(&rng);
  22892. #endif
  22893. if (ret != 0)
  22894. return -11000;
  22895. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  22896. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  22897. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  22898. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  22899. #endif
  22900. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  22901. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  22902. /* helper functions for signature and key size */
  22903. keySz = wc_ed25519_size(&key);
  22904. sigSz = wc_ed25519_sig_size(&key);
  22905. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  22906. defined(HAVE_ED25519_KEY_IMPORT)
  22907. for (i = 0; i < 6; i++) {
  22908. outlen = sizeof(out);
  22909. XMEMSET(out, 0, sizeof(out));
  22910. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  22911. pKeySz[i], &key) != 0)
  22912. return -11001 - i;
  22913. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  22914. return -11011 - i;
  22915. if (XMEMCMP(out, sigs[i], 64))
  22916. return -11021 - i;
  22917. #if defined(HAVE_ED25519_VERIFY)
  22918. /* test verify on good msg */
  22919. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  22920. &key) != 0 || verify != 1)
  22921. return -11031 - i;
  22922. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  22923. /* test verify on good msg using streaming interface directly */
  22924. if (wc_ed25519_verify_msg_init(out, outlen,
  22925. &key, (byte)Ed25519, NULL, 0) != 0)
  22926. return -11211 - i;
  22927. for (j = 0; j < msgSz[i]; j += i) {
  22928. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  22929. return -11221 - i;
  22930. }
  22931. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  22932. &key) != 0 || verify != 1)
  22933. return -11231 - i;
  22934. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  22935. /* test verify on bad msg */
  22936. out[outlen-1] = out[outlen-1] + 1;
  22937. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  22938. &key) == 0 || verify == 1)
  22939. return -11041 - i;
  22940. #endif /* HAVE_ED25519_VERIFY */
  22941. /* test api for import/exporting keys */
  22942. exportPSz = sizeof(exportPKey);
  22943. exportSSz = sizeof(exportSKey);
  22944. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  22945. return -11051 - i;
  22946. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  22947. return -11061 - i;
  22948. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  22949. return -11071 - i;
  22950. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  22951. exportPKey, exportPSz, &key2) != 0)
  22952. return -11081 - i;
  22953. /* clear "out" buffer and test sign with imported keys */
  22954. outlen = sizeof(out);
  22955. XMEMSET(out, 0, sizeof(out));
  22956. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  22957. return -11091 - i;
  22958. #if defined(HAVE_ED25519_VERIFY)
  22959. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  22960. &key2) != 0 || verify != 1)
  22961. return -11101 - i;
  22962. if (XMEMCMP(out, sigs[i], 64))
  22963. return -11111 - i;
  22964. #endif /* HAVE_ED25519_VERIFY */
  22965. }
  22966. ret = ed25519ctx_test();
  22967. if (ret != 0)
  22968. return ret;
  22969. ret = ed25519ph_test();
  22970. if (ret != 0)
  22971. return ret;
  22972. #ifndef NO_ASN
  22973. /* Try ASN.1 encoded private-only key and public key. */
  22974. idx = 0;
  22975. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  22976. sizeof(privateEd25519)) != 0)
  22977. return -11121;
  22978. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  22979. != BAD_FUNC_ARG)
  22980. return -11131;
  22981. idx = 0;
  22982. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  22983. sizeof(publicEd25519)) != 0)
  22984. return -11141;
  22985. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  22986. return -11151;
  22987. if (XMEMCMP(out, sigs[0], 64))
  22988. return -11161;
  22989. #if defined(HAVE_ED25519_VERIFY)
  22990. /* test verify on good msg */
  22991. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  22992. != 0 || verify != 1)
  22993. return -11171;
  22994. #endif /* HAVE_ED25519_VERIFY */
  22995. wc_ed25519_free(&key3);
  22996. wc_ed25519_init(&key3);
  22997. idx = 0;
  22998. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  22999. sizeof(privPubEd25519)) != 0)
  23000. return -11181;
  23001. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  23002. return -11191;
  23003. if (XMEMCMP(out, sigs[0], 64))
  23004. return -11201;
  23005. wc_ed25519_free(&key3);
  23006. #endif /* NO_ASN */
  23007. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  23008. /* clean up keys when done */
  23009. wc_ed25519_free(&key);
  23010. wc_ed25519_free(&key2);
  23011. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  23012. wc_FreeRng(&rng);
  23013. #endif
  23014. /* hush warnings of unused keySz and sigSz */
  23015. (void)keySz;
  23016. (void)sigSz;
  23017. #ifdef WOLFSSL_TEST_CERT
  23018. ret = ed25519_test_cert();
  23019. if (ret < 0)
  23020. return ret;
  23021. #ifdef WOLFSSL_CERT_GEN
  23022. ret = ed25519_test_make_cert();
  23023. if (ret < 0)
  23024. return ret;
  23025. #endif /* WOLFSSL_CERT_GEN */
  23026. #endif /* WOLFSSL_TEST_CERT */
  23027. return 0;
  23028. }
  23029. #endif /* HAVE_ED25519 */
  23030. #ifdef HAVE_CURVE448
  23031. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  23032. defined(HAVE_CURVE448_KEY_IMPORT)
  23033. /* Test the wc_curve448_check_public API.
  23034. *
  23035. * returns 0 on success and -ve on failure.
  23036. */
  23037. static int curve448_check_public_test(void)
  23038. {
  23039. /* Little-endian values that will fail */
  23040. byte fail_le[][CURVE448_KEY_SIZE] = {
  23041. {
  23042. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23043. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  23049. },
  23050. {
  23051. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23052. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23055. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23056. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23057. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  23058. },
  23059. };
  23060. /* Big-endian values that will fail */
  23061. byte fail_be[][CURVE448_KEY_SIZE] = {
  23062. {
  23063. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23064. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23065. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23066. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23067. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23068. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23069. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  23070. },
  23071. {
  23072. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23073. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23074. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23075. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23076. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23077. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23078. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  23079. },
  23080. };
  23081. /* Good or valid public value */
  23082. byte good[CURVE448_KEY_SIZE] = {
  23083. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23084. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23085. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23086. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23087. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23088. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23089. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  23090. };
  23091. int i;
  23092. /* Parameter checks */
  23093. /* NULL pointer */
  23094. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  23095. BAD_FUNC_ARG) {
  23096. return -11300;
  23097. }
  23098. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  23099. return -11301;
  23100. }
  23101. /* Length of 0 treated differently to other invalid lengths for TLS */
  23102. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  23103. return -11302;
  23104. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  23105. return -11303;
  23106. /* Length not CURVE448_KEY_SIZE */
  23107. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  23108. if (i == CURVE448_KEY_SIZE)
  23109. continue;
  23110. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  23111. ECC_BAD_ARG_E) {
  23112. return -11304 - i;
  23113. }
  23114. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  23115. ECC_BAD_ARG_E) {
  23116. return -11314 - i;
  23117. }
  23118. }
  23119. /* Little-endian fail cases */
  23120. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  23121. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  23122. EC448_LITTLE_ENDIAN) == 0) {
  23123. return -11324 - i;
  23124. }
  23125. }
  23126. /* Big-endian fail cases */
  23127. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  23128. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  23129. EC448_BIG_ENDIAN) == 0) {
  23130. return -11334 - i;
  23131. }
  23132. }
  23133. /* Check a valid public value works! */
  23134. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  23135. EC448_LITTLE_ENDIAN) != 0) {
  23136. return -11344;
  23137. }
  23138. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  23139. EC448_BIG_ENDIAN) != 0) {
  23140. return -11345;
  23141. }
  23142. return 0;
  23143. }
  23144. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  23145. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  23146. {
  23147. WC_RNG rng;
  23148. int ret;
  23149. #ifdef HAVE_CURVE448_SHARED_SECRET
  23150. byte sharedA[CURVE448_KEY_SIZE];
  23151. byte sharedB[CURVE448_KEY_SIZE];
  23152. word32 y;
  23153. #endif
  23154. #ifdef HAVE_CURVE448_KEY_EXPORT
  23155. byte exportBuf[CURVE448_KEY_SIZE];
  23156. #endif
  23157. word32 x;
  23158. curve448_key userA, userB, pubKey;
  23159. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  23160. defined(HAVE_CURVE448_KEY_IMPORT)
  23161. /* test vectors from
  23162. https://www.rfc-editor.org/rfc/rfc7748.html
  23163. */
  23164. /* secret key for party a */
  23165. byte sa[] = {
  23166. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  23167. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  23168. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  23169. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  23170. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  23171. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  23172. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  23173. };
  23174. /* public key for party a */
  23175. byte pa[] = {
  23176. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  23177. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  23178. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  23179. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  23180. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  23181. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  23182. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  23183. };
  23184. /* secret key for party b */
  23185. byte sb[] = {
  23186. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  23187. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  23188. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  23189. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  23190. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  23191. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  23192. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  23193. };
  23194. /* public key for party b */
  23195. byte pb[] = {
  23196. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  23197. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  23198. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  23199. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  23200. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  23201. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  23202. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  23203. };
  23204. /* expected shared key */
  23205. byte ss[] = {
  23206. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  23207. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  23208. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  23209. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  23210. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  23211. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  23212. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  23213. };
  23214. #endif /* HAVE_CURVE448_SHARED_SECRET */
  23215. (void)x;
  23216. #ifndef HAVE_FIPS
  23217. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23218. #else
  23219. ret = wc_InitRng(&rng);
  23220. #endif
  23221. if (ret != 0)
  23222. return -11400;
  23223. wc_curve448_init(&userA);
  23224. wc_curve448_init(&userB);
  23225. wc_curve448_init(&pubKey);
  23226. /* make curve448 keys */
  23227. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  23228. return -11401;
  23229. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  23230. return -11402;
  23231. #ifdef HAVE_CURVE448_SHARED_SECRET
  23232. /* find shared secret key */
  23233. x = sizeof(sharedA);
  23234. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  23235. return -11403;
  23236. y = sizeof(sharedB);
  23237. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  23238. return -11404;
  23239. /* compare shared secret keys to test they are the same */
  23240. if (y != x)
  23241. return -11405;
  23242. if (XMEMCMP(sharedA, sharedB, x))
  23243. return -11406;
  23244. #endif
  23245. #ifdef HAVE_CURVE448_KEY_EXPORT
  23246. /* export a public key and import it for another user */
  23247. x = sizeof(exportBuf);
  23248. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  23249. return -11407;
  23250. #ifdef HAVE_CURVE448_KEY_IMPORT
  23251. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  23252. return -11408;
  23253. #endif
  23254. #endif
  23255. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  23256. defined(HAVE_CURVE448_KEY_IMPORT)
  23257. /* test shared key after importing a public key */
  23258. XMEMSET(sharedB, 0, sizeof(sharedB));
  23259. y = sizeof(sharedB);
  23260. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  23261. return -11409;
  23262. if (XMEMCMP(sharedA, sharedB, y))
  23263. return -11410;
  23264. /* import RFC test vectors and compare shared key */
  23265. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  23266. != 0)
  23267. return -11411;
  23268. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  23269. != 0)
  23270. return -11412;
  23271. /* test against known test vector */
  23272. XMEMSET(sharedB, 0, sizeof(sharedB));
  23273. y = sizeof(sharedB);
  23274. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  23275. return -11413;
  23276. if (XMEMCMP(ss, sharedB, y))
  23277. return -11414;
  23278. /* test swapping roles of keys and generating same shared key */
  23279. XMEMSET(sharedB, 0, sizeof(sharedB));
  23280. y = sizeof(sharedB);
  23281. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  23282. return -11415;
  23283. if (XMEMCMP(ss, sharedB, y))
  23284. return -11416;
  23285. /* test with 1 generated key and 1 from known test vector */
  23286. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  23287. != 0)
  23288. return -11417;
  23289. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  23290. return -11418;
  23291. x = sizeof(sharedA);
  23292. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  23293. return -11419;
  23294. y = sizeof(sharedB);
  23295. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  23296. return -11420;
  23297. /* compare shared secret keys to test they are the same */
  23298. if (y != x)
  23299. return -11421;
  23300. if (XMEMCMP(sharedA, sharedB, x))
  23301. return -11422;
  23302. ret = curve448_check_public_test();
  23303. if (ret != 0)
  23304. return ret;
  23305. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  23306. /* clean up keys when done */
  23307. wc_curve448_free(&pubKey);
  23308. wc_curve448_free(&userB);
  23309. wc_curve448_free(&userA);
  23310. wc_FreeRng(&rng);
  23311. return 0;
  23312. }
  23313. #endif /* HAVE_CURVE448 */
  23314. #ifdef HAVE_ED448
  23315. #ifdef WOLFSSL_TEST_CERT
  23316. static int ed448_test_cert(void)
  23317. {
  23318. DecodedCert cert[2];
  23319. DecodedCert* serverCert = NULL;
  23320. DecodedCert* caCert = NULL;
  23321. #ifdef HAVE_ED448_VERIFY
  23322. ed448_key key;
  23323. ed448_key* pubKey = NULL;
  23324. int verify;
  23325. #endif /* HAVE_ED448_VERIFY */
  23326. int ret;
  23327. byte* tmp;
  23328. size_t bytes;
  23329. XFILE file;
  23330. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23331. if (tmp == NULL) {
  23332. ERROR_OUT(-11430, done);
  23333. }
  23334. #ifdef USE_CERT_BUFFERS_256
  23335. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  23336. bytes = sizeof_ca_ed448_cert;
  23337. #elif !defined(NO_FILESYSTEM)
  23338. file = XFOPEN(caEd448Cert, "rb");
  23339. if (file == NULL) {
  23340. ERROR_OUT(-11431, done);
  23341. }
  23342. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  23343. XFCLOSE(file);
  23344. #else
  23345. /* No certificate to use. */
  23346. ERROR_OUT(-11432, done);
  23347. #endif
  23348. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  23349. caCert = &cert[0];
  23350. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  23351. if (ret != 0) {
  23352. ERROR_OUT(-11433, done);
  23353. }
  23354. #ifdef USE_CERT_BUFFERS_256
  23355. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  23356. bytes = sizeof_server_ed448_cert;
  23357. #elif !defined(NO_FILESYSTEM)
  23358. file = XFOPEN(serverEd448Cert, "rb");
  23359. if (file == NULL) {
  23360. ERROR_OUT(-11434, done);
  23361. }
  23362. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  23363. XFCLOSE(file);
  23364. #else
  23365. /* No certificate to use. */
  23366. ERROR_OUT(-11435, done);
  23367. #endif
  23368. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  23369. serverCert = &cert[1];
  23370. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  23371. if (ret != 0) {
  23372. ERROR_OUT(-11436, done);
  23373. }
  23374. #ifdef HAVE_ED448_VERIFY
  23375. ret = wc_ed448_init(&key);
  23376. if (ret < 0) {
  23377. ERROR_OUT(-11437, done);
  23378. }
  23379. pubKey = &key;
  23380. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  23381. if (ret < 0) {
  23382. ERROR_OUT(-11438, done);
  23383. }
  23384. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  23385. serverCert->source + serverCert->certBegin,
  23386. serverCert->sigIndex - serverCert->certBegin,
  23387. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  23388. ERROR_OUT(-11439, done);
  23389. }
  23390. #endif /* HAVE_ED448_VERIFY */
  23391. done:
  23392. if (tmp != NULL)
  23393. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23394. #ifdef HAVE_ED448_VERIFY
  23395. wc_ed448_free(pubKey);
  23396. #endif /* HAVE_ED448_VERIFY */
  23397. if (caCert != NULL)
  23398. FreeDecodedCert(caCert);
  23399. if (serverCert != NULL)
  23400. FreeDecodedCert(serverCert);
  23401. return ret;
  23402. }
  23403. static int ed448_test_make_cert(void)
  23404. {
  23405. WC_RNG rng;
  23406. Cert cert;
  23407. DecodedCert decode;
  23408. ed448_key key;
  23409. ed448_key* privKey = NULL;
  23410. int ret = 0;
  23411. byte* tmp = NULL;
  23412. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  23413. #ifndef HAVE_FIPS
  23414. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23415. #else
  23416. ret = wc_InitRng(&rng);
  23417. #endif
  23418. if (ret != 0)
  23419. return -11450;
  23420. wc_ed448_init(&key);
  23421. privKey = &key;
  23422. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  23423. cert.daysValid = 365 * 2;
  23424. cert.selfSigned = 1;
  23425. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  23426. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  23427. cert.isCA = 0;
  23428. #ifdef WOLFSSL_CERT_EXT
  23429. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  23430. if (ret < 0) {
  23431. ERROR_OUT(-11451, done);
  23432. }
  23433. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  23434. if (ret < 0) {
  23435. ERROR_OUT(-11452, done);
  23436. }
  23437. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  23438. if (ret < 0) {
  23439. ERROR_OUT(-11453, done);
  23440. }
  23441. #endif
  23442. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23443. if (tmp == NULL) {
  23444. ERROR_OUT(-11454, done);
  23445. }
  23446. cert.sigType = CTC_ED448;
  23447. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  23448. if (ret < 0) {
  23449. ERROR_OUT(-11455, done);
  23450. }
  23451. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  23452. privKey, &rng);
  23453. if (ret < 0) {
  23454. ERROR_OUT(-11456, done);
  23455. }
  23456. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  23457. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  23458. FreeDecodedCert(&decode);
  23459. if (ret != 0) {
  23460. ERROR_OUT(-11457, done);
  23461. }
  23462. done:
  23463. if (tmp != NULL)
  23464. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23465. wc_ed448_free(privKey);
  23466. wc_FreeRng(&rng);
  23467. return ret;
  23468. }
  23469. #endif /* WOLFSSL_TEST_CERT */
  23470. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  23471. defined(HAVE_ED448_KEY_IMPORT)
  23472. static int ed448_ctx_test(void)
  23473. {
  23474. byte out[ED448_SIG_SIZE];
  23475. word32 outlen;
  23476. #ifdef HAVE_ED448_VERIFY
  23477. int verify;
  23478. #endif /* HAVE_ED448_VERIFY */
  23479. ed448_key key;
  23480. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  23481. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  23482. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  23483. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  23484. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  23485. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  23486. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  23487. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  23488. 0x4e
  23489. };
  23490. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  23491. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  23492. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  23493. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  23494. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  23495. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  23496. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  23497. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  23498. 0x80
  23499. };
  23500. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  23501. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  23502. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  23503. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  23504. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  23505. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  23506. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  23507. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  23508. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  23509. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  23510. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  23511. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  23512. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  23513. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  23514. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  23515. 0x3c, 0x00
  23516. };
  23517. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  23518. 0x03
  23519. };
  23520. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  23521. 0x66,0x6f,0x6f
  23522. };
  23523. outlen = sizeof(out);
  23524. XMEMSET(out, 0, sizeof(out));
  23525. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  23526. return -11500;
  23527. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  23528. sizeof(pKeyCtx), &key) != 0)
  23529. return -11501;
  23530. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  23531. contextCtx, sizeof(contextCtx)) != 0)
  23532. return -11502;
  23533. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  23534. return -11503;
  23535. #if defined(HAVE_ED448_VERIFY)
  23536. /* test verify on good msg */
  23537. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  23538. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  23539. return -11504;
  23540. #endif
  23541. wc_ed448_free(&key);
  23542. return 0;
  23543. }
  23544. static int ed448ph_test(void)
  23545. {
  23546. byte out[ED448_SIG_SIZE];
  23547. word32 outlen;
  23548. #ifdef HAVE_ED448_VERIFY
  23549. int verify;
  23550. #endif /* HAVE_ED448_VERIFY */
  23551. ed448_key key;
  23552. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  23553. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  23554. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  23555. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  23556. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  23557. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  23558. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  23559. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  23560. 0x49
  23561. };
  23562. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  23563. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  23564. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  23565. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  23566. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  23567. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  23568. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  23569. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  23570. 0x80
  23571. };
  23572. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  23573. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  23574. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  23575. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  23576. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  23577. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  23578. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  23579. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  23580. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  23581. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  23582. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  23583. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  23584. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  23585. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  23586. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  23587. 0x0f, 0x00
  23588. };
  23589. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  23590. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  23591. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  23592. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  23593. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  23594. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  23595. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  23596. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  23597. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  23598. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  23599. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  23600. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  23601. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  23602. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  23603. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  23604. 0x21, 0x00
  23605. };
  23606. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  23607. 0x61,0x62,0x63
  23608. };
  23609. /* SHA-512 hash of msgPh */
  23610. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  23611. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  23612. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  23613. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  23614. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  23615. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  23616. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  23617. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  23618. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  23619. };
  23620. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  23621. 0x66,0x6f,0x6f
  23622. };
  23623. outlen = sizeof(out);
  23624. XMEMSET(out, 0, sizeof(out));
  23625. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  23626. return -11600;
  23627. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  23628. sizeof(pKeyPh), &key) != 0) {
  23629. return -11601;
  23630. }
  23631. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  23632. 0) != 0) {
  23633. return -11602;
  23634. }
  23635. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  23636. return -11603;
  23637. #if defined(HAVE_ED448_VERIFY)
  23638. /* test verify on good msg */
  23639. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  23640. NULL, 0) != 0 || verify != 1) {
  23641. return -11604;
  23642. }
  23643. #endif
  23644. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  23645. contextPh2, sizeof(contextPh2)) != 0) {
  23646. return -11605;
  23647. }
  23648. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  23649. return -11606;
  23650. #if defined(HAVE_ED448_VERIFY)
  23651. /* test verify on good msg */
  23652. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  23653. contextPh2, sizeof(contextPh2)) != 0 ||
  23654. verify != 1) {
  23655. return -11607;
  23656. }
  23657. #endif
  23658. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  23659. 0) != 0) {
  23660. return -11608;
  23661. }
  23662. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  23663. return -11609;
  23664. #if defined(HAVE_ED448_VERIFY)
  23665. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  23666. &key, NULL, 0) != 0 || verify != 1) {
  23667. return -11610;
  23668. }
  23669. #endif
  23670. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  23671. contextPh2, sizeof(contextPh2)) != 0) {
  23672. return -11611;
  23673. }
  23674. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  23675. return -11612;
  23676. #if defined(HAVE_ED448_VERIFY)
  23677. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  23678. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  23679. verify != 1) {
  23680. return -11613;
  23681. }
  23682. #endif
  23683. wc_ed448_free(&key);
  23684. return 0;
  23685. }
  23686. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23687. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  23688. {
  23689. int ret;
  23690. WC_RNG rng;
  23691. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  23692. defined(HAVE_ED448_KEY_IMPORT)
  23693. byte out[ED448_SIG_SIZE];
  23694. byte exportPKey[ED448_KEY_SIZE];
  23695. byte exportSKey[ED448_KEY_SIZE];
  23696. word32 exportPSz;
  23697. word32 exportSSz;
  23698. int i;
  23699. word32 outlen;
  23700. #ifdef HAVE_ED448_VERIFY
  23701. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  23702. int j;
  23703. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  23704. int verify;
  23705. #endif /* HAVE_ED448_VERIFY */
  23706. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23707. word32 keySz, sigSz;
  23708. ed448_key key;
  23709. ed448_key key2;
  23710. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  23711. defined(HAVE_ED448_KEY_IMPORT)
  23712. /* test vectors from
  23713. https://tools.ietf.org/html/rfc8032
  23714. */
  23715. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  23716. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  23717. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  23718. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  23719. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  23720. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  23721. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  23722. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  23723. 0x5b
  23724. };
  23725. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  23726. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  23727. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  23728. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  23729. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  23730. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  23731. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  23732. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  23733. 0x4e
  23734. };
  23735. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  23736. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  23737. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  23738. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  23739. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  23740. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  23741. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  23742. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  23743. 0x1b
  23744. };
  23745. /* uncompressed test */
  23746. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  23747. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  23748. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  23749. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  23750. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  23751. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  23752. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  23753. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  23754. 0x5b
  23755. };
  23756. /* compressed prefix test */
  23757. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  23758. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  23759. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  23760. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  23761. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  23762. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  23763. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  23764. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  23765. 0x5b
  23766. };
  23767. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  23768. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  23769. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  23770. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  23771. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  23772. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  23773. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  23774. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  23775. 0xe8
  23776. };
  23777. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  23778. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  23779. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  23780. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  23781. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  23782. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  23783. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  23784. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  23785. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  23786. 0x80
  23787. };
  23788. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  23789. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  23790. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  23791. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  23792. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  23793. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  23794. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  23795. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  23796. 0x80
  23797. };
  23798. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  23799. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  23800. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  23801. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  23802. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  23803. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  23804. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  23805. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  23806. 0x80
  23807. };
  23808. /* uncompressed test */
  23809. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  23810. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  23811. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  23812. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  23813. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  23814. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  23815. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  23816. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  23817. 0x80
  23818. };
  23819. /* compressed prefix */
  23820. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  23821. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  23822. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  23823. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  23824. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  23825. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  23826. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  23827. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  23828. 0x80
  23829. };
  23830. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  23831. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  23832. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  23833. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  23834. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  23835. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  23836. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  23837. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  23838. 0x00
  23839. };
  23840. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  23841. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  23842. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  23843. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  23844. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  23845. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  23846. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  23847. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  23848. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  23849. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  23850. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  23851. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  23852. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  23853. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  23854. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  23855. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  23856. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  23857. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  23858. 0x26, 0x00
  23859. };
  23860. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  23861. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  23862. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  23863. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  23864. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  23865. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  23866. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  23867. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  23868. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  23869. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  23870. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  23871. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  23872. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  23873. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  23874. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  23875. 0x3a, 0x00
  23876. };
  23877. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  23878. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  23879. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  23880. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  23881. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  23882. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  23883. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  23884. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  23885. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  23886. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  23887. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  23888. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  23889. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  23890. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  23891. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  23892. 0x3c, 0x00
  23893. };
  23894. /* uncompressed test */
  23895. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  23896. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  23897. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  23898. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  23899. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  23900. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  23901. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  23902. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  23903. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  23904. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  23905. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  23906. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  23907. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  23908. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  23909. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  23910. 0x26, 0x00
  23911. };
  23912. /* compressed prefix */
  23913. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  23914. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  23915. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  23916. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  23917. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  23918. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  23919. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  23920. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  23921. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  23922. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  23923. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  23924. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  23925. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  23926. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  23927. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  23928. 0x26, 0x00
  23929. };
  23930. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  23931. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  23932. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  23933. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  23934. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  23935. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  23936. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  23937. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  23938. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  23939. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  23940. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  23941. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  23942. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  23943. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  23944. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  23945. 0x1a, 0x00
  23946. };
  23947. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  23948. #define SIGSZ sizeof(sig1)
  23949. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  23950. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  23951. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  23952. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  23953. /* test of a 1023 byte long message */
  23954. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  23955. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  23956. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  23957. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  23958. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  23959. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  23960. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  23961. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  23962. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  23963. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  23964. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  23965. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  23966. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  23967. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  23968. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  23969. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  23970. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  23971. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  23972. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  23973. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  23974. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  23975. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  23976. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  23977. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  23978. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  23979. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  23980. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  23981. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  23982. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  23983. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  23984. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  23985. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  23986. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  23987. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  23988. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  23989. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  23990. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  23991. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  23992. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  23993. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  23994. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  23995. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  23996. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  23997. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  23998. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  23999. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  24000. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  24001. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  24002. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  24003. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  24004. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  24005. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  24006. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  24007. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  24008. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  24009. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  24010. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  24011. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  24012. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  24013. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  24014. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  24015. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  24016. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  24017. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  24018. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  24019. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  24020. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  24021. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  24022. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  24023. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  24024. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  24025. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  24026. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  24027. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  24028. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  24029. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  24030. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  24031. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  24032. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  24033. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  24034. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  24035. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  24036. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  24037. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  24038. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  24039. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  24040. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  24041. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  24042. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  24043. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  24044. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  24045. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  24046. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  24047. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  24048. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  24049. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  24050. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  24051. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  24052. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  24053. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  24054. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  24055. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  24056. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  24057. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  24058. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  24059. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  24060. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  24061. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  24062. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  24063. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  24064. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  24065. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  24066. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  24067. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  24068. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  24069. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  24070. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  24071. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  24072. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  24073. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  24074. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  24075. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  24076. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  24077. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  24078. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  24079. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  24080. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  24081. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  24082. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  24083. };
  24084. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  24085. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  24086. sizeof(msg2),
  24087. sizeof(msg3),
  24088. 0 /*sizeof(msg1)*/,
  24089. 0 /*sizeof(msg1)*/,
  24090. sizeof(msg4)
  24091. };
  24092. #ifndef NO_ASN
  24093. static byte privateEd448[] = {
  24094. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  24095. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  24096. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  24097. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  24098. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  24099. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  24100. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  24101. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  24102. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  24103. 0x5b
  24104. };
  24105. static byte publicEd448[] = {
  24106. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  24107. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  24108. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  24109. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  24110. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  24111. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  24112. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  24113. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  24114. 0xaf, 0xe8, 0x25, 0x61, 0x80
  24115. };
  24116. static byte privPubEd448[] = {
  24117. 0x30, 0x81, 0x84, 0x02, 0x01, 0x00, 0x30, 0x05,
  24118. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  24119. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  24120. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  24121. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  24122. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  24123. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  24124. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  24125. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  24126. 0xf9, 0x5b, 0xa1, 0x3b, 0x04, 0x39, 0x5f, 0xd7,
  24127. 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  24128. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1,
  24129. 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e,
  24130. 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1,
  24131. 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6,
  24132. 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  24133. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  24134. };
  24135. word32 idx;
  24136. #endif /* NO_ASN */
  24137. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  24138. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  24139. ed448_key key3;
  24140. #endif
  24141. /* create ed448 keys */
  24142. #ifndef HAVE_FIPS
  24143. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24144. #else
  24145. ret = wc_InitRng(&rng);
  24146. #endif
  24147. if (ret != 0)
  24148. return -11700;
  24149. wc_ed448_init(&key);
  24150. wc_ed448_init(&key2);
  24151. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  24152. wc_ed448_init(&key3);
  24153. #endif
  24154. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  24155. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key2);
  24156. /* helper functions for signature and key size */
  24157. keySz = wc_ed448_size(&key);
  24158. sigSz = wc_ed448_sig_size(&key);
  24159. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  24160. defined(HAVE_ED448_KEY_IMPORT)
  24161. for (i = 0; i < 6; i++) {
  24162. outlen = sizeof(out);
  24163. XMEMSET(out, 0, sizeof(out));
  24164. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  24165. pKeySz[i], &key) != 0)
  24166. return -11701 - i;
  24167. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key, NULL,
  24168. 0) != 0) {
  24169. return -11711 - i;
  24170. }
  24171. if (XMEMCMP(out, sigs[i], 114))
  24172. return -11721 - i;
  24173. #if defined(HAVE_ED448_VERIFY)
  24174. /* test verify on good msg */
  24175. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  24176. NULL, 0) != 0 || verify != 1) {
  24177. return -11731 - i;
  24178. }
  24179. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  24180. /* test verify on good msg using streaming interface directly */
  24181. if (wc_ed448_verify_msg_init(out, outlen,
  24182. &key, (byte)Ed448, NULL, 0) != 0)
  24183. return -11911 - i;
  24184. for (j = 0; j < msgSz[i]; j += i) {
  24185. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  24186. return -11921 - i;
  24187. }
  24188. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  24189. &key) != 0 || verify != 1)
  24190. return -11931 - i;
  24191. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  24192. /* test verify on bad msg */
  24193. out[outlen-2] = out[outlen-2] + 1;
  24194. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  24195. NULL, 0) == 0 || verify == 1) {
  24196. return -11741 - i;
  24197. }
  24198. #endif /* HAVE_ED448_VERIFY */
  24199. /* test api for import/exporting keys */
  24200. exportPSz = sizeof(exportPKey);
  24201. exportSSz = sizeof(exportSKey);
  24202. if (wc_ed448_export_public(&key, exportPKey, &exportPSz) != 0)
  24203. return -11751 - i;
  24204. if (wc_ed448_import_public(exportPKey, exportPSz, &key2) != 0)
  24205. return -11761 - i;
  24206. if (wc_ed448_export_private_only(&key, exportSKey, &exportSSz) != 0)
  24207. return -11771 - i;
  24208. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  24209. exportPKey, exportPSz, &key2) != 0)
  24210. return -11781 - i;
  24211. /* clear "out" buffer and test sign with imported keys */
  24212. outlen = sizeof(out);
  24213. XMEMSET(out, 0, sizeof(out));
  24214. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2, NULL,
  24215. 0) != 0) {
  24216. return -11791 - i;
  24217. }
  24218. #if defined(HAVE_ED448_VERIFY)
  24219. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key2,
  24220. NULL, 0) != 0 || verify != 1)
  24221. return -11801 - i;
  24222. if (XMEMCMP(out, sigs[i], SIGSZ))
  24223. return -11811 - i;
  24224. #endif /* HAVE_ED448_VERIFY */
  24225. }
  24226. ret = ed448_ctx_test();
  24227. if (ret != 0)
  24228. return ret;
  24229. ret = ed448ph_test();
  24230. if (ret != 0)
  24231. return ret;
  24232. #ifndef NO_ASN
  24233. /* Try ASN.1 encoded private-only key and public key. */
  24234. idx = 0;
  24235. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, &key3,
  24236. sizeof(privateEd448)) != 0)
  24237. return -11821;
  24238. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0)
  24239. != BAD_FUNC_ARG)
  24240. return -11831;
  24241. idx = 0;
  24242. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, &key3,
  24243. sizeof(publicEd448)) != 0)
  24244. return -11841;
  24245. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  24246. return -11851;
  24247. if (XMEMCMP(out, sigs[0], SIGSZ))
  24248. return -11861;
  24249. #if defined(HAVE_ED448_VERIFY)
  24250. /* test verify on good msg */
  24251. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3,
  24252. NULL, 0) != 0 || verify != 1)
  24253. return -11871;
  24254. #endif /* HAVE_ED448_VERIFY */
  24255. wc_ed448_free(&key3);
  24256. wc_ed448_init(&key3);
  24257. idx = 0;
  24258. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, &key3,
  24259. sizeof(privPubEd448)) != 0)
  24260. return -11881;
  24261. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  24262. return -11891;
  24263. if (XMEMCMP(out, sigs[0], SIGSZ))
  24264. return -11901;
  24265. wc_ed448_free(&key3);
  24266. #endif /* NO_ASN */
  24267. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  24268. /* clean up keys when done */
  24269. wc_ed448_free(&key);
  24270. wc_ed448_free(&key2);
  24271. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  24272. wc_FreeRng(&rng);
  24273. #endif
  24274. /* hush warnings of unused keySz and sigSz */
  24275. (void)keySz;
  24276. (void)sigSz;
  24277. #ifdef WOLFSSL_TEST_CERT
  24278. ret = ed448_test_cert();
  24279. if (ret < 0)
  24280. return ret;
  24281. #ifdef WOLFSSL_CERT_GEN
  24282. ret = ed448_test_make_cert();
  24283. if (ret < 0)
  24284. return ret;
  24285. #endif /* WOLFSSL_CERT_GEN */
  24286. #endif /* WOLFSSL_TEST_CERT */
  24287. return 0;
  24288. }
  24289. #endif /* HAVE_ED448 */
  24290. #ifdef WOLFCRYPT_HAVE_ECCSI
  24291. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  24292. ecc_point* pvt)
  24293. {
  24294. int ret;
  24295. byte id[1] = { 0x00 };
  24296. int valid;
  24297. word32 sz;
  24298. byte data[256];
  24299. byte hash[WC_MAX_DIGEST_SIZE];
  24300. byte hashSz;
  24301. byte sig[257];
  24302. word32 sigSz;
  24303. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  24304. if (ret != BAD_FUNC_ARG)
  24305. return -10023;
  24306. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  24307. if (ret != BAD_FUNC_ARG)
  24308. return -10024;
  24309. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  24310. if (ret != BAD_FUNC_ARG)
  24311. return -10025;
  24312. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  24313. if (ret != BAD_FUNC_ARG)
  24314. return -10026;
  24315. wc_FreeEccsiKey(NULL);
  24316. /* Create a valid key. */
  24317. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  24318. if (ret != 0)
  24319. return -10027;
  24320. ret = wc_MakeEccsiKey(NULL, NULL);
  24321. if (ret != BAD_FUNC_ARG)
  24322. return -10028;
  24323. ret = wc_MakeEccsiKey(key, NULL);
  24324. if (ret != BAD_FUNC_ARG)
  24325. return -10029;
  24326. ret = wc_MakeEccsiKey(NULL, rng);
  24327. if (ret != BAD_FUNC_ARG)
  24328. return -10030;
  24329. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  24330. NULL);
  24331. if (ret != BAD_FUNC_ARG)
  24332. return -10031;
  24333. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  24334. if (ret != BAD_FUNC_ARG)
  24335. return -10032;
  24336. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  24337. if (ret != BAD_FUNC_ARG)
  24338. return -10033;
  24339. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  24340. if (ret != BAD_FUNC_ARG)
  24341. return -10034;
  24342. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  24343. if (ret != BAD_FUNC_ARG)
  24344. return -10035;
  24345. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  24346. if (ret != BAD_FUNC_ARG)
  24347. return -10036;
  24348. /* No key set */
  24349. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  24350. if (ret != BAD_STATE_E)
  24351. return -10037;
  24352. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  24353. NULL);
  24354. if (ret != BAD_FUNC_ARG)
  24355. return -10038;
  24356. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  24357. NULL);
  24358. if (ret != BAD_FUNC_ARG)
  24359. return -10039;
  24360. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  24361. &valid);
  24362. if (ret != BAD_FUNC_ARG)
  24363. return -10040;
  24364. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  24365. &valid);
  24366. if (ret != BAD_FUNC_ARG)
  24367. return -10041;
  24368. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  24369. &valid);
  24370. if (ret != BAD_FUNC_ARG)
  24371. return -10042;
  24372. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  24373. &valid);
  24374. if (ret != BAD_FUNC_ARG)
  24375. return -10043;
  24376. /* No key set */
  24377. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  24378. &valid);
  24379. if (ret != BAD_STATE_E)
  24380. return -10044;
  24381. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  24382. if (ret != BAD_FUNC_ARG)
  24383. return -10045;
  24384. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  24385. if (ret != BAD_FUNC_ARG)
  24386. return -10046;
  24387. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  24388. if (ret != BAD_FUNC_ARG)
  24389. return -10047;
  24390. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  24391. if (ret != BAD_FUNC_ARG)
  24392. return -10048;
  24393. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  24394. if (ret != BAD_FUNC_ARG)
  24395. return -10049;
  24396. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  24397. if (ret != BAD_FUNC_ARG)
  24398. return -10050;
  24399. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  24400. if (ret != BAD_FUNC_ARG)
  24401. return -10051;
  24402. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  24403. if (ret != BAD_FUNC_ARG)
  24404. return -10052;
  24405. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  24406. if (ret != BAD_FUNC_ARG)
  24407. return -10053;
  24408. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  24409. if (ret != BAD_FUNC_ARG)
  24410. return -10054;
  24411. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  24412. if (ret != BAD_FUNC_ARG)
  24413. return -10055;
  24414. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  24415. if (ret != BAD_FUNC_ARG)
  24416. return -10056;
  24417. /* No key created so no curve information. */
  24418. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  24419. if (ret != LENGTH_ONLY_E)
  24420. return -10057;
  24421. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  24422. if (ret != BAD_FUNC_ARG)
  24423. return -10058;
  24424. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  24425. if (ret != BAD_FUNC_ARG)
  24426. return -10059;
  24427. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  24428. if (ret != BAD_FUNC_ARG)
  24429. return -10060;
  24430. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  24431. if (ret != BAD_FUNC_ARG)
  24432. return -10061;
  24433. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  24434. if (ret != BAD_FUNC_ARG)
  24435. return -10058;
  24436. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  24437. if (ret != BAD_FUNC_ARG)
  24438. return -10059;
  24439. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  24440. if (ret != BAD_FUNC_ARG)
  24441. return -10060;
  24442. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  24443. if (ret != BAD_FUNC_ARG)
  24444. return -10061;
  24445. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  24446. if (ret != BAD_FUNC_ARG)
  24447. return -10062;
  24448. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  24449. if (ret != BAD_FUNC_ARG)
  24450. return -10063;
  24451. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  24452. if (ret != BAD_FUNC_ARG)
  24453. return -10064;
  24454. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  24455. if (ret != BAD_FUNC_ARG)
  24456. return -10065;
  24457. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  24458. if (ret != BAD_FUNC_ARG)
  24459. return -10066;
  24460. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  24461. if (ret != BAD_FUNC_ARG)
  24462. return -10067;
  24463. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  24464. if (ret != BAD_FUNC_ARG)
  24465. return -10068;
  24466. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  24467. if (ret != BAD_FUNC_ARG)
  24468. return -10069;
  24469. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  24470. if (ret != BAD_FUNC_ARG)
  24471. return -10070;
  24472. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  24473. if (ret != BAD_FUNC_ARG)
  24474. return -10067;
  24475. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  24476. if (ret != BAD_FUNC_ARG)
  24477. return -10068;
  24478. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  24479. if (ret != BAD_FUNC_ARG)
  24480. return -10069;
  24481. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  24482. if (ret != BAD_FUNC_ARG)
  24483. return -10070;
  24484. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  24485. if (ret != BAD_FUNC_ARG)
  24486. return -10067;
  24487. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  24488. if (ret != BAD_FUNC_ARG)
  24489. return -10068;
  24490. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  24491. if (ret != BAD_FUNC_ARG)
  24492. return -10069;
  24493. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  24494. if (ret != BAD_FUNC_ARG)
  24495. return -10070;
  24496. ret = wc_ExportEccsiKey(NULL, data, NULL);
  24497. if (ret != BAD_FUNC_ARG)
  24498. return -10071;
  24499. ret = wc_ExportEccsiKey(key, data, NULL);
  24500. if (ret != BAD_FUNC_ARG)
  24501. return -10072;
  24502. ret = wc_ExportEccsiKey(NULL, data, &sz);
  24503. if (ret != BAD_FUNC_ARG)
  24504. return -10073;
  24505. /* No key to export */
  24506. ret = wc_ExportEccsiKey(key, NULL, &sz);
  24507. if (ret != BAD_STATE_E)
  24508. return -10074;
  24509. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  24510. if (ret != BAD_FUNC_ARG)
  24511. return -10075;
  24512. ret = wc_ImportEccsiKey(key, NULL, 0);
  24513. if (ret != BAD_FUNC_ARG)
  24514. return -10076;
  24515. ret = wc_ImportEccsiKey(NULL, data, 0);
  24516. if (ret != BAD_FUNC_ARG)
  24517. return -10077;
  24518. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  24519. if (ret != BAD_FUNC_ARG)
  24520. return -10071;
  24521. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  24522. if (ret != BAD_FUNC_ARG)
  24523. return -10072;
  24524. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  24525. if (ret != BAD_FUNC_ARG)
  24526. return -10073;
  24527. /* No key to export */
  24528. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  24529. if (ret != BAD_STATE_E)
  24530. return -10074;
  24531. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  24532. if (ret != BAD_FUNC_ARG)
  24533. return -10075;
  24534. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  24535. if (ret != BAD_FUNC_ARG)
  24536. return -10076;
  24537. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  24538. if (ret != BAD_FUNC_ARG)
  24539. return -10077;
  24540. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  24541. if (ret != BAD_FUNC_ARG)
  24542. return -10078;
  24543. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  24544. if (ret != BAD_FUNC_ARG)
  24545. return -10079;
  24546. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  24547. if (ret != BAD_FUNC_ARG)
  24548. return -10080;
  24549. /* No key to export */
  24550. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  24551. if (ret != BAD_STATE_E)
  24552. return -10081;
  24553. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  24554. if (ret != BAD_FUNC_ARG)
  24555. return -10082;
  24556. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  24557. if (ret != BAD_FUNC_ARG)
  24558. return -10083;
  24559. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  24560. if (ret != BAD_FUNC_ARG)
  24561. return -10084;
  24562. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  24563. if (ret != BAD_FUNC_ARG)
  24564. return -10085;
  24565. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  24566. if (ret != BAD_FUNC_ARG)
  24567. return -10086;
  24568. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  24569. if (ret != BAD_FUNC_ARG)
  24570. return -10087;
  24571. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  24572. if (ret != BAD_FUNC_ARG)
  24573. return -10088;
  24574. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  24575. &hashSz);
  24576. if (ret != BAD_FUNC_ARG)
  24577. return -10089;
  24578. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  24579. if (ret != BAD_FUNC_ARG)
  24580. return -10090;
  24581. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  24582. if (ret != BAD_STATE_E)
  24583. return -10091;
  24584. ret = wc_SetEccsiHash(NULL, NULL, 1);
  24585. if (ret != BAD_FUNC_ARG)
  24586. return -10090;
  24587. ret = wc_SetEccsiHash(key, NULL, 1);
  24588. if (ret != BAD_FUNC_ARG)
  24589. return -10090;
  24590. ret = wc_SetEccsiHash(NULL, hash, 1);
  24591. if (ret != BAD_FUNC_ARG)
  24592. return -10090;
  24593. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  24594. if (ret != BAD_FUNC_ARG)
  24595. return -10090;
  24596. ret = wc_SetEccsiPair(key, NULL, NULL);
  24597. if (ret != BAD_FUNC_ARG)
  24598. return -10090;
  24599. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  24600. if (ret != BAD_FUNC_ARG)
  24601. return -10090;
  24602. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  24603. if (ret != BAD_FUNC_ARG)
  24604. return -10090;
  24605. ret = wc_SetEccsiPair(key, ssk, NULL);
  24606. if (ret != BAD_FUNC_ARG)
  24607. return -10090;
  24608. ret = wc_SetEccsiPair(key, NULL, pvt);
  24609. if (ret != BAD_FUNC_ARG)
  24610. return -10090;
  24611. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  24612. if (ret != BAD_FUNC_ARG)
  24613. return -10090;
  24614. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  24615. if (ret != BAD_FUNC_ARG)
  24616. return -10092;
  24617. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  24618. if (ret != BAD_FUNC_ARG)
  24619. return -10093;
  24620. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  24621. &sigSz);
  24622. if (ret != BAD_FUNC_ARG)
  24623. return -10096;
  24624. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  24625. &sigSz);
  24626. if (ret != BAD_FUNC_ARG)
  24627. return -10098;
  24628. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  24629. &sigSz);
  24630. if (ret != BAD_FUNC_ARG)
  24631. return -10099;
  24632. /* Key not set. */
  24633. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  24634. &sigSz);
  24635. if (ret != BAD_STATE_E)
  24636. return -10100;
  24637. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  24638. if (ret != BAD_FUNC_ARG)
  24639. return -10101;
  24640. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  24641. if (ret != BAD_FUNC_ARG)
  24642. return -10101;
  24643. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  24644. if (ret != BAD_FUNC_ARG)
  24645. return -10101;
  24646. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  24647. if (ret != BAD_FUNC_ARG)
  24648. return -10101;
  24649. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  24650. &valid);
  24651. if (ret != BAD_FUNC_ARG)
  24652. return -10101;
  24653. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  24654. if (ret != BAD_FUNC_ARG)
  24655. return -10102;
  24656. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  24657. &valid);
  24658. if (ret != BAD_FUNC_ARG)
  24659. return -10103;
  24660. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  24661. &valid);
  24662. if (ret != BAD_FUNC_ARG)
  24663. return -10104;
  24664. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  24665. &valid);
  24666. if (ret != BAD_FUNC_ARG)
  24667. return -10106;
  24668. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  24669. &valid);
  24670. if (ret != BAD_STATE_E)
  24671. return -10106;
  24672. ret = wc_SetEccsiPair(key, ssk, pvt);
  24673. if (ret != 0)
  24674. return -10107;
  24675. /* Identity hash not set. */
  24676. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  24677. &sigSz);
  24678. if (ret != BAD_STATE_E)
  24679. return -10108;
  24680. wc_FreeEccsiKey(key);
  24681. return 0;
  24682. }
  24683. /* RFC 6507: Appendix A */
  24684. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  24685. {
  24686. int ret;
  24687. int verified;
  24688. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  24689. word32 msgSz = sizeof(msg);
  24690. byte hash[WC_SHA256_DIGEST_SIZE];
  24691. byte hashSz = WC_SHA256_DIGEST_SIZE;
  24692. static const byte id[] = {
  24693. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  24694. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  24695. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  24696. 0x33, 0x00
  24697. };
  24698. word32 idSz = sizeof(id);
  24699. static const byte sig[] = {
  24700. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  24701. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  24702. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  24703. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  24704. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  24705. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  24706. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  24707. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  24708. 0x04,
  24709. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  24710. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  24711. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  24712. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  24713. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  24714. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  24715. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  24716. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  24717. };
  24718. word32 sigSz = sizeof(sig);
  24719. static const byte pubData[] = {
  24720. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  24721. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  24722. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  24723. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  24724. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  24725. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  24726. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  24727. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  24728. };
  24729. static const byte expHash[] = {
  24730. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  24731. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  24732. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  24733. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  24734. };
  24735. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  24736. if (ret != 0)
  24737. return -10108;
  24738. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  24739. if (ret != 0)
  24740. return -10109;
  24741. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  24742. &hashSz);
  24743. if (ret != 0)
  24744. return -10112;
  24745. if (hashSz != sizeof(expHash))
  24746. return -10113;
  24747. if (XMEMCMP(hash, expHash, hashSz) != 0)
  24748. return -10114;
  24749. ret = wc_SetEccsiHash(key, hash, hashSz);
  24750. if (ret != 0)
  24751. return -10112;
  24752. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  24753. &verified);
  24754. if (ret != 0)
  24755. return -10115;
  24756. if (!verified)
  24757. return -10116;
  24758. return 0;
  24759. }
  24760. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  24761. {
  24762. int ret;
  24763. byte data[32 * 3];
  24764. word32 sz;
  24765. mp_int decSsk;
  24766. ecc_point* decPvt = NULL;
  24767. ret = mp_init(&decSsk);
  24768. if (ret != 0)
  24769. return -10117;
  24770. decPvt = wc_ecc_new_point();
  24771. if (decPvt == NULL)
  24772. return -10118;
  24773. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  24774. if (ret != LENGTH_ONLY_E)
  24775. return -10119;
  24776. if (sz != 32 * 3)
  24777. return -10120;
  24778. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  24779. if (ret != 0)
  24780. return -10121;
  24781. if (sz != 32* 3)
  24782. return -10122;
  24783. ret = wc_DecodeEccsiPair(priv, data, sz, &decSsk, decPvt);
  24784. if (ret != 0)
  24785. return -10123;
  24786. if (mp_cmp(ssk, &decSsk) != MP_EQ)
  24787. return -10124;
  24788. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  24789. return -10125;
  24790. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  24791. if (ret != LENGTH_ONLY_E)
  24792. return -10119;
  24793. if (sz != 32)
  24794. return -10120;
  24795. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  24796. if (ret != 0)
  24797. return -10121;
  24798. if (sz != 32)
  24799. return -10122;
  24800. ret = wc_DecodeEccsiSsk(priv, data, sz, &decSsk);
  24801. if (ret != 0)
  24802. return -10123;
  24803. if (mp_cmp(ssk, &decSsk) != MP_EQ)
  24804. return -10124;
  24805. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  24806. if (ret != LENGTH_ONLY_E)
  24807. return -10126;
  24808. if (sz != 32 * 2)
  24809. return -10127;
  24810. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  24811. if (ret != 0)
  24812. return -10128;
  24813. if (sz != 32 * 2)
  24814. return -10129;
  24815. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  24816. if (ret != 0)
  24817. return -10130;
  24818. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  24819. return -10131;
  24820. sz = sizeof(data);
  24821. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  24822. if (ret != 0)
  24823. return -10128;
  24824. if (sz != 32 * 2 + 1)
  24825. return -10129;
  24826. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  24827. if (ret != 0)
  24828. return -10130;
  24829. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  24830. return -10131;
  24831. wc_ecc_del_point(decPvt);
  24832. mp_free(&decSsk);
  24833. return 0;
  24834. }
  24835. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  24836. {
  24837. int ret;
  24838. byte data[32 * 3];
  24839. byte out[32 * 3];
  24840. word32 sz;
  24841. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  24842. if (ret != LENGTH_ONLY_E)
  24843. return -10132;
  24844. if (sz != 32 * 3)
  24845. return -10133;
  24846. ret = wc_ExportEccsiKey(priv, data, &sz);
  24847. if (ret != 0)
  24848. return -10134;
  24849. ret = wc_ImportEccsiKey(priv, data, sz);
  24850. if (ret != 0)
  24851. return -10135;
  24852. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  24853. if (ret != LENGTH_ONLY_E)
  24854. return -10132;
  24855. if (sz != 32 * 3)
  24856. return -10143;
  24857. ret = wc_ExportEccsiKey(priv, out, &sz);
  24858. if (ret != 0)
  24859. return -10144;
  24860. if (sz != 32 * 3)
  24861. return -10145;
  24862. if (XMEMCMP(data, out, sz) != 0)
  24863. return -10146;
  24864. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  24865. if (ret != LENGTH_ONLY_E)
  24866. return -10156;
  24867. if (sz != 32)
  24868. return -10157;
  24869. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  24870. if (ret != 0)
  24871. return -10158;
  24872. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  24873. if (ret != 0)
  24874. return -10159;
  24875. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  24876. if (ret != LENGTH_ONLY_E)
  24877. return -10152;
  24878. if (sz != 32)
  24879. return -10163;
  24880. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  24881. if (ret != 0)
  24882. return -10164;
  24883. if (sz != 32)
  24884. return -10165;
  24885. if (XMEMCMP(data, out, sz) != 0)
  24886. return -10166;
  24887. return 0;
  24888. }
  24889. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  24890. {
  24891. int ret;
  24892. byte data[32 * 2 + 1];
  24893. byte pubData[32 * 2 + 1];
  24894. word32 sz;
  24895. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  24896. if (ret != LENGTH_ONLY_E)
  24897. return -10136;
  24898. if (sz != 32 * 2)
  24899. return -10137;
  24900. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  24901. if (ret != 0)
  24902. return -10138;
  24903. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  24904. if (ret != 0)
  24905. return -10139;
  24906. sz = sizeof(pubData);
  24907. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  24908. if (ret != 0)
  24909. return -10140;
  24910. if (sz != 32 * 2)
  24911. return -10141;
  24912. if (XMEMCMP(data, pubData, sz) != 0)
  24913. return -10142;
  24914. sz = sizeof(pubData);
  24915. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  24916. if (ret != 0)
  24917. return -10140;
  24918. if (sz != 32 * 2 + 1)
  24919. return -10141;
  24920. if (pubData[0] != 0x04)
  24921. return -10140;
  24922. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  24923. return -10142;
  24924. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  24925. if (ret != 0)
  24926. return -10139;
  24927. return 0;
  24928. }
  24929. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  24930. mp_int* ssk, ecc_point* pvt)
  24931. {
  24932. int ret;
  24933. char mail[] = "test@wolfssl.com";
  24934. byte* id = (byte*)mail;
  24935. word32 idSz = (word32) XSTRLEN(mail);
  24936. int valid;
  24937. ret = wc_MakeEccsiKey(priv, rng);
  24938. if (ret != 0)
  24939. return -10143;
  24940. ret = eccsi_imp_exp_key_test(priv);
  24941. if (ret < 0)
  24942. return ret;
  24943. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  24944. if (ret < 0)
  24945. return ret;
  24946. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  24947. if (ret != 0)
  24948. return -10144;
  24949. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  24950. &valid);
  24951. if (ret != 0)
  24952. return -10145;
  24953. if (!valid)
  24954. return -10146;
  24955. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  24956. if (ret != 0)
  24957. return ret;
  24958. return 0;
  24959. }
  24960. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  24961. mp_int* ssk, ecc_point* pvt)
  24962. {
  24963. int ret;
  24964. byte hashPriv[WC_MAX_DIGEST_SIZE];
  24965. byte hashPub[WC_MAX_DIGEST_SIZE];
  24966. byte hashSz;
  24967. byte sig[144];
  24968. word32 sigSz;
  24969. int verified, valid;
  24970. char mail[] = "test@wolfssl.com";
  24971. byte* id = (byte*)mail;
  24972. word32 idSz = (word32) XSTRLEN(mail);
  24973. byte msg[] = { 0x00 };
  24974. word32 msgSz = sizeof(msg);
  24975. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  24976. &hashSz);
  24977. if (ret != 0)
  24978. return -10147;
  24979. if (hashSz != 32)
  24980. return -10148;
  24981. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  24982. &hashSz);
  24983. if (ret != 0)
  24984. return -10149;
  24985. if (hashSz != 32)
  24986. return -10150;
  24987. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  24988. return -10151;
  24989. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  24990. if (ret != 0)
  24991. return -10149;
  24992. ret = wc_SetEccsiPair(priv, ssk, pvt);
  24993. if (ret != 0)
  24994. return -10149;
  24995. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  24996. &sigSz);
  24997. if (ret != LENGTH_ONLY_E)
  24998. return -10152;
  24999. if (sigSz != 129)
  25000. return -10153;
  25001. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  25002. &sigSz);
  25003. if (ret != 0)
  25004. return -10154;
  25005. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  25006. if (ret != 0)
  25007. return -10149;
  25008. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25009. &verified);
  25010. if (ret != 0)
  25011. return -10155;
  25012. if (!verified)
  25013. return -10156;
  25014. /* Check that changing HS results in verification failure. */
  25015. hashPub[0] ^= 0x80;
  25016. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  25017. if (ret != 0)
  25018. return -10149;
  25019. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25020. &verified);
  25021. if (ret != 0)
  25022. return -10157;
  25023. if (verified)
  25024. return -10158;
  25025. hashPub[0] ^= 0x80;
  25026. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  25027. if (ret != 0)
  25028. return -10149;
  25029. /* Check that changing msg results in verification failure. */
  25030. msg[0] ^= 0x80;
  25031. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25032. &verified);
  25033. if (ret != 0)
  25034. return -10159;
  25035. if (verified)
  25036. return -10160;
  25037. msg[0] ^= 0x80;
  25038. /* Check that changing signature results in verification failure. */
  25039. sig[0] ^= 0x80;
  25040. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25041. &verified);
  25042. if (ret != 0)
  25043. return -10161;
  25044. if (verified)
  25045. return -10162;
  25046. sig[0] ^= 0x80;
  25047. /* Check that key state hasn't been invalidated. */
  25048. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25049. &verified);
  25050. if (ret != 0)
  25051. return -10163;
  25052. if (!verified)
  25053. return -10164;
  25054. /* Check that verifying with the private key works. */
  25055. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25056. &verified);
  25057. if (ret != 0)
  25058. return -10165;
  25059. if (!verified)
  25060. return -10166;
  25061. /* Check that the KPAK is converted from montogmery form. */
  25062. ret = eccsi_imp_exp_key_test(priv);
  25063. if (ret != 0)
  25064. return ret;
  25065. /* Check that KPAK can converted to Montogmery form again. */
  25066. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25067. &verified);
  25068. if (ret != 0)
  25069. return -10167;
  25070. if (!verified)
  25071. return -10168;
  25072. /* Check that the KPAK is converted from montogmery form. */
  25073. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  25074. &valid);
  25075. if (ret != 0)
  25076. return -10169;
  25077. if (!valid)
  25078. return -10170;
  25079. /* Check that KPAK can converted to Montogmery form again. */
  25080. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  25081. &verified);
  25082. if (ret != 0)
  25083. return -10171;
  25084. if (!verified)
  25085. return -10172;
  25086. /* Check that the KPAK is converted from montogmery form. */
  25087. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  25088. if (ret != 0)
  25089. return ret;
  25090. return 0;
  25091. }
  25092. int eccsi_test(void)
  25093. {
  25094. int ret = 0;
  25095. WC_RNG rng;
  25096. EccsiKey* priv = NULL;
  25097. EccsiKey* pub = NULL;
  25098. mp_int* ssk = NULL;
  25099. ecc_point* pvt = NULL;
  25100. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  25101. DYNAMIC_TYPE_TMP_BUFFER);
  25102. if (priv == NULL) {
  25103. ret = -10205;
  25104. }
  25105. if (ret == 0) {
  25106. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  25107. DYNAMIC_TYPE_TMP_BUFFER);
  25108. if (pub == NULL) {
  25109. ret = -10206;
  25110. }
  25111. }
  25112. if (ret == 0) {
  25113. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  25114. DYNAMIC_TYPE_TMP_BUFFER);
  25115. if (ssk == NULL) {
  25116. ret = -10207;
  25117. }
  25118. }
  25119. if (ret == 0) {
  25120. #ifndef HAVE_FIPS
  25121. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25122. #else
  25123. ret = wc_InitRng(&rng);
  25124. #endif
  25125. if (ret != 0)
  25126. ret = -10200;
  25127. }
  25128. if (ret == 0) {
  25129. pvt = wc_ecc_new_point();
  25130. if (pvt == NULL)
  25131. ret = -10201;
  25132. }
  25133. if (ret == 0) {
  25134. ret = mp_init(ssk);
  25135. if (ret != 0)
  25136. ret = -10202;
  25137. }
  25138. if (ret == 0) {
  25139. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  25140. }
  25141. if (ret == 0) {
  25142. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  25143. if (ret != 0)
  25144. ret = -10203;
  25145. }
  25146. if (ret == 0) {
  25147. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  25148. if (ret != 0)
  25149. ret = -10204;
  25150. }
  25151. if (ret == 0) {
  25152. ret = eccsi_kat_verify_test(pub, pvt);
  25153. }
  25154. if (ret == 0) {
  25155. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  25156. }
  25157. if (ret == 0) {
  25158. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  25159. }
  25160. wc_FreeEccsiKey(priv);
  25161. wc_FreeEccsiKey(pub);
  25162. mp_free(ssk);
  25163. wc_ecc_del_point(pvt);
  25164. if (ret != -10200)
  25165. wc_FreeRng(&rng);
  25166. if (ssk != NULL)
  25167. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25168. if (pub != NULL)
  25169. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25170. if (priv != NULL)
  25171. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25172. return ret;
  25173. }
  25174. #endif /* WOLFCRYPT_HAVE_ECCSI */
  25175. #ifdef WOLFCRYPT_HAVE_SAKKE
  25176. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  25177. {
  25178. int ret;
  25179. byte id[1] = { 0x00 };
  25180. int valid;
  25181. byte data[256];
  25182. word32 sz;
  25183. byte auth[257];
  25184. word16 authSz;
  25185. byte ssv[256];
  25186. word16 ssvSz;
  25187. word32 len;
  25188. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  25189. if (ret != BAD_FUNC_ARG)
  25190. return -10205;
  25191. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  25192. if (ret != BAD_FUNC_ARG)
  25193. return -10206;
  25194. wc_FreeSakkeKey(NULL);
  25195. XMEMSET(key, 0, sizeof(*key));
  25196. wc_FreeSakkeKey(key);
  25197. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  25198. if (ret != 0)
  25199. return -10207;
  25200. ret = wc_MakeSakkeKey(NULL, NULL);
  25201. if (ret != BAD_FUNC_ARG)
  25202. return -10208;
  25203. ret = wc_MakeSakkeKey(key, NULL);
  25204. if (ret != BAD_FUNC_ARG)
  25205. return -10209;
  25206. ret = wc_MakeSakkeKey(NULL, rng);
  25207. if (ret != BAD_FUNC_ARG)
  25208. return -10210;
  25209. ret = wc_MakeSakkePublicKey(NULL, NULL);
  25210. if (ret != BAD_FUNC_ARG)
  25211. return -10211;
  25212. ret = wc_MakeSakkePublicKey(key, NULL);
  25213. if (ret != BAD_FUNC_ARG)
  25214. return -10212;
  25215. ret = wc_MakeSakkePublicKey(NULL, rsk);
  25216. if (ret != BAD_FUNC_ARG)
  25217. return -10213;
  25218. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  25219. if (ret != BAD_FUNC_ARG)
  25220. return -10214;
  25221. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  25222. if (ret != BAD_FUNC_ARG)
  25223. return -10215;
  25224. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  25225. if (ret != BAD_FUNC_ARG)
  25226. return -10216;
  25227. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  25228. if (ret != BAD_FUNC_ARG)
  25229. return -10217;
  25230. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  25231. if (ret != BAD_FUNC_ARG)
  25232. return -10218;
  25233. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  25234. if (ret != BAD_FUNC_ARG)
  25235. return -10219;
  25236. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  25237. if (ret != BAD_FUNC_ARG)
  25238. return -10220;
  25239. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  25240. if (ret != BAD_FUNC_ARG)
  25241. return -10221;
  25242. ret = wc_ExportSakkeKey(key, data, NULL);
  25243. if (ret != BAD_FUNC_ARG)
  25244. return -10222;
  25245. ret = wc_ExportSakkeKey(NULL, data, &sz);
  25246. if (ret != BAD_FUNC_ARG)
  25247. return -10223;
  25248. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  25249. if (ret != BAD_FUNC_ARG)
  25250. return -10224;
  25251. ret = wc_ImportSakkeKey(key, NULL, 1);
  25252. if (ret != BAD_FUNC_ARG)
  25253. return -10225;
  25254. ret = wc_ImportSakkeKey(NULL, data, 1);
  25255. if (ret != BAD_FUNC_ARG)
  25256. return -10226;
  25257. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  25258. if (ret != BAD_FUNC_ARG)
  25259. return -10227;
  25260. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  25261. if (ret != BAD_FUNC_ARG)
  25262. return -10228;
  25263. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  25264. if (ret != BAD_FUNC_ARG)
  25265. return -10229;
  25266. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  25267. if (ret != BAD_FUNC_ARG)
  25268. return -10230;
  25269. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  25270. if (ret != BAD_FUNC_ARG)
  25271. return -10231;
  25272. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  25273. if (ret != BAD_FUNC_ARG)
  25274. return -10232;
  25275. sz = sizeof(data);
  25276. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  25277. if (ret != BAD_FUNC_ARG)
  25278. return -10233;
  25279. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  25280. if (ret != BAD_FUNC_ARG)
  25281. return -10234;
  25282. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  25283. if (ret != BAD_FUNC_ARG)
  25284. return -10235;
  25285. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  25286. if (ret != BAD_FUNC_ARG)
  25287. return -10236;
  25288. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  25289. if (ret != BAD_FUNC_ARG)
  25290. return -10237;
  25291. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  25292. if (ret != BAD_FUNC_ARG)
  25293. return -10238;
  25294. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  25295. if (ret != BAD_FUNC_ARG)
  25296. return -10239;
  25297. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  25298. if (ret != BAD_FUNC_ARG)
  25299. return -10240;
  25300. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  25301. if (ret != BAD_FUNC_ARG)
  25302. return -10237;
  25303. ret = wc_ImportSakkeRsk(key, NULL, sz);
  25304. if (ret != BAD_FUNC_ARG)
  25305. return -10237;
  25306. ret = wc_ImportSakkeRsk(NULL, data, sz);
  25307. if (ret != BAD_FUNC_ARG)
  25308. return -10237;
  25309. ret = wc_ImportSakkeRsk(key, data, 1);
  25310. if (ret != BUFFER_E)
  25311. return -10237;
  25312. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  25313. if (ret != BAD_FUNC_ARG)
  25314. return -10241;
  25315. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  25316. if (ret != BAD_FUNC_ARG)
  25317. return -10242;
  25318. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  25319. if (ret != BAD_FUNC_ARG)
  25320. return -10243;
  25321. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  25322. if (ret != BAD_FUNC_ARG)
  25323. return -10244;
  25324. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  25325. if (ret != BAD_FUNC_ARG)
  25326. return -10245;
  25327. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  25328. if (ret != BAD_FUNC_ARG)
  25329. return -10246;
  25330. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  25331. if (ret != BAD_FUNC_ARG)
  25332. return -10247;
  25333. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  25334. if (ret != LENGTH_ONLY_E)
  25335. return -10248;
  25336. len--;
  25337. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  25338. if (ret != BUFFER_E)
  25339. return -10249;
  25340. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  25341. if (ret != BAD_FUNC_ARG)
  25342. return -10250;
  25343. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  25344. if (ret != BAD_FUNC_ARG)
  25345. return -10251;
  25346. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  25347. if (ret != BAD_FUNC_ARG)
  25348. return -10252;
  25349. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  25350. if (ret != BAD_FUNC_ARG)
  25351. return -10253;
  25352. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  25353. if (ret != BAD_FUNC_ARG)
  25354. return -10254;
  25355. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  25356. if (ret != BAD_FUNC_ARG)
  25357. return -10255;
  25358. ret = wc_GetSakkeAuthSize(NULL, NULL);
  25359. if (ret != BAD_FUNC_ARG)
  25360. return -10256;
  25361. ret = wc_GetSakkeAuthSize(key, NULL);
  25362. if (ret != BAD_FUNC_ARG)
  25363. return -10257;
  25364. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  25365. if (ret != BAD_FUNC_ARG)
  25366. return -10258;
  25367. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  25368. if (ret != BAD_FUNC_ARG)
  25369. return -10259;
  25370. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  25371. if (ret != BAD_FUNC_ARG)
  25372. return -10260;
  25373. ret = wc_MakeSakkePointI(NULL, id, 1);
  25374. if (ret != BAD_FUNC_ARG)
  25375. return -10261;
  25376. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  25377. if (ret != BAD_FUNC_ARG)
  25378. return -10262;
  25379. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  25380. if (ret != BAD_FUNC_ARG)
  25381. return -10263;
  25382. ret = wc_MakeSakkePointI(key, NULL, 1);
  25383. if (ret != BAD_FUNC_ARG)
  25384. return -10264;
  25385. ret = wc_MakeSakkePointI(NULL, id, 1);
  25386. if (ret != BAD_FUNC_ARG)
  25387. return -10265;
  25388. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  25389. if (ret != BAD_FUNC_ARG)
  25390. return -10266;
  25391. ret = wc_GenerateSakkePointITable(key, data, NULL);
  25392. if (ret != BAD_FUNC_ARG)
  25393. return -10267;
  25394. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  25395. if (ret != BAD_FUNC_ARG)
  25396. return -10268;
  25397. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  25398. if (ret != LENGTH_ONLY_E)
  25399. return -10269;
  25400. len--;
  25401. ret = wc_GenerateSakkePointITable(key, data, &len);
  25402. if (ret != BUFFER_E)
  25403. return -10270;
  25404. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  25405. if (ret != BAD_FUNC_ARG)
  25406. return -10271;
  25407. ret = wc_SetSakkePointITable(key, NULL, 1);
  25408. if (ret != BAD_FUNC_ARG)
  25409. return -10272;
  25410. ret = wc_SetSakkePointITable(NULL, data, 1);
  25411. if (ret != BAD_FUNC_ARG)
  25412. return -10273;
  25413. ret = wc_SetSakkePointITable(key, data, 1);
  25414. if (ret != BUFFER_E)
  25415. return -10274;
  25416. ret = wc_ClearSakkePointITable(NULL);
  25417. if (ret != BAD_FUNC_ARG)
  25418. return -10275;
  25419. ret = wc_GetSakkePointI(NULL, data, NULL);
  25420. if (ret != BAD_FUNC_ARG)
  25421. return -10276;
  25422. ret = wc_GetSakkePointI(key, data, NULL);
  25423. if (ret != BAD_FUNC_ARG)
  25424. return -10277;
  25425. ret = wc_GetSakkePointI(NULL, data, &sz);
  25426. if (ret != BAD_FUNC_ARG)
  25427. return -10278;
  25428. sz = 1;
  25429. ret = wc_GetSakkePointI(key, data, &sz);
  25430. if (ret != BUFFER_E)
  25431. return -10279;
  25432. sz = 256;
  25433. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  25434. if (ret != BAD_FUNC_ARG)
  25435. return -10280;
  25436. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  25437. if (ret != BAD_FUNC_ARG)
  25438. return -10281;
  25439. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  25440. if (ret != BAD_FUNC_ARG)
  25441. return -10282;
  25442. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  25443. if (ret != BAD_FUNC_ARG)
  25444. return -10283;
  25445. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  25446. if (ret != BAD_FUNC_ARG)
  25447. return -10284;
  25448. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  25449. if (ret != BAD_FUNC_ARG)
  25450. return -10285;
  25451. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  25452. if (ret != BAD_FUNC_ARG)
  25453. return -10286;
  25454. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  25455. if (ret != BUFFER_E)
  25456. return -10287;
  25457. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  25458. if (ret != BUFFER_E)
  25459. return -10288;
  25460. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  25461. if (ret != BAD_FUNC_ARG)
  25462. return -10286;
  25463. ret = wc_SetSakkeIdentity(key, NULL, 1);
  25464. if (ret != BAD_FUNC_ARG)
  25465. return -10286;
  25466. ret = wc_SetSakkeIdentity(NULL, id, 1);
  25467. if (ret != BAD_FUNC_ARG)
  25468. return -10286;
  25469. ssvSz = sizeof(ssv);
  25470. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  25471. auth, NULL);
  25472. if (ret != BAD_FUNC_ARG)
  25473. return -10289;
  25474. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  25475. auth, NULL);
  25476. if (ret != BAD_FUNC_ARG)
  25477. return -10290;
  25478. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  25479. auth, NULL);
  25480. if (ret != BAD_FUNC_ARG)
  25481. return -10291;
  25482. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  25483. auth, &authSz);
  25484. if (ret != BAD_FUNC_ARG)
  25485. return -10292;
  25486. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  25487. auth, NULL);
  25488. if (ret != BAD_FUNC_ARG)
  25489. return -10293;
  25490. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  25491. auth, &authSz);
  25492. if (ret != BAD_FUNC_ARG)
  25493. return -10294;
  25494. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  25495. auth, &authSz);
  25496. if (ret != BAD_FUNC_ARG)
  25497. return -10295;
  25498. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  25499. auth, &authSz);
  25500. if (ret != BAD_STATE_E)
  25501. return -10295;
  25502. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  25503. if (ret != BAD_FUNC_ARG)
  25504. return -10296;
  25505. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  25506. if (ret != BAD_FUNC_ARG)
  25507. return -10297;
  25508. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  25509. if (ret != BAD_FUNC_ARG)
  25510. return -10298;
  25511. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  25512. if (ret != BAD_FUNC_ARG)
  25513. return -10299;
  25514. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  25515. if (ret != BAD_FUNC_ARG)
  25516. return -10286;
  25517. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  25518. if (ret != BAD_FUNC_ARG)
  25519. return -10286;
  25520. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  25521. if (ret != BAD_FUNC_ARG)
  25522. return -10286;
  25523. ssvSz = sizeof(ssv);
  25524. authSz = sizeof(auth);
  25525. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  25526. authSz);
  25527. if (ret != BAD_FUNC_ARG)
  25528. return -10300;
  25529. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  25530. authSz);
  25531. if (ret != BAD_FUNC_ARG)
  25532. return -10300;
  25533. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  25534. authSz);
  25535. if (ret != BAD_FUNC_ARG)
  25536. return -10300;
  25537. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  25538. authSz);
  25539. if (ret != BAD_FUNC_ARG)
  25540. return -10300;
  25541. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  25542. authSz);
  25543. if (ret != BAD_FUNC_ARG)
  25544. return -10300;
  25545. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  25546. authSz);
  25547. if (ret != BAD_FUNC_ARG)
  25548. return -10300;
  25549. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  25550. authSz);
  25551. if (ret != BAD_FUNC_ARG)
  25552. return -10300;
  25553. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  25554. authSz);
  25555. if (ret != BAD_STATE_E)
  25556. return -10300;
  25557. ret = wc_SetSakkeIdentity(key, id, 1);
  25558. if (ret != 0)
  25559. return -10286;
  25560. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  25561. authSz);
  25562. if (ret != BAD_STATE_E)
  25563. return -10300;
  25564. ret = wc_SetSakkeIdentity(key, id, 0);
  25565. if (ret != 0)
  25566. return -10286;
  25567. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  25568. if (ret != 0)
  25569. return -10286;
  25570. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  25571. authSz);
  25572. if (ret != BAD_STATE_E)
  25573. return -10300;
  25574. wc_FreeSakkeKey(key);
  25575. return 0;
  25576. }
  25577. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  25578. {
  25579. static const byte pubData[] = {
  25580. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  25581. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  25582. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  25583. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  25584. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  25585. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  25586. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  25587. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  25588. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  25589. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  25590. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  25591. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  25592. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  25593. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  25594. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  25595. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  25596. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  25597. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  25598. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  25599. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  25600. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  25601. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  25602. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  25603. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  25604. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  25605. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  25606. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  25607. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  25608. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  25609. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  25610. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  25611. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  25612. };
  25613. static const byte rskData[] = {
  25614. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  25615. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  25616. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  25617. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  25618. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  25619. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  25620. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  25621. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  25622. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  25623. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  25624. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  25625. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  25626. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  25627. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  25628. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  25629. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  25630. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  25631. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  25632. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  25633. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  25634. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  25635. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  25636. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  25637. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  25638. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  25639. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  25640. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  25641. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  25642. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  25643. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  25644. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  25645. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  25646. };
  25647. static const byte id[] = {
  25648. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  25649. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  25650. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  25651. 0x33, 0x00
  25652. };
  25653. static const byte ssv[] = {
  25654. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  25655. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  25656. };
  25657. static const byte auth[] = {
  25658. 0x04,
  25659. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  25660. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  25661. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  25662. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  25663. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  25664. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  25665. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  25666. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  25667. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  25668. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  25669. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  25670. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  25671. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  25672. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  25673. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  25674. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  25675. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  25676. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  25677. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  25678. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  25679. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  25680. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  25681. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  25682. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  25683. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  25684. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  25685. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  25686. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  25687. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  25688. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  25689. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  25690. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  25691. };
  25692. byte encSsv[] = {
  25693. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  25694. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  25695. };
  25696. int ret;
  25697. int valid;
  25698. byte pubKey[sizeof(pubData) + 1];
  25699. word32 sz = sizeof(pubKey);
  25700. byte tmpSsv[sizeof(encSsv)];
  25701. byte* iTable = NULL;
  25702. word32 iTableLen;
  25703. byte* table = NULL;
  25704. word32 len;
  25705. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  25706. if (ret != 0)
  25707. return -10315;
  25708. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  25709. if (ret != 0)
  25710. return -10316;
  25711. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  25712. if (ret != 0)
  25713. return -10317;
  25714. if (valid != 1)
  25715. return -10318;
  25716. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  25717. if (ret != 0)
  25718. return -10319;
  25719. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  25720. if (ret != 0)
  25721. return -10319;
  25722. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  25723. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  25724. auth, sizeof(auth));
  25725. if (ret != 0)
  25726. return -10322;
  25727. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  25728. return -10320;
  25729. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  25730. if (ret != 0)
  25731. return -10321;
  25732. iTableLen = 0;
  25733. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  25734. if (ret != LENGTH_ONLY_E)
  25735. return -10322;
  25736. if (iTableLen != 0) {
  25737. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25738. if (iTable == NULL)
  25739. return -10323;
  25740. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  25741. if (ret != 0)
  25742. return -10324;
  25743. }
  25744. len = 0;
  25745. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  25746. if (ret != LENGTH_ONLY_E)
  25747. return -10325;
  25748. if (len > 0) {
  25749. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25750. if (table == NULL)
  25751. return -10326;
  25752. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  25753. if (ret != 0)
  25754. return -10327;
  25755. }
  25756. ret = wc_SetSakkeRsk(key, rsk, table, len);
  25757. if (ret != 0)
  25758. return -10319;
  25759. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  25760. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  25761. auth, sizeof(auth));
  25762. if (ret != 0)
  25763. return -10328;
  25764. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  25765. return -10329;
  25766. /* Don't reference table that is about to be freed. */
  25767. ret = wc_ClearSakkePointITable(key);
  25768. if (ret != 0)
  25769. return -10330;
  25770. /* Dispose of tables */
  25771. if (iTable != NULL)
  25772. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25773. if (table != NULL)
  25774. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25775. /* Make sure the key public key is exportable - convert to Montgomery form
  25776. * in Validation.
  25777. */
  25778. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  25779. if (ret != 0)
  25780. return -10331;
  25781. if (sz != sizeof(pubData))
  25782. return -10332;
  25783. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  25784. return -10333;
  25785. sz = sizeof(pubData) + 1;
  25786. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  25787. if (ret != 0)
  25788. return -10334;
  25789. if (sz != sizeof(pubData) + 1)
  25790. return -10335;
  25791. if (pubKey[0] != 0x04)
  25792. return -10336;
  25793. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  25794. return -10337;
  25795. return 0;
  25796. }
  25797. static int sakke_kat_encapsulate_test(SakkeKey* key)
  25798. {
  25799. static const byte pubData[] = {
  25800. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  25801. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  25802. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  25803. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  25804. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  25805. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  25806. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  25807. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  25808. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  25809. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  25810. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  25811. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  25812. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  25813. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  25814. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  25815. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  25816. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  25817. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  25818. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  25819. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  25820. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  25821. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  25822. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  25823. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  25824. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  25825. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  25826. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  25827. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  25828. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  25829. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  25830. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  25831. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  25832. };
  25833. static const byte id[] = {
  25834. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  25835. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  25836. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  25837. 0x33, 0x00
  25838. };
  25839. static word32 idSz = sizeof(id);
  25840. byte ssv[] = {
  25841. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  25842. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  25843. };
  25844. static word16 ssvSz = sizeof(ssv);
  25845. static const byte expAuth[] = {
  25846. 0x04,
  25847. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  25848. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  25849. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  25850. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  25851. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  25852. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  25853. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  25854. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  25855. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  25856. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  25857. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  25858. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  25859. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  25860. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  25861. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  25862. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  25863. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  25864. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  25865. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  25866. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  25867. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  25868. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  25869. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  25870. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  25871. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  25872. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  25873. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  25874. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  25875. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  25876. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  25877. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  25878. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  25879. };
  25880. static const byte encSsv[] = {
  25881. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  25882. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  25883. };
  25884. int ret;
  25885. byte auth[257];
  25886. word16 authSz = sizeof(auth);
  25887. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  25888. if (ret != 0)
  25889. return -10334;
  25890. ret = wc_SetSakkeIdentity(key, id, idSz);
  25891. if (ret != 0)
  25892. return -10335;
  25893. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  25894. auth, &authSz);
  25895. if (ret != 0)
  25896. return -10336;
  25897. if (authSz != 257)
  25898. return -10337;
  25899. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  25900. return -10338;
  25901. if (XMEMCMP(auth, expAuth, authSz) != 0)
  25902. return -10339;
  25903. return 0;
  25904. }
  25905. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  25906. WC_RNG* rng, ecc_point* rsk)
  25907. {
  25908. int ret;
  25909. byte data[440];
  25910. byte pubData[257];
  25911. word32 sz;
  25912. char mail[] = "test@wolfssl.com";
  25913. byte* id = (byte*)mail;
  25914. word32 idSz = (word32)XSTRLEN(mail);
  25915. int valid;
  25916. ecc_point* pubKey = rsk;
  25917. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  25918. if (ret != 0)
  25919. return -10339;
  25920. ret = wc_MakeSakkeKey(priv, rng);
  25921. if (ret != 0)
  25922. return -10340;
  25923. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  25924. if (ret != LENGTH_ONLY_E)
  25925. return -10341;
  25926. if (sz != 384)
  25927. return -10342;
  25928. sz--;
  25929. ret = wc_ExportSakkeKey(priv, data, &sz);
  25930. if (ret == 0)
  25931. return -10343;
  25932. sz++;
  25933. ret = wc_ExportSakkeKey(priv, data, &sz);
  25934. if (ret != 0)
  25935. return -10344;
  25936. if (sz != 384)
  25937. return -10345;
  25938. ret = wc_ImportSakkeKey(key, data, sz - 1);
  25939. if (ret == 0)
  25940. return -10346;
  25941. ret = wc_ImportSakkeKey(key, data, sz);
  25942. if (ret != 0)
  25943. return -10347;
  25944. wc_FreeSakkeKey(key);
  25945. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  25946. if (ret != 0)
  25947. return -10348;
  25948. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  25949. if (ret != LENGTH_ONLY_E)
  25950. return -10349;
  25951. if (sz != 128)
  25952. return -10350;
  25953. sz--;
  25954. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  25955. if (ret == 0)
  25956. return -10351;
  25957. sz++;
  25958. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  25959. if (ret != 0)
  25960. return -10352;
  25961. if (sz != 128)
  25962. return -10353;
  25963. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  25964. if (ret == 0)
  25965. return -10354;
  25966. ret = wc_ImportSakkePrivateKey(key, data, sz);
  25967. if (ret != 0)
  25968. return -10355;
  25969. ret = wc_MakeSakkePublicKey(key, pubKey);
  25970. if (ret != 0)
  25971. return -10356;
  25972. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  25973. if (ret != LENGTH_ONLY_E)
  25974. return -10357;
  25975. if (sz != 256)
  25976. return -10358;
  25977. sz--;
  25978. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  25979. if (ret == 0)
  25980. return -10359;
  25981. sz++;
  25982. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  25983. if (ret != 0)
  25984. return -10360;
  25985. if (sz != 256)
  25986. return -10361;
  25987. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  25988. if (ret == 0)
  25989. return -10362;
  25990. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  25991. if (ret != 0)
  25992. return -10363;
  25993. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  25994. if (ret != 0)
  25995. return -10364;
  25996. if (sz != 256)
  25997. return -10365;
  25998. if (XMEMCMP(data, pubData, sz) != 0)
  25999. return -10366;
  26000. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  26001. if (ret != 0)
  26002. return -10367;
  26003. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  26004. if (ret != 0)
  26005. return -10368;
  26006. if (valid != 1)
  26007. return -10369;
  26008. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  26009. if (ret != 0)
  26010. return -10370;
  26011. if (valid != 1)
  26012. return -10371;
  26013. sz = sizeof(data);
  26014. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  26015. if (ret != 0)
  26016. return -10372;
  26017. if (sz != 256)
  26018. return -10373;
  26019. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  26020. if (ret != 0)
  26021. return -10374;
  26022. sz = sizeof(pubData);
  26023. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  26024. if (ret != 0)
  26025. return -10375;
  26026. if (sz != sizeof(pubData))
  26027. return -10376;
  26028. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  26029. if (ret != 0)
  26030. return -10377;
  26031. wc_FreeSakkeKey(key);
  26032. return 0;
  26033. }
  26034. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  26035. ecc_point* rsk)
  26036. {
  26037. int ret;
  26038. byte ssv[16];
  26039. word16 ssvSz;
  26040. byte auth[257];
  26041. word16 authSz;
  26042. char mail[] = "test@wolfssl.com";
  26043. byte* id = (byte*)mail;
  26044. word32 idSz = (word32)XSTRLEN(mail);
  26045. byte pointI[256];
  26046. word32 sz;
  26047. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  26048. if (ret != LENGTH_ONLY_E)
  26049. return -10375;
  26050. if (ssvSz != 16)
  26051. return -10376;
  26052. ssvSz += 128;
  26053. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  26054. if (ret == 0)
  26055. return -10377;
  26056. ssvSz -= 128;
  26057. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  26058. if (ret != 0)
  26059. return -10378;
  26060. if (ssvSz != 16)
  26061. return -10379;
  26062. ret = wc_GetSakkeAuthSize(pub, &authSz);
  26063. if (ret != 0)
  26064. return -10380;
  26065. ret = wc_SetSakkeIdentity(pub, id, idSz);
  26066. if (ret != 0)
  26067. return -10380;
  26068. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  26069. NULL, &authSz);
  26070. if (ret != LENGTH_ONLY_E)
  26071. return -10381;
  26072. if (authSz != 257)
  26073. return -10382;
  26074. authSz--;
  26075. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  26076. auth, &authSz);
  26077. if (ret == 0)
  26078. return -10383;
  26079. authSz++;
  26080. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  26081. auth, &authSz);
  26082. if (ret != 0)
  26083. return -10384;
  26084. if (authSz != 257)
  26085. return -10385;
  26086. ret = wc_GetSakkePointI(pub, NULL, &sz);
  26087. if (ret != LENGTH_ONLY_E)
  26088. return -10386;
  26089. if (sz != 256)
  26090. return -10387;
  26091. ret = wc_GetSakkePointI(pub, pointI, &sz);
  26092. if (ret != 0)
  26093. return -10388;
  26094. if (sz != 256)
  26095. return -10389;
  26096. /* Bogus identity - make it check and regenerate I. */
  26097. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  26098. if (ret != 0)
  26099. return -10391;
  26100. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  26101. auth, &authSz);
  26102. if (ret != 0)
  26103. return -10392;
  26104. if (authSz != 257)
  26105. return -10393;
  26106. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  26107. if (ret != 0)
  26108. return -10392;
  26109. ret = wc_SetSakkeIdentity(priv, id, idSz);
  26110. if (ret != 0)
  26111. return -10392;
  26112. authSz--;
  26113. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  26114. authSz);
  26115. if (ret == 0)
  26116. return -10394;
  26117. authSz++;
  26118. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  26119. authSz);
  26120. if (ret != 0)
  26121. return -10395;
  26122. ssv[0] ^= 0x80;
  26123. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  26124. authSz);
  26125. if (ret != SAKKE_VERIFY_FAIL_E)
  26126. return -10396;
  26127. ssv[0] ^= 0x80;
  26128. /* Bogus identity - make it check and regenerate I. */
  26129. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  26130. if (ret != 0)
  26131. return -10397;
  26132. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  26133. authSz);
  26134. if (ret != 0)
  26135. return -10398;
  26136. return 0;
  26137. }
  26138. int sakke_test(void)
  26139. {
  26140. int ret = 0;
  26141. WC_RNG rng;
  26142. SakkeKey* priv = NULL;
  26143. SakkeKey* pub = NULL;
  26144. SakkeKey* key = NULL;
  26145. ecc_point* rsk = NULL;
  26146. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  26147. DYNAMIC_TYPE_TMP_BUFFER);
  26148. if (priv == NULL) {
  26149. ret = -10404;
  26150. }
  26151. if (ret == 0) {
  26152. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  26153. DYNAMIC_TYPE_TMP_BUFFER);
  26154. if (pub == NULL) {
  26155. ret = -10405;
  26156. }
  26157. }
  26158. if (ret == 0) {
  26159. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  26160. DYNAMIC_TYPE_TMP_BUFFER);
  26161. if (key == NULL) {
  26162. ret = -10406;
  26163. }
  26164. }
  26165. if (ret == 0) {
  26166. #ifndef HAVE_FIPS
  26167. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26168. #else
  26169. ret = wc_InitRng(&rng);
  26170. #endif
  26171. if (ret != 0)
  26172. ret = -10400;
  26173. }
  26174. if (ret == 0) {
  26175. rsk = wc_ecc_new_point();
  26176. if (rsk == NULL)
  26177. ret = -10401;
  26178. }
  26179. if (ret == 0) {
  26180. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  26181. if (ret != 0)
  26182. ret = -10402;
  26183. }
  26184. if (ret == 0) {
  26185. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  26186. if (ret != 0)
  26187. ret = -10403;
  26188. }
  26189. if (ret == 0) {
  26190. ret = sakke_api_test(&rng, key, rsk);
  26191. }
  26192. if (ret == 0) {
  26193. ret = sakke_kat_derive_test(pub, rsk);
  26194. }
  26195. if (ret == 0) {
  26196. ret = sakke_kat_encapsulate_test(pub);
  26197. }
  26198. if (ret == 0) {
  26199. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  26200. }
  26201. if (ret == 0) {
  26202. ret = sakke_op_test(priv, pub, &rng, rsk);
  26203. }
  26204. wc_FreeSakkeKey(priv);
  26205. wc_FreeSakkeKey(pub);
  26206. wc_ecc_forcezero_point(rsk);
  26207. wc_ecc_del_point(rsk);
  26208. if (ret != -10400)
  26209. wc_FreeRng(&rng);
  26210. if (key != NULL)
  26211. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26212. if (pub != NULL)
  26213. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26214. if (priv != NULL)
  26215. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26216. return ret;
  26217. }
  26218. #endif /* WOLFCRYPT_HAVE_SAKKE */
  26219. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  26220. typedef struct CMAC_Test_Case {
  26221. int type;
  26222. int partial;
  26223. const byte* m;
  26224. word32 mSz;
  26225. const byte* k;
  26226. word32 kSz;
  26227. const byte* t;
  26228. word32 tSz;
  26229. } CMAC_Test_Case;
  26230. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  26231. {
  26232. #ifdef WOLFSSL_AES_128
  26233. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  26234. {
  26235. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  26236. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  26237. };
  26238. #define KLEN_128 (sizeof(k128))
  26239. #endif
  26240. #ifdef WOLFSSL_AES_192
  26241. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  26242. {
  26243. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  26244. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  26245. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  26246. };
  26247. #define KLEN_192 (sizeof(k192))
  26248. #endif
  26249. #ifdef WOLFSSL_AES_256
  26250. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  26251. {
  26252. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  26253. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  26254. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  26255. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  26256. };
  26257. #define KLEN_256 (sizeof(k256))
  26258. #endif
  26259. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  26260. {
  26261. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  26262. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  26263. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  26264. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  26265. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  26266. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  26267. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  26268. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  26269. };
  26270. #define MLEN_0 (0)
  26271. #define MLEN_128 (128/8)
  26272. #define MLEN_320 (320/8)
  26273. #define MLEN_319 (MLEN_320 - 1)
  26274. #define MLEN_512 (512/8)
  26275. #ifdef WOLFSSL_AES_128
  26276. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  26277. {
  26278. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  26279. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  26280. };
  26281. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  26282. {
  26283. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  26284. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  26285. };
  26286. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  26287. {
  26288. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  26289. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  26290. };
  26291. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  26292. {
  26293. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  26294. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  26295. };
  26296. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  26297. {
  26298. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  26299. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  26300. };
  26301. #endif
  26302. #ifdef WOLFSSL_AES_192
  26303. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  26304. {
  26305. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  26306. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  26307. };
  26308. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  26309. {
  26310. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  26311. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  26312. };
  26313. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  26314. {
  26315. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  26316. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  26317. };
  26318. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  26319. {
  26320. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  26321. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  26322. };
  26323. #endif
  26324. #ifdef WOLFSSL_AES_256
  26325. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  26326. {
  26327. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  26328. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  26329. };
  26330. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  26331. {
  26332. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  26333. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  26334. };
  26335. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  26336. {
  26337. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  26338. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  26339. };
  26340. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  26341. {
  26342. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  26343. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  26344. };
  26345. #endif
  26346. const CMAC_Test_Case testCases[] =
  26347. {
  26348. #ifdef WOLFSSL_AES_128
  26349. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  26350. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  26351. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  26352. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  26353. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  26354. #endif
  26355. #ifdef WOLFSSL_AES_192
  26356. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  26357. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  26358. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  26359. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  26360. #endif
  26361. #ifdef WOLFSSL_AES_256
  26362. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  26363. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  26364. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  26365. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  26366. #endif
  26367. #ifdef WOLFSSL_AES_128
  26368. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  26369. #endif
  26370. };
  26371. #ifdef WOLFSSL_SMALL_STACK
  26372. Cmac *cmac;
  26373. #else
  26374. Cmac cmac[1];
  26375. #endif
  26376. byte tag[AES_BLOCK_SIZE];
  26377. const CMAC_Test_Case* tc;
  26378. word32 i, tagSz;
  26379. int ret;
  26380. #ifdef WOLFSSL_SMALL_STACK
  26381. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  26382. ERROR_OUT(-12009, out);
  26383. #endif
  26384. for (i = 0, tc = testCases;
  26385. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  26386. i++, tc++) {
  26387. XMEMSET(tag, 0, sizeof(tag));
  26388. tagSz = AES_BLOCK_SIZE;
  26389. #if !defined(HAVE_FIPS) || \
  26390. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  26391. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  26392. #else
  26393. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  26394. #endif
  26395. {
  26396. ERROR_OUT(-12000, out);
  26397. }
  26398. if (tc->partial) {
  26399. if (wc_CmacUpdate(cmac, tc->m,
  26400. tc->mSz/2 - tc->partial) != 0)
  26401. ERROR_OUT(-12001, out);
  26402. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  26403. tc->mSz/2 + tc->partial) != 0)
  26404. ERROR_OUT(-12002, out);
  26405. }
  26406. else {
  26407. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  26408. ERROR_OUT(-12003, out);
  26409. }
  26410. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  26411. ERROR_OUT(-12004, out);
  26412. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  26413. ERROR_OUT(-12005, out);
  26414. XMEMSET(tag, 0, sizeof(tag));
  26415. tagSz = sizeof(tag);
  26416. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  26417. tc->k, tc->kSz) != 0)
  26418. ERROR_OUT(-12006, out);
  26419. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  26420. ERROR_OUT(-12007, out);
  26421. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  26422. tc->k, tc->kSz) != 0)
  26423. ERROR_OUT(-12008, out);
  26424. }
  26425. ret = 0;
  26426. out:
  26427. #ifdef WOLFSSL_SMALL_STACK
  26428. if (cmac)
  26429. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  26430. #endif
  26431. return ret;
  26432. }
  26433. #endif /* NO_AES && WOLFSSL_CMAC */
  26434. #ifdef HAVE_LIBZ
  26435. static const byte sample_text[] =
  26436. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  26437. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  26438. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  26439. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  26440. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  26441. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  26442. "\n"
  26443. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  26444. "four loko you probably haven't heard of them high life. Messenger bag\n"
  26445. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  26446. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  26447. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  26448. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  26449. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  26450. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  26451. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  26452. "four loko whatever street art yr farm-to-table.\n"
  26453. "\n"
  26454. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  26455. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  26456. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  26457. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  26458. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  26459. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  26460. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  26461. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  26462. "locavore.\n"
  26463. "\n"
  26464. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  26465. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  26466. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  26467. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  26468. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  26469. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  26470. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  26471. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  26472. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  26473. "neutra PBR selvage.\n"
  26474. "\n"
  26475. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  26476. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  26477. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  26478. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  26479. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  26480. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  26481. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  26482. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  26483. "mollit magna, sriracha sartorial helvetica.\n"
  26484. "\n"
  26485. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  26486. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  26487. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  26488. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  26489. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  26490. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  26491. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  26492. "\n"
  26493. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  26494. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  26495. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  26496. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  26497. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  26498. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  26499. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  26500. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  26501. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  26502. "\n"
  26503. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  26504. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  26505. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  26506. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  26507. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  26508. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  26509. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  26510. "bag dolor terry richardson sapiente.\n";
  26511. static const byte sample_text_gz[] = {
  26512. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  26513. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  26514. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  26515. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  26516. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  26517. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  26518. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  26519. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  26520. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  26521. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  26522. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  26523. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  26524. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  26525. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  26526. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  26527. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  26528. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  26529. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  26530. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  26531. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  26532. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  26533. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  26534. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  26535. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  26536. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  26537. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  26538. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  26539. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  26540. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  26541. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  26542. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  26543. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  26544. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  26545. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  26546. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  26547. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  26548. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  26549. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  26550. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  26551. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  26552. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  26553. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  26554. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  26555. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  26556. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  26557. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  26558. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  26559. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  26560. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  26561. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  26562. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  26563. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  26564. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  26565. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  26566. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  26567. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  26568. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  26569. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  26570. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  26571. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  26572. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  26573. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  26574. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  26575. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  26576. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  26577. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  26578. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  26579. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  26580. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  26581. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  26582. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  26583. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  26584. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  26585. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  26586. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  26587. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  26588. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  26589. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  26590. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  26591. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  26592. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  26593. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  26594. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  26595. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  26596. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  26597. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  26598. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  26599. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  26600. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  26601. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  26602. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  26603. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  26604. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  26605. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  26606. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  26607. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  26608. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  26609. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  26610. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  26611. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  26612. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  26613. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  26614. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  26615. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  26616. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  26617. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  26618. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  26619. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  26620. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  26621. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  26622. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  26623. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  26624. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  26625. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  26626. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  26627. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  26628. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  26629. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  26630. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  26631. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  26632. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  26633. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  26634. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  26635. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  26636. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  26637. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  26638. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  26639. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  26640. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  26641. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  26642. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  26643. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  26644. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  26645. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  26646. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  26647. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  26648. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  26649. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  26650. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  26651. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  26652. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  26653. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  26654. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  26655. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  26656. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  26657. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  26658. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  26659. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  26660. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  26661. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  26662. };
  26663. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  26664. {
  26665. int ret = 0;
  26666. word32 dSz = sizeof(sample_text);
  26667. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  26668. byte *c;
  26669. byte *d;
  26670. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26671. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26672. if (c == NULL || d == NULL) {
  26673. ERROR_OUT(-12100, exit);
  26674. }
  26675. /* follow calloc and initialize to 0 */
  26676. XMEMSET(c, 0, cSz);
  26677. XMEMSET(d, 0, dSz);
  26678. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  26679. ERROR_OUT(-12101, exit);
  26680. }
  26681. cSz = (word32)ret;
  26682. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  26683. ERROR_OUT(-12102, exit);
  26684. }
  26685. if (XMEMCMP(d, sample_text, dSz) != 0) {
  26686. ERROR_OUT(-12103, exit);
  26687. }
  26688. /* GZIP tests */
  26689. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  26690. XMEMSET(c, 0, cSz);
  26691. XMEMSET(d, 0, dSz);
  26692. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  26693. if (ret < 0) {
  26694. ERROR_OUT(-12104, exit);
  26695. }
  26696. cSz = (word32)ret;
  26697. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  26698. if (ret < 0) {
  26699. ERROR_OUT(-12105, exit);
  26700. }
  26701. if (XMEMCMP(d, sample_text, dSz) != 0) {
  26702. ERROR_OUT(-12106, exit);
  26703. }
  26704. /* Try with gzip generated output */
  26705. XMEMSET(d, 0, dSz);
  26706. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  26707. LIBZ_WINBITS_GZIP);
  26708. if (ret < 0) {
  26709. ERROR_OUT(-12107, exit);
  26710. }
  26711. dSz = (word32)ret;
  26712. if (XMEMCMP(d, sample_text, dSz) != 0) {
  26713. ERROR_OUT(-12108, exit);
  26714. }
  26715. ret = 0; /* success */
  26716. exit:
  26717. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26718. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26719. return ret;
  26720. }
  26721. #endif /* HAVE_LIBZ */
  26722. #ifdef HAVE_PKCS7
  26723. /* External Debugging/Testing Note:
  26724. *
  26725. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  26726. * additional testing. To dump bundles to files DER encoded files, please
  26727. * define:
  26728. *
  26729. * #define PKCS7_OUTPUT_TEST_BUNDLES
  26730. */
  26731. /* Loads certs and keys for use with PKCS7 tests, from either files
  26732. * or buffers.
  26733. *
  26734. * rsaClientCertBuf - output buffer for RSA client cert
  26735. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  26736. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  26737. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  26738. *
  26739. * rsaServerCertBuf - output buffer for RSA server cert
  26740. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  26741. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  26742. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  26743. *
  26744. * rsaCaCertBuf - output buffer for RSA CA cert
  26745. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  26746. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  26747. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  26748. *
  26749. * eccClientCertBuf - output buffer for ECC cert
  26750. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  26751. * eccClientPrivKeyBuf - output buffer for ECC private key
  26752. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  26753. *
  26754. * Returns 0 on success, negative on error
  26755. */
  26756. static int pkcs7_load_certs_keys(
  26757. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  26758. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  26759. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  26760. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  26761. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  26762. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  26763. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  26764. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  26765. {
  26766. #ifndef NO_FILESYSTEM
  26767. XFILE certFile;
  26768. XFILE keyFile;
  26769. (void)certFile;
  26770. (void)keyFile;
  26771. #endif
  26772. #ifndef NO_RSA
  26773. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  26774. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  26775. return BAD_FUNC_ARG;
  26776. #endif
  26777. #ifdef HAVE_ECC
  26778. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  26779. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  26780. return BAD_FUNC_ARG;
  26781. #endif
  26782. /* RSA */
  26783. #ifndef NO_RSA
  26784. #ifdef USE_CERT_BUFFERS_1024
  26785. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  26786. return -12110;
  26787. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  26788. sizeof_client_cert_der_1024);
  26789. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  26790. if (rsaServerCertBuf != NULL) {
  26791. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  26792. return -12111;
  26793. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  26794. sizeof_server_cert_der_1024);
  26795. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  26796. }
  26797. if (rsaCaCertBuf != NULL) {
  26798. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  26799. return -12112;
  26800. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  26801. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  26802. }
  26803. #elif defined(USE_CERT_BUFFERS_2048)
  26804. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  26805. return -12113;
  26806. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  26807. sizeof_client_cert_der_2048);
  26808. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  26809. if (rsaServerCertBuf != NULL) {
  26810. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  26811. return -12114;
  26812. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  26813. sizeof_server_cert_der_2048);
  26814. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  26815. }
  26816. if (rsaCaCertBuf != NULL) {
  26817. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  26818. return -12115;
  26819. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  26820. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  26821. }
  26822. #else
  26823. certFile = XFOPEN(clientCert, "rb");
  26824. if (!certFile)
  26825. return -12116;
  26826. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  26827. *rsaClientCertBufSz, certFile);
  26828. XFCLOSE(certFile);
  26829. if (rsaServerCertBuf != NULL) {
  26830. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  26831. if (!certFile)
  26832. return -12117;
  26833. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  26834. *rsaServerCertBufSz, certFile);
  26835. XFCLOSE(certFile);
  26836. }
  26837. if (rsaCaCertBuf != NULL) {
  26838. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  26839. if (!certFile)
  26840. return -12118;
  26841. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  26842. certFile);
  26843. XFCLOSE(certFile);
  26844. }
  26845. #endif
  26846. #ifdef USE_CERT_BUFFERS_1024
  26847. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  26848. return -12119;
  26849. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  26850. sizeof_client_key_der_1024);
  26851. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  26852. if (rsaServerPrivKeyBuf != NULL) {
  26853. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  26854. return -12120;
  26855. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  26856. sizeof_server_key_der_1024);
  26857. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  26858. }
  26859. if (rsaCaPrivKeyBuf != NULL) {
  26860. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  26861. return -12121;
  26862. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  26863. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  26864. }
  26865. #elif defined(USE_CERT_BUFFERS_2048)
  26866. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  26867. return -12122;
  26868. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  26869. sizeof_client_key_der_2048);
  26870. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  26871. if (rsaServerPrivKeyBuf != NULL) {
  26872. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  26873. return -12123;
  26874. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  26875. sizeof_server_key_der_2048);
  26876. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  26877. }
  26878. if (rsaCaPrivKeyBuf != NULL) {
  26879. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  26880. return -12124;
  26881. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  26882. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  26883. }
  26884. #else
  26885. keyFile = XFOPEN(clientKey, "rb");
  26886. if (!keyFile)
  26887. return -12125;
  26888. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  26889. *rsaClientPrivKeyBufSz, keyFile);
  26890. XFCLOSE(keyFile);
  26891. if (rsaServerPrivKeyBuf != NULL) {
  26892. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  26893. if (!keyFile)
  26894. return -12126;
  26895. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  26896. *rsaServerPrivKeyBufSz, keyFile);
  26897. XFCLOSE(keyFile);
  26898. }
  26899. if (rsaCaPrivKeyBuf != NULL) {
  26900. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  26901. if (!keyFile)
  26902. return -12127;
  26903. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  26904. *rsaCaPrivKeyBufSz, keyFile);
  26905. XFCLOSE(keyFile);
  26906. }
  26907. #endif /* USE_CERT_BUFFERS */
  26908. #endif /* NO_RSA */
  26909. /* ECC */
  26910. #ifdef HAVE_ECC
  26911. #ifdef USE_CERT_BUFFERS_256
  26912. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  26913. return -12128;
  26914. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  26915. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  26916. #else
  26917. certFile = XFOPEN(eccClientCert, "rb");
  26918. if (!certFile)
  26919. return -12129;
  26920. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  26921. *eccClientCertBufSz, certFile);
  26922. XFCLOSE(certFile);
  26923. #endif /* USE_CERT_BUFFERS_256 */
  26924. #ifdef USE_CERT_BUFFERS_256
  26925. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  26926. return -12130;
  26927. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  26928. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  26929. #else
  26930. keyFile = XFOPEN(eccClientKey, "rb");
  26931. if (!keyFile)
  26932. return -12131;
  26933. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  26934. *eccClientPrivKeyBufSz, keyFile);
  26935. XFCLOSE(keyFile);
  26936. #endif /* USE_CERT_BUFFERS_256 */
  26937. #endif /* HAVE_ECC */
  26938. #ifdef NO_RSA
  26939. (void)rsaClientCertBuf;
  26940. (void)rsaClientCertBufSz;
  26941. (void)rsaClientPrivKeyBuf;
  26942. (void)rsaClientPrivKeyBufSz;
  26943. (void)rsaServerCertBuf;
  26944. (void)rsaServerCertBufSz;
  26945. (void)rsaServerPrivKeyBuf;
  26946. (void)rsaServerPrivKeyBufSz;
  26947. (void)rsaCaCertBuf;
  26948. (void)rsaCaCertBufSz;
  26949. (void)rsaCaPrivKeyBuf;
  26950. (void)rsaCaPrivKeyBufSz;
  26951. #endif
  26952. #ifndef HAVE_ECC
  26953. (void)eccClientCertBuf;
  26954. (void)eccClientCertBufSz;
  26955. (void)eccClientPrivKeyBuf;
  26956. (void)eccClientPrivKeyBufSz;
  26957. #endif
  26958. #ifndef NO_FILESYSTEM
  26959. (void)certFile;
  26960. (void)keyFile;
  26961. #endif
  26962. return 0;
  26963. }
  26964. typedef struct {
  26965. const byte* content;
  26966. word32 contentSz;
  26967. int contentOID;
  26968. int encryptOID;
  26969. int keyWrapOID;
  26970. int keyAgreeOID;
  26971. byte* cert;
  26972. size_t certSz;
  26973. byte* privateKey;
  26974. word32 privateKeySz;
  26975. byte* optionalUkm;
  26976. word32 optionalUkmSz;
  26977. int ktriOptions; /* KTRI options flags */
  26978. int kariOptions; /* KARI options flags */
  26979. /* KEKRI specific */
  26980. byte* secretKey; /* key, only for kekri RecipientInfo types */
  26981. word32 secretKeySz; /* size of secretKey, bytes */
  26982. byte* secretKeyId; /* key identifier */
  26983. word32 secretKeyIdSz; /* size of key identifier, bytes */
  26984. void* timePtr; /* time_t pointer */
  26985. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  26986. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  26987. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  26988. word32 otherAttrSz; /* size of otherAttr, bytes */
  26989. int kekriOptions; /* KEKRI options flags */
  26990. /* PWRI specific */
  26991. char* password;
  26992. word32 passwordSz;
  26993. byte* salt;
  26994. word32 saltSz;
  26995. int kdfOID;
  26996. int hashOID;
  26997. int kdfIterations;
  26998. int pwriOptions; /* PWRI options flags */
  26999. /* ORI specific */
  27000. int isOri;
  27001. int oriOptions; /* ORI options flags */
  27002. const char* outFileName;
  27003. } pkcs7EnvelopedVector;
  27004. static const byte asnDataOid[] = {
  27005. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  27006. };
  27007. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  27008. * and giving wolfCrypt the value for oriOID and oriValue to place in
  27009. * OtherRecipientInfo.
  27010. *
  27011. * Returns 0 on success, negative upon error. */
  27012. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  27013. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  27014. void* ctx)
  27015. {
  27016. int i;
  27017. /* make sure buffers are large enough */
  27018. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  27019. return -12140;
  27020. /* our simple encryption algorithm will be take the bitwise complement */
  27021. oriValue[0] = 0x04; /*ASN OCTET STRING */
  27022. oriValue[1] = (byte)cekSz; /* length */
  27023. for (i = 0; i < (int)cekSz; i++) {
  27024. oriValue[2 + i] = ~cek[i];
  27025. }
  27026. *oriValueSz = 2 + cekSz;
  27027. /* set oriType to ASN.1 encoded data OID */
  27028. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  27029. *oriTypeSz = sizeof(asnDataOid);
  27030. (void)pkcs7;
  27031. (void)ctx;
  27032. return 0;
  27033. }
  27034. /* ORI decrypt callback, responsible for providing a decrypted content
  27035. * encryption key (CEK) placed into decryptedKey and size placed into
  27036. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  27037. * in decrypting the encrypted CEK.
  27038. *
  27039. * Returns 0 on success, negative upon error. */
  27040. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  27041. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  27042. word32* decryptedKeySz, void* ctx)
  27043. {
  27044. int i;
  27045. /* make sure oriType matches what we expect */
  27046. if (oriTypeSz != sizeof(asnDataOid))
  27047. return -12150;
  27048. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  27049. return -12151;
  27050. /* make sure decrypted buffer is large enough */
  27051. if (*decryptedKeySz < oriValueSz)
  27052. return -12152;
  27053. /* decrypt encrypted CEK using simple bitwise complement,
  27054. only for example */
  27055. for (i = 0; i < (int)oriValueSz - 2; i++) {
  27056. decryptedKey[i] = ~oriValue[2 + i];
  27057. }
  27058. *decryptedKeySz = oriValueSz - 2;
  27059. (void)pkcs7;
  27060. (void)ctx;
  27061. return 0;
  27062. }
  27063. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27064. /* returns 0 on success */
  27065. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  27066. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  27067. byte* in, int inSz, byte* out, void* usrCtx)
  27068. {
  27069. int keyId = -1, ret, keySz;
  27070. word32 keyIdSz = 8;
  27071. const byte* key;
  27072. byte keyIdRaw[8];
  27073. #ifdef WOLFSSL_SMALL_STACK
  27074. Aes *aes;
  27075. #else
  27076. Aes aes[1];
  27077. #endif
  27078. /* looking for KEY ID
  27079. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  27080. */
  27081. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  27082. /* 0x06, 0x0B do not pass in tag and length */
  27083. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  27084. 0x01, 0x09, 0x10, 0x02, 0x25
  27085. };
  27086. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  27087. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27088. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27089. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27090. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27091. };
  27092. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  27093. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27094. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27095. };
  27096. /* test user context passed in */
  27097. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  27098. return -12160;
  27099. }
  27100. #ifdef WOLFSSL_SMALL_STACK
  27101. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  27102. return -12164;
  27103. #endif
  27104. /* if needing to find keyIdSz can call with NULL */
  27105. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  27106. &keyIdSz);
  27107. if (ret != LENGTH_ONLY_E) {
  27108. printf("Unexpected error %d when getting keyIdSz\n", ret);
  27109. printf("Possibly no KEY ID attribute set\n");
  27110. ERROR_OUT(-12161, out);
  27111. }
  27112. else {
  27113. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  27114. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  27115. &keyIdSz);
  27116. if (ret < 0) {
  27117. goto out;
  27118. }
  27119. if (keyIdSz < 3) {
  27120. printf("keyIdSz is smaller than expected\n");
  27121. ERROR_OUT(-12162, out);
  27122. }
  27123. if (keyIdSz > 2 + sizeof(int)) {
  27124. printf("example case was only expecting a keyId of int size\n");
  27125. ERROR_OUT(-12163, out);
  27126. }
  27127. /* keyIdRaw[0] OCTET TAG */
  27128. /* keyIdRaw[1] Length */
  27129. #ifdef BIG_ENDIAN_ORDER
  27130. if (keyIdRaw[1] == 0x01) {
  27131. keyId = 1;
  27132. }
  27133. #else
  27134. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  27135. #endif
  27136. }
  27137. /* Use keyID here if found to select key and decrypt in HSM or in this
  27138. * example just select key and do software decryption */
  27139. if (keyId == 1) {
  27140. key = altKey;
  27141. keySz = sizeof(altKey);
  27142. }
  27143. else {
  27144. key = defKey;
  27145. keySz = sizeof(defKey);
  27146. }
  27147. switch (encryptOID) {
  27148. #ifdef WOLFSSL_AES_256
  27149. case AES256CBCb:
  27150. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  27151. ERROR_OUT(BAD_FUNC_ARG, out);
  27152. break;
  27153. #endif
  27154. #ifdef WOLFSSL_AES_128
  27155. case AES128CBCb:
  27156. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  27157. ERROR_OUT(BAD_FUNC_ARG, out);
  27158. break;
  27159. #endif
  27160. default:
  27161. printf("Unsupported content cipher type for example");
  27162. ERROR_OUT(ALGO_ID_E, out);
  27163. };
  27164. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  27165. if (ret == 0) {
  27166. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  27167. if (ret == 0)
  27168. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  27169. wc_AesFree(aes);
  27170. }
  27171. out:
  27172. #ifdef WOLFSSL_SMALL_STACK
  27173. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27174. #endif
  27175. (void)aad;
  27176. (void)aadSz;
  27177. (void)authTag;
  27178. (void)authTagSz;
  27179. return ret;
  27180. }
  27181. #endif /* !NO_AES && HAVE_AES_CBC */
  27182. #define PKCS7_BUF_SIZE 2048
  27183. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  27184. byte* rsaPrivKey, word32 rsaPrivKeySz,
  27185. byte* eccCert, word32 eccCertSz,
  27186. byte* eccPrivKey, word32 eccPrivKeySz)
  27187. {
  27188. int ret = 0, testSz, i;
  27189. int envelopedSz, decodedSz;
  27190. byte *enveloped;
  27191. byte *decoded;
  27192. PKCS7* pkcs7;
  27193. #ifdef ECC_TIMING_RESISTANT
  27194. WC_RNG rng;
  27195. #endif
  27196. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  27197. XFILE pkcs7File;
  27198. #endif
  27199. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  27200. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  27201. 0x72,0x6c,0x64
  27202. };
  27203. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  27204. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  27205. byte optionalUkm[] = {
  27206. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  27207. };
  27208. #endif /* NO_AES */
  27209. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  27210. !defined(NO_SHA)
  27211. /* encryption key for kekri recipient types */
  27212. byte secretKey[] = {
  27213. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  27214. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  27215. };
  27216. /* encryption key identifier */
  27217. byte secretKeyId[] = {
  27218. 0x02,0x02,0x03,0x04
  27219. };
  27220. #endif
  27221. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  27222. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  27223. #ifndef HAVE_FIPS
  27224. char password[] = "password"; /* NOTE: Password is too short for FIPS */
  27225. #else
  27226. char password[] = "passwordFIPS_MODE";
  27227. #endif
  27228. byte salt[] = {
  27229. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  27230. };
  27231. #endif
  27232. const pkcs7EnvelopedVector testVectors[] =
  27233. {
  27234. /* key transport key encryption technique */
  27235. #ifndef NO_RSA
  27236. #ifndef NO_DES3
  27237. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  27238. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  27239. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27240. "pkcs7envelopedDataDES3.der"},
  27241. #endif
  27242. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27243. #ifdef WOLFSSL_AES_128
  27244. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  27245. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  27246. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27247. "pkcs7envelopedDataAES128CBC.der"},
  27248. #endif
  27249. #ifdef WOLFSSL_AES_192
  27250. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  27251. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  27252. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27253. "pkcs7envelopedDataAES192CBC.der"},
  27254. #endif
  27255. #ifdef WOLFSSL_AES_256
  27256. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  27257. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  27258. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27259. "pkcs7envelopedDataAES256CBC.der"},
  27260. /* explicitly using SKID for SubjectKeyIdentifier */
  27261. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  27262. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  27263. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27264. "pkcs7envelopedDataAES256CBC_SKID.der"},
  27265. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  27266. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  27267. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  27268. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  27269. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der"},
  27270. #endif
  27271. #endif /* !NO_AES && HAVE_AES_CBC */
  27272. #endif
  27273. /* key agreement key encryption technique*/
  27274. #ifdef HAVE_ECC
  27275. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27276. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  27277. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  27278. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27279. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  27280. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27281. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  27282. #endif
  27283. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  27284. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  27285. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27286. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  27287. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27288. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  27289. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  27290. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  27291. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  27292. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27293. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  27294. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27295. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  27296. /* with optional user keying material (ukm) */
  27297. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  27298. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27299. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  27300. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27301. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  27302. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  27303. #endif /* !NO_AES && HAVE_AES_CBC */
  27304. #endif
  27305. /* kekri (KEKRecipientInfo) recipient types */
  27306. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27307. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  27308. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  27309. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  27310. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  27311. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  27312. "pkcs7envelopedDataAES128CBC_KEKRI.der"},
  27313. #endif
  27314. #endif /* !NO_AES && HAVE_AES_CBC */
  27315. /* pwri (PasswordRecipientInfo) recipient types */
  27316. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  27317. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  27318. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  27319. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  27320. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  27321. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  27322. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der"},
  27323. #endif
  27324. #endif
  27325. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  27326. /* ori (OtherRecipientInfo) recipient types */
  27327. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  27328. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  27329. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der"},
  27330. #endif
  27331. };
  27332. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27333. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27334. if ((! enveloped) || (! decoded)) {
  27335. ERROR_OUT(-12170, out);
  27336. }
  27337. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  27338. #ifdef ECC_TIMING_RESISTANT
  27339. #ifndef HAVE_FIPS
  27340. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27341. #else
  27342. ret = wc_InitRng(&rng);
  27343. #endif
  27344. if (ret != 0) {
  27345. ERROR_OUT(-12171, out);
  27346. }
  27347. #endif
  27348. for (i = 0; i < testSz; i++) {
  27349. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  27350. #ifdef WOLFSSL_ASYNC_CRYPT
  27351. INVALID_DEVID /* async PKCS7 is not supported */
  27352. #else
  27353. devId
  27354. #endif
  27355. );
  27356. if (pkcs7 == NULL) {
  27357. ERROR_OUT(-12172, out);
  27358. }
  27359. if (testVectors[i].secretKey != NULL) {
  27360. /* KEKRI recipient type */
  27361. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  27362. if (ret != 0) {
  27363. ERROR_OUT(-12173, out);
  27364. }
  27365. pkcs7->content = (byte*)testVectors[i].content;
  27366. pkcs7->contentSz = testVectors[i].contentSz;
  27367. pkcs7->contentOID = testVectors[i].contentOID;
  27368. pkcs7->encryptOID = testVectors[i].encryptOID;
  27369. pkcs7->ukm = testVectors[i].optionalUkm;
  27370. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  27371. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  27372. testVectors[i].secretKey, testVectors[i].secretKeySz,
  27373. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  27374. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  27375. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  27376. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  27377. if (ret < 0) {
  27378. wc_PKCS7_Free(pkcs7);
  27379. ERROR_OUT(-12174, out);
  27380. }
  27381. /* set key, for decryption */
  27382. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  27383. testVectors[i].secretKeySz);
  27384. if (ret != 0) {
  27385. wc_PKCS7_Free(pkcs7);
  27386. ERROR_OUT(-12175, out);
  27387. }
  27388. } else if (testVectors[i].password != NULL) {
  27389. #ifndef NO_PWDBASED
  27390. /* PWRI recipient type */
  27391. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  27392. if (ret != 0) {
  27393. ERROR_OUT(-12176, out);
  27394. }
  27395. pkcs7->content = (byte*)testVectors[i].content;
  27396. pkcs7->contentSz = testVectors[i].contentSz;
  27397. pkcs7->contentOID = testVectors[i].contentOID;
  27398. pkcs7->encryptOID = testVectors[i].encryptOID;
  27399. pkcs7->ukm = testVectors[i].optionalUkm;
  27400. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  27401. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  27402. (byte*)testVectors[i].password,
  27403. testVectors[i].passwordSz, testVectors[i].salt,
  27404. testVectors[i].saltSz, testVectors[i].kdfOID,
  27405. testVectors[i].hashOID, testVectors[i].kdfIterations,
  27406. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  27407. if (ret < 0) {
  27408. wc_PKCS7_Free(pkcs7);
  27409. ERROR_OUT(-12177, out);
  27410. }
  27411. /* set password, for decryption */
  27412. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  27413. testVectors[i].passwordSz);
  27414. if (ret < 0) {
  27415. wc_PKCS7_Free(pkcs7);
  27416. ERROR_OUT(-12178, out);
  27417. }
  27418. #endif /* NO_PWDBASED */
  27419. } else if (testVectors[i].isOri == 1) {
  27420. /* ORI recipient type */
  27421. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  27422. if (ret != 0) {
  27423. ERROR_OUT(-12179, out);
  27424. }
  27425. pkcs7->content = (byte*)testVectors[i].content;
  27426. pkcs7->contentSz = testVectors[i].contentSz;
  27427. pkcs7->contentOID = testVectors[i].contentOID;
  27428. pkcs7->encryptOID = testVectors[i].encryptOID;
  27429. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  27430. testVectors[i].oriOptions);
  27431. if (ret < 0) {
  27432. wc_PKCS7_Free(pkcs7);
  27433. ERROR_OUT(-12180, out);
  27434. }
  27435. /* set decrypt callback for decryption */
  27436. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  27437. if (ret < 0) {
  27438. wc_PKCS7_Free(pkcs7);
  27439. ERROR_OUT(-12181, out);
  27440. }
  27441. } else {
  27442. /* KTRI or KARI recipient types */
  27443. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  27444. if (ret != 0) {
  27445. ERROR_OUT(-12182, out);
  27446. }
  27447. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  27448. (word32)testVectors[i].certSz);
  27449. if (ret != 0) {
  27450. wc_PKCS7_Free(pkcs7);
  27451. ERROR_OUT(-12183, out);
  27452. }
  27453. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  27454. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  27455. pkcs7->privateKey = testVectors[i].privateKey;
  27456. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  27457. pkcs7->content = (byte*)testVectors[i].content;
  27458. pkcs7->contentSz = testVectors[i].contentSz;
  27459. pkcs7->contentOID = testVectors[i].contentOID;
  27460. pkcs7->encryptOID = testVectors[i].encryptOID;
  27461. pkcs7->ukm = testVectors[i].optionalUkm;
  27462. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  27463. /* set SubjectIdentifier type for KTRI types */
  27464. if (testVectors[i].ktriOptions & CMS_SKID) {
  27465. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  27466. if (ret != 0) {
  27467. wc_PKCS7_Free(pkcs7);
  27468. ERROR_OUT(-12184, out);
  27469. }
  27470. } else if (testVectors[i].ktriOptions &
  27471. CMS_ISSUER_AND_SERIAL_NUMBER) {
  27472. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  27473. CMS_ISSUER_AND_SERIAL_NUMBER);
  27474. if (ret != 0) {
  27475. wc_PKCS7_Free(pkcs7);
  27476. ERROR_OUT(-12185, out);
  27477. }
  27478. }
  27479. }
  27480. #ifdef ECC_TIMING_RESISTANT
  27481. pkcs7->rng = &rng;
  27482. #endif
  27483. /* encode envelopedData */
  27484. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  27485. PKCS7_BUF_SIZE);
  27486. if (envelopedSz <= 0) {
  27487. wc_PKCS7_Free(pkcs7);
  27488. ERROR_OUT(-12186, out);
  27489. }
  27490. /* decode envelopedData */
  27491. pkcs7->contentOID = 0;
  27492. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  27493. decoded, PKCS7_BUF_SIZE);
  27494. if (pkcs7->contentOID != testVectors[i].contentOID ||
  27495. decodedSz <= 0) {
  27496. wc_PKCS7_Free(pkcs7);
  27497. ERROR_OUT(-12187, out);
  27498. }
  27499. /* test decode result */
  27500. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  27501. wc_PKCS7_Free(pkcs7);
  27502. ERROR_OUT(-12188, out);
  27503. }
  27504. #ifndef NO_PKCS7_STREAM
  27505. { /* test reading byte by byte */
  27506. int z;
  27507. for (z = 0; z < envelopedSz; z++) {
  27508. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  27509. decoded, PKCS7_BUF_SIZE);
  27510. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  27511. printf("unexpected error %d\n", decodedSz);
  27512. ERROR_OUT(-12189, out);
  27513. }
  27514. }
  27515. /* test decode result */
  27516. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  27517. printf("stream read compare failed\n");
  27518. wc_PKCS7_Free(pkcs7);
  27519. ERROR_OUT(-12190, out);
  27520. }
  27521. }
  27522. #endif
  27523. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  27524. /* output pkcs7 envelopedData for external testing */
  27525. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  27526. if (!pkcs7File) {
  27527. wc_PKCS7_Free(pkcs7);
  27528. ERROR_OUT(-12191, out);
  27529. }
  27530. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  27531. XFCLOSE(pkcs7File);
  27532. if (ret != envelopedSz) {
  27533. wc_PKCS7_Free(pkcs7);
  27534. ERROR_OUT(-12192, out);
  27535. } else {
  27536. /* reset ret to 0 for success */
  27537. ret = 0;
  27538. }
  27539. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  27540. wc_PKCS7_Free(pkcs7);
  27541. pkcs7 = NULL;
  27542. }
  27543. #ifdef ECC_TIMING_RESISTANT
  27544. wc_FreeRng(&rng);
  27545. #endif
  27546. (void)eccCert;
  27547. (void)eccCertSz;
  27548. (void)eccPrivKey;
  27549. (void)eccPrivKeySz;
  27550. (void)rsaCert;
  27551. (void)rsaCertSz;
  27552. (void)rsaPrivKey;
  27553. (void)rsaPrivKeySz;
  27554. out:
  27555. if (enveloped)
  27556. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27557. if (decoded)
  27558. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27559. return ret;
  27560. }
  27561. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  27562. {
  27563. int ret = 0;
  27564. byte* rsaCert = NULL;
  27565. byte* rsaPrivKey = NULL;
  27566. word32 rsaCertSz = 0;
  27567. word32 rsaPrivKeySz = 0;
  27568. byte* eccCert = NULL;
  27569. byte* eccPrivKey = NULL;
  27570. word32 eccCertSz = 0;
  27571. word32 eccPrivKeySz = 0;
  27572. #ifndef NO_RSA
  27573. /* read client RSA cert and key in DER format */
  27574. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27575. if (rsaCert == NULL)
  27576. return -12200;
  27577. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27578. if (rsaPrivKey == NULL) {
  27579. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27580. return -12201;
  27581. }
  27582. rsaCertSz = FOURK_BUF;
  27583. rsaPrivKeySz = FOURK_BUF;
  27584. #endif /* NO_RSA */
  27585. #ifdef HAVE_ECC
  27586. /* read client ECC cert and key in DER format */
  27587. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27588. if (eccCert == NULL) {
  27589. #ifndef NO_RSA
  27590. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27591. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27592. #endif
  27593. return -12202;
  27594. }
  27595. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27596. if (eccPrivKey == NULL) {
  27597. #ifndef NO_RSA
  27598. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27599. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27600. #endif
  27601. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27602. return -12203;
  27603. }
  27604. eccCertSz = FOURK_BUF;
  27605. eccPrivKeySz = FOURK_BUF;
  27606. #endif /* HAVE_ECC */
  27607. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  27608. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  27609. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  27610. eccPrivKey, &eccPrivKeySz);
  27611. if (ret < 0) {
  27612. #ifndef NO_RSA
  27613. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27614. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27615. #endif
  27616. #ifdef HAVE_ECC
  27617. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27618. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27619. #endif
  27620. return -12204;
  27621. }
  27622. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  27623. rsaPrivKey, (word32)rsaPrivKeySz,
  27624. eccCert, (word32)eccCertSz,
  27625. eccPrivKey, (word32)eccPrivKeySz);
  27626. #ifndef NO_RSA
  27627. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27628. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27629. #endif
  27630. #ifdef HAVE_ECC
  27631. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27632. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27633. #endif
  27634. return ret;
  27635. }
  27636. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  27637. typedef struct {
  27638. const byte* content;
  27639. word32 contentSz;
  27640. int contentOID;
  27641. int encryptOID;
  27642. int keyWrapOID;
  27643. int keyAgreeOID;
  27644. byte* cert;
  27645. size_t certSz;
  27646. byte* privateKey;
  27647. word32 privateKeySz;
  27648. PKCS7Attrib* authAttribs;
  27649. word32 authAttribsSz;
  27650. PKCS7Attrib* unauthAttribs;
  27651. word32 unauthAttribsSz;
  27652. /* KARI / KTRI specific */
  27653. byte* optionalUkm;
  27654. word32 optionalUkmSz;
  27655. int ktriOptions; /* KTRI options flags */
  27656. int kariOptions; /* KARI options flags */
  27657. /* KEKRI specific */
  27658. byte* secretKey; /* key, only for kekri RecipientInfo types */
  27659. word32 secretKeySz; /* size of secretKey, bytes */
  27660. byte* secretKeyId; /* key identifier */
  27661. word32 secretKeyIdSz; /* size of key identifier, bytes */
  27662. void* timePtr; /* time_t pointer */
  27663. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  27664. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  27665. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  27666. word32 otherAttrSz; /* size of otherAttr, bytes */
  27667. int kekriOptions; /* KEKRI options flags */
  27668. /* PWRI specific */
  27669. char* password; /* password */
  27670. word32 passwordSz; /* password size, bytes */
  27671. byte* salt; /* KDF salt */
  27672. word32 saltSz; /* KDF salt size, bytes */
  27673. int kdfOID; /* KDF OID */
  27674. int hashOID; /* KDF hash algorithm OID */
  27675. int kdfIterations; /* KDF iterations */
  27676. int kekEncryptOID; /* KEK encryption algorithm OID */
  27677. int pwriOptions; /* PWRI options flags */
  27678. /* ORI specific */
  27679. int isOri;
  27680. int oriOptions; /* ORI options flags */
  27681. const char* outFileName;
  27682. } pkcs7AuthEnvelopedVector;
  27683. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  27684. byte* rsaPrivKey, word32 rsaPrivKeySz,
  27685. byte* eccCert, word32 eccCertSz,
  27686. byte* eccPrivKey, word32 eccPrivKeySz)
  27687. {
  27688. int ret = 0, testSz = 0, i;
  27689. int envelopedSz, decodedSz;
  27690. byte *enveloped = NULL;
  27691. byte *decoded = NULL;
  27692. WC_RNG rng;
  27693. PKCS7* pkcs7;
  27694. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  27695. XFILE pkcs7File;
  27696. #endif
  27697. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  27698. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  27699. 0x72,0x6c,0x64
  27700. };
  27701. byte senderNonce[PKCS7_NONCE_SZ + 2];
  27702. #ifdef HAVE_ECC
  27703. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  27704. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  27705. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  27706. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  27707. 0x09, 0x05 };
  27708. PKCS7Attrib attribs[] =
  27709. {
  27710. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  27711. sizeof(senderNonce) }
  27712. };
  27713. #endif
  27714. #endif
  27715. #endif
  27716. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  27717. defined(WOLFSSL_SHA512)
  27718. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  27719. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  27720. };
  27721. #endif /* NO_AES */
  27722. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  27723. /* encryption key for kekri recipient types */
  27724. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  27725. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  27726. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  27727. };
  27728. /* encryption key identifier */
  27729. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  27730. 0x02,0x02,0x03,0x04
  27731. };
  27732. #endif
  27733. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  27734. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  27735. #ifndef HAVE_FIPS
  27736. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  27737. #else
  27738. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  27739. #endif
  27740. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  27741. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  27742. };
  27743. #endif
  27744. pkcs7AuthEnvelopedVector *testVectors = NULL;
  27745. {
  27746. #define ADD_PKCS7_TEST_VEC(...) { \
  27747. const pkcs7AuthEnvelopedVector vec = __VA_ARGS__; \
  27748. testVectors = (pkcs7AuthEnvelopedVector *) \
  27749. XREALLOC(testVectors, \
  27750. sizeof *testVectors * (testSz + 1), \
  27751. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); \
  27752. if (testVectors == NULL) \
  27753. ERROR_OUT(-12233, out); \
  27754. XMEMCPY(&testVectors[testSz++], &vec, sizeof *testVectors); \
  27755. }
  27756. /* key transport key encryption technique */
  27757. #ifndef NO_RSA
  27758. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  27759. #ifdef WOLFSSL_AES_128
  27760. ADD_PKCS7_TEST_VEC(
  27761. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  27762. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  27763. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  27764. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der"});
  27765. #endif
  27766. #ifdef WOLFSSL_AES_192
  27767. ADD_PKCS7_TEST_VEC(
  27768. {data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  27769. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  27770. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  27771. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der"});
  27772. #endif
  27773. #ifdef WOLFSSL_AES_256
  27774. ADD_PKCS7_TEST_VEC(
  27775. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  27776. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  27777. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  27778. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der"});
  27779. /* test with contentType set to FirmwarePkgData */
  27780. ADD_PKCS7_TEST_VEC(
  27781. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  27782. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  27783. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  27784. 0, 0, 0, 0, 0, 0, 0, 0,
  27785. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der"});
  27786. /* explicitly using SKID for SubjectKeyIdentifier */
  27787. ADD_PKCS7_TEST_VEC(
  27788. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  27789. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  27790. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  27791. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der"});
  27792. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  27793. ADD_PKCS7_TEST_VEC(
  27794. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  27795. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  27796. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  27797. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  27798. "pkcs7authEnvelopedDataAES256GCM_IANDS.der"});
  27799. #endif
  27800. #endif /* NO_AES */
  27801. #endif
  27802. /* key agreement key encryption technique*/
  27803. #ifdef HAVE_ECC
  27804. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  27805. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  27806. ADD_PKCS7_TEST_VEC(
  27807. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  27808. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27809. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  27810. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  27811. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der"});
  27812. #endif
  27813. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  27814. ADD_PKCS7_TEST_VEC(
  27815. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  27816. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27817. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  27818. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  27819. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der"});
  27820. /* with authenticated attributes */
  27821. ADD_PKCS7_TEST_VEC(
  27822. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  27823. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27824. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  27825. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  27826. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  27827. 0, 0, 0,
  27828. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der"});
  27829. /* with unauthenticated attributes */
  27830. ADD_PKCS7_TEST_VEC(
  27831. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  27832. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27833. eccPrivKeySz, NULL, 0, attribs,
  27834. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  27835. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  27836. 0, 0, 0,
  27837. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der"});
  27838. /* with authenticated AND unauthenticated attributes */
  27839. ADD_PKCS7_TEST_VEC(
  27840. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  27841. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27842. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  27843. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  27844. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  27845. 0, 0, 0, 0, 0, 0,
  27846. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der"});
  27847. /* with authenticated AND unauthenticated attributes AND
  27848. * contentType of FirmwarePkgData */
  27849. ADD_PKCS7_TEST_VEC(
  27850. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  27851. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27852. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  27853. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  27854. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  27855. 0, 0, 0, 0, 0, 0,
  27856. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der"});
  27857. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  27858. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  27859. ADD_PKCS7_TEST_VEC(
  27860. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  27861. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27862. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  27863. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  27864. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der"});
  27865. /* with optional user keying material (ukm) */
  27866. ADD_PKCS7_TEST_VEC(
  27867. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  27868. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  27869. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  27870. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  27871. 0, 0, 0, 0, 0, 0,
  27872. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der"});
  27873. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  27874. #endif /* NO_AES */
  27875. #endif
  27876. /* kekri (KEKRecipientInfo) recipient types */
  27877. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  27878. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  27879. ADD_PKCS7_TEST_VEC(
  27880. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  27881. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  27882. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  27883. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  27884. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der"});
  27885. #endif
  27886. #endif
  27887. /* pwri (PasswordRecipientInfo) recipient types */
  27888. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  27889. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  27890. ADD_PKCS7_TEST_VEC(
  27891. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  27892. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  27893. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  27894. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  27895. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der"});
  27896. #endif
  27897. #endif
  27898. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  27899. #ifdef WOLFSSL_AES_128
  27900. /* ori (OtherRecipientInfo) recipient types */
  27901. ADD_PKCS7_TEST_VEC(
  27902. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  27903. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  27904. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  27905. "pkcs7authEnvelopedDataAES128GCM_ORI.der"});
  27906. #endif
  27907. #endif
  27908. }
  27909. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27910. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27911. if ((! enveloped) || (! decoded)) {
  27912. ERROR_OUT(-12210, out);
  27913. }
  27914. /* generate senderNonce */
  27915. {
  27916. #ifndef HAVE_FIPS
  27917. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27918. #else
  27919. ret = wc_InitRng(&rng);
  27920. #endif
  27921. if (ret != 0) {
  27922. ERROR_OUT(-12211, out);
  27923. }
  27924. senderNonce[0] = 0x04;
  27925. senderNonce[1] = PKCS7_NONCE_SZ;
  27926. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  27927. if (ret != 0) {
  27928. wc_FreeRng(&rng);
  27929. ERROR_OUT(-12212, out);
  27930. }
  27931. }
  27932. for (i = 0; i < testSz; i++) {
  27933. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  27934. #ifdef WOLFSSL_ASYNC_CRYPT
  27935. INVALID_DEVID /* async PKCS7 is not supported */
  27936. #else
  27937. devId
  27938. #endif
  27939. );
  27940. if (pkcs7 == NULL) {
  27941. ERROR_OUT(-12213, out);
  27942. }
  27943. if (testVectors[i].secretKey != NULL) {
  27944. /* KEKRI recipient type */
  27945. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  27946. if (ret != 0) {
  27947. ERROR_OUT(-12214, out);
  27948. }
  27949. pkcs7->content = (byte*)testVectors[i].content;
  27950. pkcs7->contentSz = testVectors[i].contentSz;
  27951. pkcs7->contentOID = testVectors[i].contentOID;
  27952. pkcs7->encryptOID = testVectors[i].encryptOID;
  27953. pkcs7->ukm = testVectors[i].optionalUkm;
  27954. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  27955. pkcs7->authAttribs = testVectors[i].authAttribs;
  27956. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  27957. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  27958. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  27959. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  27960. testVectors[i].secretKey, testVectors[i].secretKeySz,
  27961. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  27962. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  27963. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  27964. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  27965. if (ret < 0) {
  27966. wc_PKCS7_Free(pkcs7);
  27967. ERROR_OUT(-12215, out);
  27968. }
  27969. /* set key, for decryption */
  27970. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  27971. testVectors[i].secretKeySz);
  27972. if (ret != 0) {
  27973. wc_PKCS7_Free(pkcs7);
  27974. ERROR_OUT(-12216, out);
  27975. }
  27976. } else if (testVectors[i].password != NULL) {
  27977. #ifndef NO_PWDBASED
  27978. /* PWRI recipient type */
  27979. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  27980. if (ret != 0) {
  27981. ERROR_OUT(-12217, out);
  27982. }
  27983. pkcs7->content = (byte*)testVectors[i].content;
  27984. pkcs7->contentSz = testVectors[i].contentSz;
  27985. pkcs7->contentOID = testVectors[i].contentOID;
  27986. pkcs7->encryptOID = testVectors[i].encryptOID;
  27987. pkcs7->ukm = testVectors[i].optionalUkm;
  27988. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  27989. pkcs7->authAttribs = testVectors[i].authAttribs;
  27990. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  27991. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  27992. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  27993. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  27994. (byte*)testVectors[i].password,
  27995. testVectors[i].passwordSz, testVectors[i].salt,
  27996. testVectors[i].saltSz, testVectors[i].kdfOID,
  27997. testVectors[i].hashOID, testVectors[i].kdfIterations,
  27998. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  27999. if (ret < 0) {
  28000. wc_PKCS7_Free(pkcs7);
  28001. ERROR_OUT(-12218, out);
  28002. }
  28003. /* set password, for decryption */
  28004. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  28005. testVectors[i].passwordSz);
  28006. if (ret < 0) {
  28007. wc_PKCS7_Free(pkcs7);
  28008. ERROR_OUT(-12219, out);
  28009. }
  28010. #endif /* NO_PWDBASED */
  28011. } else if (testVectors[i].isOri == 1) {
  28012. /* ORI recipient type */
  28013. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  28014. if (ret != 0) {
  28015. ERROR_OUT(-12220, out);
  28016. }
  28017. pkcs7->content = (byte*)testVectors[i].content;
  28018. pkcs7->contentSz = testVectors[i].contentSz;
  28019. pkcs7->contentOID = testVectors[i].contentOID;
  28020. pkcs7->encryptOID = testVectors[i].encryptOID;
  28021. pkcs7->authAttribs = testVectors[i].authAttribs;
  28022. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  28023. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  28024. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  28025. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  28026. testVectors[i].oriOptions);
  28027. if (ret < 0) {
  28028. wc_PKCS7_Free(pkcs7);
  28029. ERROR_OUT(-12221, out);
  28030. }
  28031. /* set decrypt callback for decryption */
  28032. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  28033. if (ret < 0) {
  28034. wc_PKCS7_Free(pkcs7);
  28035. ERROR_OUT(-12222, out);
  28036. }
  28037. } else {
  28038. /* KTRI or KARI recipient types */
  28039. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  28040. (word32)testVectors[i].certSz);
  28041. if (ret != 0) {
  28042. wc_PKCS7_Free(pkcs7);
  28043. ERROR_OUT(-12223, out);
  28044. }
  28045. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  28046. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  28047. pkcs7->privateKey = testVectors[i].privateKey;
  28048. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  28049. pkcs7->content = (byte*)testVectors[i].content;
  28050. pkcs7->contentSz = testVectors[i].contentSz;
  28051. pkcs7->contentOID = testVectors[i].contentOID;
  28052. pkcs7->encryptOID = testVectors[i].encryptOID;
  28053. pkcs7->ukm = testVectors[i].optionalUkm;
  28054. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  28055. pkcs7->authAttribs = testVectors[i].authAttribs;
  28056. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  28057. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  28058. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  28059. /* set SubjectIdentifier type for KTRI types */
  28060. if (testVectors[i].ktriOptions & CMS_SKID) {
  28061. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  28062. if (ret != 0) {
  28063. wc_PKCS7_Free(pkcs7);
  28064. ERROR_OUT(-12224, out);
  28065. }
  28066. } else if (testVectors[i].ktriOptions &
  28067. CMS_ISSUER_AND_SERIAL_NUMBER) {
  28068. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  28069. CMS_ISSUER_AND_SERIAL_NUMBER);
  28070. if (ret != 0) {
  28071. wc_PKCS7_Free(pkcs7);
  28072. ERROR_OUT(-12225, out);
  28073. }
  28074. }
  28075. }
  28076. #ifdef ECC_TIMING_RESISTANT
  28077. pkcs7->rng = &rng;
  28078. #endif
  28079. /* encode envelopedData */
  28080. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  28081. PKCS7_BUF_SIZE);
  28082. if (envelopedSz <= 0) {
  28083. wc_PKCS7_Free(pkcs7);
  28084. ERROR_OUT(-12226, out);
  28085. }
  28086. #ifndef NO_PKCS7_STREAM
  28087. { /* test reading byte by byte */
  28088. int z;
  28089. for (z = 0; z < envelopedSz; z++) {
  28090. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  28091. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  28092. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  28093. printf("unexpected error %d\n", decodedSz);
  28094. ERROR_OUT(-12227, out);
  28095. }
  28096. }
  28097. /* test decode result */
  28098. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  28099. printf("stream read compare failed\n");
  28100. wc_PKCS7_Free(pkcs7);
  28101. ERROR_OUT(-12228, out);
  28102. }
  28103. }
  28104. #endif
  28105. /* decode envelopedData */
  28106. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  28107. envelopedSz, decoded,
  28108. PKCS7_BUF_SIZE);
  28109. if (decodedSz <= 0) {
  28110. wc_PKCS7_Free(pkcs7);
  28111. ERROR_OUT(-12229, out);
  28112. }
  28113. /* test decode result */
  28114. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  28115. wc_PKCS7_Free(pkcs7);
  28116. ERROR_OUT(-12230, out);
  28117. }
  28118. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  28119. /* output pkcs7 envelopedData for external testing */
  28120. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  28121. if (!pkcs7File) {
  28122. wc_PKCS7_Free(pkcs7);
  28123. ERROR_OUT(-12231, out);
  28124. }
  28125. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  28126. XFCLOSE(pkcs7File);
  28127. if (ret != envelopedSz) {
  28128. wc_PKCS7_Free(pkcs7);
  28129. ERROR_OUT(-12232, out);
  28130. } else {
  28131. /* reset ret to 0 for success */
  28132. ret = 0;
  28133. }
  28134. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  28135. wc_PKCS7_Free(pkcs7);
  28136. pkcs7 = NULL;
  28137. }
  28138. wc_FreeRng(&rng);
  28139. (void)eccCert;
  28140. (void)eccCertSz;
  28141. (void)eccPrivKey;
  28142. (void)eccPrivKeySz;
  28143. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  28144. (void)secretKey;
  28145. (void)secretKeyId;
  28146. #endif
  28147. #ifdef NO_RSA
  28148. (void)rsaCert;
  28149. (void)rsaCertSz;
  28150. (void)rsaPrivKey;
  28151. (void)rsaPrivKeySz;
  28152. #endif
  28153. out:
  28154. if (testVectors)
  28155. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28156. if (enveloped)
  28157. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28158. if (decoded)
  28159. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28160. return ret;
  28161. }
  28162. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  28163. {
  28164. int ret = 0;
  28165. byte* rsaCert = NULL;
  28166. byte* rsaPrivKey = NULL;
  28167. word32 rsaCertSz = 0;
  28168. word32 rsaPrivKeySz = 0;
  28169. byte* eccCert = NULL;
  28170. byte* eccPrivKey = NULL;
  28171. word32 eccCertSz = 0;
  28172. word32 eccPrivKeySz = 0;
  28173. #ifndef NO_RSA
  28174. /* read client RSA cert and key in DER format */
  28175. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28176. if (rsaCert == NULL)
  28177. return -12300;
  28178. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28179. if (rsaPrivKey == NULL) {
  28180. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28181. return -12301;
  28182. }
  28183. rsaCertSz = FOURK_BUF;
  28184. rsaPrivKeySz = FOURK_BUF;
  28185. #endif /* NO_RSA */
  28186. #ifdef HAVE_ECC
  28187. /* read client ECC cert and key in DER format */
  28188. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28189. if (eccCert == NULL) {
  28190. #ifndef NO_RSA
  28191. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28192. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28193. #endif
  28194. return -12302;
  28195. }
  28196. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28197. if (eccPrivKey == NULL) {
  28198. #ifndef NO_RSA
  28199. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28200. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28201. #endif
  28202. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28203. return -12303;
  28204. }
  28205. eccCertSz = FOURK_BUF;
  28206. eccPrivKeySz = FOURK_BUF;
  28207. #endif /* HAVE_ECC */
  28208. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  28209. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  28210. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  28211. eccPrivKey, &eccPrivKeySz);
  28212. if (ret < 0) {
  28213. #ifndef NO_RSA
  28214. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28215. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28216. #endif
  28217. #ifdef HAVE_ECC
  28218. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28219. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28220. #endif
  28221. return -12304;
  28222. }
  28223. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  28224. rsaPrivKey, (word32)rsaPrivKeySz,
  28225. eccCert, (word32)eccCertSz,
  28226. eccPrivKey, (word32)eccPrivKeySz);
  28227. #ifndef NO_RSA
  28228. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28229. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28230. #endif
  28231. #ifdef HAVE_ECC
  28232. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28233. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28234. #endif
  28235. return ret;
  28236. }
  28237. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  28238. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  28239. static const byte p7DefKey[] = {
  28240. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28241. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28242. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28243. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  28244. };
  28245. static const byte p7AltKey[] = {
  28246. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28247. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  28248. };
  28249. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  28250. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  28251. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  28252. {
  28253. int ret;
  28254. if (cek == NULL || out == NULL)
  28255. return BAD_FUNC_ARG;
  28256. /* test case sanity checks */
  28257. if (keyIdSz != 1) {
  28258. return -12310;
  28259. }
  28260. if (keyId[0] != 0x00) {
  28261. return -12311;
  28262. }
  28263. if (type != (int)PKCS7_KEKRI) {
  28264. return -12312;
  28265. }
  28266. switch (keyWrapAlgo) {
  28267. case AES256_WRAP:
  28268. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  28269. out, outSz, NULL);
  28270. if (ret <= 0)
  28271. return ret;
  28272. break;
  28273. default:
  28274. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  28275. return BAD_KEYWRAP_ALG_E;
  28276. };
  28277. (void)pkcs7;
  28278. (void)direction;
  28279. (void)orginKey; /* used with KAKRI */
  28280. (void)orginKeySz;
  28281. return ret;
  28282. }
  28283. /* returns key size on success */
  28284. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  28285. {
  28286. int ret;
  28287. word32 atrSz;
  28288. byte atr[256];
  28289. /* Additionally can look for fwWrappedFirmwareKey
  28290. * 1.2.840.113529.1.9.16.1.16 */
  28291. const unsigned char fwWrappedFirmwareKey[] = {
  28292. /* 0x06, 0x0B */
  28293. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  28294. 0x01, 0x09, 0x10, 0x02, 0x27
  28295. };
  28296. /* find keyID in fwWrappedFirmwareKey */
  28297. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  28298. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  28299. if (ret == LENGTH_ONLY_E) {
  28300. XMEMSET(atr, 0, sizeof(atr));
  28301. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  28302. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  28303. /* keyIdRaw[0] OCTET TAG */
  28304. /* keyIdRaw[1] Length */
  28305. if (ret > 0) {
  28306. PKCS7* envPkcs7;
  28307. envPkcs7 = wc_PKCS7_New(NULL, 0);
  28308. if (envPkcs7 == NULL) {
  28309. return MEMORY_E;
  28310. }
  28311. wc_PKCS7_Init(envPkcs7, NULL, 0);
  28312. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  28313. if (ret == 0) {
  28314. /* expecting FIRMWARE_PKG_DATA content */
  28315. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  28316. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  28317. key, keySz);
  28318. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  28319. /* the contentOID should have been set to the inner
  28320. * FIRMWARE_PKG_DATA content */
  28321. ret = BAD_STATE_E;
  28322. }
  28323. }
  28324. wc_PKCS7_Free(envPkcs7);
  28325. }
  28326. }
  28327. return ret;
  28328. }
  28329. /* create a KEKRI enveloped data
  28330. * return size on success */
  28331. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  28332. word32 outSz)
  28333. {
  28334. int ret;
  28335. PKCS7* pkcs7;
  28336. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  28337. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  28338. if (pkcs7 == NULL)
  28339. return -12330;
  28340. pkcs7->content = in;
  28341. pkcs7->contentSz = inSz;
  28342. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  28343. pkcs7->encryptOID = AES256CBCb;
  28344. pkcs7->ukm = NULL;
  28345. pkcs7->ukmSz = 0;
  28346. /* add recipient (KEKRI type) */
  28347. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  28348. sizeof(p7DefKey), (byte*)keyId,
  28349. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  28350. if (ret < 0) {
  28351. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  28352. wc_PKCS7_Free(pkcs7);
  28353. return -12331;
  28354. }
  28355. /* encode envelopedData, returns size */
  28356. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  28357. if (ret <= 0) {
  28358. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  28359. wc_PKCS7_Free(pkcs7);
  28360. return -12332;
  28361. }
  28362. wc_PKCS7_Free(pkcs7);
  28363. return ret;
  28364. }
  28365. /*
  28366. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  28367. * returns size of buffer output on success
  28368. */
  28369. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  28370. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  28371. byte* key, word32 keySz)
  28372. {
  28373. int ret, attribNum = 1;
  28374. PKCS7* pkcs7;
  28375. /* KEY ID
  28376. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  28377. */
  28378. const unsigned char fwDecryptKeyID[] = {
  28379. 0x06, 0x0B,
  28380. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  28381. 0x01, 0x09, 0x10, 0x02, 0x25
  28382. };
  28383. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  28384. const unsigned char fwWrappedFirmwareKey[] = {
  28385. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  28386. 0x01, 0x09, 0x10, 0x02, 0x27
  28387. };
  28388. byte keyID[] = { 0x04, 0x01, 0x00 };
  28389. byte env[256];
  28390. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  28391. PKCS7Attrib attribs[] =
  28392. {
  28393. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  28394. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  28395. };
  28396. keyID[2] = keyHint;
  28397. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  28398. if (keyHint == 0) {
  28399. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  28400. sizeof(env));
  28401. if (ret <= 0) {
  28402. return ret;
  28403. }
  28404. attribs[1].valueSz = ret;
  28405. attribNum++;
  28406. }
  28407. /* init PKCS7 */
  28408. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  28409. if (pkcs7 == NULL)
  28410. return -12340;
  28411. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  28412. if (ret != 0) {
  28413. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  28414. wc_PKCS7_Free(pkcs7);
  28415. return -12341;
  28416. }
  28417. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  28418. if (ret != 0) {
  28419. wc_PKCS7_Free(pkcs7);
  28420. return -12342;
  28421. }
  28422. /* encode Signed Encrypted FirmwarePkgData */
  28423. if (encryptKeySz == 16) {
  28424. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  28425. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  28426. (byte*)data, sizeof(data), NULL, 0,
  28427. attribs, attribNum, out, *outSz);
  28428. }
  28429. else {
  28430. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  28431. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  28432. (byte*)data, sizeof(data), NULL, 0,
  28433. attribs, attribNum, out, *outSz);
  28434. }
  28435. if (ret <= 0) {
  28436. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  28437. "ret = %d\n", ret);
  28438. wc_PKCS7_Free(pkcs7);
  28439. return -12343;
  28440. } else {
  28441. *outSz = ret;
  28442. }
  28443. wc_PKCS7_Free(pkcs7);
  28444. return ret;
  28445. }
  28446. /* test verification and decryption of PKCS7 bundle
  28447. * return 0 on success
  28448. */
  28449. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  28450. {
  28451. int ret = 0;
  28452. int usrCtx = 1; /* test value to pass as user context to callback */
  28453. PKCS7* pkcs7;
  28454. byte* sid;
  28455. word32 sidSz;
  28456. byte key[256];
  28457. word32 keySz = sizeof(key);
  28458. byte decoded[FOURK_BUF/2];
  28459. int decodedSz = FOURK_BUF/2;
  28460. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  28461. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  28462. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  28463. 0xD7, 0x85, 0x65, 0xC0
  28464. };
  28465. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  28466. if (pkcs7 == NULL) {
  28467. return MEMORY_E;
  28468. }
  28469. /* Test verify */
  28470. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  28471. if (ret != 0) {
  28472. wc_PKCS7_Free(pkcs7);
  28473. return ret;
  28474. }
  28475. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  28476. if (ret != 0) {
  28477. wc_PKCS7_Free(pkcs7);
  28478. return ret;
  28479. }
  28480. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  28481. if (ret != 0) {
  28482. wc_PKCS7_Free(pkcs7);
  28483. return ret;
  28484. }
  28485. /* Get size of SID and print it out */
  28486. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  28487. if (ret != LENGTH_ONLY_E) {
  28488. wc_PKCS7_Free(pkcs7);
  28489. return ret;
  28490. }
  28491. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28492. if (sid == NULL) {
  28493. wc_PKCS7_Free(pkcs7);
  28494. return ret;
  28495. }
  28496. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  28497. if (ret != 0) {
  28498. wc_PKCS7_Free(pkcs7);
  28499. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28500. return ret;
  28501. }
  28502. ret = XMEMCMP(sid, expectedSid, sidSz);
  28503. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28504. if (ret != 0) {
  28505. wc_PKCS7_Free(pkcs7);
  28506. return ret;
  28507. }
  28508. /* get expected fwWrappedFirmwareKey */
  28509. if (keyHint == 0) {
  28510. ret = getFirmwareKey(pkcs7, key, keySz);
  28511. if (ret < 0) {
  28512. wc_PKCS7_Free(pkcs7);
  28513. return ret;
  28514. }
  28515. pkcs7->encryptionKey = key;
  28516. pkcs7->encryptionKeySz = ret;
  28517. }
  28518. else {
  28519. decodedSz = PKCS7_BUF_SIZE;
  28520. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  28521. if (ret != 0) {
  28522. wc_PKCS7_Free(pkcs7);
  28523. return ret;
  28524. }
  28525. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  28526. if (ret != 0) {
  28527. wc_PKCS7_Free(pkcs7);
  28528. return ret;
  28529. }
  28530. }
  28531. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  28532. pkcs7->contentSz, decoded, decodedSz);
  28533. if (decodedSz < 0) {
  28534. ret = decodedSz;
  28535. wc_PKCS7_Free(pkcs7);
  28536. return ret;
  28537. }
  28538. wc_PKCS7_Free(pkcs7);
  28539. return 0;
  28540. }
  28541. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  28542. {
  28543. int ret = 0;
  28544. word32 derSz;
  28545. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28546. if (! derBuf)
  28547. ERROR_OUT(-12360, out);
  28548. /* Doing default generation and verify */
  28549. derSz = FOURK_BUF;
  28550. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  28551. certSz, key, keySz);
  28552. if (ret <= 0) {
  28553. ERROR_OUT(-12361, out);
  28554. }
  28555. ret = verifyBundle(derBuf, derSz, 0);
  28556. if (ret != 0) {
  28557. ERROR_OUT(-12362, out);
  28558. }
  28559. /* test choosing other key with keyID */
  28560. derSz = FOURK_BUF;
  28561. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  28562. cert, certSz, key, keySz);
  28563. if (ret <= 0) {
  28564. ERROR_OUT(-12363, out);
  28565. }
  28566. ret = verifyBundle(derBuf, derSz, 1);
  28567. if (ret != 0) {
  28568. ERROR_OUT(-12364, out);
  28569. }
  28570. /* test fail case with wrong keyID */
  28571. derSz = FOURK_BUF;
  28572. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  28573. cert, certSz, key, keySz);
  28574. if (ret <= 0) {
  28575. ERROR_OUT(-12365, out);
  28576. }
  28577. ret = verifyBundle(derBuf, derSz, 1);
  28578. if (ret == 0) {
  28579. ERROR_OUT(-12366, out);
  28580. }
  28581. ret = 0;
  28582. out:
  28583. if (derBuf)
  28584. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28585. return ret;
  28586. }
  28587. #endif /* !NO_AES && HAVE_AES_CBC */
  28588. #ifndef NO_PKCS7_ENCRYPTED_DATA
  28589. typedef struct {
  28590. const byte* content;
  28591. word32 contentSz;
  28592. int contentOID;
  28593. int encryptOID;
  28594. byte* encryptionKey;
  28595. word32 encryptionKeySz;
  28596. PKCS7Attrib* attribs;
  28597. word32 attribsSz;
  28598. const char* outFileName;
  28599. } pkcs7EncryptedVector;
  28600. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  28601. {
  28602. int ret = 0;
  28603. int i, testSz;
  28604. int encryptedSz, decodedSz, attribIdx;
  28605. PKCS7* pkcs7;
  28606. byte *encrypted;
  28607. byte *decoded;
  28608. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  28609. XFILE pkcs7File;
  28610. #endif
  28611. PKCS7Attrib* expectedAttrib;
  28612. PKCS7DecodedAttrib* decodedAttrib;
  28613. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  28614. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  28615. 0x72,0x6c,0x64
  28616. };
  28617. #ifndef NO_DES3
  28618. byte desKey[] = {
  28619. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  28620. };
  28621. byte des3Key[] = {
  28622. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  28623. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  28624. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  28625. };
  28626. #endif
  28627. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  28628. #ifdef WOLFSSL_AES_128
  28629. byte aes128Key[] = {
  28630. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28631. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  28632. };
  28633. #endif
  28634. #ifdef WOLFSSL_AES_192
  28635. byte aes192Key[] = {
  28636. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28637. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28638. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  28639. };
  28640. #endif
  28641. #ifdef WOLFSSL_AES_256
  28642. byte aes256Key[] = {
  28643. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28644. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28645. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  28646. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  28647. };
  28648. #endif
  28649. #ifdef WOLFSSL_AES_256
  28650. /* Attribute example from RFC 4134, Section 7.2
  28651. * OID = 1.2.5555
  28652. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  28653. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  28654. static byte genAttr[] = { 0x04, 47,
  28655. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  28656. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  28657. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  28658. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  28659. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  28660. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  28661. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  28662. static byte genAttr2[] = { 0x04, 47,
  28663. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  28664. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  28665. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  28666. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  28667. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  28668. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  28669. PKCS7Attrib attribs[] =
  28670. {
  28671. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  28672. };
  28673. PKCS7Attrib multiAttribs[] =
  28674. {
  28675. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  28676. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  28677. };
  28678. #endif
  28679. #endif /* NO_AES */
  28680. const pkcs7EncryptedVector testVectors[] =
  28681. {
  28682. #ifndef NO_DES3
  28683. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  28684. NULL, 0, "pkcs7encryptedDataDES3.der"},
  28685. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  28686. NULL, 0, "pkcs7encryptedDataDES.der"},
  28687. #endif /* NO_DES3 */
  28688. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  28689. #ifdef WOLFSSL_AES_128
  28690. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  28691. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  28692. #endif
  28693. #ifdef WOLFSSL_AES_192
  28694. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  28695. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  28696. #endif
  28697. #ifdef WOLFSSL_AES_256
  28698. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  28699. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  28700. /* test with optional unprotected attributes */
  28701. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  28702. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  28703. "pkcs7encryptedDataAES256CBC_attribs.der"},
  28704. /* test with multiple optional unprotected attributes */
  28705. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  28706. sizeof(aes256Key), multiAttribs,
  28707. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  28708. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  28709. /* test with contentType set to FirmwarePkgData */
  28710. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  28711. sizeof(aes256Key), NULL, 0,
  28712. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  28713. #endif
  28714. #endif /* !NO_AES && HAVE_AES_CBC */
  28715. };
  28716. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28717. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28718. if ((! encrypted) || (! decoded)) {
  28719. ERROR_OUT(MEMORY_E, out);
  28720. }
  28721. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  28722. for (i = 0; i < testSz; i++) {
  28723. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  28724. if (pkcs7 == NULL) {
  28725. ERROR_OUT(-12400, out);
  28726. }
  28727. pkcs7->content = (byte*)testVectors[i].content;
  28728. pkcs7->contentSz = testVectors[i].contentSz;
  28729. pkcs7->contentOID = testVectors[i].contentOID;
  28730. pkcs7->encryptOID = testVectors[i].encryptOID;
  28731. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  28732. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  28733. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  28734. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  28735. /* encode encryptedData */
  28736. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  28737. PKCS7_BUF_SIZE);
  28738. if (encryptedSz <= 0) {
  28739. wc_PKCS7_Free(pkcs7);
  28740. ERROR_OUT(-12401, out);
  28741. }
  28742. /* decode encryptedData */
  28743. #ifndef NO_PKCS7_STREAM
  28744. { /* test reading byte by byte */
  28745. int z;
  28746. for (z = 0; z < encryptedSz; z++) {
  28747. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  28748. decoded, PKCS7_BUF_SIZE);
  28749. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  28750. printf("unexpected error %d\n", decodedSz);
  28751. ERROR_OUT(-12402, out);
  28752. }
  28753. }
  28754. /* test decode result */
  28755. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  28756. printf("stream read failed\n");
  28757. wc_PKCS7_Free(pkcs7);
  28758. ERROR_OUT(-12403, out);
  28759. }
  28760. }
  28761. #endif
  28762. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  28763. decoded, PKCS7_BUF_SIZE);
  28764. if (decodedSz <= 0){
  28765. wc_PKCS7_Free(pkcs7);
  28766. ERROR_OUT(-12404, out);
  28767. }
  28768. /* test decode result */
  28769. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  28770. wc_PKCS7_Free(pkcs7);
  28771. ERROR_OUT(-12405, out);
  28772. }
  28773. /* verify decoded unprotected attributes */
  28774. if (pkcs7->decodedAttrib != NULL) {
  28775. decodedAttrib = pkcs7->decodedAttrib;
  28776. attribIdx = 1;
  28777. while (decodedAttrib != NULL) {
  28778. /* expected attribute, stored list is reversed */
  28779. expectedAttrib = &(pkcs7->unprotectedAttribs
  28780. [pkcs7->unprotectedAttribsSz - attribIdx]);
  28781. /* verify oid */
  28782. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  28783. decodedAttrib->oidSz) != 0) {
  28784. wc_PKCS7_Free(pkcs7);
  28785. ERROR_OUT(-12406, out);
  28786. }
  28787. /* verify value */
  28788. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  28789. decodedAttrib->valueSz) != 0) {
  28790. wc_PKCS7_Free(pkcs7);
  28791. ERROR_OUT(-12407, out);
  28792. }
  28793. decodedAttrib = decodedAttrib->next;
  28794. attribIdx++;
  28795. }
  28796. }
  28797. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  28798. /* output pkcs7 envelopedData for external testing */
  28799. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  28800. if (!pkcs7File) {
  28801. wc_PKCS7_Free(pkcs7);
  28802. ERROR_OUT(-12408, out);
  28803. }
  28804. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  28805. XFCLOSE(pkcs7File);
  28806. if (ret > 0)
  28807. ret = 0;
  28808. #endif
  28809. wc_PKCS7_Free(pkcs7);
  28810. }
  28811. out:
  28812. if (encrypted)
  28813. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28814. if (decoded)
  28815. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28816. return ret;
  28817. }
  28818. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  28819. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  28820. typedef struct {
  28821. const byte* content;
  28822. word32 contentSz;
  28823. int contentOID;
  28824. const char* outFileName;
  28825. } pkcs7CompressedVector;
  28826. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  28827. {
  28828. int ret = 0;
  28829. int i, testSz;
  28830. int compressedSz, decodedSz;
  28831. PKCS7* pkcs7;
  28832. #ifdef WOLFSSL_SMALL_STACK
  28833. byte *compressed;
  28834. byte *decoded;
  28835. #else
  28836. byte compressed[PKCS7_BUF_SIZE];
  28837. byte decoded[PKCS7_BUF_SIZE];
  28838. #endif
  28839. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  28840. XFILE pkcs7File;
  28841. #endif
  28842. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  28843. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  28844. 0x72,0x6c,0x64
  28845. };
  28846. const pkcs7CompressedVector testVectors[] =
  28847. {
  28848. {data, (word32)sizeof(data), DATA,
  28849. "pkcs7compressedData_data_zlib.der"},
  28850. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  28851. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  28852. };
  28853. #ifdef WOLFSSL_SMALL_STACK
  28854. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28855. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28856. if ((! compressed) || (! decoded)) {
  28857. ERROR_OUT(MEMORY_E, out);
  28858. }
  28859. #endif
  28860. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  28861. for (i = 0; i < testSz; i++) {
  28862. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  28863. if (pkcs7 == NULL) {
  28864. ERROR_OUT(-12500, out);
  28865. }
  28866. pkcs7->content = (byte*)testVectors[i].content;
  28867. pkcs7->contentSz = testVectors[i].contentSz;
  28868. pkcs7->contentOID = testVectors[i].contentOID;
  28869. /* encode compressedData */
  28870. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  28871. PKCS7_BUF_SIZE);
  28872. if (compressedSz <= 0) {
  28873. wc_PKCS7_Free(pkcs7);
  28874. ERROR_OUT(-12501, out);
  28875. }
  28876. /* decode compressedData */
  28877. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  28878. compressedSz, decoded,
  28879. PKCS7_BUF_SIZE);
  28880. if (decodedSz <= 0){
  28881. wc_PKCS7_Free(pkcs7);
  28882. ERROR_OUT(-12502, out);
  28883. }
  28884. /* test decode result */
  28885. if (XMEMCMP(decoded, testVectors[i].content,
  28886. testVectors[i].contentSz) != 0) {
  28887. wc_PKCS7_Free(pkcs7);
  28888. ERROR_OUT(-12503, out);
  28889. }
  28890. /* make sure content type is the same */
  28891. if (testVectors[i].contentOID != pkcs7->contentOID) {
  28892. ERROR_OUT(-12504, out);
  28893. }
  28894. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  28895. /* output pkcs7 compressedData for external testing */
  28896. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  28897. if (!pkcs7File) {
  28898. wc_PKCS7_Free(pkcs7);
  28899. ERROR_OUT(-12505, out);
  28900. }
  28901. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  28902. XFCLOSE(pkcs7File);
  28903. if (ret > 0)
  28904. ret = 0;
  28905. #endif
  28906. wc_PKCS7_Free(pkcs7);
  28907. }
  28908. out:
  28909. #ifdef WOLFSSL_SMALL_STACK
  28910. if (compressed)
  28911. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28912. if (decoded)
  28913. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28914. #endif
  28915. return ret;
  28916. } /* pkcs7compressed_test() */
  28917. #undef PKCS7_BUF_SIZE
  28918. #endif /* HAVE_LIBZ */
  28919. typedef struct {
  28920. const byte* content;
  28921. word32 contentSz;
  28922. int hashOID;
  28923. int signOID;
  28924. byte* privateKey;
  28925. word32 privateKeySz;
  28926. byte* cert;
  28927. size_t certSz;
  28928. byte* caCert;
  28929. size_t caCertSz;
  28930. PKCS7Attrib* signedAttribs;
  28931. word32 signedAttribsSz;
  28932. const char* outFileName;
  28933. int contentOID;
  28934. byte* contentType;
  28935. word32 contentTypeSz;
  28936. int sidType;
  28937. int encryptOID; /* for single-shot encrypt alg OID */
  28938. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  28939. byte* encryptKey; /* for single-shot, encryptedData */
  28940. word32 encryptKeySz; /* for single-shot, encryptedData */
  28941. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  28942. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  28943. word16 detachedSignature; /* generate detached signature (0:1) */
  28944. } pkcs7SignedVector;
  28945. static int pkcs7signed_run_vectors(
  28946. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  28947. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  28948. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  28949. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  28950. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  28951. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  28952. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  28953. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  28954. {
  28955. int ret, testSz, i;
  28956. int encodedSz;
  28957. byte* out;
  28958. word32 outSz;
  28959. WC_RNG rng;
  28960. PKCS7* pkcs7;
  28961. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  28962. XFILE file;
  28963. #endif
  28964. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  28965. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  28966. 0x72,0x6c,0x64
  28967. };
  28968. static byte transIdOid[] =
  28969. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  28970. 0x09, 0x07 };
  28971. static byte messageTypeOid[] =
  28972. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  28973. 0x09, 0x02 };
  28974. static byte senderNonceOid[] =
  28975. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  28976. 0x09, 0x05 };
  28977. #ifndef NO_SHA
  28978. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  28979. #else
  28980. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  28981. #endif
  28982. static byte messageType[] = { 0x13, 2, '1', '9' };
  28983. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  28984. static PKCS7Attrib attribs[] =
  28985. {
  28986. { transIdOid, sizeof(transIdOid), transId,
  28987. sizeof(transId) - 1 }, /* take off the null */
  28988. { messageTypeOid, sizeof(messageTypeOid), messageType,
  28989. sizeof(messageType) },
  28990. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  28991. sizeof(senderNonce) }
  28992. };
  28993. /* for testing custom contentType, FirmwarePkgData */
  28994. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  28995. 0x48, 0x86, 0xF7, 0x0D,
  28996. 0x01, 0x09, 0x10, 0x01, 0x10 };
  28997. const pkcs7SignedVector testVectors[] =
  28998. {
  28999. #ifndef NO_RSA
  29000. #ifndef NO_SHA
  29001. /* RSA with SHA */
  29002. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  29003. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29004. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29005. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  29006. 0, 0},
  29007. /* RSA with SHA, no signed attributes */
  29008. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  29009. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  29010. NULL, 0, NULL, 0,
  29011. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29012. NULL, 0, 0},
  29013. #endif
  29014. #ifdef WOLFSSL_SHA224
  29015. /* RSA with SHA224 */
  29016. {data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  29017. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29018. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29019. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29020. NULL, 0, 0},
  29021. #endif
  29022. #ifndef NO_SHA256
  29023. /* RSA with SHA256 */
  29024. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29025. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29026. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29027. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29028. NULL, 0, 0},
  29029. /* RSA with SHA256, detached signature */
  29030. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29031. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29032. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29033. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  29034. NULL, 0, NULL, 0, 1},
  29035. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  29036. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29037. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29038. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29039. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  29040. NULL, 0, NULL, 0, 0},
  29041. /* RSA with SHA256 and custom contentType */
  29042. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29043. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29044. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29045. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  29046. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  29047. NULL, 0, 0},
  29048. /* RSA with SHA256 and FirmwarePkgData contentType */
  29049. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29050. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29051. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29052. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  29053. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  29054. /* RSA with SHA256 using server cert and ca cert */
  29055. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  29056. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  29057. rsaCaCertBuf, rsaCaCertBufSz,
  29058. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29059. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  29060. NULL, 0, NULL, 0, 0},
  29061. #endif
  29062. #if defined(WOLFSSL_SHA384)
  29063. /* RSA with SHA384 */
  29064. {data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  29065. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29066. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29067. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29068. NULL, 0, 0},
  29069. #endif
  29070. #if defined(WOLFSSL_SHA512)
  29071. /* RSA with SHA512 */
  29072. {data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  29073. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29074. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29075. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29076. NULL, 0, 0},
  29077. #endif
  29078. #endif /* NO_RSA */
  29079. #ifdef HAVE_ECC
  29080. #ifndef NO_SHA
  29081. /* ECDSA with SHA */
  29082. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  29083. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29084. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29085. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29086. NULL, 0, 0},
  29087. /* ECDSA with SHA, no signed attributes */
  29088. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  29089. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  29090. NULL, 0, NULL, 0,
  29091. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29092. NULL, 0, 0},
  29093. #endif
  29094. #ifdef WOLFSSL_SHA224
  29095. /* ECDSA with SHA224 */
  29096. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  29097. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29098. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29099. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29100. NULL, 0, 0},
  29101. #endif
  29102. #ifndef NO_SHA256
  29103. /* ECDSA with SHA256 */
  29104. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29105. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29106. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29107. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29108. NULL, 0, 0},
  29109. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  29110. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29111. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29112. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29113. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  29114. NULL, 0, NULL, 0, 0},
  29115. /* ECDSA with SHA256 and custom contentType */
  29116. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29117. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29118. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29119. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  29120. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  29121. NULL, 0, 0},
  29122. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  29123. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29124. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29125. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29126. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  29127. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  29128. #endif
  29129. #ifdef WOLFSSL_SHA384
  29130. /* ECDSA with SHA384 */
  29131. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  29132. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29133. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29134. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29135. NULL, 0, 0},
  29136. #endif
  29137. #ifdef WOLFSSL_SHA512
  29138. /* ECDSA with SHA512 */
  29139. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  29140. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29141. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29142. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  29143. NULL, 0, 0},
  29144. #endif
  29145. #endif /* HAVE_ECC */
  29146. };
  29147. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  29148. outSz = FOURK_BUF;
  29149. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29150. if (out == NULL)
  29151. return -12510;
  29152. XMEMSET(out, 0, outSz);
  29153. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  29154. if (ret < 0) {
  29155. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29156. return -12511;
  29157. }
  29158. #ifndef HAVE_FIPS
  29159. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29160. #else
  29161. ret = wc_InitRng(&rng);
  29162. #endif
  29163. if (ret != 0) {
  29164. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29165. return -12512;
  29166. }
  29167. for (i = 0; i < testSz; i++) {
  29168. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  29169. if (pkcs7 == NULL)
  29170. return -12513;
  29171. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  29172. (word32)testVectors[i].certSz);
  29173. if (ret != 0) {
  29174. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29175. wc_PKCS7_Free(pkcs7);
  29176. return -12514;
  29177. }
  29178. /* load CA certificate, if present */
  29179. if (testVectors[i].caCert != NULL) {
  29180. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  29181. (word32)testVectors[i].caCertSz);
  29182. if (ret != 0) {
  29183. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29184. wc_PKCS7_Free(pkcs7);
  29185. return -12515;
  29186. }
  29187. }
  29188. pkcs7->rng = &rng;
  29189. pkcs7->content = (byte*)testVectors[i].content;
  29190. pkcs7->contentSz = testVectors[i].contentSz;
  29191. pkcs7->contentOID = testVectors[i].contentOID;
  29192. pkcs7->hashOID = testVectors[i].hashOID;
  29193. pkcs7->encryptOID = testVectors[i].signOID;
  29194. pkcs7->privateKey = testVectors[i].privateKey;
  29195. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  29196. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  29197. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  29198. /* optional custom contentType, default is DATA,
  29199. overrides contentOID if set */
  29200. if (testVectors[i].contentType != NULL) {
  29201. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  29202. testVectors[i].contentTypeSz);
  29203. if (ret != 0) {
  29204. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29205. wc_PKCS7_Free(pkcs7);
  29206. return -12516;
  29207. }
  29208. }
  29209. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  29210. default is IssuerAndSerialNumber */
  29211. if (testVectors[i].sidType == CMS_SKID) {
  29212. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  29213. if (ret != 0) {
  29214. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29215. wc_PKCS7_Free(pkcs7);
  29216. return -12517;
  29217. }
  29218. }
  29219. /* generate senderNonce */
  29220. {
  29221. senderNonce[0] = 0x04;
  29222. senderNonce[1] = PKCS7_NONCE_SZ;
  29223. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  29224. if (ret != 0) {
  29225. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29226. wc_PKCS7_Free(pkcs7);
  29227. return -12518;
  29228. }
  29229. }
  29230. /* generate transactionID (used with SCEP) */
  29231. {
  29232. #ifndef NO_SHA
  29233. wc_Sha sha;
  29234. byte digest[WC_SHA_DIGEST_SIZE];
  29235. #else
  29236. wc_Sha256 sha;
  29237. byte digest[WC_SHA256_DIGEST_SIZE];
  29238. #endif
  29239. int j,k;
  29240. transId[0] = 0x13;
  29241. transId[1] = sizeof(digest) * 2;
  29242. #ifndef NO_SHA
  29243. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  29244. if (ret != 0) {
  29245. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29246. wc_PKCS7_Free(pkcs7);
  29247. return -12519;
  29248. }
  29249. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  29250. wc_ShaFinal(&sha, digest);
  29251. wc_ShaFree(&sha);
  29252. #else
  29253. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  29254. if (ret != 0) {
  29255. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29256. wc_PKCS7_Free(pkcs7);
  29257. return -12520;
  29258. }
  29259. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  29260. wc_Sha256Final(&sha, digest);
  29261. wc_Sha256Free(&sha);
  29262. #endif
  29263. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  29264. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  29265. }
  29266. }
  29267. /* enable detached signature generation, if set */
  29268. if (testVectors[i].detachedSignature == 1) {
  29269. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  29270. if (ret != 0) {
  29271. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29272. wc_PKCS7_Free(pkcs7);
  29273. return -12521;
  29274. }
  29275. }
  29276. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  29277. if (encodedSz < 0) {
  29278. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29279. wc_PKCS7_Free(pkcs7);
  29280. return -12522;
  29281. }
  29282. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  29283. /* write PKCS#7 to output file for more testing */
  29284. file = XFOPEN(testVectors[i].outFileName, "wb");
  29285. if (!file) {
  29286. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29287. wc_PKCS7_Free(pkcs7);
  29288. return -12523;
  29289. }
  29290. ret = (int)XFWRITE(out, 1, encodedSz, file);
  29291. XFCLOSE(file);
  29292. if (ret != (int)encodedSz) {
  29293. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29294. wc_PKCS7_Free(pkcs7);
  29295. return -12524;
  29296. }
  29297. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  29298. wc_PKCS7_Free(pkcs7);
  29299. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  29300. if (pkcs7 == NULL)
  29301. return -12525;
  29302. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  29303. if (testVectors[i].detachedSignature == 1) {
  29304. /* set content for verifying detached signatures */
  29305. pkcs7->content = (byte*)testVectors[i].content;
  29306. pkcs7->contentSz = testVectors[i].contentSz;
  29307. }
  29308. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  29309. if (ret < 0) {
  29310. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29311. wc_PKCS7_Free(pkcs7);
  29312. return -12526;
  29313. }
  29314. /* verify contentType extracted successfully for custom content types */
  29315. if (testVectors[i].contentTypeSz > 0) {
  29316. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  29317. return -12527;
  29318. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  29319. pkcs7->contentTypeSz) != 0) {
  29320. return -12528;
  29321. }
  29322. }
  29323. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  29324. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29325. wc_PKCS7_Free(pkcs7);
  29326. return -12529;
  29327. }
  29328. {
  29329. /* check getting signed attributes */
  29330. #ifndef NO_SHA
  29331. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  29332. #else
  29333. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  29334. #endif
  29335. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  29336. int oidSz = (int)sizeof(transIdOid) - 2;
  29337. int bufSz = 0;
  29338. if (testVectors[i].signedAttribs != NULL &&
  29339. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  29340. NULL, (word32*)&bufSz) != LENGTH_ONLY_E) {
  29341. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29342. wc_PKCS7_Free(pkcs7);
  29343. return -12530;
  29344. }
  29345. if (bufSz > (int)sizeof(buf)) {
  29346. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29347. wc_PKCS7_Free(pkcs7);
  29348. return -12531;
  29349. }
  29350. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  29351. buf, (word32*)&bufSz);
  29352. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  29353. (testVectors[i].signedAttribs == NULL && bufSz > 0)) {
  29354. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29355. wc_PKCS7_Free(pkcs7);
  29356. return -12532;
  29357. }
  29358. }
  29359. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  29360. file = XFOPEN("./pkcs7cert.der", "wb");
  29361. if (!file) {
  29362. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29363. wc_PKCS7_Free(pkcs7);
  29364. return -12533;
  29365. }
  29366. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  29367. XFCLOSE(file);
  29368. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  29369. wc_PKCS7_Free(pkcs7);
  29370. }
  29371. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29372. wc_FreeRng(&rng);
  29373. if (ret > 0)
  29374. return 0;
  29375. (void)rsaClientCertBuf;
  29376. (void)rsaClientCertBufSz;
  29377. (void)rsaClientPrivKeyBuf;
  29378. (void)rsaClientPrivKeyBufSz;
  29379. (void)rsaServerCertBuf;
  29380. (void)rsaServerCertBufSz;
  29381. (void)rsaServerPrivKeyBuf;
  29382. (void)rsaServerPrivKeyBufSz;
  29383. (void)rsaCaCertBuf;
  29384. (void)rsaCaCertBufSz;
  29385. (void)rsaCaPrivKeyBuf;
  29386. (void)rsaCaPrivKeyBufSz;
  29387. (void)eccClientCertBuf;
  29388. (void)eccClientCertBufSz;
  29389. (void)eccClientPrivKeyBuf;
  29390. (void)eccClientPrivKeyBufSz;
  29391. return ret;
  29392. }
  29393. static int pkcs7signed_run_SingleShotVectors(
  29394. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  29395. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  29396. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  29397. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  29398. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  29399. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  29400. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  29401. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  29402. {
  29403. int ret, testSz, i;
  29404. int encodedSz;
  29405. byte* out;
  29406. word32 outSz;
  29407. WC_RNG rng;
  29408. PKCS7* pkcs7;
  29409. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  29410. XFILE file;
  29411. #endif
  29412. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  29413. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  29414. 0x72,0x6c,0x64
  29415. };
  29416. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  29417. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  29418. static byte aes256Key[] = {
  29419. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29420. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29421. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29422. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  29423. };
  29424. #endif
  29425. static byte messageTypeOid[] =
  29426. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  29427. 0x09, 0x02 };
  29428. static byte messageType[] = { 0x13, 2, '1', '9' };
  29429. PKCS7Attrib attribs[] =
  29430. {
  29431. { messageTypeOid, sizeof(messageTypeOid), messageType,
  29432. sizeof(messageType) },
  29433. };
  29434. const pkcs7SignedVector testVectors[] =
  29435. {
  29436. #ifndef NO_RSA
  29437. #ifndef NO_SHA256
  29438. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  29439. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29440. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29441. NULL, 0,
  29442. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  29443. 0, 0, NULL, 0, NULL, 0, 0},
  29444. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  29445. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29446. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29447. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29448. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  29449. NULL, 0, NULL, 0, 0},
  29450. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  29451. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29452. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29453. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29454. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  29455. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  29456. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  29457. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  29458. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  29459. rsaCaCertBuf, rsaCaCertBufSz,
  29460. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29461. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  29462. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  29463. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  29464. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  29465. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  29466. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29467. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29468. NULL, 0,
  29469. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  29470. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  29471. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  29472. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29473. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29474. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29475. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  29476. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  29477. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  29478. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  29479. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  29480. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  29481. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29482. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29483. NULL, 0,
  29484. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  29485. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  29486. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  29487. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29488. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29489. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29490. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  29491. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  29492. #ifndef NO_PKCS7_ENCRYPTED_DATA
  29493. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  29494. no attribs */
  29495. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29496. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29497. NULL, 0,
  29498. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  29499. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  29500. 0, 0},
  29501. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  29502. attribs */
  29503. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  29504. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  29505. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29506. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  29507. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  29508. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  29509. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  29510. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  29511. #endif /* NO_SHA256 */
  29512. #endif /* NO_RSA */
  29513. #ifdef HAVE_ECC
  29514. #ifndef NO_SHA256
  29515. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  29516. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29517. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29518. NULL, 0,
  29519. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  29520. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  29521. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  29522. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29523. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29524. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29525. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  29526. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  29527. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  29528. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29529. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29530. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29531. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  29532. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  29533. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  29534. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  29535. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  29536. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29537. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29538. NULL, 0,
  29539. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  29540. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  29541. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  29542. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29543. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29544. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29545. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  29546. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  29547. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  29548. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  29549. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  29550. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  29551. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29552. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29553. NULL, 0,
  29554. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  29555. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  29556. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  29557. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29558. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29559. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29560. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  29561. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  29562. #ifndef NO_PKCS7_ENCRYPTED_DATA
  29563. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  29564. no attribs */
  29565. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29566. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29567. NULL, 0,
  29568. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  29569. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  29570. 0, 0},
  29571. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  29572. attribs */
  29573. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  29574. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  29575. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  29576. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  29577. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  29578. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  29579. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  29580. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  29581. #endif /* NO_SHA256 */
  29582. #endif /* HAVE_ECC */
  29583. };
  29584. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  29585. outSz = FOURK_BUF;
  29586. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29587. if (out == NULL)
  29588. return -12540;
  29589. XMEMSET(out, 0, outSz);
  29590. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  29591. if (ret < 0) {
  29592. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29593. return -12541;
  29594. }
  29595. #ifndef HAVE_FIPS
  29596. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29597. #else
  29598. ret = wc_InitRng(&rng);
  29599. #endif
  29600. if (ret != 0) {
  29601. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29602. return -12542;
  29603. }
  29604. for (i = 0; i < testSz; i++) {
  29605. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  29606. if (pkcs7 == NULL)
  29607. return -12543;
  29608. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  29609. (word32)testVectors[i].certSz);
  29610. if (ret != 0) {
  29611. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29612. wc_PKCS7_Free(pkcs7);
  29613. return -12544;
  29614. }
  29615. /* load CA certificate, if present */
  29616. if (testVectors[i].caCert != NULL) {
  29617. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  29618. (word32)testVectors[i].caCertSz);
  29619. if (ret != 0) {
  29620. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29621. wc_PKCS7_Free(pkcs7);
  29622. return -12545;
  29623. }
  29624. }
  29625. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  29626. default is IssuerAndSerialNumber */
  29627. if (testVectors[i].sidType == CMS_SKID) {
  29628. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  29629. if (ret != 0) {
  29630. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29631. wc_PKCS7_Free(pkcs7);
  29632. return -12546;
  29633. }
  29634. }
  29635. if (testVectors[i].encCompFlag == 0) {
  29636. /* encode Signed FirmwarePkgData */
  29637. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  29638. testVectors[i].privateKey, testVectors[i].privateKeySz,
  29639. testVectors[i].signOID, testVectors[i].hashOID,
  29640. (byte*)testVectors[i].content, testVectors[i].contentSz,
  29641. testVectors[i].signedAttribs,
  29642. testVectors[i].signedAttribsSz, out, outSz);
  29643. if (encodedSz < 0) {
  29644. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29645. wc_PKCS7_Free(pkcs7);
  29646. return -12547;
  29647. }
  29648. #ifndef NO_PKCS7_ENCRYPTED_DATA
  29649. } else if (testVectors[i].encCompFlag == 1) {
  29650. /* encode Signed Encrypted FirmwarePkgData */
  29651. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  29652. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  29653. testVectors[i].privateKey, testVectors[i].privateKeySz,
  29654. testVectors[i].encryptOID, testVectors[i].signOID,
  29655. testVectors[i].hashOID, (byte*)testVectors[i].content,
  29656. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  29657. testVectors[i].unprotectedAttribsSz,
  29658. testVectors[i].signedAttribs,
  29659. testVectors[i].signedAttribsSz, out, outSz);
  29660. if (encodedSz <= 0) {
  29661. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29662. wc_PKCS7_Free(pkcs7);
  29663. return -12548;
  29664. }
  29665. #endif
  29666. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  29667. } else if (testVectors[i].encCompFlag == 2) {
  29668. /* encode Signed Compressed FirmwarePkgData */
  29669. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  29670. testVectors[i].privateKey, testVectors[i].privateKeySz,
  29671. testVectors[i].signOID, testVectors[i].hashOID,
  29672. (byte*)testVectors[i].content, testVectors[i].contentSz,
  29673. testVectors[i].signedAttribs,
  29674. testVectors[i].signedAttribsSz, out, outSz);
  29675. if (encodedSz <= 0) {
  29676. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29677. wc_PKCS7_Free(pkcs7);
  29678. return -12549;
  29679. }
  29680. #ifndef NO_PKCS7_ENCRYPTED_DATA
  29681. } else if (testVectors[i].encCompFlag == 3) {
  29682. /* encode Signed Encrypted Compressed FirmwarePkgData */
  29683. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  29684. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  29685. testVectors[i].privateKey, testVectors[i].privateKeySz,
  29686. testVectors[i].encryptOID, testVectors[i].signOID,
  29687. testVectors[i].hashOID, (byte*)testVectors[i].content,
  29688. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  29689. testVectors[i].unprotectedAttribsSz,
  29690. testVectors[i].signedAttribs,
  29691. testVectors[i].signedAttribsSz, out, outSz);
  29692. if (encodedSz <= 0) {
  29693. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29694. wc_PKCS7_Free(pkcs7);
  29695. return -12550;
  29696. }
  29697. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  29698. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  29699. } else {
  29700. /* unsupported SignedData single-shot combination */
  29701. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29702. wc_PKCS7_Free(pkcs7);
  29703. return -12551;
  29704. }
  29705. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  29706. /* write PKCS#7 to output file for more testing */
  29707. file = XFOPEN(testVectors[i].outFileName, "wb");
  29708. if (!file) {
  29709. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29710. wc_PKCS7_Free(pkcs7);
  29711. return -12552;
  29712. }
  29713. ret = (int)XFWRITE(out, 1, encodedSz, file);
  29714. XFCLOSE(file);
  29715. if (ret != (int)encodedSz) {
  29716. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29717. wc_PKCS7_Free(pkcs7);
  29718. return -12553;
  29719. }
  29720. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  29721. wc_PKCS7_Free(pkcs7);
  29722. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  29723. if (pkcs7 == NULL)
  29724. return -12554;
  29725. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  29726. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  29727. if (ret < 0) {
  29728. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29729. wc_PKCS7_Free(pkcs7);
  29730. return -12555;
  29731. }
  29732. #ifndef NO_PKCS7_STREAM
  29733. {
  29734. word32 z;
  29735. for (z = 0; z < outSz && ret != 0; z++) {
  29736. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  29737. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  29738. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29739. wc_PKCS7_Free(pkcs7);
  29740. printf("unexpected error %d\n", ret);
  29741. return -12556;
  29742. }
  29743. }
  29744. }
  29745. #endif
  29746. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  29747. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29748. wc_PKCS7_Free(pkcs7);
  29749. return -12557;
  29750. }
  29751. if (testVectors[i].encCompFlag == 0) {
  29752. /* verify decoded content matches expected */
  29753. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  29754. XMEMCMP(pkcs7->content, testVectors[i].content,
  29755. pkcs7->contentSz)) {
  29756. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29757. wc_PKCS7_Free(pkcs7);
  29758. return -12558;
  29759. }
  29760. }
  29761. #ifndef NO_PKCS7_ENCRYPTED_DATA
  29762. else if (testVectors[i].encCompFlag == 1) {
  29763. /* decrypt inner encryptedData */
  29764. pkcs7->encryptionKey = testVectors[i].encryptKey;
  29765. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  29766. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  29767. pkcs7->contentSz, out, outSz);
  29768. if (ret < 0) {
  29769. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29770. wc_PKCS7_Free(pkcs7);
  29771. return -12559;
  29772. }
  29773. /* compare decrypted to expected */
  29774. if (((word32)ret != testVectors[i].contentSz) ||
  29775. XMEMCMP(out, testVectors[i].content, ret)) {
  29776. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29777. wc_PKCS7_Free(pkcs7);
  29778. return -12560;
  29779. }
  29780. }
  29781. #endif
  29782. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  29783. else if (testVectors[i].encCompFlag == 2) {
  29784. /* decompress inner compressedData */
  29785. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  29786. pkcs7->contentSz, out, outSz);
  29787. if (ret < 0) {
  29788. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29789. wc_PKCS7_Free(pkcs7);
  29790. return -12561;
  29791. }
  29792. /* compare decompressed to expected */
  29793. if (((word32)ret != testVectors[i].contentSz) ||
  29794. XMEMCMP(out, testVectors[i].content, ret)) {
  29795. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29796. wc_PKCS7_Free(pkcs7);
  29797. return -12562;
  29798. }
  29799. }
  29800. #ifndef NO_PKCS7_ENCRYPTED_DATA
  29801. else if (testVectors[i].encCompFlag == 3) {
  29802. byte* encryptedTmp;
  29803. int encryptedTmpSz;
  29804. encryptedTmpSz = FOURK_BUF;
  29805. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  29806. DYNAMIC_TYPE_TMP_BUFFER);
  29807. if (encryptedTmp == NULL) {
  29808. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29809. wc_PKCS7_Free(pkcs7);
  29810. return -12563;
  29811. }
  29812. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  29813. /* decrypt inner encryptedData */
  29814. pkcs7->encryptionKey = testVectors[i].encryptKey;
  29815. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  29816. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  29817. pkcs7->contentSz, encryptedTmp,
  29818. encryptedTmpSz);
  29819. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA) {
  29820. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29821. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29822. wc_PKCS7_Free(pkcs7);
  29823. return -12564;
  29824. }
  29825. /* decompress inner compressedData */
  29826. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  29827. encryptedTmpSz, out, outSz);
  29828. if (ret < 0) {
  29829. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29830. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29831. wc_PKCS7_Free(pkcs7);
  29832. return -12565;
  29833. }
  29834. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29835. /* compare decompressed to expected */
  29836. if (((word32)ret != testVectors[i].contentSz) ||
  29837. XMEMCMP(out, testVectors[i].content, ret)) {
  29838. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29839. wc_PKCS7_Free(pkcs7);
  29840. return -12566;
  29841. }
  29842. }
  29843. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  29844. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  29845. wc_PKCS7_Free(pkcs7);
  29846. }
  29847. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29848. wc_FreeRng(&rng);
  29849. if (ret > 0)
  29850. return 0;
  29851. (void)eccClientCertBuf;
  29852. (void)eccClientCertBufSz;
  29853. (void)eccClientPrivKeyBuf;
  29854. (void)eccClientPrivKeyBufSz;
  29855. (void)rsaClientCertBuf;
  29856. (void)rsaClientCertBufSz;
  29857. (void)rsaClientPrivKeyBuf;
  29858. (void)rsaClientPrivKeyBufSz;
  29859. (void)rsaServerCertBuf;
  29860. (void)rsaServerCertBufSz;
  29861. (void)rsaServerPrivKeyBuf;
  29862. (void)rsaServerPrivKeyBufSz;
  29863. (void)rsaCaCertBuf;
  29864. (void)rsaCaCertBufSz;
  29865. (void)rsaCaPrivKeyBuf;
  29866. (void)rsaCaPrivKeyBufSz;
  29867. return ret;
  29868. }
  29869. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  29870. {
  29871. int ret = 0;
  29872. byte* rsaClientCertBuf = NULL;
  29873. byte* rsaServerCertBuf = NULL;
  29874. byte* rsaCaCertBuf = NULL;
  29875. byte* eccClientCertBuf = NULL;
  29876. byte* rsaClientPrivKeyBuf = NULL;
  29877. byte* rsaServerPrivKeyBuf = NULL;
  29878. byte* rsaCaPrivKeyBuf = NULL;
  29879. byte* eccClientPrivKeyBuf = NULL;
  29880. word32 rsaClientCertBufSz = 0;
  29881. word32 rsaServerCertBufSz = 0;
  29882. word32 rsaCaCertBufSz = 0;
  29883. word32 eccClientCertBufSz = 0;
  29884. word32 rsaClientPrivKeyBufSz = 0;
  29885. word32 rsaServerPrivKeyBufSz = 0;
  29886. word32 rsaCaPrivKeyBufSz = 0;
  29887. word32 eccClientPrivKeyBufSz = 0;
  29888. #ifndef NO_RSA
  29889. /* read client RSA cert and key in DER format */
  29890. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29891. DYNAMIC_TYPE_TMP_BUFFER);
  29892. if (rsaClientCertBuf == NULL)
  29893. ret = -12600;
  29894. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29895. DYNAMIC_TYPE_TMP_BUFFER);
  29896. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  29897. ret = -12601;
  29898. }
  29899. rsaClientCertBufSz = FOURK_BUF;
  29900. rsaClientPrivKeyBufSz = FOURK_BUF;
  29901. /* read server RSA cert and key in DER format */
  29902. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29903. DYNAMIC_TYPE_TMP_BUFFER);
  29904. if (ret == 0 && rsaServerCertBuf == NULL)
  29905. ret = -12602;
  29906. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29907. DYNAMIC_TYPE_TMP_BUFFER);
  29908. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  29909. ret = -12603;
  29910. }
  29911. rsaServerCertBufSz = FOURK_BUF;
  29912. rsaServerPrivKeyBufSz = FOURK_BUF;
  29913. /* read CA RSA cert and key in DER format, for use with server cert */
  29914. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29915. DYNAMIC_TYPE_TMP_BUFFER);
  29916. if (ret == 0 && rsaCaCertBuf == NULL)
  29917. ret = -12604;
  29918. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29919. DYNAMIC_TYPE_TMP_BUFFER);
  29920. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  29921. ret = -12605;
  29922. }
  29923. rsaCaCertBufSz = FOURK_BUF;
  29924. rsaCaPrivKeyBufSz = FOURK_BUF;
  29925. #endif /* NO_RSA */
  29926. #ifdef HAVE_ECC
  29927. /* read client ECC cert and key in DER format */
  29928. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29929. DYNAMIC_TYPE_TMP_BUFFER);
  29930. if (ret == 0 && eccClientCertBuf == NULL) {
  29931. ret = -12606;
  29932. }
  29933. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  29934. DYNAMIC_TYPE_TMP_BUFFER);
  29935. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  29936. ret = -12607;
  29937. }
  29938. eccClientCertBufSz = FOURK_BUF;
  29939. eccClientPrivKeyBufSz = FOURK_BUF;
  29940. #endif /* HAVE_ECC */
  29941. if (ret >= 0)
  29942. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  29943. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  29944. rsaServerCertBuf, &rsaServerCertBufSz,
  29945. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  29946. rsaCaCertBuf, &rsaCaCertBufSz,
  29947. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  29948. eccClientCertBuf, &eccClientCertBufSz,
  29949. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  29950. if (ret < 0) {
  29951. ret = -12608;
  29952. }
  29953. if (ret >= 0)
  29954. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  29955. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  29956. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  29957. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  29958. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  29959. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  29960. eccClientCertBuf, (word32)eccClientCertBufSz,
  29961. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  29962. if (ret >= 0)
  29963. ret = pkcs7signed_run_SingleShotVectors(
  29964. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  29965. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  29966. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  29967. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  29968. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  29969. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  29970. eccClientCertBuf, (word32)eccClientCertBufSz,
  29971. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  29972. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  29973. if (ret >= 0)
  29974. ret = pkcs7callback_test(
  29975. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  29976. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  29977. #endif
  29978. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29979. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29980. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29981. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29982. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29983. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29984. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29985. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29986. return ret;
  29987. }
  29988. #endif /* HAVE_PKCS7 */
  29989. #ifdef HAVE_VALGRIND
  29990. /* Need a static build to have access to symbols. */
  29991. /* Maximum number of bytes in a number to test. */
  29992. #define MP_MAX_TEST_BYTE_LEN 32
  29993. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  29994. {
  29995. byte d[MP_MAX_TEST_BYTE_LEN];
  29996. int ret;
  29997. (void)heap;
  29998. do {
  29999. ret = wc_RNG_GenerateBlock(rng, d, len);
  30000. if (ret != 0)
  30001. return ret;
  30002. ret = mp_read_unsigned_bin(n, d, len);
  30003. if (ret != 0)
  30004. return ret;
  30005. } while (mp_iszero(n));
  30006. return 0;
  30007. }
  30008. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  30009. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  30010. {
  30011. int i, j;
  30012. mp_digit rem;
  30013. mp_digit rem2;
  30014. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  30015. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  30016. for (i = 0; i < 10; i++) {
  30017. for (j = 1; j < 10; j++) {
  30018. if (randNum(a, j, rng, NULL) != 0)
  30019. return -12620;
  30020. if (mp_div_3(a, r, &rem) != 0)
  30021. return -12621;
  30022. if (mp_mul_d(r, 3, r) != 0)
  30023. return -12622;
  30024. if (mp_add_d(r, rem, r) != 0)
  30025. return -12623;
  30026. if (mp_cmp(r, a) != MP_EQ)
  30027. return -12624;
  30028. }
  30029. }
  30030. if (mp_div_3(a, r, &rem) != 0)
  30031. return -12625;
  30032. if (mp_div_3(a, a, NULL) != 0)
  30033. return -12626;
  30034. if (mp_cmp(r, a) != MP_EQ)
  30035. return -12627;
  30036. #endif
  30037. #if defined(WOLFSSL_SP_MATH_ALL)
  30038. if (mp_div_d(a, 10, r, &rem) != 0)
  30039. return -12628;
  30040. if (mp_div_d(a, 10, a, NULL) != 0)
  30041. return -12629;
  30042. if (mp_cmp(r, a) != MP_EQ)
  30043. return -12630;
  30044. if (mp_div_d(a, 12, r, &rem) != 0)
  30045. return -12631;
  30046. if (mp_div_d(a, 12, a, NULL) != 0)
  30047. return -12632;
  30048. if (mp_cmp(r, a) != MP_EQ)
  30049. return -12633;
  30050. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  30051. return -12634;
  30052. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  30053. return -12635;
  30054. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  30055. return -12636;
  30056. if (mp_cmp(r, a) != MP_EQ)
  30057. return -12637;
  30058. if (rem != rem2)
  30059. return -12638;
  30060. #endif
  30061. (void)a;
  30062. (void)r;
  30063. (void)rng;
  30064. (void)i;
  30065. (void)j;
  30066. (void)rem;
  30067. (void)rem2;
  30068. return 0;
  30069. }
  30070. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  30071. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  30072. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  30073. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  30074. {
  30075. int ret;
  30076. int i, j;
  30077. int size;
  30078. char str[30];
  30079. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  30080. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  30081. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  30082. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  30083. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  30084. for (i = 0; i < 10; i++) {
  30085. for (j = 2; j < 12; j++) {
  30086. if (randNum(a, j, rng, NULL) != 0)
  30087. return -12640;
  30088. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  30089. return -12641;
  30090. mp_toradix(a, str, MP_RADIX_DEC);
  30091. if ((int)XSTRLEN(str) != size - 1)
  30092. return -12642;
  30093. mp_read_radix(r, str, MP_RADIX_DEC);
  30094. if (mp_cmp(a, r) != MP_EQ)
  30095. return -12643;
  30096. }
  30097. }
  30098. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  30099. return -12644;
  30100. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  30101. return -12645;
  30102. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  30103. return -12646;
  30104. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  30105. return -12647;
  30106. if (!mp_iszero(r))
  30107. return -12648;
  30108. mp_set(r, 1);
  30109. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  30110. return -12649;
  30111. if (!mp_iszero(r))
  30112. return -12650;
  30113. mp_zero(a);
  30114. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  30115. if (ret != 0)
  30116. return -12651;
  30117. if (size != 2)
  30118. return -12652;
  30119. ret = mp_toradix(a, str, MP_RADIX_DEC);
  30120. if (ret != 0)
  30121. return -12653;
  30122. if ((int)XSTRLEN(str) != size - 1)
  30123. return -12654;
  30124. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  30125. if (ret != 0)
  30126. return -12655;
  30127. if (!mp_iszero(r))
  30128. return -12656;
  30129. return 0;
  30130. }
  30131. #endif
  30132. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  30133. defined(HAVE_ECC))
  30134. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  30135. {
  30136. int ret;
  30137. int i, j;
  30138. int size;
  30139. char str[30];
  30140. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  30141. static char longStr[2 * sizeof(a->dp) + 2];
  30142. #endif
  30143. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  30144. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  30145. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  30146. for (i = 0; i < 10; i++) {
  30147. for (j = 2; j < 12; j++) {
  30148. if (randNum(a, j, rng, NULL) != 0)
  30149. return -12660;
  30150. mp_radix_size(a, MP_RADIX_HEX, &size);
  30151. mp_toradix(a, str, MP_RADIX_HEX);
  30152. if ((int)XSTRLEN(str) != size - 1)
  30153. return -12661;
  30154. mp_read_radix(r, str, MP_RADIX_HEX);
  30155. if (mp_cmp(a, r) != MP_EQ)
  30156. return -12662;
  30157. }
  30158. }
  30159. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  30160. return -12663;
  30161. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  30162. return -12664;
  30163. mp_set(r, 1);
  30164. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  30165. return -12665;
  30166. if (!mp_iszero(r))
  30167. return -12666;
  30168. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  30169. /* Fixed MP data size - string can be too long. */
  30170. longStr[0] = '8';
  30171. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  30172. longStr[sizeof(longStr)-1] = '\0';
  30173. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  30174. return -12667;
  30175. #endif
  30176. mp_zero(a);
  30177. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  30178. if (ret != 0)
  30179. return -12668;
  30180. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  30181. if (size != 3)
  30182. #else
  30183. if (size != 2)
  30184. #endif
  30185. return -12669;
  30186. ret = mp_toradix(a, str, MP_RADIX_HEX);
  30187. if (ret != 0)
  30188. return -12670;
  30189. if ((int)XSTRLEN(str) != size - 1)
  30190. return -12671;
  30191. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  30192. if (ret != 0)
  30193. return -12672;
  30194. if (!mp_iszero(r))
  30195. return -12673;
  30196. #ifdef WOLFSSL_SP_MATH
  30197. ret = mp_toradix(a, str, 8);
  30198. if (ret != MP_VAL)
  30199. return -12674;
  30200. ret = mp_radix_size(a, 8, &size);
  30201. if (ret != MP_VAL)
  30202. return -12675;
  30203. #endif
  30204. return 0;
  30205. }
  30206. #endif
  30207. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  30208. {
  30209. int i;
  30210. if (randNum(a, 4, rng, NULL) != 0)
  30211. return -12680;
  30212. for (i = 0; i < 4; i++) {
  30213. mp_copy(r1, a);
  30214. if (mp_lshd(r1, i) != MP_OKAY)
  30215. return -12681;
  30216. mp_rshd(r1, i);
  30217. if (mp_cmp(a, r1) != MP_EQ)
  30218. return -12682;
  30219. }
  30220. #ifndef WOLFSSL_SP_MATH
  30221. for (i = 0; i < DIGIT_BIT+1; i++) {
  30222. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  30223. return -12683;
  30224. mp_rshb(r1, i);
  30225. if (mp_cmp(a, r1) != MP_EQ)
  30226. return -12684;
  30227. }
  30228. #endif
  30229. return 0;
  30230. }
  30231. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  30232. {
  30233. int i, j;
  30234. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  30235. mp_zero(a);
  30236. mp_set_bit(a, i);
  30237. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  30238. return -12690;
  30239. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  30240. mp_zero(r1);
  30241. mp_set_bit(r1, i);
  30242. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  30243. return -12691;
  30244. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  30245. return -12692;
  30246. if (mp_cmp(a, r1) != MP_EQ)
  30247. return -12693;
  30248. }
  30249. }
  30250. mp_zero(r1);
  30251. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  30252. return -12694;
  30253. if (r1->used != 1)
  30254. return -12695;
  30255. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  30256. return -12696;
  30257. if (r1->used != 0)
  30258. return -12697;
  30259. return 0;
  30260. }
  30261. static int mp_test_read_to_bin(mp_int* a)
  30262. {
  30263. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  30264. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  30265. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  30266. };
  30267. byte out[24];
  30268. int i, j, k;
  30269. const byte* p;
  30270. int ret;
  30271. for (i = 0; i < (int)sizeof(in); i++) {
  30272. p = in + sizeof(in) - i;
  30273. ret = mp_read_unsigned_bin(a, p, i);
  30274. if (ret != 0)
  30275. return -12710;
  30276. for (j = i; j < (int)sizeof(out); j++) {
  30277. XMEMSET(out, 0xff, sizeof(out));
  30278. ret = mp_to_unsigned_bin_len(a, out, j);
  30279. if (ret != 0)
  30280. return -12711;
  30281. for (k = 0; k < j - i; k++) {
  30282. if (out[k] != 0)
  30283. return -12712;
  30284. }
  30285. for (; k < j; k++) {
  30286. if (out[k] != p[k - (j - i)])
  30287. return -12713;
  30288. }
  30289. }
  30290. }
  30291. ret = mp_read_unsigned_bin(a, NULL, 0);
  30292. if (ret != 0)
  30293. return -12714;
  30294. if (!mp_iszero(a))
  30295. return -12715;
  30296. return 0;
  30297. }
  30298. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  30299. static int mp_test_set_int(mp_int* a)
  30300. {
  30301. #if SP_ULONG_BITS == 64
  30302. unsigned long n = 0xfedcba9876543210UL;
  30303. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  30304. byte out[8] = { 0 };
  30305. #elif SP_ULONG_BITS == 32
  30306. unsigned long n = 0xfedcba98UL;
  30307. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  30308. byte out[4] = { 0 };
  30309. #elif SP_ULONG_BITS == 16
  30310. unsigned long n = 0xfedc;
  30311. byte exp[2] = { 0xfe, 0xdc };
  30312. byte out[2] = { 0 };
  30313. #elif SP_ULONG_BITS == 8
  30314. unsigned long n = 0xfe;
  30315. byte exp[1] = { 0xfe };
  30316. byte out[1] = { 0 };
  30317. #endif
  30318. int ret;
  30319. ret = mp_set_int(a, n);
  30320. if (ret != 0)
  30321. return -12720;
  30322. ret = mp_unsigned_bin_size(a);
  30323. if (ret != sizeof(exp))
  30324. return -12721;
  30325. ret = mp_to_unsigned_bin(a, out);
  30326. if (ret != 0)
  30327. return -12722;
  30328. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  30329. return -12723;
  30330. return 0;
  30331. }
  30332. #endif
  30333. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  30334. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  30335. {
  30336. byte buffer[16];
  30337. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  30338. char hexStr[] = "abcdef0123456789";
  30339. #ifndef WOLFSSL_SP_INT_NEGATIVE
  30340. char negStr[] = "-1234";
  30341. #endif
  30342. #endif
  30343. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  30344. defined(HAVE_COMP_KEY)
  30345. char decStr[] = "0987654321";
  30346. #endif
  30347. int ret;
  30348. #ifdef WOLFSSL_SP_MATH_ALL
  30349. mp_digit rho;
  30350. int size;
  30351. #endif
  30352. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  30353. int result;
  30354. #endif
  30355. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  30356. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  30357. mp_digit rd;
  30358. #endif
  30359. (void)rng;
  30360. (void)r;
  30361. ret = mp_init(NULL);
  30362. if (ret != MP_VAL)
  30363. return -12730;
  30364. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  30365. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  30366. if (ret != MP_OKAY)
  30367. return -12731;
  30368. #endif
  30369. mp_free(NULL);
  30370. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  30371. ret = mp_grow(NULL, 1);
  30372. if (ret != MP_VAL)
  30373. return -12732;
  30374. #ifdef WOLFSSL_SP_MATH
  30375. ret = mp_grow(a, SP_INT_DIGITS + 1);
  30376. if (ret != MP_MEM)
  30377. return -12733;
  30378. #endif
  30379. #endif
  30380. mp_clear(NULL);
  30381. ret = mp_abs(NULL, NULL);
  30382. if (ret != MP_VAL)
  30383. return -12734;
  30384. ret = mp_abs(a, NULL);
  30385. if (ret != MP_VAL)
  30386. return -12735;
  30387. ret = mp_abs(NULL, b);
  30388. if (ret != MP_VAL)
  30389. return -12736;
  30390. ret = mp_unsigned_bin_size(NULL);
  30391. if (ret != 0)
  30392. return -12737;
  30393. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  30394. if (ret != MP_VAL)
  30395. return -12738;
  30396. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  30397. if (ret != MP_VAL)
  30398. return -12739;
  30399. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  30400. if (ret != MP_VAL)
  30401. return -12740;
  30402. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  30403. if (ret != MP_VAL)
  30404. return -12741;
  30405. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  30406. ret = mp_read_radix(NULL, NULL, 16);
  30407. if (ret != MP_VAL)
  30408. return -12742;
  30409. ret = mp_read_radix(a, NULL, 16);
  30410. if (ret != MP_VAL)
  30411. return -12743;
  30412. ret = mp_read_radix(NULL, hexStr, 16);
  30413. if (ret != MP_VAL)
  30414. return -12744;
  30415. #ifndef WOLFSSL_SP_INT_NEGATIVE
  30416. ret = mp_read_radix(a, negStr, 16);
  30417. if (ret != MP_VAL)
  30418. return -12745;
  30419. #ifdef WOLFSSL_SP_MATH_ALL
  30420. ret = mp_read_radix(a, negStr, 10);
  30421. if (ret != MP_VAL)
  30422. return -12746;
  30423. #endif /* WOLFSSL_SP_MATH_ALL */
  30424. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  30425. #endif
  30426. #ifndef WOLFSSL_SP_MATH_ALL
  30427. /* Radix 10 only supported with ALL. */
  30428. ret = mp_read_radix(a, decStr, 10);
  30429. if (ret != MP_VAL)
  30430. return -12747;
  30431. #endif
  30432. /* Radix 8 not supported SP_INT. */
  30433. ret = mp_read_radix(a, "0123", 8);
  30434. if (ret != MP_VAL)
  30435. return -12748;
  30436. ret = mp_count_bits(NULL);
  30437. if (ret != 0)
  30438. return -12749;
  30439. ret = mp_is_bit_set(NULL, 0);
  30440. if (ret != 0)
  30441. return -12750;
  30442. ret = mp_leading_bit(NULL);
  30443. if (ret != 0)
  30444. return -12751;
  30445. mp_zero(a);
  30446. ret = mp_leading_bit(a);
  30447. if (ret != 0)
  30448. return -12752;
  30449. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  30450. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  30451. !defined(NO_RSA)
  30452. ret = mp_set_bit(NULL, 1);
  30453. if (ret != MP_VAL)
  30454. return -12753;
  30455. #endif
  30456. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  30457. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  30458. ret = mp_to_unsigned_bin(NULL, NULL);
  30459. if (ret != MP_VAL)
  30460. return -12754;
  30461. ret = mp_to_unsigned_bin(a, NULL);
  30462. if (ret != MP_VAL)
  30463. return -12755;
  30464. ret = mp_to_unsigned_bin(NULL, buffer);
  30465. if (ret != MP_VAL)
  30466. return -12756;
  30467. #endif
  30468. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  30469. if (ret != MP_VAL)
  30470. return -12757;
  30471. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  30472. if (ret != MP_VAL)
  30473. return -12758;
  30474. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  30475. if (ret != MP_VAL)
  30476. return -12759;
  30477. #ifdef WOLFSSL_SP_MATH_ALL
  30478. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  30479. if (ret != MP_VAL)
  30480. return -12760;
  30481. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  30482. if (ret != MP_VAL)
  30483. return -12761;
  30484. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  30485. if (ret != MP_VAL)
  30486. return -12762;
  30487. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  30488. if (ret != MP_OKAY)
  30489. return -12763;
  30490. #endif
  30491. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  30492. ret = mp_copy(NULL, NULL);
  30493. if (ret != MP_VAL)
  30494. return -12764;
  30495. ret = mp_copy(a, NULL);
  30496. if (ret != MP_VAL)
  30497. return -12765;
  30498. ret = mp_copy(NULL, b);
  30499. if (ret != MP_VAL)
  30500. return -12766;
  30501. #endif
  30502. #if defined(WOLFSSL_KEY_GEN)
  30503. ret = sp_2expt(NULL, 1);
  30504. if (ret != MP_VAL)
  30505. return -12767;
  30506. #endif
  30507. ret = mp_set(NULL, 0);
  30508. if (ret != MP_VAL)
  30509. return -12768;
  30510. ret = mp_cmp_d(NULL, 0);
  30511. if (ret != MP_LT)
  30512. return -12769;
  30513. ret = mp_cmp(NULL, NULL);
  30514. if (ret != MP_EQ)
  30515. return -12770;
  30516. ret = mp_cmp(a, NULL);
  30517. if (ret != MP_GT)
  30518. return -12771;
  30519. ret = mp_cmp(NULL, b);
  30520. if (ret != MP_LT)
  30521. return -12772;
  30522. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  30523. mp_rshd(NULL, 1);
  30524. #endif
  30525. mp_zero(NULL);
  30526. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  30527. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  30528. ret = mp_lshd(NULL, 0);
  30529. if (ret != MP_VAL)
  30530. return -12773;
  30531. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  30532. if (ret != MP_VAL)
  30533. return -12774;
  30534. #endif
  30535. #if defined(WOLFSSL_SP_MATH_ALL)
  30536. ret = mp_div(NULL, NULL, a, b);
  30537. if (ret != MP_VAL)
  30538. return -12775;
  30539. ret = mp_div(a, NULL, a, b);
  30540. if (ret != MP_VAL)
  30541. return -12776;
  30542. ret = mp_div(NULL, b, a, b);
  30543. if (ret != MP_VAL)
  30544. return -12777;
  30545. ret = mp_div(a, b, NULL, NULL);
  30546. if (ret != MP_VAL)
  30547. return -12778;
  30548. #endif
  30549. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  30550. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  30551. ret = mp_mod(NULL, NULL, NULL);
  30552. if (ret != MP_VAL)
  30553. return -12779;
  30554. ret = mp_mod(a, NULL, NULL);
  30555. if (ret != MP_VAL)
  30556. return -12780;
  30557. ret = mp_mod(NULL, b, NULL);
  30558. if (ret != MP_VAL)
  30559. return -12781;
  30560. ret = mp_mod(NULL, NULL, r);
  30561. if (ret != MP_VAL)
  30562. return -12782;
  30563. ret = mp_mod(a, b, NULL);
  30564. if (ret != MP_VAL)
  30565. return -12783;
  30566. ret = mp_mod(a, NULL, r);
  30567. if (ret != MP_VAL)
  30568. return -12784;
  30569. ret = mp_mod(NULL, b, r);
  30570. if (ret != MP_VAL)
  30571. return -12785;
  30572. #endif
  30573. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  30574. ret = mp_set_int(NULL, 0);
  30575. if (ret != MP_VAL)
  30576. return -12786;
  30577. #endif
  30578. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  30579. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  30580. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  30581. if (ret != MP_VAL)
  30582. return 9950;
  30583. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  30584. if (ret != MP_VAL)
  30585. return 9951;
  30586. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  30587. if (ret != MP_VAL)
  30588. return 9952;
  30589. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  30590. if (ret != MP_VAL)
  30591. return 9953;
  30592. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  30593. if (ret != MP_VAL)
  30594. return 9954;
  30595. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  30596. if (ret != MP_VAL)
  30597. return 9955;
  30598. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  30599. if (ret != MP_VAL)
  30600. return 9956;
  30601. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  30602. if (ret != MP_VAL)
  30603. return 9957;
  30604. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  30605. if (ret != MP_VAL)
  30606. return 9958;
  30607. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  30608. if (ret != MP_VAL)
  30609. return 9960;
  30610. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  30611. if (ret != MP_VAL)
  30612. return 9961;
  30613. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  30614. if (ret != MP_VAL)
  30615. return 9962;
  30616. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  30617. if (ret != MP_VAL)
  30618. return 9963;
  30619. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  30620. if (ret != MP_VAL)
  30621. return 9964;
  30622. ret = mp_exptmod_nct(a, a, a, NULL);
  30623. if (ret != MP_VAL)
  30624. return 9965;
  30625. ret = mp_exptmod_nct(a, a, NULL, a);
  30626. if (ret != MP_VAL)
  30627. return 9966;
  30628. ret = mp_exptmod_nct(a, NULL, a, a);
  30629. if (ret != MP_VAL)
  30630. return 9967;
  30631. ret = mp_exptmod_nct(NULL, a, a, a);
  30632. if (ret != MP_VAL)
  30633. return 9968;
  30634. #endif
  30635. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  30636. !defined(WC_NO_RNG)
  30637. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  30638. if (ret != MP_VAL)
  30639. return -12787;
  30640. ret = mp_rand_prime(a, 32, NULL, NULL);
  30641. if (ret != MP_VAL)
  30642. return -12788;
  30643. ret = mp_rand_prime(NULL, 32, rng, NULL);
  30644. if (ret != MP_VAL)
  30645. return -12789;
  30646. ret = mp_rand_prime(a, 0, rng, NULL);
  30647. if (ret != MP_VAL)
  30648. return -9969;
  30649. #endif
  30650. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  30651. ret = mp_mul(NULL, NULL, NULL);
  30652. if (ret != MP_VAL)
  30653. return -12790;
  30654. ret = mp_mul(a, NULL, NULL);
  30655. if (ret != MP_VAL)
  30656. return -12791;
  30657. ret = mp_mul(NULL, b, NULL);
  30658. if (ret != MP_VAL)
  30659. return -12792;
  30660. ret = mp_mul(NULL, NULL, r);
  30661. if (ret != MP_VAL)
  30662. return -12793;
  30663. ret = mp_mul(a, b, NULL);
  30664. if (ret != MP_VAL)
  30665. return -12794;
  30666. ret = mp_mul(a, NULL, r);
  30667. if (ret != MP_VAL)
  30668. return -12795;
  30669. ret = mp_mul(NULL, b, r);
  30670. if (ret != MP_VAL)
  30671. return -12796;
  30672. #endif
  30673. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  30674. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  30675. ret = mp_sqr(NULL, NULL);
  30676. if (ret != MP_VAL)
  30677. return -12797;
  30678. ret = mp_sqr(a, NULL);
  30679. if (ret != MP_VAL)
  30680. return -12798;
  30681. ret = mp_sqr(NULL, r);
  30682. if (ret != MP_VAL)
  30683. return -12799;
  30684. #endif
  30685. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  30686. ret = mp_sqrmod(NULL, NULL, NULL);
  30687. if (ret != MP_VAL)
  30688. return -12800;
  30689. ret = mp_sqrmod(a, NULL, NULL);
  30690. if (ret != MP_VAL)
  30691. return -12801;
  30692. ret = mp_sqrmod(NULL, a, NULL);
  30693. if (ret != MP_VAL)
  30694. return -12802;
  30695. ret = mp_sqrmod(NULL, NULL, a);
  30696. if (ret != MP_VAL)
  30697. return -12803;
  30698. ret = mp_sqrmod(a, b, NULL);
  30699. if (ret != MP_VAL)
  30700. return -12804;
  30701. ret = mp_sqrmod(a, NULL, b);
  30702. if (ret != MP_VAL)
  30703. return -12805;
  30704. ret = mp_sqrmod(NULL, a, b);
  30705. if (ret != MP_VAL)
  30706. return -12806;
  30707. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  30708. if (ret != MP_VAL)
  30709. return -12807;
  30710. ret = mp_mulmod(a, NULL, NULL, NULL);
  30711. if (ret != MP_VAL)
  30712. return -12808;
  30713. ret = mp_mulmod(NULL, a, NULL, NULL);
  30714. if (ret != MP_VAL)
  30715. return -12809;
  30716. ret = mp_mulmod(NULL, NULL, a, NULL);
  30717. if (ret != MP_VAL)
  30718. return -12810;
  30719. ret = mp_mulmod(NULL, NULL, NULL, a);
  30720. if (ret != MP_VAL)
  30721. return -12811;
  30722. ret = mp_mulmod(a, b, b, NULL);
  30723. if (ret != MP_VAL)
  30724. return -12812;
  30725. ret = mp_mulmod(a, b, NULL, a);
  30726. if (ret != MP_VAL)
  30727. return -12813;
  30728. ret = mp_mulmod(a, NULL, b, a);
  30729. if (ret != MP_VAL)
  30730. return -12814;
  30731. ret = mp_mulmod(NULL, b, b, a);
  30732. if (ret != MP_VAL)
  30733. return -12815;
  30734. #endif
  30735. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  30736. !defined(NO_RSA) || !defined(NO_DSA)
  30737. ret = mp_add_d(NULL, 1, NULL);
  30738. if (ret != MP_VAL)
  30739. return -12816;
  30740. ret = mp_add_d(a, 1, NULL);
  30741. if (ret != MP_VAL)
  30742. return -12817;
  30743. ret = mp_add_d(NULL, 1, b);
  30744. if (ret != MP_VAL)
  30745. return -12818;
  30746. #endif
  30747. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  30748. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  30749. ret = mp_sub_d(NULL, 1, NULL);
  30750. if (ret != MP_VAL)
  30751. return -12819;
  30752. ret = mp_sub_d(a, 1, NULL);
  30753. if (ret != MP_VAL)
  30754. return -12820;
  30755. ret = mp_sub_d(NULL, 1, b);
  30756. if (ret != MP_VAL)
  30757. return -12821;
  30758. #endif
  30759. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  30760. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  30761. ret = mp_div_d(NULL, 0, NULL, NULL);
  30762. if (ret != MP_VAL)
  30763. return -12822;
  30764. ret = mp_div_d(a, 0, NULL, NULL);
  30765. if (ret != MP_VAL)
  30766. return -12823;
  30767. ret = mp_div_d(NULL, 1, NULL, NULL);
  30768. if (ret != MP_VAL)
  30769. return -12824;
  30770. #endif
  30771. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  30772. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  30773. ret = mp_mod_d(NULL, 0, NULL);
  30774. if (ret != MP_VAL)
  30775. return -12825;
  30776. ret = mp_mod_d(a, 0, NULL);
  30777. if (ret != MP_VAL)
  30778. return -12826;
  30779. ret = mp_mod_d(NULL, 0, &rd);
  30780. if (ret != MP_VAL)
  30781. return -12827;
  30782. #endif
  30783. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  30784. ret = mp_gcd(NULL, NULL, NULL);
  30785. if (ret != MP_VAL)
  30786. return -12828;
  30787. ret = mp_gcd(a, NULL, NULL);
  30788. if (ret != MP_VAL)
  30789. return -12829;
  30790. ret = mp_gcd(NULL, a, NULL);
  30791. if (ret != MP_VAL)
  30792. return -12830;
  30793. ret = mp_gcd(NULL, NULL, a);
  30794. if (ret != MP_VAL)
  30795. return -12831;
  30796. ret = mp_gcd(a, b, NULL);
  30797. if (ret != MP_VAL)
  30798. return -12832;
  30799. ret = mp_gcd(a, NULL, b);
  30800. if (ret != MP_VAL)
  30801. return -12833;
  30802. ret = mp_gcd(NULL, a, b);
  30803. if (ret != MP_VAL)
  30804. return -12834;
  30805. #endif
  30806. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  30807. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  30808. if (ret != MP_VAL)
  30809. return -12835;
  30810. ret = mp_div_2_mod_ct(a, NULL, NULL);
  30811. if (ret != MP_VAL)
  30812. return -12836;
  30813. ret = mp_div_2_mod_ct(NULL, b, NULL);
  30814. if (ret != MP_VAL)
  30815. return -12837;
  30816. ret = mp_div_2_mod_ct(NULL, NULL, a);
  30817. if (ret != MP_VAL)
  30818. return -12838;
  30819. ret = mp_div_2_mod_ct(a, b, NULL);
  30820. if (ret != MP_VAL)
  30821. return -12839;
  30822. ret = mp_div_2_mod_ct(a, b, NULL);
  30823. if (ret != MP_VAL)
  30824. return -12840;
  30825. ret = mp_div_2_mod_ct(NULL, b, a);
  30826. if (ret != MP_VAL)
  30827. return -12841;
  30828. ret = mp_div_2(NULL, NULL);
  30829. if (ret != MP_VAL)
  30830. return -12842;
  30831. ret = mp_div_2(a, NULL);
  30832. if (ret != MP_VAL)
  30833. return -12843;
  30834. ret = mp_div_2(NULL, a);
  30835. if (ret != MP_VAL)
  30836. return -12844;
  30837. #endif
  30838. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  30839. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  30840. ret = mp_invmod(NULL, NULL, NULL);
  30841. if (ret != MP_VAL)
  30842. return -12845;
  30843. ret = mp_invmod(a, NULL, NULL);
  30844. if (ret != MP_VAL)
  30845. return -12846;
  30846. ret = mp_invmod(NULL, b, NULL);
  30847. if (ret != MP_VAL)
  30848. return -12847;
  30849. ret = mp_invmod(NULL, NULL, a);
  30850. if (ret != MP_VAL)
  30851. return -12848;
  30852. ret = mp_invmod(a, b, NULL);
  30853. if (ret != MP_VAL)
  30854. return -12849;
  30855. ret = mp_invmod(a, NULL, a);
  30856. if (ret != MP_VAL)
  30857. return -12850;
  30858. ret = mp_invmod(NULL, b, a);
  30859. if (ret != MP_VAL)
  30860. return -12851;
  30861. #endif
  30862. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  30863. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  30864. if (ret != MP_VAL)
  30865. return -12852;
  30866. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  30867. if (ret != MP_VAL)
  30868. return -12853;
  30869. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  30870. if (ret != MP_VAL)
  30871. return -12854;
  30872. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  30873. if (ret != MP_VAL)
  30874. return -12855;
  30875. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  30876. if (ret != MP_VAL)
  30877. return -12856;
  30878. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  30879. if (ret != MP_VAL)
  30880. return -12857;
  30881. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  30882. if (ret != MP_VAL)
  30883. return -12858;
  30884. #endif
  30885. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  30886. ret = mp_lcm(NULL, NULL, NULL);
  30887. if (ret != MP_VAL)
  30888. return -12859;
  30889. ret = mp_lcm(a, NULL, NULL);
  30890. if (ret != MP_VAL)
  30891. return -12860;
  30892. ret = mp_lcm(NULL, b, NULL);
  30893. if (ret != MP_VAL)
  30894. return -12861;
  30895. ret = mp_lcm(NULL, NULL, a);
  30896. if (ret != MP_VAL)
  30897. return -12862;
  30898. ret = mp_lcm(a, b, NULL);
  30899. if (ret != MP_VAL)
  30900. return -12863;
  30901. ret = mp_lcm(a, NULL, a);
  30902. if (ret != MP_VAL)
  30903. return -12864;
  30904. ret = mp_lcm(NULL, b, a);
  30905. if (ret != MP_VAL)
  30906. return -12865;
  30907. #endif
  30908. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  30909. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  30910. if (ret != MP_VAL)
  30911. return -12866;
  30912. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  30913. if (ret != MP_VAL)
  30914. return -12867;
  30915. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  30916. if (ret != MP_VAL)
  30917. return -12868;
  30918. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  30919. if (ret != MP_VAL)
  30920. return -12869;
  30921. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  30922. if (ret != MP_VAL)
  30923. return -12870;
  30924. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  30925. if (ret != MP_VAL)
  30926. return -12871;
  30927. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  30928. if (ret != MP_VAL)
  30929. return -12872;
  30930. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  30931. if (ret != MP_VAL)
  30932. return -12873;
  30933. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  30934. if (ret != MP_VAL)
  30935. return -12874;
  30936. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  30937. if (ret != MP_VAL)
  30938. return -12875;
  30939. ret = mp_exptmod(a, NULL, NULL, NULL);
  30940. if (ret != MP_VAL)
  30941. return -12876;
  30942. ret = mp_exptmod(NULL, b, NULL, NULL);
  30943. if (ret != MP_VAL)
  30944. return -12877;
  30945. ret = mp_exptmod(NULL, NULL, b, NULL);
  30946. if (ret != MP_VAL)
  30947. return -12878;
  30948. ret = mp_exptmod(NULL, NULL, NULL, a);
  30949. if (ret != MP_VAL)
  30950. return -12879;
  30951. ret = mp_exptmod(a, b, b, NULL);
  30952. if (ret != MP_VAL)
  30953. return -12880;
  30954. ret = mp_exptmod(a, b, NULL, a);
  30955. if (ret != MP_VAL)
  30956. return -12881;
  30957. ret = mp_exptmod(a, NULL, b, a);
  30958. if (ret != MP_VAL)
  30959. return -12882;
  30960. ret = mp_exptmod(NULL, b, b, a);
  30961. if (ret != MP_VAL)
  30962. return -12883;
  30963. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  30964. if (ret != MP_VAL)
  30965. return -12884;
  30966. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  30967. if (ret != MP_VAL)
  30968. return -12885;
  30969. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  30970. if (ret != MP_VAL)
  30971. return -12886;
  30972. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  30973. if (ret != MP_VAL)
  30974. return -12887;
  30975. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  30976. if (ret != MP_VAL)
  30977. return -12888;
  30978. ret = mp_exptmod_nct(a, b, b, NULL);
  30979. if (ret != MP_VAL)
  30980. return -12889;
  30981. ret = mp_exptmod_nct(a, b, NULL, a);
  30982. if (ret != MP_VAL)
  30983. return -12890;
  30984. ret = mp_exptmod_nct(a, NULL, b, a);
  30985. if (ret != MP_VAL)
  30986. return -12891;
  30987. ret = mp_exptmod_nct(NULL, b, b, a);
  30988. if (ret != MP_VAL)
  30989. return -12892;
  30990. #endif
  30991. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  30992. ret = mp_cnt_lsb(NULL);
  30993. if (ret != 0)
  30994. return -12893;
  30995. #endif
  30996. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  30997. ret = mp_prime_is_prime(NULL, 1, NULL);
  30998. if (ret != MP_VAL)
  30999. return -12894;
  31000. ret = mp_prime_is_prime(a, 1, NULL);
  31001. if (ret != MP_VAL)
  31002. return -12895;
  31003. ret = mp_prime_is_prime(NULL, 1, &result);
  31004. if (ret != MP_VAL)
  31005. return -12896;
  31006. ret = mp_prime_is_prime(a, 0, &result);
  31007. if (ret != MP_VAL)
  31008. return -12897;
  31009. ret = mp_prime_is_prime(a, 1024, &result);
  31010. if (ret != MP_VAL)
  31011. return -12898;
  31012. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  31013. if (ret != MP_VAL)
  31014. return -12899;
  31015. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  31016. if (ret != MP_VAL)
  31017. return -12900;
  31018. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  31019. if (ret != MP_VAL)
  31020. return -12901;
  31021. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  31022. if (ret != MP_VAL)
  31023. return -12902;
  31024. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  31025. if (ret != MP_VAL)
  31026. return -12903;
  31027. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  31028. if (ret != MP_VAL)
  31029. return -12904;
  31030. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  31031. if (ret != MP_VAL)
  31032. return -12905;
  31033. #endif
  31034. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  31035. ret = mp_exch(NULL, NULL);
  31036. if (ret != MP_VAL)
  31037. return -12906;
  31038. ret = mp_exch(a, NULL);
  31039. if (ret != MP_VAL)
  31040. return -12907;
  31041. ret = mp_exch(NULL, b);
  31042. if (ret != MP_VAL)
  31043. return -12908;
  31044. #endif
  31045. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  31046. defined(WOLFSSL_SP_MATH_ALL)
  31047. ret = mp_mul_d(NULL, 1, NULL);
  31048. if (ret != MP_VAL)
  31049. return -12909;
  31050. ret = mp_mul_d(a, 1, NULL);
  31051. if (ret != MP_VAL)
  31052. return -12910;
  31053. ret = mp_mul_d(NULL, 1, b);
  31054. if (ret != MP_VAL)
  31055. return -12911;
  31056. #endif
  31057. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  31058. ret = mp_add(NULL, NULL, NULL);
  31059. if (ret != MP_VAL)
  31060. return -12912;
  31061. ret = mp_add(a, NULL, NULL);
  31062. if (ret != MP_VAL)
  31063. return -12913;
  31064. ret = mp_add(NULL, b, NULL);
  31065. if (ret != MP_VAL)
  31066. return -12914;
  31067. ret = mp_add(NULL, NULL, r);
  31068. if (ret != MP_VAL)
  31069. return -12915;
  31070. ret = mp_add(a, b, NULL);
  31071. if (ret != MP_VAL)
  31072. return -12916;
  31073. ret = mp_add(a, NULL, r);
  31074. if (ret != MP_VAL)
  31075. return -12917;
  31076. ret = mp_add(NULL, b, r);
  31077. if (ret != MP_VAL)
  31078. return -12918;
  31079. #endif
  31080. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  31081. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  31082. ret = mp_sub(NULL, NULL, NULL);
  31083. if (ret != MP_VAL)
  31084. return -12919;
  31085. ret = mp_sub(a, NULL, NULL);
  31086. if (ret != MP_VAL)
  31087. return -12920;
  31088. ret = mp_sub(NULL, b, NULL);
  31089. if (ret != MP_VAL)
  31090. return -12921;
  31091. ret = mp_sub(NULL, NULL, r);
  31092. if (ret != MP_VAL)
  31093. return -12922;
  31094. ret = mp_sub(a, b, NULL);
  31095. if (ret != MP_VAL)
  31096. return -12923;
  31097. ret = mp_sub(a, NULL, r);
  31098. if (ret != MP_VAL)
  31099. return -12924;
  31100. ret = mp_sub(NULL, b, r);
  31101. if (ret != MP_VAL)
  31102. return -12925;
  31103. #endif
  31104. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  31105. defined(WOLFSSL_CUSTOM_CURVES))
  31106. ret = mp_addmod(NULL, NULL, NULL, NULL);
  31107. if (ret != MP_VAL)
  31108. return -12926;
  31109. ret = mp_addmod(a, NULL, NULL, NULL);
  31110. if (ret != MP_VAL)
  31111. return -12927;
  31112. ret = mp_addmod(NULL, b, NULL, NULL);
  31113. if (ret != MP_VAL)
  31114. return -12928;
  31115. ret = mp_addmod(NULL, NULL, b, NULL);
  31116. if (ret != MP_VAL)
  31117. return -12929;
  31118. ret = mp_addmod(NULL, NULL, NULL, a);
  31119. if (ret != MP_VAL)
  31120. return -12930;
  31121. ret = mp_addmod(a, b, b, NULL);
  31122. if (ret != MP_VAL)
  31123. return -12931;
  31124. ret = mp_addmod(a, b, NULL, a);
  31125. if (ret != MP_VAL)
  31126. return -12932;
  31127. ret = mp_addmod(a, NULL, b, a);
  31128. if (ret != MP_VAL)
  31129. return -12933;
  31130. ret = mp_addmod(NULL, b, b, a);
  31131. if (ret != MP_VAL)
  31132. return -12934;
  31133. #endif
  31134. #ifdef WOLFSSL_SP_MATH_ALL
  31135. ret = mp_submod(NULL, NULL, NULL, NULL);
  31136. if (ret != MP_VAL)
  31137. return -12935;
  31138. ret = mp_submod(a, NULL, NULL, NULL);
  31139. if (ret != MP_VAL)
  31140. return -12936;
  31141. ret = mp_submod(NULL, b, NULL, NULL);
  31142. if (ret != MP_VAL)
  31143. return -12937;
  31144. ret = mp_submod(NULL, NULL, b, NULL);
  31145. if (ret != MP_VAL)
  31146. return -12938;
  31147. ret = mp_submod(NULL, NULL, NULL, a);
  31148. if (ret != MP_VAL)
  31149. return -12939;
  31150. ret = mp_submod(a, b, b, NULL);
  31151. if (ret != MP_VAL)
  31152. return -12940;
  31153. ret = mp_submod(a, b, NULL, a);
  31154. if (ret != MP_VAL)
  31155. return -12941;
  31156. ret = mp_submod(a, NULL, b, a);
  31157. if (ret != MP_VAL)
  31158. return -12942;
  31159. ret = mp_submod(NULL, b, b, a);
  31160. if (ret != MP_VAL)
  31161. return -12943;
  31162. #endif
  31163. #ifdef WOLFSSL_SP_MATH_ALL
  31164. ret = mp_div_2d(NULL, 1, a, b);
  31165. if (ret != MP_VAL)
  31166. return -12944;
  31167. ret = mp_mod_2d(NULL, 1, NULL);
  31168. if (ret != MP_VAL)
  31169. return -12945;
  31170. ret = mp_mod_2d(a, 1, NULL);
  31171. if (ret != MP_VAL)
  31172. return -12946;
  31173. ret = mp_mod_2d(NULL, 1, b);
  31174. if (ret != MP_VAL)
  31175. return -12947;
  31176. ret = mp_mul_2d(NULL, 1, NULL);
  31177. if (ret != MP_VAL)
  31178. return -12948;
  31179. ret = mp_mul_2d(a, 1, NULL);
  31180. if (ret != MP_VAL)
  31181. return -12949;
  31182. ret = mp_mul_2d(NULL, 1, b);
  31183. if (ret != MP_VAL)
  31184. return -12950;
  31185. #endif
  31186. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  31187. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  31188. ret = mp_montgomery_reduce(NULL, NULL, 1);
  31189. if (ret != MP_VAL)
  31190. return -12951;
  31191. ret = mp_montgomery_reduce(a, NULL, 1);
  31192. if (ret != MP_VAL)
  31193. return -12952;
  31194. ret = mp_montgomery_reduce(NULL, b, 1);
  31195. if (ret != MP_VAL)
  31196. return -12953;
  31197. mp_zero(b);
  31198. ret = mp_montgomery_reduce(a, b, 1);
  31199. if (ret != MP_VAL)
  31200. return -12954;
  31201. #endif
  31202. #ifdef WOLFSSL_SP_MATH_ALL
  31203. ret = mp_montgomery_setup(NULL, NULL);
  31204. if (ret != MP_VAL)
  31205. return -12955;
  31206. ret = mp_montgomery_setup(a, NULL);
  31207. if (ret != MP_VAL)
  31208. return -12956;
  31209. ret = mp_montgomery_setup(NULL, &rho);
  31210. if (ret != MP_VAL)
  31211. return -12957;
  31212. ret = mp_montgomery_calc_normalization(NULL, NULL);
  31213. if (ret != MP_VAL)
  31214. return -12958;
  31215. ret = mp_montgomery_calc_normalization(a, NULL);
  31216. if (ret != MP_VAL)
  31217. return -12959;
  31218. ret = mp_montgomery_calc_normalization(NULL, b);
  31219. if (ret != MP_VAL)
  31220. return -12960;
  31221. #endif
  31222. ret = mp_unsigned_bin_size(NULL);
  31223. if (ret != 0)
  31224. return -12961;
  31225. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  31226. ret = mp_tohex(NULL, NULL);
  31227. if (ret != MP_VAL)
  31228. return -12962;
  31229. ret = mp_tohex(a, NULL);
  31230. if (ret != MP_VAL)
  31231. return -12963;
  31232. ret = mp_tohex(NULL, hexStr);
  31233. if (ret != MP_VAL)
  31234. return -12964;
  31235. #endif
  31236. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31237. ret = mp_todecimal(NULL, NULL);
  31238. if (ret != MP_VAL)
  31239. return -12965;
  31240. ret = mp_todecimal(a, NULL);
  31241. if (ret != MP_VAL)
  31242. return -12966;
  31243. ret = mp_todecimal(NULL, decStr);
  31244. if (ret != MP_VAL)
  31245. return -12967;
  31246. #endif
  31247. #ifdef WOLFSSL_SP_MATH_ALL
  31248. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  31249. if (ret != MP_VAL)
  31250. return -12968;
  31251. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  31252. if (ret != MP_VAL)
  31253. return -12969;
  31254. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  31255. if (ret != MP_VAL)
  31256. return -12970;
  31257. ret = mp_toradix(a, hexStr, 3);
  31258. if (ret != MP_VAL)
  31259. return -12971;
  31260. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  31261. if (ret != MP_VAL)
  31262. return -12972;
  31263. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  31264. if (ret != MP_VAL)
  31265. return -12973;
  31266. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  31267. if (ret != MP_VAL)
  31268. return -12974;
  31269. ret = mp_radix_size(a, 3, &size);
  31270. if (ret != MP_VAL)
  31271. return -12975;
  31272. #endif
  31273. return 0;
  31274. }
  31275. #endif
  31276. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  31277. static int mp_test_set_is_bit(mp_int* a)
  31278. {
  31279. int i, j;
  31280. mp_zero(a);
  31281. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  31282. if (mp_is_bit_set(a, i))
  31283. return -12980;
  31284. for (j = 0; j < i; j++) {
  31285. if (!mp_is_bit_set(a, j))
  31286. return -12981;
  31287. }
  31288. if (mp_set_bit(a, i) != 0)
  31289. return -12982;
  31290. if (!mp_is_bit_set(a, i))
  31291. return -12983;
  31292. }
  31293. mp_zero(a);
  31294. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  31295. if (mp_is_bit_set(a, i))
  31296. return -12984;
  31297. }
  31298. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  31299. mp_zero(a);
  31300. if (mp_set_bit(a, i) != 0)
  31301. return -12985;
  31302. for (j = 0; j < i; j++) {
  31303. if (mp_is_bit_set(a, j))
  31304. return -12986;
  31305. }
  31306. if (!mp_is_bit_set(a, i))
  31307. return -12987;
  31308. }
  31309. #ifdef WOLFSSL_KEY_GEN
  31310. for (i = 0; i < DIGIT_BIT * 2; i++) {
  31311. mp_set(a, 1);
  31312. if (mp_2expt(a, i) != 0)
  31313. return -12988;
  31314. for (j = 0; j < i; j++) {
  31315. if (mp_is_bit_set(a, j))
  31316. return -12989;
  31317. }
  31318. if (!mp_is_bit_set(a, i))
  31319. return -12990;
  31320. }
  31321. #endif
  31322. #ifdef WOLFSSL_SP_MATH
  31323. mp_zero(a);
  31324. for (j = 1; j <= 3; j++) {
  31325. i = SP_INT_MAX_BITS - j;
  31326. if (mp_is_bit_set(a, i))
  31327. return -12991;
  31328. if (mp_set_bit(a, i) != 0)
  31329. return -12992;
  31330. if (!mp_is_bit_set(a, i))
  31331. return -12993;
  31332. #ifdef WOLFSSL_KEY_GEN
  31333. if (mp_2expt(a, i) != 0)
  31334. return -12994;
  31335. if (!mp_is_bit_set(a, i))
  31336. return -12995;
  31337. #endif
  31338. }
  31339. mp_zero(a);
  31340. for (j = 0; j <= 3; j++) {
  31341. i = SP_INT_MAX_BITS + j;
  31342. if (mp_is_bit_set(a, i))
  31343. return -12996;
  31344. if (mp_set_bit(a, i) != MP_VAL)
  31345. return -12997;
  31346. #ifdef WOLFSSL_KEY_GEN
  31347. if (mp_2expt(a, i) != MP_VAL)
  31348. return -12998;
  31349. #endif
  31350. }
  31351. #endif
  31352. return 0;
  31353. }
  31354. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  31355. static int mp_test_cmp(mp_int* a, mp_int* b)
  31356. {
  31357. int ret;
  31358. mp_zero(a);
  31359. mp_zero(b);
  31360. ret = mp_cmp_d(a, 0);
  31361. if (ret != MP_EQ)
  31362. return -13000;
  31363. ret = mp_cmp_d(a, 1);
  31364. if (ret != MP_LT)
  31365. return -13001;
  31366. ret = mp_cmp(a, b);
  31367. if (ret != MP_EQ)
  31368. return -13002;
  31369. mp_set(a, 1);
  31370. ret = mp_cmp_d(a, 0);
  31371. if (ret != MP_GT)
  31372. return -13003;
  31373. ret = mp_cmp_d(a, 1);
  31374. if (ret != MP_EQ)
  31375. return -13004;
  31376. ret = mp_cmp_d(a, 2);
  31377. if (ret != MP_LT)
  31378. return -13005;
  31379. ret = mp_cmp(a, b);
  31380. if (ret != MP_GT)
  31381. return -13006;
  31382. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  31383. ret = mp_cmp_d(b, -1);
  31384. if (ret != MP_GT)
  31385. return -13007;
  31386. ret = mp_cmp(a, b);
  31387. if (ret != MP_LT)
  31388. return -13008;
  31389. ret = mp_cmp(b, a);
  31390. if (ret != MP_GT)
  31391. return -13009;
  31392. ret = mp_cmp(b, b);
  31393. if (ret != MP_EQ)
  31394. return -13010;
  31395. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  31396. defined(WOLFSSL_SP_INT_NEGATIVE)
  31397. mp_read_radix(a, "-1", MP_RADIX_HEX);
  31398. mp_read_radix(a, "1", MP_RADIX_HEX);
  31399. ret = mp_cmp(a, b);
  31400. if (ret != MP_LT)
  31401. return -13011;
  31402. ret = mp_cmp(b, a);
  31403. if (ret != MP_GT)
  31404. return -13012;
  31405. mp_read_radix(b, "-2", MP_RADIX_HEX);
  31406. ret = mp_cmp(a, b);
  31407. if (ret != MP_GT)
  31408. return -13013;
  31409. ret = mp_cmp(b, a);
  31410. if (ret != MP_LT)
  31411. return -13014;
  31412. mp_read_radix(a, "-2", MP_RADIX_HEX);
  31413. ret = mp_cmp(a, b);
  31414. if (ret != MP_EQ)
  31415. return -13015;
  31416. #endif
  31417. return 0;
  31418. }
  31419. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  31420. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  31421. {
  31422. int ret;
  31423. int i, j, k;
  31424. #ifndef WOLFSSL_SP_MATH
  31425. for (i = 0; i < 10; i++) {
  31426. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  31427. ret = randNum(a, j, rng, NULL);
  31428. if (ret != MP_OKAY)
  31429. return -13020;
  31430. mp_copy(a, b);
  31431. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  31432. ret = mp_mul_2d(a, k, a);
  31433. if (ret != MP_OKAY)
  31434. return -13021;
  31435. mp_rshb(a, k);
  31436. if (mp_cmp(a, b) != MP_EQ)
  31437. return -13022;
  31438. }
  31439. }
  31440. }
  31441. #endif
  31442. for (i = 0; i < 10; i++) {
  31443. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  31444. ret = randNum(a, j, rng, NULL);
  31445. if (ret != MP_OKAY)
  31446. return -13023;
  31447. mp_copy(a, b);
  31448. for (k = 0; k < 10; k++) {
  31449. ret = mp_lshd(a, k);
  31450. if (ret != MP_OKAY)
  31451. return -13024;
  31452. mp_rshd(a, k);
  31453. if (mp_cmp(a, b) != MP_EQ)
  31454. return -13025;
  31455. }
  31456. }
  31457. }
  31458. mp_zero(a);
  31459. mp_rshd(a, 1);
  31460. if (!mp_iszero(a))
  31461. return -13026;
  31462. mp_set(a, 1);
  31463. mp_rshd(a, 1);
  31464. if (!mp_iszero(a))
  31465. return -13027;
  31466. mp_set(a, 1);
  31467. mp_rshd(a, 2);
  31468. if (!mp_iszero(a))
  31469. return -13028;
  31470. return 0;
  31471. }
  31472. #endif
  31473. #ifndef WOLFSSL_SP_MATH
  31474. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  31475. WC_RNG* rng)
  31476. {
  31477. int ret;
  31478. int i, j, k;
  31479. mp_zero(a);
  31480. mp_zero(d);
  31481. ret = mp_div(a, d, r, rem);
  31482. if (ret != MP_VAL)
  31483. return -13030;
  31484. mp_set(d, 1);
  31485. ret = mp_div(a, d, r, rem);
  31486. if (ret != MP_OKAY)
  31487. return -13031;
  31488. if (!mp_iszero(r))
  31489. return -13032;
  31490. if (!mp_iszero(rem))
  31491. return -13033;
  31492. mp_set(a, 1);
  31493. ret = mp_div(a, d, r, rem);
  31494. if (ret != MP_OKAY)
  31495. return -13034;
  31496. if (!mp_isone(r))
  31497. return -13035;
  31498. if (!mp_iszero(rem))
  31499. return -13036;
  31500. for (i = 0; i < 100; i++) {
  31501. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  31502. ret = randNum(d, j, rng, NULL);
  31503. if (ret != MP_OKAY)
  31504. return -13037;
  31505. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  31506. ret = randNum(a, k, rng, NULL);
  31507. if (ret != MP_OKAY)
  31508. return -13038;
  31509. ret = mp_div(a, d, NULL, rem);
  31510. if (ret != MP_OKAY)
  31511. return -13039;
  31512. ret = mp_div(a, d, r, NULL);
  31513. if (ret != MP_OKAY)
  31514. return -13040;
  31515. ret = mp_div(a, d, r, rem);
  31516. if (ret != MP_OKAY)
  31517. return -13041;
  31518. mp_mul(r, d, r);
  31519. mp_add(r, rem, r);
  31520. if (mp_cmp(r, a) != MP_EQ)
  31521. return -13042;
  31522. }
  31523. }
  31524. }
  31525. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  31526. if (ret != MP_OKAY)
  31527. return -13043;
  31528. mp_add(d, d, a);
  31529. mp_set(rem, 1);
  31530. mp_div(a, d, NULL, rem);
  31531. if (ret != MP_OKAY)
  31532. return -13044;
  31533. if (!mp_iszero(rem))
  31534. return -13045;
  31535. mp_set(r, 1);
  31536. mp_div(a, d, r, NULL);
  31537. if (ret != MP_OKAY)
  31538. return -13046;
  31539. if (mp_cmp_d(r, 2) != MP_EQ)
  31540. return -13047;
  31541. mp_set(r, 1);
  31542. mp_set(rem, 1);
  31543. mp_div(a, d, r, rem);
  31544. if (ret != MP_OKAY)
  31545. return -13048;
  31546. if (mp_cmp_d(r, 2) != MP_EQ)
  31547. return -13049;
  31548. if (!mp_iszero(rem))
  31549. return -13050;
  31550. mp_set(a, 0xfe);
  31551. mp_lshd(a, 3);
  31552. mp_add_d(a, 0xff, a);
  31553. mp_set(d, 0xfe);
  31554. mp_lshd(d, 2);
  31555. ret = mp_div(a, d, r, rem);
  31556. if (ret != MP_OKAY)
  31557. return -13051;
  31558. mp_mul(r, d, d);
  31559. mp_add(rem, d, d);
  31560. if (mp_cmp(a, d) != MP_EQ)
  31561. return -13052;
  31562. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  31563. * a digit. So mp_div must detect and handle.
  31564. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  31565. */
  31566. mp_set(a, 1);
  31567. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  31568. mp_set(d, 1);
  31569. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  31570. mp_add_d(d, 1, d);
  31571. ret = mp_div(a, d, r, rem);
  31572. if (ret != MP_OKAY)
  31573. return -13053;
  31574. return 0;
  31575. }
  31576. #endif
  31577. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  31578. !defined(WC_NO_RNG)
  31579. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  31580. {
  31581. int ret;
  31582. int res;
  31583. ret = mp_rand_prime(a, 1, rng, NULL);
  31584. #if defined(WOLFSSL_SP_MATH_ALL)
  31585. if (ret != 0)
  31586. return -13060;
  31587. #else
  31588. if (ret != MP_VAL)
  31589. return -13060;
  31590. #endif
  31591. #ifndef WOLFSSL_SP_MATH
  31592. ret = mp_rand_prime(a, -5, rng, NULL);
  31593. if (ret != 0)
  31594. return -13061;
  31595. #endif
  31596. ret = mp_prime_is_prime(a, 1, &res);
  31597. if (ret != MP_OKAY)
  31598. return -13062;
  31599. #ifndef WOLFSSL_SP_MATH
  31600. if (res != MP_YES)
  31601. return -13063;
  31602. #else
  31603. if (res != MP_NO)
  31604. return -13063;
  31605. #endif
  31606. ret = mp_prime_is_prime(a, 0, &res);
  31607. if (ret != MP_VAL)
  31608. return -13064;
  31609. ret = mp_prime_is_prime(a, -1, &res);
  31610. if (ret != MP_VAL)
  31611. return -13065;
  31612. ret = mp_prime_is_prime(a, 257, &res);
  31613. if (ret != MP_VAL)
  31614. return -13066;
  31615. mp_set(a, 1);
  31616. ret = mp_prime_is_prime(a, 1, &res);
  31617. if (ret != MP_OKAY)
  31618. return -13067;
  31619. if (res != MP_NO)
  31620. return -13068;
  31621. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  31622. if (ret != MP_OKAY)
  31623. return -13069;
  31624. if (res != MP_NO)
  31625. return -13070;
  31626. mp_set(a, 2);
  31627. ret = mp_prime_is_prime(a, 1, &res);
  31628. if (ret != MP_OKAY)
  31629. return -13071;
  31630. if (res != MP_YES)
  31631. return -13072;
  31632. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  31633. if (ret != MP_OKAY)
  31634. return -13073;
  31635. if (res != MP_YES)
  31636. return -13074;
  31637. mp_set(a, 0xfb);
  31638. ret = mp_prime_is_prime(a, 1, &res);
  31639. if (ret != MP_OKAY)
  31640. return -13075;
  31641. if (res != MP_YES)
  31642. return -13076;
  31643. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  31644. if (ret != MP_OKAY)
  31645. return -13077;
  31646. if (res != MP_YES)
  31647. return -13078;
  31648. mp_set(a, 0x6);
  31649. ret = mp_prime_is_prime(a, 1, &res);
  31650. if (ret != MP_OKAY)
  31651. return -13079;
  31652. if (res != MP_NO)
  31653. return -13080;
  31654. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  31655. if (ret != MP_OKAY)
  31656. return -13081;
  31657. if (res != MP_NO)
  31658. return -13082;
  31659. mp_set_int(a, 0x655 * 0x65b);
  31660. ret = mp_prime_is_prime(a, 10, &res);
  31661. if (ret != MP_OKAY)
  31662. return -13083;
  31663. if (res != MP_NO)
  31664. return -13084;
  31665. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  31666. if (ret != MP_OKAY)
  31667. return -13085;
  31668. if (res != MP_NO)
  31669. return -13086;
  31670. return 0;
  31671. }
  31672. #endif
  31673. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  31674. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  31675. WC_RNG* rng)
  31676. {
  31677. int ret;
  31678. int i;
  31679. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  31680. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  31681. { 12, 56, 168 }, { 56, 12, 168 }
  31682. };
  31683. (void)exp;
  31684. mp_set(a, 0);
  31685. mp_set(b, 1);
  31686. ret = mp_lcm(a, a, r);
  31687. if (ret != MP_VAL)
  31688. return -13090;
  31689. ret = mp_lcm(a, b, r);
  31690. if (ret != MP_VAL)
  31691. return -13091;
  31692. ret = mp_lcm(b, a, r);
  31693. if (ret != MP_VAL)
  31694. return -13092;
  31695. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  31696. mp_set(a, kat[i][0]);
  31697. mp_set(b, kat[i][1]);
  31698. ret = mp_lcm(a, b, r);
  31699. if (ret != MP_OKAY)
  31700. return -13093;
  31701. mp_set(exp, kat[i][2]);
  31702. if (mp_cmp(r, exp) != MP_EQ)
  31703. return -13094;
  31704. }
  31705. (void)rng;
  31706. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  31707. !defined(WC_NO_RNG)
  31708. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  31709. return -13095;
  31710. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  31711. return -13096;
  31712. if (mp_mul(a, b, exp) != MP_OKAY)
  31713. return -13097;
  31714. ret = mp_lcm(a, b, r);
  31715. if (ret != MP_OKAY)
  31716. return -13098;
  31717. if (mp_cmp(r, exp) != MP_EQ)
  31718. return -13099;
  31719. ret = mp_lcm(b, a, r);
  31720. if (ret != MP_OKAY)
  31721. return -13100;
  31722. if (mp_cmp(r, exp) != MP_EQ)
  31723. return -13101;
  31724. #endif
  31725. mp_set(a, 11);
  31726. mp_zero(b);
  31727. ret = mp_gcd(a, b, r);
  31728. if (ret != MP_OKAY)
  31729. return -13102;
  31730. if (mp_cmp_d(r, 11) != MP_EQ)
  31731. return -13103;
  31732. ret = mp_gcd(b, a, r);
  31733. if (ret != MP_OKAY)
  31734. return -13104;
  31735. if (mp_cmp_d(r, 11) != MP_EQ)
  31736. return -13105;
  31737. ret = mp_gcd(b, b, r);
  31738. if (ret != MP_VAL)
  31739. return -13106;
  31740. return 0;
  31741. }
  31742. #endif
  31743. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  31744. defined(WOLFSSL_SP_MATH_ALL)
  31745. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  31746. {
  31747. int ret;
  31748. int i;
  31749. int j;
  31750. mp_set(a, 10);
  31751. ret = mp_mod_2d(a, 0, r);
  31752. if (ret != MP_OKAY)
  31753. return -13110;
  31754. if (!mp_iszero(r))
  31755. return -13111;
  31756. ret = mp_mod_2d(a, 1, r);
  31757. if (ret != MP_OKAY)
  31758. return -13112;
  31759. if (!mp_iszero(r))
  31760. return -13113;
  31761. ret = mp_mod_2d(a, 2, r);
  31762. if (ret != MP_OKAY)
  31763. return -13114;
  31764. if (mp_cmp_d(r, 2))
  31765. return -13115;
  31766. for (i = 2; i < 20; i++) {
  31767. ret = randNum(a, i, rng, NULL);
  31768. if (ret != 0)
  31769. return -13116;
  31770. for (j = 1; j <= mp_count_bits(a); j++) {
  31771. /* Get top part */
  31772. ret = mp_div_2d(a, j, t, NULL);
  31773. if (ret != 0)
  31774. return -13117;
  31775. ret = mp_mul_2d(t, j, t);
  31776. if (ret != 0)
  31777. return -13118;
  31778. /* Get bottom part */
  31779. ret = mp_mod_2d(a, j, r);
  31780. if (ret != 0)
  31781. return -13119;
  31782. /* Reassemble */
  31783. ret = mp_add(t, r, r);
  31784. if (ret != 0)
  31785. return -13120;
  31786. if (mp_cmp(a, r) != MP_EQ)
  31787. return -13121;
  31788. }
  31789. }
  31790. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  31791. /* Test negative value being moded. */
  31792. for (j = 0; j < 20; j++) {
  31793. ret = randNum(a, 2, rng, NULL);
  31794. if (ret != 0)
  31795. return -13122;
  31796. a->sign = MP_NEG;
  31797. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  31798. ret = mp_mod_2d(a, i, r);
  31799. if (ret != 0)
  31800. return -13124;
  31801. mp_zero(t);
  31802. ret = mp_set_bit(t, i);
  31803. if (ret != 0)
  31804. return -13125;
  31805. ret = mp_mod(a, t, t);
  31806. if (ret != 0)
  31807. return -13126;
  31808. if (mp_cmp(r, t) != MP_EQ)
  31809. return -13127;
  31810. }
  31811. }
  31812. #endif
  31813. return 0;
  31814. }
  31815. #endif
  31816. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  31817. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  31818. static int mp_test_mod_d(mp_int* a)
  31819. {
  31820. int ret;
  31821. mp_digit r;
  31822. if (mp_set(a, 1) != MP_OKAY)
  31823. return -13130;
  31824. ret = mp_mod_d(a, 0, &r);
  31825. if (ret != MP_VAL)
  31826. return -13131;
  31827. mp_zero(a);
  31828. ret = mp_mod_d(a, 1, &r);
  31829. if (ret != MP_OKAY)
  31830. return -13132;
  31831. ret = mp_mod_d(a, 3, &r);
  31832. if (ret != MP_OKAY)
  31833. return -13133;
  31834. ret = mp_mod_d(a, 5, &r);
  31835. if (ret != MP_OKAY)
  31836. return -13134;
  31837. return 0;
  31838. }
  31839. #endif
  31840. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  31841. WC_RNG* rng)
  31842. {
  31843. int ret;
  31844. int i;
  31845. for (i = 1; i < 16; i++) {
  31846. ret = randNum(a, i, rng, NULL);
  31847. if (ret != 0)
  31848. return -13140;
  31849. ret = mp_mul(a, a, r1);
  31850. if (ret != 0)
  31851. return -13141;
  31852. ret = mp_sqr(a, r2);
  31853. if (ret != 0)
  31854. return -13142;
  31855. if (mp_cmp(r1, r2) != MP_EQ)
  31856. return -13143;
  31857. }
  31858. ret = mp_set(b, 0);
  31859. if (ret != MP_OKAY)
  31860. return -13144;
  31861. ret = mp_mul(a, b, r1);
  31862. if (ret != MP_OKAY)
  31863. return -13145;
  31864. if (!mp_iszero(r1))
  31865. return -13146;
  31866. ret = mp_sqr(b, r1);
  31867. if (ret != MP_OKAY)
  31868. return -13147;
  31869. if (!mp_iszero(r1))
  31870. return -13148;
  31871. #ifdef WOLFSSL_SP_MATH_ALL
  31872. ret = mp_set(a, 1);
  31873. if (ret != MP_OKAY)
  31874. return -13149;
  31875. i = (SP_INT_DIGITS + 1) / 2;
  31876. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  31877. if (ret != MP_OKAY)
  31878. return -13150;
  31879. ret = mp_set(b, 1);
  31880. if (ret != MP_OKAY)
  31881. return -13151;
  31882. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  31883. if (ret != MP_OKAY)
  31884. return -13152;
  31885. ret = mp_mul(a, b, r1);
  31886. if (ret != MP_OKAY)
  31887. return -13153;
  31888. ret = mp_mul(a, a, r1);
  31889. if (ret == MP_OKAY)
  31890. return -13154;
  31891. ret = mp_sqr(a, r1);
  31892. if (ret == MP_OKAY)
  31893. return -13155;
  31894. ret = mp_sqr(b, r1);
  31895. if (ret != MP_OKAY)
  31896. return -13156;
  31897. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  31898. (defined(HAVE_ECC) && defined(FP_ECC))
  31899. ret = mp_mulmod(a, b, b, r1);
  31900. if (ret != MP_OKAY)
  31901. return -13157;
  31902. ret = mp_mulmod(a, a, b, r1);
  31903. if (ret == MP_OKAY)
  31904. return -13158;
  31905. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  31906. ret = mp_sqrmod(a, b, r1);
  31907. if (ret == MP_OKAY)
  31908. return -13159;
  31909. ret = mp_sqrmod(b, a, r1);
  31910. if (ret != MP_OKAY)
  31911. return -13160;
  31912. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  31913. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  31914. #endif /* WOLFSSL_SP_MATH_ALL */
  31915. return 0;
  31916. }
  31917. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  31918. defined(OPENSSL_EXTRA)
  31919. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  31920. {
  31921. int ret;
  31922. mp_set(a, 0);
  31923. mp_set(m, 1);
  31924. ret = mp_invmod(a, m, r);
  31925. if (ret != MP_VAL)
  31926. return -13170;
  31927. ret = mp_invmod(m, a, r);
  31928. if (ret != MP_VAL)
  31929. return -13171;
  31930. mp_set(a, 2);
  31931. mp_set(m, 4);
  31932. ret = mp_invmod(a, m, r);
  31933. if (ret != MP_VAL)
  31934. return -13172;
  31935. mp_set(a, 1);
  31936. mp_set(m, 4);
  31937. ret = mp_invmod(a, m, r);
  31938. if (ret != MP_OKAY)
  31939. return -13173;
  31940. if (!mp_isone(r))
  31941. return -13174;
  31942. mp_set(a, 3);
  31943. mp_set(m, 4);
  31944. ret = mp_invmod(a, m, r);
  31945. if (ret != MP_OKAY)
  31946. return -13175;
  31947. mp_set(a, 3);
  31948. mp_set(m, 5);
  31949. ret = mp_invmod(a, m, r);
  31950. if (ret != MP_OKAY)
  31951. return -13176;
  31952. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  31953. mp_read_radix(a, "-3", 16);
  31954. ret = mp_invmod(a, m, r);
  31955. if (ret != MP_OKAY)
  31956. return -13177;
  31957. #endif
  31958. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  31959. mp_set(a, 0);
  31960. mp_set(m, 3);
  31961. ret = mp_invmod_mont_ct(a, m, r, 1);
  31962. if (ret != MP_VAL)
  31963. return -13178;
  31964. mp_set(a, 1);
  31965. mp_set(m, 0);
  31966. ret = mp_invmod_mont_ct(a, m, r, 1);
  31967. if (ret != MP_VAL)
  31968. return -13179;
  31969. mp_set(a, 1);
  31970. mp_set(m, 1);
  31971. ret = mp_invmod_mont_ct(a, m, r, 1);
  31972. if (ret != MP_VAL)
  31973. return -13180;
  31974. mp_set(a, 1);
  31975. mp_set(m, 2);
  31976. ret = mp_invmod_mont_ct(a, m, r, 1);
  31977. if (ret != MP_VAL)
  31978. return -13181;
  31979. mp_set(a, 1);
  31980. mp_set(m, 3);
  31981. ret = mp_invmod_mont_ct(a, m, r, 1);
  31982. if (ret != MP_OKAY)
  31983. return -13182;
  31984. #endif
  31985. return 0;
  31986. }
  31987. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  31988. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  31989. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  31990. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  31991. {
  31992. int ret;
  31993. mp_set(b, 0x2);
  31994. mp_set(e, 0x3);
  31995. mp_set(m, 0x0);
  31996. ret = mp_exptmod_ex(b, e, 1, m, r);
  31997. if (ret != MP_VAL)
  31998. return -13190;
  31999. ret = mp_exptmod_nct(b, e, m, r);
  32000. if (ret != MP_VAL)
  32001. return -13191;
  32002. mp_set(b, 0x2);
  32003. mp_set(e, 0x3);
  32004. mp_set(m, 0x1);
  32005. ret = mp_exptmod_ex(b, e, 1, m, r);
  32006. if (ret != MP_OKAY)
  32007. return -13192;
  32008. if (!mp_iszero(r))
  32009. return -13193;
  32010. ret = mp_exptmod_nct(b, e, m, r);
  32011. if (ret != MP_OKAY)
  32012. return -13194;
  32013. if (!mp_iszero(r))
  32014. return -13195;
  32015. mp_set(b, 0x2);
  32016. mp_set(e, 0x0);
  32017. mp_set(m, 0x7);
  32018. ret = mp_exptmod_ex(b, e, 1, m, r);
  32019. if (ret != MP_OKAY)
  32020. return -13196;
  32021. if (!mp_isone(r))
  32022. return -13197;
  32023. ret = mp_exptmod_nct(b, e, m, r);
  32024. if (ret != MP_OKAY)
  32025. return -13198;
  32026. if (!mp_isone(r))
  32027. return -13199;
  32028. mp_set(b, 0x0);
  32029. mp_set(e, 0x3);
  32030. mp_set(m, 0x7);
  32031. ret = mp_exptmod_ex(b, e, 1, m, r);
  32032. if (ret != MP_OKAY)
  32033. return -13200;
  32034. if (!mp_iszero(r))
  32035. return -13201;
  32036. ret = mp_exptmod_nct(b, e, m, r);
  32037. if (ret != MP_OKAY)
  32038. return -13202;
  32039. if (!mp_iszero(r))
  32040. return -13203;
  32041. mp_set(b, 0x10);
  32042. mp_set(e, 0x3);
  32043. mp_set(m, 0x7);
  32044. ret = mp_exptmod_ex(b, e, 1, m, r);
  32045. if (ret != MP_OKAY)
  32046. return -13204;
  32047. ret = mp_exptmod_nct(b, e, m, r);
  32048. if (ret != MP_OKAY)
  32049. return -13205;
  32050. mp_set(b, 0x7);
  32051. mp_set(e, 0x3);
  32052. mp_set(m, 0x7);
  32053. ret = mp_exptmod_ex(b, e, 1, m, r);
  32054. if (ret != MP_OKAY)
  32055. return -13206;
  32056. if (!mp_iszero(r))
  32057. return -13207;
  32058. ret = mp_exptmod_nct(b, e, m, r);
  32059. if (ret != MP_OKAY)
  32060. return -13208;
  32061. if (!mp_iszero(r))
  32062. return -13209;
  32063. #ifndef WOLFSSL_SP_MATH
  32064. mp_set(b, 0x01);
  32065. mp_mul_2d(b, DIGIT_BIT, b);
  32066. mp_add_d(b, 1, b);
  32067. mp_set(e, 0x3);
  32068. mp_copy(b, m);
  32069. ret = mp_exptmod_ex(b, e, 1, m, r);
  32070. if (ret != MP_OKAY)
  32071. return -13210;
  32072. if (!mp_iszero(r))
  32073. return -13211;
  32074. ret = mp_exptmod_nct(b, e, m, r);
  32075. if (ret != MP_OKAY)
  32076. return -13212;
  32077. if (!mp_iszero(r))
  32078. return -13213;
  32079. #endif
  32080. mp_set(b, 0x2);
  32081. mp_set(e, 0x3);
  32082. mp_set(m, 0x7);
  32083. ret = mp_exptmod_ex(b, e, 1, m, r);
  32084. if (ret != MP_OKAY)
  32085. return -13214;
  32086. ret = mp_exptmod_nct(b, e, m, r);
  32087. if (ret != MP_OKAY)
  32088. return -13215;
  32089. #ifdef WOLFSSL_SP_MATH_ALL
  32090. mp_set(b, 0x2);
  32091. mp_set(e, 0x3);
  32092. mp_set(m, 0x01);
  32093. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  32094. mp_add_d(m, 0x01, m);
  32095. ret = mp_exptmod_ex(b, e, 1, m, r);
  32096. if (ret != MP_VAL)
  32097. return -13216;
  32098. ret = mp_exptmod_nct(b, e, m, r);
  32099. if (ret != MP_VAL)
  32100. return -13217;
  32101. #endif
  32102. return 0;
  32103. }
  32104. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  32105. * OPENSSL_EXTRA */
  32106. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  32107. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  32108. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  32109. {
  32110. int ret;
  32111. mp_digit mp;
  32112. static int exp[] = { 7, 8, 16, 27, 32, 64,
  32113. 127, 128, 255, 256,
  32114. 383, 384, 2033, 2048
  32115. };
  32116. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  32117. 0x01, 0x9f, 0x13, 0xbd,
  32118. 0x1f, 0x13d, 0x45, 0x615
  32119. };
  32120. int bits[] = { 256, 384, 2048, 3072 };
  32121. int i;
  32122. int j;
  32123. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  32124. if (exp[i] >= DIGIT_BIT)
  32125. continue;
  32126. mp_zero(m);
  32127. ret = mp_set_bit(m, exp[i]);
  32128. if (ret != MP_OKAY)
  32129. return -13220;
  32130. ret = mp_sub_d(m, sub[i], m);
  32131. if (ret != MP_OKAY)
  32132. return -13221;
  32133. ret = mp_montgomery_setup(m, &mp);
  32134. if (ret != MP_OKAY)
  32135. return -13222;
  32136. ret = mp_montgomery_calc_normalization(n, m);
  32137. if (ret != MP_OKAY)
  32138. return -13223;
  32139. for (j = 0; j < 10; j++) {
  32140. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  32141. if (ret != 0)
  32142. return -13224;
  32143. ret = mp_mod(a, m, a);
  32144. if (ret != 0)
  32145. return -13225;
  32146. /* r = a * a */
  32147. ret = mp_sqrmod(a, m, r);
  32148. if (ret != MP_OKAY)
  32149. return -13226;
  32150. /* Convert to Montgomery form = a*n */
  32151. ret = mp_mulmod(a, n, m, a);
  32152. if (ret != MP_OKAY)
  32153. return -13227;
  32154. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  32155. ret = mp_sqr(a, a);
  32156. if (ret != MP_OKAY)
  32157. return -13228;
  32158. ret = mp_montgomery_reduce(a, m, mp);
  32159. if (ret != MP_OKAY)
  32160. return -13229;
  32161. ret = mp_montgomery_reduce(a, m, mp);
  32162. if (ret != MP_OKAY)
  32163. return -13230;
  32164. if (mp_cmp(a, r) != MP_EQ)
  32165. return -13231;
  32166. }
  32167. }
  32168. /* Force carries. */
  32169. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  32170. /* a = 2^(bits*2) - 1 */
  32171. mp_zero(a);
  32172. mp_set_bit(a, bits[i] * 2);
  32173. mp_sub_d(a, 1, a);
  32174. /* m = 2^(bits) - 1 */
  32175. mp_zero(m);
  32176. mp_set_bit(m, bits[i]);
  32177. mp_sub_d(m, 1, m);
  32178. mp = 1;
  32179. /* result = r = 2^(bits) - 1 */
  32180. mp_zero(r);
  32181. mp_set_bit(r, bits[i]);
  32182. mp_sub_d(r, 1, r);
  32183. ret = mp_montgomery_reduce(a, m, mp);
  32184. if (ret != MP_OKAY)
  32185. return -13240;
  32186. /* Result is m or 0 if reduced to range of modulus. */
  32187. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  32188. return -13241;
  32189. }
  32190. return 0;
  32191. }
  32192. #endif
  32193. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  32194. {
  32195. WC_RNG rng;
  32196. int ret;
  32197. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  32198. int i, j;
  32199. #ifndef WOLFSSL_SP_MATH
  32200. int k;
  32201. #endif
  32202. mp_digit d;
  32203. #endif
  32204. mp_int a, b, r1, r2, p;
  32205. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  32206. if (ret != 0)
  32207. return -13300;
  32208. #ifdef WOLFSSL_SP_MATH_ALL
  32209. mp_init_copy(&p, &a);
  32210. #else
  32211. ret = mp_init(&p);
  32212. if (ret != 0)
  32213. return -13301;
  32214. #endif
  32215. #ifndef HAVE_FIPS
  32216. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32217. #else
  32218. ret = wc_InitRng(&rng);
  32219. #endif
  32220. if (ret != 0)
  32221. goto done;
  32222. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  32223. mp_set_int(&a, 0);
  32224. if (a.used != 0 || a.dp[0] != 0)
  32225. return -13302;
  32226. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  32227. for (i = 0; i < 4 * j; i++) {
  32228. /* New values to use. */
  32229. ret = randNum(&p, j, &rng, NULL);
  32230. if (ret != 0)
  32231. return -13303;
  32232. ret = randNum(&a, j, &rng, NULL);
  32233. if (ret != 0)
  32234. return -13304;
  32235. ret = randNum(&b, j, &rng, NULL);
  32236. if (ret != 0)
  32237. return -13305;
  32238. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  32239. if (ret != 0)
  32240. return -13306;
  32241. d &= MP_MASK;
  32242. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  32243. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  32244. /* Ensure sqrmod produce same result as mulmod. */
  32245. ret = mp_sqrmod(&a, &p, &r1);
  32246. if (ret != 0)
  32247. return -13307;
  32248. ret = mp_mulmod(&a, &a, &p, &r2);
  32249. if (ret != 0)
  32250. return -13308;
  32251. if (mp_cmp(&r1, &r2) != 0)
  32252. return -13309;
  32253. #endif
  32254. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  32255. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  32256. !defined(WOLFSSL_SP_INT_NEGATIVE))
  32257. ret = mp_addmod(&a, &b, &p, &r1);
  32258. if (ret != 0)
  32259. return -13310;
  32260. ret = mp_submod(&r1, &b, &p, &r2);
  32261. if (ret != 0)
  32262. return -13311;
  32263. ret = mp_mod(&a, &p, &r1);
  32264. if (ret != 0)
  32265. return -13312;
  32266. if (mp_cmp(&r1, &r2) != MP_EQ)
  32267. return -13313;
  32268. #else
  32269. /* Ensure add with mod produce same result as sub with mod. */
  32270. ret = mp_addmod(&a, &b, &p, &r1);
  32271. if (ret != 0)
  32272. return -13314;
  32273. b.sign ^= 1;
  32274. ret = mp_submod(&a, &b, &p, &r2);
  32275. if (ret != 0)
  32276. return -13315;
  32277. if (mp_cmp(&r1, &r2) != 0)
  32278. return -13316;
  32279. #endif
  32280. #endif
  32281. /* Ensure add digit produce same result as sub digit. */
  32282. ret = mp_add_d(&a, d, &r1);
  32283. if (ret != 0)
  32284. return -13317;
  32285. ret = mp_sub_d(&r1, d, &r2);
  32286. if (ret != 0)
  32287. return -13318;
  32288. if (mp_cmp(&a, &r2) != 0)
  32289. return -13319;
  32290. /* Invert - if p is even it will use the slow impl.
  32291. * - if p and a are even it will fail.
  32292. */
  32293. ret = mp_invmod(&a, &p, &r1);
  32294. if (ret != 0 && ret != MP_VAL)
  32295. return -13320;
  32296. ret = 0;
  32297. #ifndef WOLFSSL_SP_MATH
  32298. /* Shift up and down number all bits in a digit. */
  32299. for (k = 0; k < DIGIT_BIT; k++) {
  32300. mp_mul_2d(&a, k, &r1);
  32301. mp_div_2d(&r1, k, &r2, &p);
  32302. if (mp_cmp(&a, &r2) != 0)
  32303. return -13321;
  32304. if (!mp_iszero(&p))
  32305. return -13322;
  32306. mp_rshb(&r1, k);
  32307. if (mp_cmp(&a, &r1) != 0)
  32308. return -13323;
  32309. }
  32310. #endif
  32311. }
  32312. }
  32313. #if DIGIT_BIT >= 32
  32314. /* Check that setting a 32-bit digit works. */
  32315. d &= 0xffffffffU;
  32316. mp_set_int(&a, d);
  32317. if (a.used != 1 || a.dp[0] != d)
  32318. return -13324;
  32319. #endif
  32320. /* Check setting a bit and testing a bit works. */
  32321. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  32322. mp_zero(&a);
  32323. mp_set_bit(&a, i);
  32324. if (!mp_is_bit_set(&a, i))
  32325. return -13325;
  32326. }
  32327. #endif
  32328. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  32329. mp_zero(&a);
  32330. i = mp_cnt_lsb(&a);
  32331. if (i != 0)
  32332. return -13326;
  32333. mp_set(&a, 1);
  32334. i = mp_cnt_lsb(&a);
  32335. if (i != 0)
  32336. return -13327;
  32337. #endif
  32338. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  32339. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  32340. return ret;
  32341. #endif
  32342. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  32343. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  32344. return ret;
  32345. #endif
  32346. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  32347. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  32348. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  32349. return ret;
  32350. #endif
  32351. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  32352. defined(HAVE_ECC))
  32353. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  32354. return ret;
  32355. #endif
  32356. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  32357. return ret;
  32358. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  32359. return ret;
  32360. if ((ret = mp_test_read_to_bin(&a)) != 0)
  32361. return ret;
  32362. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  32363. if ((ret = mp_test_set_int(&a)) != 0)
  32364. return ret;
  32365. #endif
  32366. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  32367. return ret;
  32368. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  32369. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  32370. return ret;
  32371. #endif
  32372. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  32373. if ((ret = mp_test_set_is_bit(&a)) != 0)
  32374. return ret;
  32375. #endif
  32376. #ifdef WOLFSSL_SP_MATH_ALL
  32377. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  32378. return ret;
  32379. #endif
  32380. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  32381. !defined(WC_NO_RNG)
  32382. if ((ret = mp_test_prime(&a, &rng)) != 0)
  32383. return ret;
  32384. #endif
  32385. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  32386. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  32387. return ret;
  32388. #endif
  32389. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  32390. defined(WOLFSSL_SP_MATH_ALL)
  32391. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  32392. return ret;
  32393. #endif
  32394. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  32395. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  32396. if ((ret = mp_test_mod_d(&a)) != 0)
  32397. return ret;
  32398. #endif
  32399. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  32400. return ret;
  32401. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  32402. defined(OPENSSL_EXTRA)
  32403. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  32404. return ret;
  32405. #endif
  32406. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  32407. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  32408. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  32409. return ret;
  32410. #endif
  32411. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  32412. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  32413. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  32414. return ret;
  32415. #endif
  32416. done:
  32417. mp_clear(&p);
  32418. mp_clear(&r2);
  32419. mp_clear(&r1);
  32420. mp_clear(&b);
  32421. mp_clear(&a);
  32422. wc_FreeRng(&rng);
  32423. return ret;
  32424. }
  32425. #endif
  32426. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  32427. typedef struct pairs_t {
  32428. const unsigned char* coeff;
  32429. int coeffSz;
  32430. int exp;
  32431. } pairs_t;
  32432. /*
  32433. n =p1p2p3, where pi = ki(p1−1)+1 with (k2,k3) = (173,293)
  32434. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  32435. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  32436. */
  32437. static const unsigned char c192a[] =
  32438. {
  32439. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  32440. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  32441. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  32442. };
  32443. static const unsigned char c0a[] =
  32444. {
  32445. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  32446. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  32447. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  32448. };
  32449. static const pairs_t ecPairsA[] =
  32450. {
  32451. {c192a, sizeof(c192a), 192},
  32452. {c0a, sizeof(c0a), 0}
  32453. };
  32454. static const int kA[] = {173, 293};
  32455. static const unsigned char controlPrime[] = {
  32456. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  32457. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  32458. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  32459. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  32460. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  32461. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  32462. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  32463. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  32464. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  32465. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  32466. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  32467. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  32468. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  32469. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  32470. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  32471. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  32472. };
  32473. static const unsigned char testOne[] = { 1 };
  32474. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  32475. {
  32476. int ret;
  32477. mp_int ki;
  32478. ret = mp_init(&ki);
  32479. if (ret == 0)
  32480. ret = mp_set(&ki, k);
  32481. if (ret == 0)
  32482. ret = mp_sub_d(p1, 1, p2);
  32483. if (ret == 0)
  32484. ret = mp_mul(p2, &ki, p2);
  32485. if (ret == 0)
  32486. ret = mp_add_d(p2, 1, p2);
  32487. mp_clear(&ki);
  32488. return ret;
  32489. }
  32490. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  32491. const pairs_t* ecPairs, int ecPairsSz,
  32492. const int* k)
  32493. {
  32494. mp_int x,y;
  32495. int ret, i;
  32496. ret = mp_init(&x);
  32497. if (ret == 0) {
  32498. ret = mp_init(&y);
  32499. if (ret != 0) {
  32500. mp_clear(&x);
  32501. return MP_MEM;
  32502. }
  32503. }
  32504. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  32505. ret = mp_read_unsigned_bin(&x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  32506. /* p1 = 2^exp */
  32507. if (ret == 0)
  32508. ret = mp_2expt(&y, ecPairs[i].exp);
  32509. /* p1 = p1 * m */
  32510. if (ret == 0)
  32511. ret = mp_mul(&x, &y, &x);
  32512. /* p1 += */
  32513. if (ret == 0)
  32514. ret = mp_add(p1, &x, p1);
  32515. mp_zero(&x);
  32516. mp_zero(&y);
  32517. }
  32518. mp_clear(&x);
  32519. mp_clear(&y);
  32520. if (ret == 0)
  32521. ret = GenerateNextP(p1, p2, k[0]);
  32522. if (ret == 0)
  32523. ret = GenerateNextP(p1, p3, k[1]);
  32524. return ret;
  32525. }
  32526. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  32527. {
  32528. #ifdef WOLFSSL_SMALL_STACK
  32529. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  32530. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  32531. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  32532. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32533. #else
  32534. mp_int n[1],
  32535. p1[1],
  32536. p2[1],
  32537. p3[1];
  32538. #endif
  32539. int ret, isPrime = 0;
  32540. WC_RNG rng;
  32541. #ifdef WOLFSSL_SMALL_STACK
  32542. if ((n == NULL) ||
  32543. (p1 == NULL) ||
  32544. (p2 == NULL) ||
  32545. (p3 == NULL))
  32546. ERROR_OUT(MEMORY_E, out);
  32547. #endif
  32548. ret = wc_InitRng(&rng);
  32549. if (ret == 0)
  32550. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  32551. if (ret == 0)
  32552. ret = GenerateP(p1, p2, p3,
  32553. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  32554. if (ret == 0)
  32555. ret = mp_mul(p1, p2, n);
  32556. if (ret == 0)
  32557. ret = mp_mul(n, p3, n);
  32558. if (ret != 0)
  32559. ERROR_OUT(-13400, out);
  32560. /* Check the old prime test using the number that false positives.
  32561. * This test result should indicate as not prime. */
  32562. ret = mp_prime_is_prime(n, 40, &isPrime);
  32563. if (ret != 0)
  32564. ERROR_OUT(-13401, out);
  32565. if (isPrime)
  32566. ERROR_OUT(-13402, out);
  32567. /* This test result should fail. It should indicate the value as prime. */
  32568. ret = mp_prime_is_prime(n, 8, &isPrime);
  32569. if (ret != 0)
  32570. ERROR_OUT(-13403, out);
  32571. if (!isPrime)
  32572. ERROR_OUT(-13404, out);
  32573. /* This test result should indicate the value as not prime. */
  32574. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  32575. if (ret != 0)
  32576. ERROR_OUT(-13405, out);
  32577. if (isPrime)
  32578. ERROR_OUT(-13406, out);
  32579. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  32580. if (ret != 0)
  32581. ERROR_OUT(-13407, out);
  32582. /* This test result should indicate the value as prime. */
  32583. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  32584. if (ret != 0)
  32585. ERROR_OUT(-13408, out);
  32586. if (!isPrime)
  32587. ERROR_OUT(-13409, out);
  32588. /* This test result should indicate the value as prime. */
  32589. isPrime = -1;
  32590. ret = mp_prime_is_prime(n, 8, &isPrime);
  32591. if (ret != 0)
  32592. ERROR_OUT(-13410, out);
  32593. if (!isPrime)
  32594. ERROR_OUT(-13411, out);
  32595. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  32596. if (ret != 0)
  32597. ERROR_OUT(-13412, out);
  32598. /* This test result should indicate the value as not prime. */
  32599. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  32600. if (ret != 0)
  32601. ERROR_OUT(-13413, out);
  32602. if (isPrime)
  32603. ERROR_OUT(-13414, out);
  32604. ret = mp_prime_is_prime(n, 8, &isPrime);
  32605. if (ret != 0)
  32606. ERROR_OUT(-13415, out);
  32607. if (isPrime)
  32608. ERROR_OUT(-13416, out);
  32609. ret = 0;
  32610. out:
  32611. #ifdef WOLFSSL_SMALL_STACK
  32612. if (n != NULL) {
  32613. mp_clear(n);
  32614. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32615. }
  32616. if (p1 != NULL) {
  32617. mp_clear(p1);
  32618. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32619. }
  32620. if (p2 != NULL) {
  32621. mp_clear(p2);
  32622. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32623. }
  32624. if (p3 != NULL) {
  32625. mp_clear(p3);
  32626. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32627. }
  32628. #else
  32629. mp_clear(p3);
  32630. mp_clear(p2);
  32631. mp_clear(p1);
  32632. mp_clear(n);
  32633. #endif
  32634. wc_FreeRng(&rng);
  32635. return ret;
  32636. }
  32637. #endif /* WOLFSSL_PUBLIC_MP */
  32638. #if defined(ASN_BER_TO_DER) && \
  32639. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  32640. defined(OPENSSL_EXTRA_X509_SMALL))
  32641. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  32642. typedef struct berDerTestData {
  32643. const byte *in;
  32644. word32 inSz;
  32645. const byte *out;
  32646. word32 outSz;
  32647. } berDerTestData;
  32648. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  32649. {
  32650. int ret;
  32651. int i;
  32652. word32 len = 0, l;
  32653. byte out[32];
  32654. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  32655. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  32656. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  32657. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  32658. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  32659. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  32660. };
  32661. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  32662. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  32663. 0x30, 0x80,
  32664. 0x02, 0x01, 0x01,
  32665. 0x30, 0x80,
  32666. 0x24, 0x80,
  32667. 0x04, 0x01, 0x01,
  32668. 0x04, 0x02, 0x02, 0x03,
  32669. 0x00, 0x00,
  32670. 0x06, 0x01, 0x01,
  32671. 0x00, 0x00,
  32672. 0x31, 0x80,
  32673. 0x06, 0x01, 0x01,
  32674. 0x00, 0x00,
  32675. 0x00, 0x00,
  32676. };
  32677. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  32678. 0x30, 0x12,
  32679. 0x02, 0x01, 0x01,
  32680. 0x30, 0x08,
  32681. 0x04, 0x03, 0x01, 0x02, 0x03,
  32682. 0x06, 0x01, 0x01,
  32683. 0x31, 0x03,
  32684. 0x06, 0x01, 0x01
  32685. };
  32686. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  32687. berDerTestData testData[] = {
  32688. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  32689. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  32690. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  32691. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  32692. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  32693. };
  32694. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  32695. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  32696. if (ret != LENGTH_ONLY_E)
  32697. return -13500 - i;
  32698. if (len != testData[i].outSz)
  32699. return -13510 - i;
  32700. len = testData[i].outSz;
  32701. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  32702. if (ret != 0)
  32703. return -13520 - i;
  32704. if (XMEMCMP(out, testData[i].out, len) != 0)
  32705. return -13530 - i;
  32706. for (l = 1; l < testData[i].inSz; l++) {
  32707. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  32708. if (ret != ASN_PARSE_E)
  32709. return -13540;
  32710. len = testData[i].outSz;
  32711. ret = wc_BerToDer(testData[i].in, l, out, &len);
  32712. if (ret != ASN_PARSE_E)
  32713. return -13541;
  32714. }
  32715. for (l = 0; l < testData[i].outSz-1; l++) {
  32716. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  32717. if (ret != BUFFER_E)
  32718. return -13542;
  32719. }
  32720. }
  32721. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  32722. if (ret != BAD_FUNC_ARG)
  32723. return -13543;
  32724. ret = wc_BerToDer(out, 4, NULL, NULL);
  32725. if (ret != BAD_FUNC_ARG)
  32726. return -13544;
  32727. ret = wc_BerToDer(NULL, 4, NULL, &len);
  32728. if (ret != BAD_FUNC_ARG)
  32729. return -13545;
  32730. ret = wc_BerToDer(NULL, 4, out, NULL);
  32731. if (ret != BAD_FUNC_ARG)
  32732. return -13546;
  32733. ret = wc_BerToDer(out, 4, out, NULL);
  32734. if (ret != BAD_FUNC_ARG)
  32735. return -13547;
  32736. ret = wc_BerToDer(NULL, 4, out, &len);
  32737. if (ret != BAD_FUNC_ARG)
  32738. return -13548;
  32739. for (l = 1; l < sizeof(good4_out); l++) {
  32740. len = l;
  32741. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  32742. if (ret != BUFFER_E)
  32743. return -13549;
  32744. }
  32745. return 0;
  32746. }
  32747. #endif
  32748. #ifdef DEBUG_WOLFSSL
  32749. static THREAD_LS_T int log_cnt = 0;
  32750. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  32751. {
  32752. (void)logLevel;
  32753. (void)logMessage;
  32754. log_cnt++;
  32755. }
  32756. #endif /* DEBUG_WOLFSSL */
  32757. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  32758. {
  32759. #ifdef DEBUG_WOLFSSL
  32760. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  32761. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  32762. byte b[256];
  32763. int i;
  32764. for (i = 0; i < (int)sizeof(b); i++)
  32765. b[i] = i;
  32766. if (wolfSSL_Debugging_ON() != 0)
  32767. return -13600;
  32768. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  32769. return -13601;
  32770. WOLFSSL_MSG(msg);
  32771. WOLFSSL_BUFFER(a, sizeof(a));
  32772. WOLFSSL_BUFFER(b, sizeof(b));
  32773. WOLFSSL_BUFFER(NULL, 0);
  32774. WOLFSSL_ERROR(MEMORY_E);
  32775. WOLFSSL_ERROR_MSG(msg);
  32776. /* turn off logs */
  32777. wolfSSL_Debugging_OFF();
  32778. /* capture log count */
  32779. i = log_cnt;
  32780. /* validate no logs are output when disabled */
  32781. WOLFSSL_MSG(msg);
  32782. WOLFSSL_BUFFER(a, sizeof(a));
  32783. WOLFSSL_BUFFER(b, sizeof(b));
  32784. WOLFSSL_BUFFER(NULL, 0);
  32785. WOLFSSL_ERROR(MEMORY_E);
  32786. WOLFSSL_ERROR_MSG(msg);
  32787. /* check the logs were disabled */
  32788. if (i != log_cnt)
  32789. return -13602;
  32790. /* restore callback and leave logging enabled */
  32791. wolfSSL_SetLoggingCb(NULL);
  32792. wolfSSL_Debugging_ON();
  32793. /* suppress unused args */
  32794. (void)a;
  32795. (void)b;
  32796. #else
  32797. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  32798. return -13603;
  32799. wolfSSL_Debugging_OFF();
  32800. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  32801. return -13604;
  32802. #endif /* DEBUG_WOLFSSL */
  32803. return 0;
  32804. }
  32805. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  32806. {
  32807. #ifdef WOLFSSL_PTHREADS
  32808. wolfSSL_Mutex m;
  32809. #endif
  32810. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  32811. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  32812. if (mm == NULL)
  32813. return -13700;
  32814. wc_FreeMutex(mm);
  32815. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  32816. #endif
  32817. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  32818. #ifdef WOLFSSL_PTHREADS
  32819. if (wc_InitMutex(&m) != 0)
  32820. return -13701;
  32821. if (wc_LockMutex(&m) != 0)
  32822. return -13702;
  32823. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  32824. /* trying to free a locked mutex is not portable behavior with pthread */
  32825. /* Attempting to destroy a locked mutex results in undefined behavior */
  32826. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  32827. return -13703;
  32828. #endif
  32829. if (wc_UnLockMutex(&m) != 0)
  32830. return -13704;
  32831. if (wc_FreeMutex(&m) != 0)
  32832. return -13705;
  32833. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  32834. /* Trying to use a pthread after free'ing is not portable behavior */
  32835. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  32836. return -13706;
  32837. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  32838. return -13707;
  32839. #endif
  32840. #endif
  32841. return 0;
  32842. }
  32843. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  32844. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  32845. static int malloc_cnt = 0;
  32846. static int realloc_cnt = 0;
  32847. static int free_cnt = 0;
  32848. #ifdef WOLFSSL_DEBUG_MEMORY
  32849. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  32850. {
  32851. (void) func;
  32852. (void) line;
  32853. #else
  32854. static void *my_Malloc_cb(size_t size)
  32855. {
  32856. #endif
  32857. malloc_cnt++;
  32858. #ifndef WOLFSSL_NO_MALLOC
  32859. return malloc(size);
  32860. #else
  32861. WOLFSSL_MSG("No malloc available");
  32862. (void)size;
  32863. return NULL;
  32864. #endif
  32865. }
  32866. #ifdef WOLFSSL_DEBUG_MEMORY
  32867. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  32868. {
  32869. (void) func;
  32870. (void) line;
  32871. #else
  32872. static void my_Free_cb(void *ptr)
  32873. {
  32874. #endif
  32875. free_cnt++;
  32876. #ifndef WOLFSSL_NO_MALLOC
  32877. free(ptr);
  32878. #else
  32879. WOLFSSL_MSG("No free available");
  32880. (void)ptr;
  32881. #endif
  32882. }
  32883. #ifdef WOLFSSL_DEBUG_MEMORY
  32884. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  32885. {
  32886. (void) func;
  32887. (void) line;
  32888. #else
  32889. static void *my_Realloc_cb(void *ptr, size_t size)
  32890. {
  32891. #endif
  32892. realloc_cnt++;
  32893. #ifndef WOLFSSL_NO_MALLOC
  32894. return realloc(ptr, size);
  32895. #else
  32896. WOLFSSL_MSG("No realloc available");
  32897. (void)ptr;
  32898. (void)size;
  32899. return NULL;
  32900. #endif
  32901. }
  32902. #endif /* !WOLFSSL_NO_MALLOC */
  32903. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  32904. {
  32905. int ret = 0;
  32906. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  32907. byte* b = NULL;
  32908. #endif
  32909. wolfSSL_Malloc_cb mc;
  32910. wolfSSL_Free_cb fc;
  32911. wolfSSL_Realloc_cb rc;
  32912. /* Save existing memory callbacks */
  32913. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  32914. return -13800;
  32915. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  32916. /* test realloc */
  32917. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32918. if (b == NULL) {
  32919. ERROR_OUT(-13801, exit_memcb);
  32920. }
  32921. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32922. b = NULL;
  32923. /* Use API. */
  32924. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  32925. (wolfSSL_Free_cb)my_Free_cb,
  32926. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  32927. ERROR_OUT(-13802, exit_memcb);
  32928. }
  32929. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32930. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32931. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32932. #ifndef WOLFSSL_STATIC_MEMORY
  32933. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  32934. #else
  32935. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  32936. #endif
  32937. ret = -13803;
  32938. #endif /* !WOLFSSL_NO_MALLOC */
  32939. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_STATIC_MEMORY)
  32940. exit_memcb:
  32941. #endif
  32942. /* restore memory callbacks */
  32943. wolfSSL_SetAllocators(mc, fc, rc);
  32944. return ret;
  32945. }
  32946. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  32947. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  32948. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  32949. {
  32950. int ret = 0;
  32951. byte out[112];
  32952. byte blob[112];
  32953. word32 outSz;
  32954. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  32955. {
  32956. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  32957. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  32958. };
  32959. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  32960. {
  32961. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  32962. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  32963. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  32964. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  32965. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  32966. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  32967. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  32968. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  32969. };
  32970. XMEMSET(blob, 0, sizeof(blob));
  32971. XMEMSET(out, 0, sizeof(out));
  32972. outSz = sizeof(blob);
  32973. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  32974. if (ret != 0) {
  32975. ERROR_OUT(-13900, exit_blob);
  32976. }
  32977. blob[outSz - 2] += 1;
  32978. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  32979. if (ret == 0) { /* should fail with altered blob */
  32980. ERROR_OUT(-13901, exit_blob);
  32981. }
  32982. XMEMSET(blob, 0, sizeof(blob));
  32983. outSz = sizeof(blob);
  32984. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  32985. if (ret != 0) {
  32986. ERROR_OUT(-13902, exit_blob);
  32987. }
  32988. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  32989. if (ret != 0) {
  32990. ERROR_OUT(-13903, exit_blob);
  32991. }
  32992. if (XMEMCMP(out, iv, sizeof(iv))) {
  32993. ERROR_OUT(-13904, exit_blob);
  32994. }
  32995. XMEMSET(blob, 0, sizeof(blob));
  32996. outSz = sizeof(blob);
  32997. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  32998. if (ret != 0) {
  32999. ERROR_OUT(-13905, exit_blob);
  33000. }
  33001. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  33002. if (ret != 0) {
  33003. ERROR_OUT(-13906, exit_blob);
  33004. }
  33005. if (XMEMCMP(out, text, sizeof(text))) {
  33006. ERROR_OUT(-13907, exit_blob);
  33007. }
  33008. exit_blob:
  33009. return ret;
  33010. }
  33011. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  33012. #ifdef WOLF_CRYPTO_CB
  33013. /* Example custom context for crypto callback */
  33014. typedef struct {
  33015. int exampleVar; /* example, not used */
  33016. } myCryptoDevCtx;
  33017. /* Example crypto dev callback function that calls software version */
  33018. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  33019. {
  33020. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  33021. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  33022. if (info == NULL)
  33023. return BAD_FUNC_ARG;
  33024. #ifdef DEBUG_WOLFSSL
  33025. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  33026. #endif
  33027. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  33028. #ifndef WC_NO_RNG
  33029. /* set devId to invalid, so software is used */
  33030. info->rng.rng->devId = INVALID_DEVID;
  33031. ret = wc_RNG_GenerateBlock(info->rng.rng,
  33032. info->rng.out, info->rng.sz);
  33033. /* reset devId */
  33034. info->rng.rng->devId = devIdArg;
  33035. #endif
  33036. }
  33037. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  33038. #ifndef WC_NO_RNG
  33039. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  33040. word32* seedWord32 = (word32*)seed;
  33041. word32 len;
  33042. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  33043. while (info->seed.sz > 0) {
  33044. len = (word32)sizeof(seed);
  33045. if (info->seed.sz < len)
  33046. len = info->seed.sz;
  33047. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  33048. info->seed.seed += len;
  33049. info->seed.sz -= len;
  33050. (*seedWord32)++;
  33051. }
  33052. ret = 0;
  33053. #endif
  33054. }
  33055. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  33056. #ifdef DEBUG_WOLFSSL
  33057. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  33058. #endif
  33059. #ifndef NO_RSA
  33060. if (info->pk.type == WC_PK_TYPE_RSA) {
  33061. /* set devId to invalid, so software is used */
  33062. info->pk.rsa.key->devId = INVALID_DEVID;
  33063. switch (info->pk.rsa.type) {
  33064. case RSA_PUBLIC_ENCRYPT:
  33065. case RSA_PUBLIC_DECRYPT:
  33066. /* perform software based RSA public op */
  33067. ret = wc_RsaFunction(
  33068. info->pk.rsa.in, info->pk.rsa.inLen,
  33069. info->pk.rsa.out, info->pk.rsa.outLen,
  33070. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  33071. break;
  33072. case RSA_PRIVATE_ENCRYPT:
  33073. case RSA_PRIVATE_DECRYPT:
  33074. /* perform software based RSA private op */
  33075. ret = wc_RsaFunction(
  33076. info->pk.rsa.in, info->pk.rsa.inLen,
  33077. info->pk.rsa.out, info->pk.rsa.outLen,
  33078. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  33079. break;
  33080. }
  33081. /* reset devId */
  33082. info->pk.rsa.key->devId = devIdArg;
  33083. }
  33084. #ifdef WOLFSSL_KEY_GEN
  33085. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  33086. info->pk.rsakg.key->devId = INVALID_DEVID;
  33087. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  33088. info->pk.rsakg.e, info->pk.rsakg.rng);
  33089. /* reset devId */
  33090. info->pk.rsakg.key->devId = devIdArg;
  33091. }
  33092. #endif
  33093. #endif /* !NO_RSA */
  33094. #ifdef HAVE_ECC
  33095. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  33096. /* set devId to invalid, so software is used */
  33097. info->pk.eckg.key->devId = INVALID_DEVID;
  33098. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  33099. info->pk.eckg.key, info->pk.eckg.curveId);
  33100. /* reset devId */
  33101. info->pk.eckg.key->devId = devIdArg;
  33102. }
  33103. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  33104. /* set devId to invalid, so software is used */
  33105. info->pk.eccsign.key->devId = INVALID_DEVID;
  33106. ret = wc_ecc_sign_hash(
  33107. info->pk.eccsign.in, info->pk.eccsign.inlen,
  33108. info->pk.eccsign.out, info->pk.eccsign.outlen,
  33109. info->pk.eccsign.rng, info->pk.eccsign.key);
  33110. /* reset devId */
  33111. info->pk.eccsign.key->devId = devIdArg;
  33112. }
  33113. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  33114. /* set devId to invalid, so software is used */
  33115. info->pk.eccverify.key->devId = INVALID_DEVID;
  33116. ret = wc_ecc_verify_hash(
  33117. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  33118. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  33119. info->pk.eccverify.res, info->pk.eccverify.key);
  33120. /* reset devId */
  33121. info->pk.eccverify.key->devId = devIdArg;
  33122. }
  33123. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  33124. /* set devId to invalid, so software is used */
  33125. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  33126. ret = wc_ecc_shared_secret(
  33127. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  33128. info->pk.ecdh.out, info->pk.ecdh.outlen);
  33129. /* reset devId */
  33130. info->pk.ecdh.private_key->devId = devIdArg;
  33131. }
  33132. #endif /* HAVE_ECC */
  33133. #ifdef HAVE_CURVE25519
  33134. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  33135. /* set devId to invalid, so software is used */
  33136. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  33137. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  33138. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  33139. /* reset devId */
  33140. info->pk.curve25519kg.key->devId = devIdArg;
  33141. }
  33142. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  33143. /* set devId to invalid, so software is used */
  33144. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  33145. ret = wc_curve25519_shared_secret_ex(
  33146. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  33147. info->pk.curve25519.out, info->pk.curve25519.outlen,
  33148. info->pk.curve25519.endian);
  33149. /* reset devId */
  33150. info->pk.curve25519.private_key->devId = devIdArg;
  33151. }
  33152. #endif /* HAVE_CURVE25519 */
  33153. #ifdef HAVE_ED25519
  33154. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  33155. /* set devId to invalid, so software is used */
  33156. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  33157. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  33158. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  33159. /* reset devId */
  33160. info->pk.ed25519kg.key->devId = devIdArg;
  33161. }
  33162. #ifdef HAVE_ED25519_SIGN
  33163. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  33164. /* set devId to invalid, so software is used */
  33165. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  33166. ret = wc_ed25519_sign_msg_ex(
  33167. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  33168. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  33169. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  33170. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  33171. /* reset devId */
  33172. info->pk.ed25519sign.key->devId = devIdArg;
  33173. }
  33174. #endif
  33175. #ifdef HAVE_ED25519_VERIFY
  33176. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  33177. /* set devId to invalid, so software is used */
  33178. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  33179. ret = wc_ed25519_verify_msg_ex(
  33180. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  33181. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  33182. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  33183. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  33184. info->pk.ed25519verify.contextLen);
  33185. /* reset devId */
  33186. info->pk.ed25519verify.key->devId = devIdArg;
  33187. }
  33188. #endif
  33189. #endif /* HAVE_ED25519 */
  33190. }
  33191. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  33192. #if !defined(NO_AES) || !defined(NO_DES3)
  33193. #ifdef HAVE_AESGCM
  33194. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  33195. if (info->cipher.enc) {
  33196. /* set devId to invalid, so software is used */
  33197. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  33198. ret = wc_AesGcmEncrypt(
  33199. info->cipher.aesgcm_enc.aes,
  33200. info->cipher.aesgcm_enc.out,
  33201. info->cipher.aesgcm_enc.in,
  33202. info->cipher.aesgcm_enc.sz,
  33203. info->cipher.aesgcm_enc.iv,
  33204. info->cipher.aesgcm_enc.ivSz,
  33205. info->cipher.aesgcm_enc.authTag,
  33206. info->cipher.aesgcm_enc.authTagSz,
  33207. info->cipher.aesgcm_enc.authIn,
  33208. info->cipher.aesgcm_enc.authInSz);
  33209. /* reset devId */
  33210. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  33211. }
  33212. else {
  33213. /* set devId to invalid, so software is used */
  33214. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  33215. ret = wc_AesGcmDecrypt(
  33216. info->cipher.aesgcm_dec.aes,
  33217. info->cipher.aesgcm_dec.out,
  33218. info->cipher.aesgcm_dec.in,
  33219. info->cipher.aesgcm_dec.sz,
  33220. info->cipher.aesgcm_dec.iv,
  33221. info->cipher.aesgcm_dec.ivSz,
  33222. info->cipher.aesgcm_dec.authTag,
  33223. info->cipher.aesgcm_dec.authTagSz,
  33224. info->cipher.aesgcm_dec.authIn,
  33225. info->cipher.aesgcm_dec.authInSz);
  33226. /* reset devId */
  33227. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  33228. }
  33229. }
  33230. #endif /* HAVE_AESGCM */
  33231. #ifdef HAVE_AES_CBC
  33232. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  33233. if (info->cipher.enc) {
  33234. /* set devId to invalid, so software is used */
  33235. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  33236. ret = wc_AesCbcEncrypt(
  33237. info->cipher.aescbc.aes,
  33238. info->cipher.aescbc.out,
  33239. info->cipher.aescbc.in,
  33240. info->cipher.aescbc.sz);
  33241. /* reset devId */
  33242. info->cipher.aescbc.aes->devId = devIdArg;
  33243. }
  33244. else {
  33245. /* set devId to invalid, so software is used */
  33246. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  33247. ret = wc_AesCbcDecrypt(
  33248. info->cipher.aescbc.aes,
  33249. info->cipher.aescbc.out,
  33250. info->cipher.aescbc.in,
  33251. info->cipher.aescbc.sz);
  33252. /* reset devId */
  33253. info->cipher.aescbc.aes->devId = devIdArg;
  33254. }
  33255. }
  33256. #endif /* HAVE_AES_CBC */
  33257. #ifndef NO_DES3
  33258. if (info->cipher.type == WC_CIPHER_DES3) {
  33259. if (info->cipher.enc) {
  33260. /* set devId to invalid, so software is used */
  33261. info->cipher.des3.des->devId = INVALID_DEVID;
  33262. ret = wc_Des3_CbcEncrypt(
  33263. info->cipher.des3.des,
  33264. info->cipher.des3.out,
  33265. info->cipher.des3.in,
  33266. info->cipher.des3.sz);
  33267. /* reset devId */
  33268. info->cipher.des3.des->devId = devIdArg;
  33269. }
  33270. else {
  33271. /* set devId to invalid, so software is used */
  33272. info->cipher.des3.des->devId = INVALID_DEVID;
  33273. ret = wc_Des3_CbcDecrypt(
  33274. info->cipher.des3.des,
  33275. info->cipher.des3.out,
  33276. info->cipher.des3.in,
  33277. info->cipher.des3.sz);
  33278. /* reset devId */
  33279. info->cipher.des3.des->devId = devIdArg;
  33280. }
  33281. }
  33282. #endif /* !NO_DES3 */
  33283. #endif /* !NO_AES || !NO_DES3 */
  33284. }
  33285. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  33286. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  33287. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  33288. #if !defined(NO_SHA)
  33289. if (info->hash.type == WC_HASH_TYPE_SHA) {
  33290. if (info->hash.sha1 == NULL)
  33291. return NOT_COMPILED_IN;
  33292. /* set devId to invalid, so software is used */
  33293. info->hash.sha1->devId = INVALID_DEVID;
  33294. if (info->hash.in != NULL) {
  33295. ret = wc_ShaUpdate(
  33296. info->hash.sha1,
  33297. info->hash.in,
  33298. info->hash.inSz);
  33299. }
  33300. if (info->hash.digest != NULL) {
  33301. ret = wc_ShaFinal(
  33302. info->hash.sha1,
  33303. info->hash.digest);
  33304. }
  33305. /* reset devId */
  33306. info->hash.sha1->devId = devIdArg;
  33307. }
  33308. else
  33309. #endif
  33310. #if !defined(NO_SHA256)
  33311. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  33312. if (info->hash.sha256 == NULL)
  33313. return NOT_COMPILED_IN;
  33314. /* set devId to invalid, so software is used */
  33315. info->hash.sha256->devId = INVALID_DEVID;
  33316. if (info->hash.in != NULL) {
  33317. ret = wc_Sha256Update(
  33318. info->hash.sha256,
  33319. info->hash.in,
  33320. info->hash.inSz);
  33321. }
  33322. if (info->hash.digest != NULL) {
  33323. ret = wc_Sha256Final(
  33324. info->hash.sha256,
  33325. info->hash.digest);
  33326. }
  33327. /* reset devId */
  33328. info->hash.sha256->devId = devIdArg;
  33329. }
  33330. else
  33331. #endif
  33332. #ifdef WOLFSSL_SHA384
  33333. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  33334. if (info->hash.sha384 == NULL)
  33335. return NOT_COMPILED_IN;
  33336. /* set devId to invalid, so software is used */
  33337. info->hash.sha384->devId = INVALID_DEVID;
  33338. if (info->hash.in != NULL) {
  33339. ret = wc_Sha384Update(
  33340. info->hash.sha384,
  33341. info->hash.in,
  33342. info->hash.inSz);
  33343. }
  33344. if (info->hash.digest != NULL) {
  33345. ret = wc_Sha384Final(
  33346. info->hash.sha384,
  33347. info->hash.digest);
  33348. }
  33349. /* reset devId */
  33350. info->hash.sha384->devId = devIdArg;
  33351. }
  33352. else
  33353. #endif
  33354. #ifdef WOLFSSL_SHA512
  33355. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  33356. if (info->hash.sha512 == NULL)
  33357. return NOT_COMPILED_IN;
  33358. /* set devId to invalid, so software is used */
  33359. info->hash.sha512->devId = INVALID_DEVID;
  33360. if (info->hash.in != NULL) {
  33361. ret = wc_Sha512Update(
  33362. info->hash.sha512,
  33363. info->hash.in,
  33364. info->hash.inSz);
  33365. }
  33366. if (info->hash.digest != NULL) {
  33367. ret = wc_Sha512Final(
  33368. info->hash.sha512,
  33369. info->hash.digest);
  33370. }
  33371. /* reset devId */
  33372. info->hash.sha512->devId = devIdArg;
  33373. }
  33374. else
  33375. #endif
  33376. {
  33377. }
  33378. }
  33379. #endif /* !NO_SHA || !NO_SHA256 */
  33380. #ifndef NO_HMAC
  33381. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  33382. if (info->hmac.hmac == NULL)
  33383. return NOT_COMPILED_IN;
  33384. /* set devId to invalid, so software is used */
  33385. info->hmac.hmac->devId = INVALID_DEVID;
  33386. if (info->hash.in != NULL) {
  33387. ret = wc_HmacUpdate(
  33388. info->hmac.hmac,
  33389. info->hmac.in,
  33390. info->hmac.inSz);
  33391. }
  33392. else if (info->hash.digest != NULL) {
  33393. ret = wc_HmacFinal(
  33394. info->hmac.hmac,
  33395. info->hmac.digest);
  33396. }
  33397. /* reset devId */
  33398. info->hmac.hmac->devId = devIdArg;
  33399. }
  33400. #endif
  33401. (void)devIdArg;
  33402. (void)myCtx;
  33403. return ret;
  33404. }
  33405. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  33406. {
  33407. int ret = 0;
  33408. myCryptoDevCtx myCtx;
  33409. /* example data for callback */
  33410. myCtx.exampleVar = 1;
  33411. /* set devId to something other than INVALID_DEVID */
  33412. devId = 1;
  33413. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  33414. #ifndef WC_NO_RNG
  33415. if (ret == 0)
  33416. ret = random_test();
  33417. #endif /* WC_NO_RNG */
  33418. #ifndef NO_RSA
  33419. if (ret == 0)
  33420. ret = rsa_test();
  33421. #endif
  33422. #ifdef HAVE_ECC
  33423. if (ret == 0)
  33424. ret = ecc_test();
  33425. #endif
  33426. #ifdef HAVE_ED25519
  33427. if (ret == 0)
  33428. ret = ed25519_test();
  33429. #endif
  33430. #ifdef HAVE_CURVE25519
  33431. if (ret == 0)
  33432. ret = curve25519_test();
  33433. #endif
  33434. #ifndef NO_AES
  33435. #ifdef HAVE_AESGCM
  33436. if (ret == 0)
  33437. ret = aesgcm_test();
  33438. #endif
  33439. #ifdef HAVE_AES_CBC
  33440. if (ret == 0)
  33441. ret = aes_test();
  33442. #endif
  33443. #endif /* !NO_AES */
  33444. #ifndef NO_DES3
  33445. if (ret == 0)
  33446. ret = des3_test();
  33447. #endif /* !NO_DES3 */
  33448. #ifndef NO_SHA
  33449. if (ret == 0)
  33450. ret = sha_test();
  33451. #endif
  33452. #ifndef NO_SHA256
  33453. if (ret == 0)
  33454. ret = sha256_test();
  33455. #endif
  33456. #ifdef WOLFSSL_SHA384
  33457. if (ret == 0)
  33458. ret = sha384_test();
  33459. #endif
  33460. #ifdef WOLFSSL_SHA512
  33461. if (ret == 0)
  33462. ret = sha512_test();
  33463. #endif
  33464. #ifndef NO_HMAC
  33465. #ifndef NO_SHA
  33466. if (ret == 0)
  33467. ret = hmac_sha_test();
  33468. #endif
  33469. #ifndef NO_SHA256
  33470. if (ret == 0)
  33471. ret = hmac_sha256_test();
  33472. #endif
  33473. #endif
  33474. #ifndef NO_PWDBASED
  33475. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  33476. if (ret == 0)
  33477. ret = pbkdf2_test();
  33478. #endif
  33479. #endif
  33480. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  33481. if (ret == 0)
  33482. ret = cmac_test();
  33483. #endif
  33484. /* reset devId */
  33485. devId = INVALID_DEVID;
  33486. return ret;
  33487. }
  33488. #endif /* WOLF_CRYPTO_CB */
  33489. #ifdef WOLFSSL_CERT_PIV
  33490. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  33491. {
  33492. int ret;
  33493. wc_CertPIV piv;
  33494. /* Template for Identiv PIV cert, nonce and signature */
  33495. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  33496. 0x0A, 0x0B,
  33497. 0x53, 0x09, /* NIST PIV Cert */
  33498. 0x70, 0x02, /* Certificate */
  33499. 0x30, 0x00,
  33500. 0x71, 0x01, 0x05, /* Cert Info */
  33501. 0xFE, 0x00, /* Error Detection */
  33502. 0x0B, 0x01, 0x00, /* Nonce */
  33503. 0x0C, 0x01, 0x00, /* Signed Nonce */
  33504. };
  33505. /* PIV certificate data including certificate, info and error dectection. */
  33506. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  33507. 0x53, 0x09, /* NIST PIV Cert */
  33508. 0x70, 0x02, /* Certificate */
  33509. 0x30, 0x00,
  33510. 0x71, 0x01, 0x04, /* Cert Info */
  33511. 0xFE, 0x00, /* Error Detection */
  33512. };
  33513. XMEMSET(&piv, 0, sizeof(piv));
  33514. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  33515. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  33516. if (ret != 0) {
  33517. return -14000;
  33518. }
  33519. if (!piv.isIdentiv) {
  33520. return -14001;
  33521. }
  33522. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  33523. return -14002;
  33524. }
  33525. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  33526. return -14003;
  33527. }
  33528. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  33529. return -14004;
  33530. }
  33531. if (!piv.isX509) {
  33532. return -14005;
  33533. }
  33534. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  33535. return -14006;
  33536. }
  33537. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  33538. return -14007;
  33539. }
  33540. XMEMSET(&piv, 0, sizeof(piv));
  33541. /* Test with NIST PIV format */
  33542. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  33543. if (ret != 0) {
  33544. return -14010;
  33545. }
  33546. if (piv.isIdentiv) {
  33547. return -14011;
  33548. }
  33549. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  33550. return -14012;
  33551. }
  33552. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  33553. return -14013;
  33554. }
  33555. if ((piv.compression != 0)) {
  33556. return -14014;
  33557. }
  33558. if (!piv.isX509) {
  33559. return -14015;
  33560. }
  33561. return ret;
  33562. }
  33563. #endif /* WOLFSSL_CERT_PIV */
  33564. #undef ERROR_OUT
  33565. #else
  33566. #ifndef NO_MAIN_DRIVER
  33567. int main() { return 0; }
  33568. #endif
  33569. #endif /* NO_CRYPT_TEST */