test.c 1.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2020 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. #else
  155. #ifdef XMALLOC_USER
  156. #include <stdlib.h> /* we're using malloc / free direct here */
  157. #endif
  158. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  159. #include <stdio.h>
  160. #endif
  161. #if defined(WOLFSSL_LINUXKM) && !defined(DEBUG_WOLFSSL_VERBOSE)
  162. #undef printf
  163. #define printf(...) ({})
  164. #endif
  165. /* enable way for customer to override test/bench printf */
  166. #ifdef XPRINTF
  167. #undef printf
  168. #define printf XPRINTF
  169. #endif
  170. #endif
  171. #include <wolfssl/wolfcrypt/memory.h>
  172. #include <wolfssl/wolfcrypt/wc_port.h>
  173. #include <wolfssl/wolfcrypt/logging.h>
  174. #include <wolfssl/wolfcrypt/types.h>
  175. #include <wolfssl/wolfcrypt/asn.h>
  176. #include <wolfssl/wolfcrypt/md2.h>
  177. #include <wolfssl/wolfcrypt/md5.h>
  178. #include <wolfssl/wolfcrypt/md4.h>
  179. #include <wolfssl/wolfcrypt/sha.h>
  180. #include <wolfssl/wolfcrypt/sha256.h>
  181. #include <wolfssl/wolfcrypt/sha512.h>
  182. #include <wolfssl/wolfcrypt/rc2.h>
  183. #include <wolfssl/wolfcrypt/arc4.h>
  184. #if defined(WC_NO_RNG)
  185. #include <wolfssl/wolfcrypt/integer.h>
  186. #else
  187. #include <wolfssl/wolfcrypt/random.h>
  188. #endif
  189. #include <wolfssl/wolfcrypt/coding.h>
  190. #include <wolfssl/wolfcrypt/signature.h>
  191. #include <wolfssl/wolfcrypt/rsa.h>
  192. #include <wolfssl/wolfcrypt/des3.h>
  193. #include <wolfssl/wolfcrypt/aes.h>
  194. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  195. #include <wolfssl/wolfcrypt/cmac.h>
  196. #include <wolfssl/wolfcrypt/poly1305.h>
  197. #include <wolfssl/wolfcrypt/camellia.h>
  198. #include <wolfssl/wolfcrypt/hmac.h>
  199. #include <wolfssl/wolfcrypt/dh.h>
  200. #include <wolfssl/wolfcrypt/dsa.h>
  201. #include <wolfssl/wolfcrypt/srp.h>
  202. #include <wolfssl/wolfcrypt/idea.h>
  203. #include <wolfssl/wolfcrypt/hc128.h>
  204. #include <wolfssl/wolfcrypt/rabbit.h>
  205. #include <wolfssl/wolfcrypt/chacha.h>
  206. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  207. #include <wolfssl/wolfcrypt/pwdbased.h>
  208. #include <wolfssl/wolfcrypt/ripemd.h>
  209. #include <wolfssl/wolfcrypt/error-crypt.h>
  210. #ifdef HAVE_ECC
  211. #include <wolfssl/wolfcrypt/ecc.h>
  212. #endif
  213. #ifdef HAVE_CURVE25519
  214. #include <wolfssl/wolfcrypt/curve25519.h>
  215. #endif
  216. #ifdef HAVE_ED25519
  217. #include <wolfssl/wolfcrypt/ed25519.h>
  218. #endif
  219. #ifdef HAVE_CURVE448
  220. #include <wolfssl/wolfcrypt/curve448.h>
  221. #endif
  222. #ifdef HAVE_ED448
  223. #include <wolfssl/wolfcrypt/ed448.h>
  224. #endif
  225. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  226. #include <wolfssl/wolfcrypt/blake2.h>
  227. #endif
  228. #ifdef WOLFSSL_SHA3
  229. #include <wolfssl/wolfcrypt/sha3.h>
  230. #endif
  231. #ifdef HAVE_LIBZ
  232. #include <wolfssl/wolfcrypt/compress.h>
  233. #endif
  234. #ifdef HAVE_PKCS7
  235. #include <wolfssl/wolfcrypt/pkcs7.h>
  236. #endif
  237. #ifdef HAVE_FIPS
  238. #include <wolfssl/wolfcrypt/fips_test.h>
  239. #endif
  240. #ifdef HAVE_SELFTEST
  241. #include <wolfssl/wolfcrypt/selftest.h>
  242. #endif
  243. #ifdef WOLFSSL_ASYNC_CRYPT
  244. #include <wolfssl/wolfcrypt/async.h>
  245. #endif
  246. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  247. #include <wolfssl/wolfcrypt/logging.h>
  248. #endif
  249. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  250. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  251. #endif
  252. #ifdef WOLF_CRYPTO_CB
  253. #include <wolfssl/wolfcrypt/cryptocb.h>
  254. #ifdef HAVE_INTEL_QA_SYNC
  255. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  256. #endif
  257. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  258. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  259. #endif
  260. #endif
  261. #ifdef _MSC_VER
  262. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  263. #pragma warning(disable: 4996)
  264. #endif
  265. #ifdef OPENSSL_EXTRA
  266. #ifndef WOLFCRYPT_ONLY
  267. #include <wolfssl/openssl/evp.h>
  268. #endif
  269. #include <wolfssl/openssl/rand.h>
  270. #include <wolfssl/openssl/hmac.h>
  271. #include <wolfssl/openssl/aes.h>
  272. #include <wolfssl/openssl/des.h>
  273. #endif
  274. #if defined(NO_FILESYSTEM)
  275. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  276. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  277. #define USE_CERT_BUFFERS_2048
  278. #endif
  279. #if !defined(USE_CERT_BUFFERS_256)
  280. #define USE_CERT_BUFFERS_256
  281. #endif
  282. #endif
  283. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  284. #define ENABLE_ECC384_CERT_GEN_TEST
  285. #endif
  286. #include <wolfssl/certs_test.h>
  287. #ifdef HAVE_NTRU
  288. #include "libntruencrypt/ntru_crypto.h"
  289. #endif
  290. #ifdef WOLFSSL_STATIC_MEMORY
  291. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  292. #else
  293. #define HEAP_HINT NULL
  294. #endif /* WOLFSSL_STATIC_MEMORY */
  295. /* these cases do not have intermediate hashing support */
  296. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  297. && !defined(WOLFSSL_XILINX_CRYPT)
  298. #define NO_INTM_HASH_TEST
  299. #endif
  300. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  301. static void initDefaultName(void);
  302. #endif
  303. /* for async devices */
  304. static int devId = INVALID_DEVID;
  305. #ifdef HAVE_WNR
  306. const char* wnrConfigFile = "wnr-example.conf";
  307. #endif
  308. #define TEST_STRING "Everyone gets Friday off."
  309. #define TEST_STRING_SZ 25
  310. typedef struct testVector {
  311. const char* input;
  312. const char* output;
  313. size_t inLen;
  314. size_t outLen;
  315. } testVector;
  316. #ifndef WOLFSSL_TEST_SUBROUTINE
  317. #define WOLFSSL_TEST_SUBROUTINE
  318. #endif
  319. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  320. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  321. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  322. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  323. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  324. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  325. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  326. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  327. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  328. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  329. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  330. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  331. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  332. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  333. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  334. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  335. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  336. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  337. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  338. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  339. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  340. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  341. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void);
  342. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  343. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  344. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  345. WOLFSSL_TEST_SUBROUTINE int hc128_test(void);
  346. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void);
  347. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  348. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  349. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  350. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  351. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  352. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  353. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  354. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  355. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  356. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  357. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  358. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  359. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  360. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  361. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  362. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  363. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  364. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  365. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  366. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  367. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  368. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  369. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  370. #ifndef WC_NO_RNG
  371. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  372. #endif /* WC_NO_RNG */
  373. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  375. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  376. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  377. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  381. #endif
  382. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  386. #ifdef HAVE_ECC
  387. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  388. #ifdef HAVE_ECC_ENCRYPT
  389. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  390. #endif
  391. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  392. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  393. /* skip for ATECC508/608A, cannot import private key buffers */
  394. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  395. #endif
  396. #endif
  397. #ifdef HAVE_CURVE25519
  398. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  399. #endif
  400. #ifdef HAVE_ED25519
  401. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  402. #endif
  403. #ifdef HAVE_CURVE448
  404. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  405. #endif
  406. #ifdef HAVE_ED448
  407. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  408. #endif
  409. #ifdef HAVE_BLAKE2
  410. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  411. #endif
  412. #ifdef HAVE_BLAKE2S
  413. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  414. #endif
  415. #ifdef HAVE_LIBZ
  416. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  417. #endif
  418. #ifdef HAVE_PKCS7
  419. #ifndef NO_PKCS7_ENCRYPTED_DATA
  420. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  421. #endif
  422. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  423. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  424. #endif
  425. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  426. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  427. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  428. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  429. #endif
  430. #ifndef NO_AES
  431. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  432. word32 keySz);
  433. #endif
  434. #endif
  435. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  436. !defined(NO_FILESYSTEM)
  437. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  438. #endif
  439. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  440. !defined(NO_FILESYSTEM)
  441. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  442. #endif
  443. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  444. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  445. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  446. #endif
  447. #ifdef HAVE_IDEA
  448. WOLFSSL_TEST_SUBROUTINE int idea_test(void);
  449. #endif
  450. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  451. #ifdef HAVE_VALGRIND
  452. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  453. #endif
  454. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  455. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  456. #endif
  457. #ifdef ASN_BER_TO_DER
  458. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  459. #endif
  460. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  461. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  462. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  463. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  464. #endif
  465. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  466. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  467. #endif
  468. #ifdef WOLF_CRYPTO_CB
  469. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  470. #endif
  471. #ifdef WOLFSSL_CERT_PIV
  472. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  473. #endif
  474. /* General big buffer size for many tests. */
  475. #define FOURK_BUF 4096
  476. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  477. #ifdef HAVE_STACK_SIZE
  478. static THREAD_RETURN err_sys(const char* msg, int es)
  479. #else
  480. static int err_sys(const char* msg, int es)
  481. #endif
  482. {
  483. (void)msg;
  484. (void)es;
  485. #ifdef WOLFSSL_LINUXKM
  486. lkm_printf("%s error = %d\n", msg, es);
  487. EXIT_TEST(es);
  488. #else
  489. printf("%s error = %d\n", msg, es);
  490. EXIT_TEST(-1);
  491. #endif
  492. }
  493. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  494. /* func_args from test.h, so don't have to pull in other stuff */
  495. typedef struct func_args {
  496. int argc;
  497. char** argv;
  498. int return_code;
  499. } func_args;
  500. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  501. #ifdef HAVE_FIPS
  502. static void myFipsCb(int ok, int err, const char* hash)
  503. {
  504. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  505. printf("message = %s\n", wc_GetErrorString(err));
  506. printf("hash = %s\n", hash);
  507. if (err == IN_CORE_FIPS_E) {
  508. printf("In core integrity hash check failure, copy above hash\n");
  509. printf("into verifyCore[] in fips_test.c and rebuild\n");
  510. }
  511. }
  512. #endif /* HAVE_FIPS */
  513. #ifdef WOLFSSL_STATIC_MEMORY
  514. #ifdef BENCH_EMBEDDED
  515. static byte gTestMemory[14000];
  516. #elif defined(WOLFSSL_CERT_EXT)
  517. static byte gTestMemory[140000];
  518. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  519. static byte gTestMemory[160000];
  520. #else
  521. static byte gTestMemory[80000];
  522. #endif
  523. #endif
  524. #ifdef WOLFSSL_PB
  525. static int wolfssl_pb_print(const char* msg, ...)
  526. {
  527. int ret;
  528. va_list args;
  529. char tmpBuf[80];
  530. va_start(args, msg);
  531. ret = vsprint(tmpBuf, msg, args);
  532. va_end(args);
  533. fnDumpStringToSystemLog(tmpBuf);
  534. return ret;
  535. }
  536. #endif /* WOLFSSL_PB */
  537. /* optional macro to add sleep between tests */
  538. #ifdef TEST_SLEEP
  539. #include <stdarg.h> /* for var args */
  540. static WC_INLINE void test_pass(const char* fmt, ...)
  541. {
  542. va_list args;
  543. va_start(args, fmt);
  544. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  545. va_end(args);
  546. PRINT_HEAP_CHECKPOINT();
  547. TEST_SLEEP();
  548. }
  549. #else
  550. /* redirect to printf */
  551. #define test_pass(...) { \
  552. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  553. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  554. return err_sys("post-test check failed", -1); \
  555. } \
  556. PRINT_HEAP_CHECKPOINT(); \
  557. }
  558. /* stub the sleep macro */
  559. #define TEST_SLEEP()
  560. #endif
  561. #ifdef HAVE_STACK_SIZE
  562. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  563. #else
  564. int wolfcrypt_test(void* args)
  565. #endif
  566. {
  567. int ret;
  568. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  569. long heap_baselineAllocs, heap_baselineBytes;
  570. #endif
  571. STACK_SIZE_INIT();
  572. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  573. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  574. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  575. (void)wolfCrypt_heap_peakBytes_checkpoint();
  576. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  577. #endif
  578. printf("------------------------------------------------------------------------------\n");
  579. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  580. printf("------------------------------------------------------------------------------\n");
  581. if (args) {
  582. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  583. int ch;
  584. #endif
  585. ((func_args*)args)->return_code = -1; /* error state */
  586. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  587. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  588. switch(ch) {
  589. case 's':
  590. #ifdef HAVE_STACK_SIZE_VERBOSE
  591. max_relative_stack = (ssize_t)atoi(myoptarg);
  592. break;
  593. #else
  594. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  595. #endif
  596. case 'm':
  597. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  598. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  599. break;
  600. #else
  601. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  602. #endif
  603. case 'a':
  604. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  605. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  606. break;
  607. #else
  608. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  609. #endif
  610. case 'h':
  611. return err_sys("\
  612. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  613. [-a max_relative_heap_allocs] [-h]\n", 0);
  614. default:
  615. return err_sys("unknown test option. try -h.", -1);
  616. }
  617. }
  618. #endif
  619. }
  620. #ifdef WOLFSSL_STATIC_MEMORY
  621. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  622. WOLFMEM_GENERAL, 1) != 0) {
  623. printf("unable to load static memory.\n");
  624. return(EXIT_FAILURE);
  625. }
  626. #endif
  627. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  628. wolfSSL_Debugging_ON();
  629. #endif
  630. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  631. wc_SetLoggingHeap(HEAP_HINT);
  632. #endif
  633. #ifdef HAVE_FIPS
  634. wolfCrypt_SetCb_fips(myFipsCb);
  635. #endif
  636. #if !defined(NO_BIG_INT)
  637. if (CheckCtcSettings() != 1) {
  638. printf("Sizeof mismatch (build) %x != (run) %x\n",
  639. CTC_SETTINGS, CheckRunTimeSettings());
  640. return err_sys("Build vs runtime math mismatch\n", -1000);
  641. }
  642. #if defined(USE_FAST_MATH) && \
  643. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  644. if (CheckFastMathSettings() != 1)
  645. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  646. -1001);
  647. #endif /* USE_FAST_MATH */
  648. #endif /* !NO_BIG_INT */
  649. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  650. initDefaultName();
  651. #endif
  652. #ifdef WOLFSSL_ASYNC_CRYPT
  653. ret = wolfAsync_DevOpen(&devId);
  654. if (ret < 0) {
  655. printf("Async device open failed\nRunning without async\n");
  656. }
  657. #else
  658. (void)devId;
  659. #endif /* WOLFSSL_ASYNC_CRYPT */
  660. #ifdef WOLF_CRYPTO_CB
  661. #ifdef HAVE_INTEL_QA_SYNC
  662. devId = wc_CryptoCb_InitIntelQa();
  663. if (INVALID_DEVID == devId) {
  664. printf("Couldn't init the Intel QA\n");
  665. }
  666. #endif
  667. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  668. devId = wc_CryptoCb_InitOcteon();
  669. if (INVALID_DEVID == devId) {
  670. printf("Couldn't init the Cavium Octeon\n");
  671. }
  672. #endif
  673. #endif
  674. #ifdef HAVE_SELFTEST
  675. if ( (ret = wolfCrypt_SelfTest()) != 0)
  676. return err_sys("CAVP selftest failed!\n", ret);
  677. else
  678. test_pass("CAVP selftest passed!\n");
  679. #endif
  680. if ( (ret = error_test()) != 0)
  681. return err_sys("error test failed!\n", ret);
  682. else
  683. test_pass("error test passed!\n");
  684. if ( (ret = memory_test()) != 0)
  685. return err_sys("MEMORY test failed!\n", ret);
  686. else
  687. test_pass("MEMORY test passed!\n");
  688. #ifndef NO_CODING
  689. if ( (ret = base64_test()) != 0)
  690. return err_sys("base64 test failed!\n", ret);
  691. else
  692. test_pass("base64 test passed!\n");
  693. #ifdef WOLFSSL_BASE16
  694. if ( (ret = base16_test()) != 0)
  695. return err_sys("base16 test failed!\n", ret);
  696. else
  697. test_pass("base16 test passed!\n");
  698. #endif
  699. #endif /* !NO_CODING */
  700. #ifndef NO_ASN
  701. if ( (ret = asn_test()) != 0)
  702. return err_sys("asn test failed!\n", ret);
  703. else
  704. test_pass("asn test passed!\n");
  705. #endif
  706. #ifndef WC_NO_RNG
  707. if ( (ret = random_test()) != 0)
  708. return err_sys("RANDOM test failed!\n", ret);
  709. else
  710. test_pass("RANDOM test passed!\n");
  711. #endif /* WC_NO_RNG */
  712. #ifndef NO_MD5
  713. if ( (ret = md5_test()) != 0)
  714. return err_sys("MD5 test failed!\n", ret);
  715. else
  716. test_pass("MD5 test passed!\n");
  717. #endif
  718. #ifdef WOLFSSL_MD2
  719. if ( (ret = md2_test()) != 0)
  720. return err_sys("MD2 test failed!\n", ret);
  721. else
  722. test_pass("MD2 test passed!\n");
  723. #endif
  724. #ifndef NO_MD4
  725. if ( (ret = md4_test()) != 0)
  726. return err_sys("MD4 test failed!\n", ret);
  727. else
  728. test_pass("MD4 test passed!\n");
  729. #endif
  730. #ifndef NO_SHA
  731. if ( (ret = sha_test()) != 0)
  732. return err_sys("SHA test failed!\n", ret);
  733. else
  734. test_pass("SHA test passed!\n");
  735. #endif
  736. #ifdef WOLFSSL_SHA224
  737. if ( (ret = sha224_test()) != 0)
  738. return err_sys("SHA-224 test failed!\n", ret);
  739. else
  740. test_pass("SHA-224 test passed!\n");
  741. #endif
  742. #ifndef NO_SHA256
  743. if ( (ret = sha256_test()) != 0)
  744. return err_sys("SHA-256 test failed!\n", ret);
  745. else
  746. test_pass("SHA-256 test passed!\n");
  747. #endif
  748. #ifdef WOLFSSL_SHA384
  749. if ( (ret = sha384_test()) != 0)
  750. return err_sys("SHA-384 test failed!\n", ret);
  751. else
  752. test_pass("SHA-384 test passed!\n");
  753. #endif
  754. #ifdef WOLFSSL_SHA512
  755. if ( (ret = sha512_test()) != 0)
  756. return err_sys("SHA-512 test failed!\n", ret);
  757. else
  758. test_pass("SHA-512 test passed!\n");
  759. #endif
  760. #ifdef WOLFSSL_SHA3
  761. if ( (ret = sha3_test()) != 0)
  762. return err_sys("SHA-3 test failed!\n", ret);
  763. else
  764. test_pass("SHA-3 test passed!\n");
  765. #endif
  766. #ifdef WOLFSSL_SHAKE256
  767. if ( (ret = shake256_test()) != 0)
  768. return err_sys("SHAKE256 test failed!\n", ret);
  769. else
  770. test_pass("SHAKE256 test passed!\n");
  771. #endif
  772. if ( (ret = hash_test()) != 0)
  773. return err_sys("Hash test failed!\n", ret);
  774. else
  775. test_pass("Hash test passed!\n");
  776. #ifdef WOLFSSL_RIPEMD
  777. if ( (ret = ripemd_test()) != 0)
  778. return err_sys("RIPEMD test failed!\n", ret);
  779. else
  780. test_pass("RIPEMD test passed!\n");
  781. #endif
  782. #ifdef HAVE_BLAKE2
  783. if ( (ret = blake2b_test()) != 0)
  784. return err_sys("BLAKE2b test failed!\n", ret);
  785. else
  786. test_pass("BLAKE2b test passed!\n");
  787. #endif
  788. #ifdef HAVE_BLAKE2S
  789. if ( (ret = blake2s_test()) != 0)
  790. return err_sys("BLAKE2s test failed!\n", ret);
  791. else
  792. test_pass("BLAKE2s test passed!\n");
  793. #endif
  794. #ifndef NO_HMAC
  795. #ifndef NO_MD5
  796. if ( (ret = hmac_md5_test()) != 0)
  797. return err_sys("HMAC-MD5 test failed!\n", ret);
  798. else
  799. test_pass("HMAC-MD5 test passed!\n");
  800. #endif
  801. #ifndef NO_SHA
  802. if ( (ret = hmac_sha_test()) != 0)
  803. return err_sys("HMAC-SHA test failed!\n", ret);
  804. else
  805. test_pass("HMAC-SHA test passed!\n");
  806. #endif
  807. #ifdef WOLFSSL_SHA224
  808. if ( (ret = hmac_sha224_test()) != 0)
  809. return err_sys("HMAC-SHA224 test failed!\n", ret);
  810. else
  811. test_pass("HMAC-SHA224 test passed!\n");
  812. #endif
  813. #ifndef NO_SHA256
  814. if ( (ret = hmac_sha256_test()) != 0)
  815. return err_sys("HMAC-SHA256 test failed!\n", ret);
  816. else
  817. test_pass("HMAC-SHA256 test passed!\n");
  818. #endif
  819. #ifdef WOLFSSL_SHA384
  820. if ( (ret = hmac_sha384_test()) != 0)
  821. return err_sys("HMAC-SHA384 test failed!\n", ret);
  822. else
  823. test_pass("HMAC-SHA384 test passed!\n");
  824. #endif
  825. #ifdef WOLFSSL_SHA512
  826. if ( (ret = hmac_sha512_test()) != 0)
  827. return err_sys("HMAC-SHA512 test failed!\n", ret);
  828. else
  829. test_pass("HMAC-SHA512 test passed!\n");
  830. #endif
  831. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  832. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  833. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  834. if ( (ret = hmac_sha3_test()) != 0)
  835. return err_sys("HMAC-SHA3 test failed!\n", ret);
  836. else
  837. test_pass("HMAC-SHA3 test passed!\n");
  838. #endif
  839. #ifdef HAVE_HKDF
  840. if ( (ret = hkdf_test()) != 0)
  841. return err_sys("HMAC-KDF test failed!\n", ret);
  842. else
  843. test_pass("HMAC-KDF test passed!\n");
  844. #endif
  845. #endif /* !NO_HMAC */
  846. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  847. if ( (ret = x963kdf_test()) != 0)
  848. return err_sys("X963-KDF test failed!\n", ret);
  849. else
  850. test_pass("X963-KDF test passed!\n");
  851. #endif
  852. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  853. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  854. if ( (ret = gmac_test()) != 0)
  855. return err_sys("GMAC test failed!\n", ret);
  856. else
  857. test_pass("GMAC test passed!\n");
  858. #endif
  859. #ifdef WC_RC2
  860. if ( (ret = rc2_test()) != 0)
  861. return err_sys("RC2 test failed!\n", ret);
  862. else
  863. test_pass("RC2 test passed!\n");
  864. #endif
  865. #ifndef NO_RC4
  866. if ( (ret = arc4_test()) != 0)
  867. return err_sys("ARC4 test failed!\n", ret);
  868. else
  869. test_pass("ARC4 test passed!\n");
  870. #endif
  871. #ifndef NO_HC128
  872. if ( (ret = hc128_test()) != 0)
  873. return err_sys("HC-128 test failed!\n", ret);
  874. else
  875. test_pass("HC-128 test passed!\n");
  876. #endif
  877. #ifndef NO_RABBIT
  878. if ( (ret = rabbit_test()) != 0)
  879. return err_sys("Rabbit test failed!\n", ret);
  880. else
  881. test_pass("Rabbit test passed!\n");
  882. #endif
  883. #ifdef HAVE_CHACHA
  884. if ( (ret = chacha_test()) != 0)
  885. return err_sys("Chacha test failed!\n", ret);
  886. else
  887. test_pass("Chacha test passed!\n");
  888. #endif
  889. #ifdef HAVE_XCHACHA
  890. if ( (ret = XChaCha_test()) != 0)
  891. return err_sys("XChacha test failed!\n", ret);
  892. else
  893. test_pass("XChacha test passed!\n");
  894. #endif
  895. #ifdef HAVE_POLY1305
  896. if ( (ret = poly1305_test()) != 0)
  897. return err_sys("POLY1305 test failed!\n", ret);
  898. else
  899. test_pass("POLY1305 test passed!\n");
  900. #endif
  901. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  902. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  903. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  904. else
  905. test_pass("ChaCha20-Poly1305 AEAD test passed!\n");
  906. #endif
  907. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  908. if ( (ret = XChaCha20Poly1305_test()) != 0)
  909. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  910. else
  911. test_pass("XChaCha20-Poly1305 AEAD test passed!\n");
  912. #endif
  913. #ifndef NO_DES3
  914. if ( (ret = des_test()) != 0)
  915. return err_sys("DES test failed!\n", ret);
  916. else
  917. test_pass("DES test passed!\n");
  918. #endif
  919. #ifndef NO_DES3
  920. if ( (ret = des3_test()) != 0)
  921. return err_sys("DES3 test failed!\n", ret);
  922. else
  923. test_pass("DES3 test passed!\n");
  924. #endif
  925. #ifndef NO_AES
  926. if ( (ret = aes_test()) != 0)
  927. return err_sys("AES test failed!\n", ret);
  928. else
  929. test_pass("AES test passed!\n");
  930. #ifdef WOLFSSL_AES_192
  931. if ( (ret = aes192_test()) != 0)
  932. return err_sys("AES192 test failed!\n", ret);
  933. else
  934. test_pass("AES192 test passed!\n");
  935. #endif
  936. #ifdef WOLFSSL_AES_256
  937. if ( (ret = aes256_test()) != 0)
  938. return err_sys("AES256 test failed!\n", ret);
  939. else
  940. test_pass("AES256 test passed!\n");
  941. #endif
  942. #ifdef WOLFSSL_AES_OFB
  943. if ( (ret = aesofb_test()) != 0)
  944. return err_sys("AES-OFB test failed!\n", ret);
  945. else
  946. test_pass("AESOFB test passed!\n");
  947. #endif
  948. #ifdef HAVE_AESGCM
  949. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  950. if ( (ret = aesgcm_test()) != 0)
  951. return err_sys("AES-GCM test failed!\n", ret);
  952. #endif
  953. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  954. !(defined(WOLF_CRYPTO_CB) && \
  955. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  956. if ((ret = aesgcm_default_test()) != 0) {
  957. return err_sys("AES-GCM test failed!\n", ret);
  958. }
  959. #endif
  960. test_pass("AES-GCM test passed!\n");
  961. #endif
  962. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  963. if ( (ret = aesccm_test()) != 0)
  964. return err_sys("AES-CCM test failed!\n", ret);
  965. else
  966. test_pass("AES-CCM test passed!\n");
  967. #endif
  968. #ifdef HAVE_AES_KEYWRAP
  969. if ( (ret = aeskeywrap_test()) != 0)
  970. return err_sys("AES Key Wrap test failed!\n", ret);
  971. else
  972. test_pass("AES Key Wrap test passed!\n");
  973. #endif
  974. #endif
  975. #ifdef HAVE_CAMELLIA
  976. if ( (ret = camellia_test()) != 0)
  977. return err_sys("CAMELLIA test failed!\n", ret);
  978. else
  979. test_pass("CAMELLIA test passed!\n");
  980. #endif
  981. #ifdef HAVE_IDEA
  982. if ( (ret = idea_test()) != 0)
  983. return err_sys("IDEA test failed!\n", ret);
  984. else
  985. test_pass("IDEA test passed!\n");
  986. #endif
  987. #ifndef NO_RSA
  988. #ifdef WC_RSA_NO_PADDING
  989. if ( (ret = rsa_no_pad_test()) != 0)
  990. return err_sys("RSA NOPAD test failed!\n", ret);
  991. else
  992. test_pass("RSA NOPAD test passed!\n");
  993. #endif
  994. if ( (ret = rsa_test()) != 0)
  995. return err_sys("RSA test failed!\n", ret);
  996. else
  997. test_pass("RSA test passed!\n");
  998. #endif
  999. #ifndef NO_DH
  1000. if ( (ret = dh_test()) != 0)
  1001. return err_sys("DH test failed!\n", ret);
  1002. else
  1003. test_pass("DH test passed!\n");
  1004. #endif
  1005. #ifndef NO_DSA
  1006. if ( (ret = dsa_test()) != 0)
  1007. return err_sys("DSA test failed!\n", ret);
  1008. else
  1009. test_pass("DSA test passed!\n");
  1010. #endif
  1011. #ifdef WOLFCRYPT_HAVE_SRP
  1012. if ( (ret = srp_test()) != 0)
  1013. return err_sys("SRP test failed!\n", ret);
  1014. else
  1015. test_pass("SRP test passed!\n");
  1016. #endif
  1017. #ifndef NO_PWDBASED
  1018. if ( (ret = pwdbased_test()) != 0)
  1019. return err_sys("PWDBASED test failed!\n", ret);
  1020. else
  1021. test_pass("PWDBASED test passed!\n");
  1022. #endif
  1023. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1024. if ( (ret = openssl_test()) != 0)
  1025. return err_sys("OPENSSL test failed!\n", ret);
  1026. else
  1027. test_pass("OPENSSL test passed!\n");
  1028. if ( (ret = openSSL_evpMD_test()) != 0)
  1029. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1030. else
  1031. test_pass("OPENSSL (EVP MD) passed!\n");
  1032. if ( (ret = openssl_pkey0_test()) != 0)
  1033. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1034. else
  1035. test_pass("OPENSSL (PKEY0) passed!\n");
  1036. if ( (ret = openssl_pkey1_test()) != 0)
  1037. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1038. else
  1039. test_pass("OPENSSL (PKEY1) passed!\n");
  1040. if ( (ret = openssl_evpSig_test()) != 0)
  1041. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1042. else
  1043. test_pass("OPENSSL (EVP Sign/Verify) passed!\n");
  1044. #endif
  1045. #ifdef HAVE_ECC
  1046. if ( (ret = ecc_test()) != 0)
  1047. return err_sys("ECC test failed!\n", ret);
  1048. else
  1049. test_pass("ECC test passed!\n");
  1050. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  1051. if ( (ret = ecc_encrypt_test()) != 0)
  1052. return err_sys("ECC Enc test failed!\n", ret);
  1053. else
  1054. test_pass("ECC Enc test passed!\n");
  1055. #endif
  1056. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1057. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256)
  1058. /* skip for ATECC508/608A, cannot import private key buffers */
  1059. if ( (ret = ecc_test_buffers()) != 0)
  1060. return err_sys("ECC buffer test failed!\n", ret);
  1061. else
  1062. test_pass("ECC buffer test passed!\n");
  1063. #endif
  1064. #endif
  1065. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1066. !defined(NO_FILESYSTEM)
  1067. if ( (ret = cert_test()) != 0)
  1068. return err_sys("CERT test failed!\n", ret);
  1069. else
  1070. test_pass("CERT test passed!\n");
  1071. #endif
  1072. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1073. !defined(NO_FILESYSTEM)
  1074. if ( (ret = certext_test()) != 0)
  1075. return err_sys("CERT EXT test failed!\n", ret);
  1076. else
  1077. test_pass("CERT EXT test passed!\n");
  1078. #endif
  1079. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1080. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1081. if ( (ret = decodedCertCache_test()) != 0)
  1082. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1083. else
  1084. test_pass("DECODED CERT CACHE test passed!\n");
  1085. #endif
  1086. #ifdef HAVE_CURVE25519
  1087. if ( (ret = curve25519_test()) != 0)
  1088. return err_sys("CURVE25519 test failed!\n", ret);
  1089. else
  1090. test_pass("CURVE25519 test passed!\n");
  1091. #endif
  1092. #ifdef HAVE_ED25519
  1093. if ( (ret = ed25519_test()) != 0)
  1094. return err_sys("ED25519 test failed!\n", ret);
  1095. else
  1096. test_pass("ED25519 test passed!\n");
  1097. #endif
  1098. #ifdef HAVE_CURVE448
  1099. if ( (ret = curve448_test()) != 0)
  1100. return err_sys("CURVE448 test failed!\n", ret);
  1101. else
  1102. test_pass("CURVE448 test passed!\n");
  1103. #endif
  1104. #ifdef HAVE_ED448
  1105. if ( (ret = ed448_test()) != 0)
  1106. return err_sys("ED448 test failed!\n", ret);
  1107. else
  1108. test_pass("ED448 test passed!\n");
  1109. #endif
  1110. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1111. if ( (ret = cmac_test()) != 0)
  1112. return err_sys("CMAC test failed!\n", ret);
  1113. else
  1114. test_pass("CMAC test passed!\n");
  1115. #endif
  1116. #ifdef HAVE_LIBZ
  1117. if ( (ret = compress_test()) != 0)
  1118. return err_sys("COMPRESS test failed!\n", ret);
  1119. else
  1120. test_pass("COMPRESS test passed!\n");
  1121. #endif
  1122. #ifdef HAVE_PKCS7
  1123. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1124. if ( (ret = pkcs7encrypted_test()) != 0)
  1125. return err_sys("PKCS7encrypted test failed!\n", ret);
  1126. else
  1127. test_pass("PKCS7encrypted test passed!\n");
  1128. #endif
  1129. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1130. if ( (ret = pkcs7compressed_test()) != 0)
  1131. return err_sys("PKCS7compressed test failed!\n", ret);
  1132. else
  1133. test_pass("PKCS7compressed test passed!\n");
  1134. #endif
  1135. if ( (ret = pkcs7signed_test()) != 0)
  1136. return err_sys("PKCS7signed test failed!\n", ret);
  1137. else
  1138. test_pass("PKCS7signed test passed!\n");
  1139. if ( (ret = pkcs7enveloped_test()) != 0)
  1140. return err_sys("PKCS7enveloped test failed!\n", ret);
  1141. else
  1142. test_pass("PKCS7enveloped test passed!\n");
  1143. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1144. if ( (ret = pkcs7authenveloped_test()) != 0)
  1145. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1146. else
  1147. test_pass("PKCS7authenveloped test passed!\n");
  1148. #endif
  1149. #endif
  1150. #ifdef HAVE_VALGRIND
  1151. if ( (ret = mp_test()) != 0)
  1152. return err_sys("mp test failed!\n", ret);
  1153. else
  1154. test_pass("mp test passed!\n");
  1155. #endif
  1156. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1157. if ( (ret = prime_test()) != 0)
  1158. return err_sys("prime test failed!\n", ret);
  1159. else
  1160. test_pass("prime test passed!\n");
  1161. #endif
  1162. #if defined(ASN_BER_TO_DER) && \
  1163. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1164. defined(OPENSSL_EXTRA_X509_SMALL))
  1165. if ( (ret = berder_test()) != 0)
  1166. return err_sys("ber-der test failed!\n", ret);
  1167. else
  1168. test_pass("ber-der test passed!\n");
  1169. #endif
  1170. if ( (ret = logging_test()) != 0)
  1171. return err_sys("logging test failed!\n", ret);
  1172. else
  1173. test_pass("logging test passed!\n");
  1174. if ( (ret = mutex_test()) != 0)
  1175. return err_sys("mutex test failed!\n", ret);
  1176. else
  1177. test_pass("mutex test passed!\n");
  1178. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1179. if ( (ret = memcb_test()) != 0)
  1180. return err_sys("memcb test failed!\n", ret);
  1181. else
  1182. test_pass("memcb test passed!\n");
  1183. #endif
  1184. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1185. if ( (ret = blob_test()) != 0)
  1186. return err_sys("blob test failed!\n", ret);
  1187. else
  1188. test_pass("blob test passed!\n");
  1189. #endif
  1190. #if defined(WOLF_CRYPTO_CB) && \
  1191. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  1192. if ( (ret = cryptocb_test()) != 0)
  1193. return err_sys("crypto callback test failed!\n", ret);
  1194. else
  1195. test_pass("crypto callback test passed!\n");
  1196. #endif
  1197. #ifdef WOLFSSL_CERT_PIV
  1198. if ( (ret = certpiv_test()) != 0)
  1199. return err_sys("cert piv test failed!\n", ret);
  1200. else
  1201. test_pass("cert piv test passed!\n");
  1202. #endif
  1203. #ifdef WOLF_CRYPTO_CB
  1204. #ifdef HAVE_INTEL_QA_SYNC
  1205. wc_CryptoCb_CleanupIntelQa(&devId);
  1206. #endif
  1207. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1208. wc_CryptoCb_CleanupOcteon(&devId);
  1209. #endif
  1210. #endif
  1211. #ifdef WOLFSSL_ASYNC_CRYPT
  1212. wolfAsync_DevClose(&devId);
  1213. #endif
  1214. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1215. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1216. wc_ecc_fp_free();
  1217. #endif
  1218. if (args)
  1219. ((func_args*)args)->return_code = ret;
  1220. test_pass("Test complete\n");
  1221. EXIT_TEST(ret);
  1222. }
  1223. #ifndef NO_MAIN_DRIVER
  1224. /* so overall tests can pull in test function */
  1225. #ifdef WOLFSSL_ESPIDF
  1226. void app_main( )
  1227. #else
  1228. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1229. int myoptind = 0;
  1230. char* myoptarg = NULL;
  1231. #endif
  1232. int main(int argc, char** argv)
  1233. #endif
  1234. {
  1235. int ret;
  1236. func_args args;
  1237. #ifdef WOLFSSL_ESPIDF
  1238. /* set dummy wallclock time. */
  1239. struct timeval utctime;
  1240. struct timezone tz;
  1241. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1242. utctime.tv_usec = 0;
  1243. tz.tz_minuteswest = 0;
  1244. tz.tz_dsttime = 0;
  1245. settimeofday(&utctime, &tz);
  1246. #endif
  1247. #ifdef WOLFSSL_APACHE_MYNEWT
  1248. #ifdef ARCH_sim
  1249. mcu_sim_parse_args(argc, argv);
  1250. #endif
  1251. sysinit();
  1252. /* set dummy wallclock time. */
  1253. struct os_timeval utctime;
  1254. struct os_timezone tz;
  1255. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1256. utctime.tv_usec = 0;
  1257. tz.tz_minuteswest = 0;
  1258. tz.tz_dsttime = 0;
  1259. os_settimeofday(&utctime, &tz);
  1260. #endif
  1261. #ifdef HAVE_WNR
  1262. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1263. err_sys("Whitewood netRandom global config failed", -1001);
  1264. return -1002;
  1265. }
  1266. #endif
  1267. #ifndef WOLFSSL_ESPIDF
  1268. args.argc = argc;
  1269. args.argv = argv;
  1270. #endif
  1271. if ((ret = wolfCrypt_Init()) != 0) {
  1272. printf("wolfCrypt_Init failed %d\n", ret);
  1273. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1274. }
  1275. #ifdef HAVE_STACK_SIZE
  1276. StackSizeCheck(&args, wolfcrypt_test);
  1277. #else
  1278. wolfcrypt_test(&args);
  1279. #endif
  1280. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1281. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1282. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1283. }
  1284. #ifdef HAVE_WNR
  1285. if (wc_FreeNetRandom() < 0)
  1286. err_sys("Failed to free netRandom context", -1005);
  1287. #endif /* HAVE_WNR */
  1288. #ifndef WOLFSSL_ESPIDF
  1289. return args.return_code;
  1290. #endif
  1291. }
  1292. #endif /* NO_MAIN_DRIVER */
  1293. /* helper to save DER, convert to PEM and save PEM */
  1294. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1295. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))))
  1296. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1297. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1298. #else
  1299. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1300. #endif
  1301. static int _SaveDerAndPem(const byte* der, int derSz,
  1302. const char* fileDer, const char* filePem, int pemType, int errBase)
  1303. {
  1304. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1305. int ret;
  1306. XFILE derFile;
  1307. derFile = XFOPEN(fileDer, "wb");
  1308. if (!derFile) {
  1309. return errBase + 0;
  1310. }
  1311. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1312. XFCLOSE(derFile);
  1313. if (ret != derSz) {
  1314. return errBase + 1;
  1315. }
  1316. #endif
  1317. #ifdef WOLFSSL_DER_TO_PEM
  1318. if (filePem) {
  1319. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1320. XFILE pemFile;
  1321. #endif
  1322. byte* pem;
  1323. int pemSz;
  1324. /* calculate PEM size */
  1325. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1326. if (pemSz < 0) {
  1327. return pemSz;
  1328. }
  1329. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1330. if (pem == NULL) {
  1331. return MEMORY_E;
  1332. }
  1333. /* Convert to PEM */
  1334. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1335. if (pemSz < 0) {
  1336. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1337. return errBase + 2;
  1338. }
  1339. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1340. pemFile = XFOPEN(filePem, "wb");
  1341. if (!pemFile) {
  1342. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1343. return errBase + 3;
  1344. }
  1345. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1346. XFCLOSE(pemFile);
  1347. if (ret != pemSz) {
  1348. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1349. return errBase + 4;
  1350. }
  1351. #endif
  1352. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1353. }
  1354. #endif /* WOLFSSL_DER_TO_PEM */
  1355. /* suppress unused variable warnings */
  1356. (void)der;
  1357. (void)derSz;
  1358. (void)filePem;
  1359. (void)fileDer;
  1360. (void)pemType;
  1361. (void)errBase;
  1362. return 0;
  1363. }
  1364. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1365. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1366. {
  1367. const char* errStr;
  1368. char out[WOLFSSL_MAX_ERROR_SZ];
  1369. const char* unknownStr = wc_GetErrorString(0);
  1370. #ifdef NO_ERROR_STRINGS
  1371. /* Ensure a valid error code's string matches an invalid code's.
  1372. * The string is that error strings are not available.
  1373. */
  1374. errStr = wc_GetErrorString(OPEN_RAN_E);
  1375. wc_ErrorString(OPEN_RAN_E, out);
  1376. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1377. return -1100;
  1378. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1379. return -1101;
  1380. #else
  1381. int i;
  1382. int j = 0;
  1383. /* Values that are not or no longer error codes. */
  1384. int missing[] = { -122, -123, -124, -127, -128, -129,
  1385. -163, -164, -165, -166, -167, -168, -169,
  1386. -179, -233,
  1387. 0 };
  1388. /* Check that all errors have a string and it's the same through the two
  1389. * APIs. Check that the values that are not errors map to the unknown
  1390. * string.
  1391. */
  1392. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1393. errStr = wc_GetErrorString(i);
  1394. wc_ErrorString(i, out);
  1395. if (i != missing[j]) {
  1396. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  1397. return -1102;
  1398. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  1399. return -1103;
  1400. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  1401. return -1104;
  1402. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1403. return -1105;
  1404. }
  1405. else {
  1406. j++;
  1407. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1408. return -1106;
  1409. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1410. return -1107;
  1411. }
  1412. }
  1413. /* Check if the next possible value has been given a string. */
  1414. errStr = wc_GetErrorString(i);
  1415. wc_ErrorString(i, out);
  1416. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1417. return -1108;
  1418. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1419. return -1109;
  1420. #endif
  1421. return 0;
  1422. }
  1423. #ifndef NO_CODING
  1424. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1425. {
  1426. int ret;
  1427. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1428. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1429. byte out[128];
  1430. word32 outLen;
  1431. #ifdef WOLFSSL_BASE64_ENCODE
  1432. byte data[3];
  1433. word32 dataLen;
  1434. byte longData[79] = { 0 };
  1435. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1436. #endif
  1437. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA Gdj=";
  1438. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1439. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd AA";
  1440. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1441. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1442. byte goodChar[] =
  1443. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1444. "abcdefghijklmnopqrstuvwxyz"
  1445. "0123456789+/;";
  1446. byte charTest[] = "A+Gd\0\0\0";
  1447. int i;
  1448. /* Good Base64 encodings. */
  1449. outLen = sizeof(out);
  1450. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1451. if (ret != 0)
  1452. return -1200;
  1453. outLen = sizeof(out);
  1454. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1455. if (ret != 0)
  1456. return -1201;
  1457. outLen = sizeof(goodChar);
  1458. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1459. if (ret != 0)
  1460. return -1235;
  1461. if (outLen != 64 / 4 * 3)
  1462. return -1236;
  1463. /* Bad parameters. */
  1464. outLen = 1;
  1465. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1466. if (ret != BAD_FUNC_ARG)
  1467. return -1202;
  1468. outLen = sizeof(out);
  1469. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1470. if (ret != ASN_INPUT_E)
  1471. return -1203;
  1472. outLen = sizeof(out);
  1473. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1474. if (ret != ASN_INPUT_E)
  1475. return -1203;
  1476. /* Bad character at each offset 0-3. */
  1477. for (i = 0; i < 4; i++) {
  1478. outLen = sizeof(out);
  1479. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1480. if (ret != ASN_INPUT_E)
  1481. return -1204 - i;
  1482. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1483. if (ret != ASN_INPUT_E)
  1484. return -1214 - i;
  1485. }
  1486. /* Invalid character less than 0x2b */
  1487. for (i = 1; i < 0x2b; i++) {
  1488. outLen = sizeof(out);
  1489. charTest[0] = i;
  1490. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1491. if (ret != ASN_INPUT_E)
  1492. return -1240 - i;
  1493. }
  1494. /* Bad characters in range 0x2b - 0x7a. */
  1495. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1496. outLen = sizeof(out);
  1497. charTest[0] = badChar[i];
  1498. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1499. if (ret != ASN_INPUT_E)
  1500. return -1270 - i;
  1501. }
  1502. /* Invalid character greater than 0x7a */
  1503. for (i = 0x7b; i < 0x100; i++) {
  1504. outLen = sizeof(out);
  1505. charTest[0] = i;
  1506. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1507. if (ret != ASN_INPUT_E)
  1508. return -1290 - i;
  1509. }
  1510. #ifdef WOLFSSL_BASE64_ENCODE
  1511. /* Decode and encode all symbols - non-alphanumeric. */
  1512. dataLen = sizeof(data);
  1513. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1514. if (ret != 0)
  1515. return -1224;
  1516. outLen = sizeof(out);
  1517. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1518. if (ret != LENGTH_ONLY_E)
  1519. return -1225;
  1520. outLen = sizeof(out);
  1521. ret = Base64_Encode(data, dataLen, out, &outLen);
  1522. if (ret != 0)
  1523. return -1226;
  1524. outLen = 7;
  1525. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1526. if (ret != BUFFER_E)
  1527. return -1227;
  1528. outLen = sizeof(out);
  1529. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1530. if (ret != LENGTH_ONLY_E)
  1531. return -1228;
  1532. outLen = sizeof(out);
  1533. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1534. if (ret != 0)
  1535. return -1229;
  1536. outLen = sizeof(out);
  1537. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1538. if (ret != 0)
  1539. return -1230;
  1540. /* Data that results in an encoding longer than one line. */
  1541. outLen = sizeof(out);
  1542. dataLen = sizeof(longData);
  1543. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1544. if (ret != 0)
  1545. return -1231;
  1546. outLen = sizeof(out);
  1547. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1548. if (ret != 0)
  1549. return -1232;
  1550. outLen = sizeof(out);
  1551. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1552. if (ret != 0)
  1553. return -1233;
  1554. #endif
  1555. return 0;
  1556. }
  1557. #ifdef WOLFSSL_BASE16
  1558. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1559. {
  1560. int ret;
  1561. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1562. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1563. byte encoded[40];
  1564. word32 encodedLen;
  1565. byte plain[40];
  1566. word32 len;
  1567. /* length returned includes null termination */
  1568. encodedLen = sizeof(encoded);
  1569. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1570. if (ret != 0)
  1571. return -1300;
  1572. len = (word32)XSTRLEN((char*)encoded);
  1573. if (len != encodedLen - 1)
  1574. return -1301;
  1575. len = sizeof(plain);
  1576. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1577. if (ret != 0)
  1578. return -1302;
  1579. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1580. return -1303;
  1581. if (encodedLen != sizeof(encodedTestData) ||
  1582. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1583. return -1304;
  1584. }
  1585. return 0;
  1586. }
  1587. #endif /* WOLFSSL_BASE16 */
  1588. #endif /* !NO_CODING */
  1589. #ifndef NO_ASN
  1590. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1591. {
  1592. int ret;
  1593. /* ASN1 encoded date buffer */
  1594. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1595. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1596. byte format;
  1597. int length;
  1598. const byte* datePart;
  1599. #ifndef NO_ASN_TIME
  1600. struct tm timearg;
  1601. time_t now;
  1602. #endif
  1603. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1604. &length);
  1605. if (ret != 0)
  1606. return -1400;
  1607. #ifndef NO_ASN_TIME
  1608. /* Parameter Validation tests. */
  1609. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1610. return -1401;
  1611. if (wc_GetTime(&now, 0) != BUFFER_E)
  1612. return -1402;
  1613. now = 0;
  1614. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1615. return -1403;
  1616. }
  1617. if (now == 0) {
  1618. printf("RTC/Time not set!\n");
  1619. return -1404;
  1620. }
  1621. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1622. if (ret != 0)
  1623. return -1405;
  1624. #endif /* !NO_ASN_TIME */
  1625. return 0;
  1626. }
  1627. #endif /* !NO_ASN */
  1628. #ifdef WOLFSSL_MD2
  1629. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1630. {
  1631. Md2 md2;
  1632. byte hash[MD2_DIGEST_SIZE];
  1633. testVector a, b, c, d, e, f, g;
  1634. testVector test_md2[7];
  1635. int times = sizeof(test_md2) / sizeof(testVector), i;
  1636. a.input = "";
  1637. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1638. "\x27\x73";
  1639. a.inLen = XSTRLEN(a.input);
  1640. a.outLen = MD2_DIGEST_SIZE;
  1641. b.input = "a";
  1642. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1643. "\xb5\xd1";
  1644. b.inLen = XSTRLEN(b.input);
  1645. b.outLen = MD2_DIGEST_SIZE;
  1646. c.input = "abc";
  1647. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1648. "\xd6\xbb";
  1649. c.inLen = XSTRLEN(c.input);
  1650. c.outLen = MD2_DIGEST_SIZE;
  1651. d.input = "message digest";
  1652. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1653. "\x06\xb0";
  1654. d.inLen = XSTRLEN(d.input);
  1655. d.outLen = MD2_DIGEST_SIZE;
  1656. e.input = "abcdefghijklmnopqrstuvwxyz";
  1657. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1658. "\x94\x0b";
  1659. e.inLen = XSTRLEN(e.input);
  1660. e.outLen = MD2_DIGEST_SIZE;
  1661. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1662. "6789";
  1663. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1664. "\x38\xcd";
  1665. f.inLen = XSTRLEN(f.input);
  1666. f.outLen = MD2_DIGEST_SIZE;
  1667. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1668. "9012345678901234567890";
  1669. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1670. "\xef\xd8";
  1671. g.inLen = XSTRLEN(g.input);
  1672. g.outLen = MD2_DIGEST_SIZE;
  1673. test_md2[0] = a;
  1674. test_md2[1] = b;
  1675. test_md2[2] = c;
  1676. test_md2[3] = d;
  1677. test_md2[4] = e;
  1678. test_md2[5] = f;
  1679. test_md2[6] = g;
  1680. wc_InitMd2(&md2);
  1681. for (i = 0; i < times; ++i) {
  1682. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1683. wc_Md2Final(&md2, hash);
  1684. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1685. return -1500 - i;
  1686. }
  1687. return 0;
  1688. }
  1689. #endif
  1690. #ifndef NO_MD5
  1691. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1692. {
  1693. int ret = 0;
  1694. wc_Md5 md5, md5Copy;
  1695. byte hash[WC_MD5_DIGEST_SIZE];
  1696. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1697. testVector a, b, c, d, e, f;
  1698. testVector test_md5[6];
  1699. int times = sizeof(test_md5) / sizeof(testVector), i;
  1700. a.input = "";
  1701. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1702. "\x7e";
  1703. a.inLen = XSTRLEN(a.input);
  1704. a.outLen = WC_MD5_DIGEST_SIZE;
  1705. b.input = "abc";
  1706. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1707. "\x72";
  1708. b.inLen = XSTRLEN(b.input);
  1709. b.outLen = WC_MD5_DIGEST_SIZE;
  1710. c.input = "message digest";
  1711. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1712. "\xd0";
  1713. c.inLen = XSTRLEN(c.input);
  1714. c.outLen = WC_MD5_DIGEST_SIZE;
  1715. d.input = "abcdefghijklmnopqrstuvwxyz";
  1716. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1717. "\x3b";
  1718. d.inLen = XSTRLEN(d.input);
  1719. d.outLen = WC_MD5_DIGEST_SIZE;
  1720. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1721. "6789";
  1722. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1723. "\x9f";
  1724. e.inLen = XSTRLEN(e.input);
  1725. e.outLen = WC_MD5_DIGEST_SIZE;
  1726. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1727. "9012345678901234567890";
  1728. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1729. "\x7a";
  1730. f.inLen = XSTRLEN(f.input);
  1731. f.outLen = WC_MD5_DIGEST_SIZE;
  1732. test_md5[0] = a;
  1733. test_md5[1] = b;
  1734. test_md5[2] = c;
  1735. test_md5[3] = d;
  1736. test_md5[4] = e;
  1737. test_md5[5] = f;
  1738. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1739. if (ret != 0)
  1740. return -1600;
  1741. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1742. if (ret != 0) {
  1743. wc_Md5Free(&md5);
  1744. return -1601;
  1745. }
  1746. for (i = 0; i < times; ++i) {
  1747. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1748. (word32)test_md5[i].inLen);
  1749. if (ret != 0)
  1750. ERROR_OUT(-1602 - i, exit);
  1751. ret = wc_Md5GetHash(&md5, hashcopy);
  1752. if (ret != 0)
  1753. ERROR_OUT(-1603 - i, exit);
  1754. ret = wc_Md5Copy(&md5, &md5Copy);
  1755. if (ret != 0)
  1756. ERROR_OUT(-1604 - i, exit);
  1757. ret = wc_Md5Final(&md5, hash);
  1758. if (ret != 0)
  1759. ERROR_OUT(-1605 - i, exit);
  1760. wc_Md5Free(&md5Copy);
  1761. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1762. ERROR_OUT(-1606 - i, exit);
  1763. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1764. ERROR_OUT(-1607 - i, exit);
  1765. }
  1766. /* BEGIN LARGE HASH TEST */ {
  1767. byte large_input[1024];
  1768. const char* large_digest =
  1769. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1770. for (i = 0; i < (int)sizeof(large_input); i++) {
  1771. large_input[i] = (byte)(i & 0xFF);
  1772. }
  1773. times = 100;
  1774. #ifdef WOLFSSL_PIC32MZ_HASH
  1775. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1776. #endif
  1777. for (i = 0; i < times; ++i) {
  1778. ret = wc_Md5Update(&md5, (byte*)large_input,
  1779. (word32)sizeof(large_input));
  1780. if (ret != 0)
  1781. ERROR_OUT(-1608, exit);
  1782. }
  1783. ret = wc_Md5Final(&md5, hash);
  1784. if (ret != 0)
  1785. ERROR_OUT(-1609, exit);
  1786. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1787. ERROR_OUT(-1610, exit);
  1788. } /* END LARGE HASH TEST */
  1789. exit:
  1790. wc_Md5Free(&md5);
  1791. wc_Md5Free(&md5Copy);
  1792. return ret;
  1793. }
  1794. #endif /* NO_MD5 */
  1795. #ifndef NO_MD4
  1796. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  1797. {
  1798. Md4 md4;
  1799. byte hash[MD4_DIGEST_SIZE];
  1800. testVector a, b, c, d, e, f, g;
  1801. testVector test_md4[7];
  1802. int times = sizeof(test_md4) / sizeof(testVector), i;
  1803. a.input = "";
  1804. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1805. "\xc0";
  1806. a.inLen = XSTRLEN(a.input);
  1807. a.outLen = MD4_DIGEST_SIZE;
  1808. b.input = "a";
  1809. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1810. "\x24";
  1811. b.inLen = XSTRLEN(b.input);
  1812. b.outLen = MD4_DIGEST_SIZE;
  1813. c.input = "abc";
  1814. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1815. "\x9d";
  1816. c.inLen = XSTRLEN(c.input);
  1817. c.outLen = MD4_DIGEST_SIZE;
  1818. d.input = "message digest";
  1819. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1820. "\x4b";
  1821. d.inLen = XSTRLEN(d.input);
  1822. d.outLen = MD4_DIGEST_SIZE;
  1823. e.input = "abcdefghijklmnopqrstuvwxyz";
  1824. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1825. "\xa9";
  1826. e.inLen = XSTRLEN(e.input);
  1827. e.outLen = MD4_DIGEST_SIZE;
  1828. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1829. "6789";
  1830. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1831. "\xe4";
  1832. f.inLen = XSTRLEN(f.input);
  1833. f.outLen = MD4_DIGEST_SIZE;
  1834. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1835. "9012345678901234567890";
  1836. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1837. "\x36";
  1838. g.inLen = XSTRLEN(g.input);
  1839. g.outLen = MD4_DIGEST_SIZE;
  1840. test_md4[0] = a;
  1841. test_md4[1] = b;
  1842. test_md4[2] = c;
  1843. test_md4[3] = d;
  1844. test_md4[4] = e;
  1845. test_md4[5] = f;
  1846. test_md4[6] = g;
  1847. wc_InitMd4(&md4);
  1848. for (i = 0; i < times; ++i) {
  1849. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1850. wc_Md4Final(&md4, hash);
  1851. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1852. return -1700 - i;
  1853. }
  1854. return 0;
  1855. }
  1856. #endif /* NO_MD4 */
  1857. #ifndef NO_SHA
  1858. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  1859. {
  1860. int ret = 0;
  1861. wc_Sha sha, shaCopy;
  1862. byte hash[WC_SHA_DIGEST_SIZE];
  1863. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1864. testVector a, b, c, d, e;
  1865. testVector test_sha[5];
  1866. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1867. a.input = "";
  1868. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1869. "\x90\xaf\xd8\x07\x09";
  1870. a.inLen = XSTRLEN(a.input);
  1871. a.outLen = WC_SHA_DIGEST_SIZE;
  1872. b.input = "abc";
  1873. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1874. "\x6C\x9C\xD0\xD8\x9D";
  1875. b.inLen = XSTRLEN(b.input);
  1876. b.outLen = WC_SHA_DIGEST_SIZE;
  1877. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1878. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1879. "\xE5\xE5\x46\x70\xF1";
  1880. c.inLen = XSTRLEN(c.input);
  1881. c.outLen = WC_SHA_DIGEST_SIZE;
  1882. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1883. "aaaaaa";
  1884. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1885. "\x2A\x25\xEC\x64\x4D";
  1886. d.inLen = XSTRLEN(d.input);
  1887. d.outLen = WC_SHA_DIGEST_SIZE;
  1888. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1889. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1890. "aaaaaaaaaa";
  1891. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1892. "\x53\x99\x5E\x26\xA0";
  1893. e.inLen = XSTRLEN(e.input);
  1894. e.outLen = WC_SHA_DIGEST_SIZE;
  1895. test_sha[0] = a;
  1896. test_sha[1] = b;
  1897. test_sha[2] = c;
  1898. test_sha[3] = d;
  1899. test_sha[4] = e;
  1900. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  1901. if (ret != 0)
  1902. return -1800;
  1903. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  1904. if (ret != 0) {
  1905. wc_ShaFree(&sha);
  1906. return -1801;
  1907. }
  1908. for (i = 0; i < times; ++i) {
  1909. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  1910. (word32)test_sha[i].inLen);
  1911. if (ret != 0)
  1912. ERROR_OUT(-1802 - i, exit);
  1913. ret = wc_ShaGetHash(&sha, hashcopy);
  1914. if (ret != 0)
  1915. ERROR_OUT(-1803 - i, exit);
  1916. ret = wc_ShaCopy(&sha, &shaCopy);
  1917. if (ret != 0)
  1918. ERROR_OUT(-1804 - i, exit);
  1919. ret = wc_ShaFinal(&sha, hash);
  1920. if (ret != 0)
  1921. ERROR_OUT(-1805 - i, exit);
  1922. wc_ShaFree(&shaCopy);
  1923. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  1924. ERROR_OUT(-1806 - i, exit);
  1925. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  1926. ERROR_OUT(-1807 - i, exit);
  1927. }
  1928. /* BEGIN LARGE HASH TEST */ {
  1929. byte large_input[1024];
  1930. #ifdef WOLFSSL_RENESAS_TSIP
  1931. const char* large_digest =
  1932. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  1933. "\x15\x8c\x6d\xb6";
  1934. #else
  1935. const char* large_digest =
  1936. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  1937. "\xc5\xd9\x0a\xac";
  1938. #endif
  1939. for (i = 0; i < (int)sizeof(large_input); i++) {
  1940. large_input[i] = (byte)(i & 0xFF);
  1941. }
  1942. #ifdef WOLFSSL_RENESAS_TSIP
  1943. times = 20;
  1944. #else
  1945. times = 100;
  1946. #endif
  1947. #ifdef WOLFSSL_PIC32MZ_HASH
  1948. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  1949. #endif
  1950. for (i = 0; i < times; ++i) {
  1951. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  1952. (word32)sizeof(large_input));
  1953. if (ret != 0)
  1954. ERROR_OUT(-1808, exit);
  1955. }
  1956. ret = wc_ShaFinal(&sha, hash);
  1957. if (ret != 0)
  1958. ERROR_OUT(-1809, exit);
  1959. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  1960. ERROR_OUT(-1810, exit);
  1961. } /* END LARGE HASH TEST */
  1962. exit:
  1963. wc_ShaFree(&sha);
  1964. wc_ShaFree(&shaCopy);
  1965. return ret;
  1966. }
  1967. #endif /* NO_SHA */
  1968. #ifdef WOLFSSL_RIPEMD
  1969. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  1970. {
  1971. RipeMd ripemd;
  1972. int ret;
  1973. byte hash[RIPEMD_DIGEST_SIZE];
  1974. testVector a, b, c, d;
  1975. testVector test_ripemd[4];
  1976. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  1977. a.input = "abc";
  1978. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  1979. "\xb0\x87\xf1\x5a\x0b\xfc";
  1980. a.inLen = XSTRLEN(a.input);
  1981. a.outLen = RIPEMD_DIGEST_SIZE;
  1982. b.input = "message digest";
  1983. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  1984. "\x5f\xfa\x21\x59\x5f\x36";
  1985. b.inLen = XSTRLEN(b.input);
  1986. b.outLen = RIPEMD_DIGEST_SIZE;
  1987. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1988. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  1989. "\xf4\x9a\xda\x62\xeb\x2b";
  1990. c.inLen = XSTRLEN(c.input);
  1991. c.outLen = RIPEMD_DIGEST_SIZE;
  1992. d.input = "12345678901234567890123456789012345678901234567890123456"
  1993. "789012345678901234567890";
  1994. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  1995. "\x82\xbf\x63\x32\x6b\xfb";
  1996. d.inLen = XSTRLEN(d.input);
  1997. d.outLen = RIPEMD_DIGEST_SIZE;
  1998. test_ripemd[0] = a;
  1999. test_ripemd[1] = b;
  2000. test_ripemd[2] = c;
  2001. test_ripemd[3] = d;
  2002. ret = wc_InitRipeMd(&ripemd);
  2003. if (ret != 0) {
  2004. return -1900;
  2005. }
  2006. for (i = 0; i < times; ++i) {
  2007. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2008. (word32)test_ripemd[i].inLen);
  2009. if (ret != 0) {
  2010. return -1901 - i;
  2011. }
  2012. ret = wc_RipeMdFinal(&ripemd, hash);
  2013. if (ret != 0) {
  2014. return -1911 - i;
  2015. }
  2016. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2017. return -1921 - i;
  2018. }
  2019. return 0;
  2020. }
  2021. #endif /* WOLFSSL_RIPEMD */
  2022. #ifdef HAVE_BLAKE2
  2023. #define BLAKE2B_TESTS 3
  2024. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2025. {
  2026. {
  2027. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2028. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2029. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2030. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2031. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2032. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2033. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2034. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2035. },
  2036. {
  2037. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2038. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2039. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2040. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2041. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2042. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2043. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2044. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2045. },
  2046. {
  2047. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2048. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2049. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2050. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2051. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2052. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2053. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2054. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2055. }
  2056. };
  2057. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2058. {
  2059. Blake2b b2b;
  2060. byte digest[64];
  2061. byte input[64];
  2062. int i, ret;
  2063. for (i = 0; i < (int)sizeof(input); i++)
  2064. input[i] = (byte)i;
  2065. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2066. ret = wc_InitBlake2b(&b2b, 64);
  2067. if (ret != 0)
  2068. return -2000 - i;
  2069. ret = wc_Blake2bUpdate(&b2b, input, i);
  2070. if (ret != 0)
  2071. return -2010 - 1;
  2072. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2073. if (ret != 0)
  2074. return -2020 - i;
  2075. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2076. return -2030 - i;
  2077. }
  2078. }
  2079. return 0;
  2080. }
  2081. #endif /* HAVE_BLAKE2 */
  2082. #ifdef HAVE_BLAKE2S
  2083. #define BLAKE2S_TESTS 3
  2084. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2085. {
  2086. {
  2087. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2088. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2089. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2090. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2091. },
  2092. {
  2093. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2094. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2095. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2096. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2097. },
  2098. {
  2099. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2100. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2101. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2102. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2103. }
  2104. };
  2105. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2106. {
  2107. Blake2s b2s;
  2108. byte digest[32];
  2109. byte input[64];
  2110. int i, ret;
  2111. for (i = 0; i < (int)sizeof(input); i++)
  2112. input[i] = (byte)i;
  2113. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2114. ret = wc_InitBlake2s(&b2s, 32);
  2115. if (ret != 0)
  2116. return -2100 - i;
  2117. ret = wc_Blake2sUpdate(&b2s, input, i);
  2118. if (ret != 0)
  2119. return -2110 - 1;
  2120. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2121. if (ret != 0)
  2122. return -2120 - i;
  2123. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2124. return -2130 - i;
  2125. }
  2126. }
  2127. return 0;
  2128. }
  2129. #endif /* HAVE_BLAKE2S */
  2130. #ifdef WOLFSSL_SHA224
  2131. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2132. {
  2133. wc_Sha224 sha, shaCopy;
  2134. byte hash[WC_SHA224_DIGEST_SIZE];
  2135. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2136. int ret = 0;
  2137. testVector a, b, c;
  2138. testVector test_sha[3];
  2139. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2140. a.input = "";
  2141. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2142. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2143. a.inLen = XSTRLEN(a.input);
  2144. a.outLen = WC_SHA224_DIGEST_SIZE;
  2145. b.input = "abc";
  2146. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2147. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2148. b.inLen = XSTRLEN(b.input);
  2149. b.outLen = WC_SHA224_DIGEST_SIZE;
  2150. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2151. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2152. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2153. c.inLen = XSTRLEN(c.input);
  2154. c.outLen = WC_SHA224_DIGEST_SIZE;
  2155. test_sha[0] = a;
  2156. test_sha[1] = b;
  2157. test_sha[2] = c;
  2158. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2159. if (ret != 0)
  2160. return -2200;
  2161. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2162. if (ret != 0) {
  2163. wc_Sha224Free(&sha);
  2164. return -2201;
  2165. }
  2166. for (i = 0; i < times; ++i) {
  2167. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2168. (word32)test_sha[i].inLen);
  2169. if (ret != 0)
  2170. ERROR_OUT(-2202 - i, exit);
  2171. ret = wc_Sha224GetHash(&sha, hashcopy);
  2172. if (ret != 0)
  2173. ERROR_OUT(-2203 - i, exit);
  2174. ret = wc_Sha224Copy(&sha, &shaCopy);
  2175. if (ret != 0)
  2176. ERROR_OUT(-2204 - i, exit);
  2177. ret = wc_Sha224Final(&sha, hash);
  2178. if (ret != 0)
  2179. ERROR_OUT(-2205 - i, exit);
  2180. wc_Sha224Free(&shaCopy);
  2181. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2182. ERROR_OUT(-2206 - i, exit);
  2183. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2184. ERROR_OUT(-2207 - i, exit);
  2185. }
  2186. exit:
  2187. wc_Sha224Free(&sha);
  2188. wc_Sha224Free(&shaCopy);
  2189. return ret;
  2190. }
  2191. #endif
  2192. #ifndef NO_SHA256
  2193. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2194. {
  2195. wc_Sha256 sha, shaCopy;
  2196. byte hash[WC_SHA256_DIGEST_SIZE];
  2197. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2198. int ret = 0;
  2199. testVector a, b, c;
  2200. testVector test_sha[3];
  2201. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2202. a.input = "";
  2203. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2204. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2205. "\xb8\x55";
  2206. a.inLen = XSTRLEN(a.input);
  2207. a.outLen = WC_SHA256_DIGEST_SIZE;
  2208. b.input = "abc";
  2209. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2210. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2211. "\x15\xAD";
  2212. b.inLen = XSTRLEN(b.input);
  2213. b.outLen = WC_SHA256_DIGEST_SIZE;
  2214. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2215. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2216. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2217. "\x06\xC1";
  2218. c.inLen = XSTRLEN(c.input);
  2219. c.outLen = WC_SHA256_DIGEST_SIZE;
  2220. test_sha[0] = a;
  2221. test_sha[1] = b;
  2222. test_sha[2] = c;
  2223. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2224. if (ret != 0)
  2225. return -2300;
  2226. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2227. if (ret != 0) {
  2228. wc_Sha256Free(&sha);
  2229. return -2301;
  2230. }
  2231. for (i = 0; i < times; ++i) {
  2232. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2233. (word32)test_sha[i].inLen);
  2234. if (ret != 0) {
  2235. ERROR_OUT(-2302 - i, exit);
  2236. }
  2237. ret = wc_Sha256GetHash(&sha, hashcopy);
  2238. if (ret != 0)
  2239. ERROR_OUT(-2303 - i, exit);
  2240. ret = wc_Sha256Copy(&sha, &shaCopy);
  2241. if (ret != 0)
  2242. ERROR_OUT(-2304 - i, exit);
  2243. ret = wc_Sha256Final(&sha, hash);
  2244. if (ret != 0)
  2245. ERROR_OUT(-2305 - i, exit);
  2246. wc_Sha256Free(&shaCopy);
  2247. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2248. ERROR_OUT(-2306 - i, exit);
  2249. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2250. ERROR_OUT(-2307 - i, exit);
  2251. }
  2252. /* BEGIN LARGE HASH TEST */ {
  2253. byte large_input[1024];
  2254. #ifdef WOLFSSL_RENESAS_TSIP_CRYPT
  2255. const char* large_digest =
  2256. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2257. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2258. #else
  2259. const char* large_digest =
  2260. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2261. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2262. #endif
  2263. for (i = 0; i < (int)sizeof(large_input); i++) {
  2264. large_input[i] = (byte)(i & 0xFF);
  2265. }
  2266. #ifdef WOLFSSL_RENESAS_TSIP
  2267. times = 20;
  2268. #else
  2269. times = 100;
  2270. #endif
  2271. #ifdef WOLFSSL_PIC32MZ_HASH
  2272. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2273. #endif
  2274. for (i = 0; i < times; ++i) {
  2275. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2276. (word32)sizeof(large_input));
  2277. if (ret != 0)
  2278. ERROR_OUT(-2308, exit);
  2279. }
  2280. ret = wc_Sha256Final(&sha, hash);
  2281. if (ret != 0)
  2282. ERROR_OUT(-2309, exit);
  2283. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2284. ERROR_OUT(-2310, exit);
  2285. } /* END LARGE HASH TEST */
  2286. exit:
  2287. wc_Sha256Free(&sha);
  2288. wc_Sha256Free(&shaCopy);
  2289. return ret;
  2290. }
  2291. #endif
  2292. #ifdef WOLFSSL_SHA512
  2293. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2294. {
  2295. wc_Sha512 sha, shaCopy;
  2296. byte hash[WC_SHA512_DIGEST_SIZE];
  2297. byte hashcopy[WC_SHA512_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 = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2304. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2305. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2306. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2307. "\xf9\x27\xda\x3e";
  2308. a.inLen = XSTRLEN(a.input);
  2309. a.outLen = WC_SHA512_DIGEST_SIZE;
  2310. b.input = "abc";
  2311. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2312. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2313. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2314. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2315. "\xa5\x4c\xa4\x9f";
  2316. b.inLen = XSTRLEN(b.input);
  2317. b.outLen = WC_SHA512_DIGEST_SIZE;
  2318. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2319. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2320. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2321. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2322. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2323. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2324. "\x87\x4b\xe9\x09";
  2325. c.inLen = XSTRLEN(c.input);
  2326. c.outLen = WC_SHA512_DIGEST_SIZE;
  2327. test_sha[0] = a;
  2328. test_sha[1] = b;
  2329. test_sha[2] = c;
  2330. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2331. if (ret != 0)
  2332. return -2400;
  2333. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2334. if (ret != 0) {
  2335. wc_Sha512Free(&sha);
  2336. return -2401;
  2337. }
  2338. for (i = 0; i < times; ++i) {
  2339. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2340. (word32)test_sha[i].inLen);
  2341. if (ret != 0)
  2342. ERROR_OUT(-2402 - i, exit);
  2343. ret = wc_Sha512GetHash(&sha, hashcopy);
  2344. if (ret != 0)
  2345. ERROR_OUT(-2403 - i, exit);
  2346. ret = wc_Sha512Copy(&sha, &shaCopy);
  2347. if (ret != 0)
  2348. ERROR_OUT(-2404 - i, exit);
  2349. ret = wc_Sha512Final(&sha, hash);
  2350. if (ret != 0)
  2351. ERROR_OUT(-2405 - i, exit);
  2352. wc_Sha512Free(&shaCopy);
  2353. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2354. ERROR_OUT(-2406 - i, exit);
  2355. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2356. ERROR_OUT(-2407 - i, exit);
  2357. }
  2358. /* BEGIN LARGE HASH TEST */ {
  2359. byte large_input[1024];
  2360. const char* large_digest =
  2361. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2362. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2363. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2364. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2365. for (i = 0; i < (int)sizeof(large_input); i++) {
  2366. large_input[i] = (byte)(i & 0xFF);
  2367. }
  2368. times = 100;
  2369. for (i = 0; i < times; ++i) {
  2370. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2371. (word32)sizeof(large_input));
  2372. if (ret != 0)
  2373. ERROR_OUT(-2408, exit);
  2374. }
  2375. ret = wc_Sha512Final(&sha, hash);
  2376. if (ret != 0)
  2377. ERROR_OUT(-2409, exit);
  2378. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2379. ERROR_OUT(-2410, exit);
  2380. } /* END LARGE HASH TEST */
  2381. exit:
  2382. wc_Sha512Free(&sha);
  2383. wc_Sha512Free(&shaCopy);
  2384. return ret;
  2385. }
  2386. #endif
  2387. #ifdef WOLFSSL_SHA384
  2388. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2389. {
  2390. wc_Sha384 sha, shaCopy;
  2391. byte hash[WC_SHA384_DIGEST_SIZE];
  2392. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2393. int ret = 0;
  2394. testVector a, b, c;
  2395. testVector test_sha[3];
  2396. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2397. a.input = "";
  2398. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2399. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2400. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2401. "\x98\xb9\x5b";
  2402. a.inLen = XSTRLEN(a.input);
  2403. a.outLen = WC_SHA384_DIGEST_SIZE;
  2404. b.input = "abc";
  2405. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2406. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2407. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2408. "\xc8\x25\xa7";
  2409. b.inLen = XSTRLEN(b.input);
  2410. b.outLen = WC_SHA384_DIGEST_SIZE;
  2411. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2412. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2413. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2414. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2415. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2416. "\x74\x60\x39";
  2417. c.inLen = XSTRLEN(c.input);
  2418. c.outLen = WC_SHA384_DIGEST_SIZE;
  2419. test_sha[0] = a;
  2420. test_sha[1] = b;
  2421. test_sha[2] = c;
  2422. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2423. if (ret != 0)
  2424. return -2500;
  2425. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2426. if (ret != 0) {
  2427. wc_Sha384Free(&sha);
  2428. return -2501;
  2429. }
  2430. for (i = 0; i < times; ++i) {
  2431. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2432. (word32)test_sha[i].inLen);
  2433. if (ret != 0)
  2434. ERROR_OUT(-2502 - i, exit);
  2435. ret = wc_Sha384GetHash(&sha, hashcopy);
  2436. if (ret != 0)
  2437. ERROR_OUT(-2503 - i, exit);
  2438. ret = wc_Sha384Copy(&sha, &shaCopy);
  2439. if (ret != 0)
  2440. ERROR_OUT(-2504 - i, exit);
  2441. ret = wc_Sha384Final(&sha, hash);
  2442. if (ret != 0)
  2443. ERROR_OUT(-2505 - i, exit);
  2444. wc_Sha384Free(&shaCopy);
  2445. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2446. ERROR_OUT(-2506 - i, exit);
  2447. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2448. ERROR_OUT(-2507 - i, exit);
  2449. }
  2450. /* BEGIN LARGE HASH TEST */ {
  2451. byte large_input[1024];
  2452. const char* large_digest =
  2453. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2454. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2455. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2456. for (i = 0; i < (int)sizeof(large_input); i++) {
  2457. large_input[i] = (byte)(i & 0xFF);
  2458. }
  2459. times = 100;
  2460. for (i = 0; i < times; ++i) {
  2461. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2462. (word32)sizeof(large_input));
  2463. if (ret != 0)
  2464. ERROR_OUT(-2508, exit);
  2465. }
  2466. ret = wc_Sha384Final(&sha, hash);
  2467. if (ret != 0)
  2468. ERROR_OUT(-2509, exit);
  2469. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2470. ERROR_OUT(-2510, exit);
  2471. } /* END LARGE HASH TEST */
  2472. exit:
  2473. wc_Sha384Free(&sha);
  2474. wc_Sha384Free(&shaCopy);
  2475. return ret;
  2476. }
  2477. #endif /* WOLFSSL_SHA384 */
  2478. #ifdef WOLFSSL_SHA3
  2479. #ifndef WOLFSSL_NOSHA3_224
  2480. static int sha3_224_test(void)
  2481. {
  2482. wc_Sha3 sha;
  2483. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2484. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2485. testVector a, b, c;
  2486. testVector test_sha[3];
  2487. int ret = 0;
  2488. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2489. a.input = "";
  2490. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2491. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2492. a.inLen = XSTRLEN(a.input);
  2493. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2494. b.input = "abc";
  2495. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2496. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2497. b.inLen = XSTRLEN(b.input);
  2498. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2499. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2500. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2501. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2502. c.inLen = XSTRLEN(c.input);
  2503. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2504. test_sha[0] = a;
  2505. test_sha[1] = b;
  2506. test_sha[2] = c;
  2507. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2508. if (ret != 0)
  2509. return -2600;
  2510. for (i = 0; i < times; ++i) {
  2511. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2512. (word32)test_sha[i].inLen);
  2513. if (ret != 0)
  2514. ERROR_OUT(-2601 - i, exit);
  2515. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2516. if (ret != 0)
  2517. ERROR_OUT(-2602 - i, exit);
  2518. ret = wc_Sha3_224_Final(&sha, hash);
  2519. if (ret != 0)
  2520. ERROR_OUT(-2603 - i, exit);
  2521. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2522. ERROR_OUT(-2604 - i, exit);
  2523. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2524. ERROR_OUT(-2605 - i, exit);
  2525. }
  2526. /* BEGIN LARGE HASH TEST */ {
  2527. byte large_input[1024];
  2528. const char* large_digest =
  2529. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2530. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2531. for (i = 0; i < (int)sizeof(large_input); i++) {
  2532. large_input[i] = (byte)(i & 0xFF);
  2533. }
  2534. times = 100;
  2535. for (i = 0; i < times; ++i) {
  2536. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2537. (word32)sizeof(large_input));
  2538. if (ret != 0)
  2539. ERROR_OUT(-2606, exit);
  2540. }
  2541. ret = wc_Sha3_224_Final(&sha, hash);
  2542. if (ret != 0)
  2543. ERROR_OUT(-2607, exit);
  2544. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2545. ERROR_OUT(-2608, exit);
  2546. } /* END LARGE HASH TEST */
  2547. exit:
  2548. wc_Sha3_224_Free(&sha);
  2549. return ret;
  2550. }
  2551. #endif /* WOLFSSL_NOSHA3_224 */
  2552. #ifndef WOLFSSL_NOSHA3_256
  2553. static int sha3_256_test(void)
  2554. {
  2555. wc_Sha3 sha;
  2556. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2557. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2558. testVector a, b, c;
  2559. testVector test_sha[3];
  2560. int ret = 0;
  2561. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2562. byte large_input[1024];
  2563. const char* large_digest =
  2564. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2565. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2566. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2567. /* test vector with hash of empty string */
  2568. const char* Keccak256EmptyOut =
  2569. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2570. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2571. #endif
  2572. a.input = "";
  2573. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2574. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2575. "\x43\x4a";
  2576. a.inLen = XSTRLEN(a.input);
  2577. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2578. b.input = "abc";
  2579. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2580. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2581. "\x15\x32";
  2582. b.inLen = XSTRLEN(b.input);
  2583. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2584. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2585. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2586. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2587. "\x33\x76";
  2588. c.inLen = XSTRLEN(c.input);
  2589. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2590. test_sha[0] = a;
  2591. test_sha[1] = b;
  2592. test_sha[2] = c;
  2593. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2594. if (ret != 0)
  2595. return -2700;
  2596. for (i = 0; i < times; ++i) {
  2597. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2598. (word32)test_sha[i].inLen);
  2599. if (ret != 0)
  2600. ERROR_OUT(-2701 - i, exit);
  2601. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2602. if (ret != 0)
  2603. ERROR_OUT(-2702 - i, exit);
  2604. ret = wc_Sha3_256_Final(&sha, hash);
  2605. if (ret != 0)
  2606. ERROR_OUT(-2703 - i, exit);
  2607. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2608. ERROR_OUT(-2704 - i, exit);
  2609. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2610. ERROR_OUT(-2705 - i, exit);
  2611. }
  2612. /* BEGIN LARGE HASH TEST */ {
  2613. for (i = 0; i < (int)sizeof(large_input); i++) {
  2614. large_input[i] = (byte)(i & 0xFF);
  2615. }
  2616. times = 100;
  2617. for (i = 0; i < times; ++i) {
  2618. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2619. (word32)sizeof(large_input));
  2620. if (ret != 0)
  2621. ERROR_OUT(-2706, exit);
  2622. }
  2623. ret = wc_Sha3_256_Final(&sha, hash);
  2624. if (ret != 0)
  2625. ERROR_OUT(-2707, exit);
  2626. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2627. ERROR_OUT(-2708, exit);
  2628. } /* END LARGE HASH TEST */
  2629. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2630. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2631. /* Test for Keccak256 */
  2632. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2633. if (ret != 0) {
  2634. ERROR_OUT(-2709, exit);
  2635. }
  2636. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2637. if (ret != 0) {
  2638. ERROR_OUT(-2710, exit);
  2639. }
  2640. ret = wc_Sha3_256_Final(&sha, hash);
  2641. if (ret != 0) {
  2642. ERROR_OUT(-2711, exit);
  2643. }
  2644. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2645. ERROR_OUT(-2712, exit);
  2646. }
  2647. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2648. exit:
  2649. wc_Sha3_256_Free(&sha);
  2650. return ret;
  2651. }
  2652. #endif /* WOLFSSL_NOSHA3_256 */
  2653. #ifndef WOLFSSL_NOSHA3_384
  2654. static int sha3_384_test(void)
  2655. {
  2656. wc_Sha3 sha;
  2657. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2658. #ifndef NO_INTM_HASH_TEST
  2659. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2660. #endif
  2661. testVector a, b, c;
  2662. testVector test_sha[3];
  2663. int ret;
  2664. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2665. a.input = "";
  2666. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2667. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2668. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2669. "\xd5\xf0\x04";
  2670. a.inLen = XSTRLEN(a.input);
  2671. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2672. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
  2673. /* NIST test vector with a length that is a multiple of 4 */
  2674. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2675. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2676. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2677. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2678. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2679. "\x19\x87\x22";
  2680. b.inLen = XSTRLEN(b.input);
  2681. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2682. #else
  2683. b.input = "abc";
  2684. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2685. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2686. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2687. "\x37\x6d\x25";
  2688. b.inLen = XSTRLEN(b.input);
  2689. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2690. #endif
  2691. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2692. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2693. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2694. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2695. "\x65\x7c\x22";
  2696. c.inLen = XSTRLEN(c.input);
  2697. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2698. #ifdef WOLFSSL_XILINX_CRYPT
  2699. test_sha[0] = b; /* hardware acc. can not handle "" string */
  2700. #else
  2701. test_sha[0] = a;
  2702. #endif
  2703. test_sha[1] = b;
  2704. test_sha[2] = c;
  2705. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2706. if (ret != 0)
  2707. return -2800;
  2708. for (i = 0; i < times; ++i) {
  2709. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2710. (word32)test_sha[i].inLen);
  2711. if (ret != 0)
  2712. ERROR_OUT(-2801 - i, exit);
  2713. #ifndef NO_INTM_HASH_TEST
  2714. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2715. if (ret != 0)
  2716. ERROR_OUT(-2802 - i, exit);
  2717. #endif
  2718. ret = wc_Sha3_384_Final(&sha, hash);
  2719. if (ret != 0)
  2720. ERROR_OUT(-2803 - i, exit);
  2721. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2722. ERROR_OUT(-2804 - i, exit);
  2723. #ifndef NO_INTM_HASH_TEST
  2724. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2725. ERROR_OUT(-2805 - i, exit);
  2726. #endif
  2727. }
  2728. /* BEGIN LARGE HASH TEST */ {
  2729. byte large_input[1024];
  2730. const char* large_digest =
  2731. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2732. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2733. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2734. for (i = 0; i < (int)sizeof(large_input); i++) {
  2735. large_input[i] = (byte)(i & 0xFF);
  2736. }
  2737. times = 100;
  2738. for (i = 0; i < times; ++i) {
  2739. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2740. (word32)sizeof(large_input));
  2741. if (ret != 0)
  2742. ERROR_OUT(-2806, exit);
  2743. }
  2744. ret = wc_Sha3_384_Final(&sha, hash);
  2745. if (ret != 0)
  2746. ERROR_OUT(-2807, exit);
  2747. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2748. ERROR_OUT(-2808, exit);
  2749. } /* END LARGE HASH TEST */
  2750. exit:
  2751. wc_Sha3_384_Free(&sha);
  2752. return ret;
  2753. }
  2754. #endif /* WOLFSSL_NOSHA3_384 */
  2755. #ifndef WOLFSSL_NOSHA3_512
  2756. static int sha3_512_test(void)
  2757. {
  2758. wc_Sha3 sha;
  2759. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2760. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  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 = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2767. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2768. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2769. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2770. "\x28\x1d\xcd\x26";
  2771. a.inLen = XSTRLEN(a.input);
  2772. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2773. b.input = "abc";
  2774. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2775. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2776. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2777. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2778. "\x4e\xec\x53\xf0";
  2779. b.inLen = XSTRLEN(b.input);
  2780. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2781. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2782. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2783. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2784. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2785. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2786. "\x39\xd6\xe7\x5e";
  2787. c.inLen = XSTRLEN(c.input);
  2788. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  2789. test_sha[0] = a;
  2790. test_sha[1] = b;
  2791. test_sha[2] = c;
  2792. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2793. if (ret != 0)
  2794. return -2900;
  2795. for (i = 0; i < times; ++i) {
  2796. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2797. (word32)test_sha[i].inLen);
  2798. if (ret != 0)
  2799. ERROR_OUT(-2901 - i, exit);
  2800. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2801. if (ret != 0)
  2802. ERROR_OUT(-2902 - i, exit);
  2803. ret = wc_Sha3_512_Final(&sha, hash);
  2804. if (ret != 0)
  2805. ERROR_OUT(-2903 - i, exit);
  2806. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  2807. ERROR_OUT(-2904 - i, exit);
  2808. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  2809. ERROR_OUT(-2905 - i, exit);
  2810. }
  2811. /* BEGIN LARGE HASH TEST */ {
  2812. byte large_input[1024];
  2813. const char* large_digest =
  2814. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2815. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2816. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2817. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2818. for (i = 0; i < (int)sizeof(large_input); i++) {
  2819. large_input[i] = (byte)(i & 0xFF);
  2820. }
  2821. times = 100;
  2822. for (i = 0; i < times; ++i) {
  2823. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2824. (word32)sizeof(large_input));
  2825. if (ret != 0)
  2826. ERROR_OUT(-2906, exit);
  2827. }
  2828. ret = wc_Sha3_512_Final(&sha, hash);
  2829. if (ret != 0)
  2830. ERROR_OUT(-2907, exit);
  2831. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  2832. ERROR_OUT(-2908, exit);
  2833. } /* END LARGE HASH TEST */
  2834. exit:
  2835. wc_Sha3_512_Free(&sha);
  2836. return ret;
  2837. }
  2838. #endif /* WOLFSSL_NOSHA3_512 */
  2839. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  2840. {
  2841. int ret;
  2842. (void)ret;
  2843. #ifndef WOLFSSL_NOSHA3_224
  2844. if ((ret = sha3_224_test()) != 0)
  2845. return ret;
  2846. #endif
  2847. #ifndef WOLFSSL_NOSHA3_256
  2848. if ((ret = sha3_256_test()) != 0)
  2849. return ret;
  2850. #endif
  2851. #ifndef WOLFSSL_NOSHA3_384
  2852. if ((ret = sha3_384_test()) != 0)
  2853. return ret;
  2854. #endif
  2855. #ifndef WOLFSSL_NOSHA3_512
  2856. if ((ret = sha3_512_test()) != 0)
  2857. return ret;
  2858. #endif
  2859. return 0;
  2860. }
  2861. #endif /* WOLFSSL_SHA3 */
  2862. #ifdef WOLFSSL_SHAKE256
  2863. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  2864. {
  2865. #ifndef WOLFSSL_NO_SHAKE256
  2866. wc_Shake sha;
  2867. byte hash[114];
  2868. testVector a, b, c;
  2869. testVector test_sha[3];
  2870. int ret = 0;
  2871. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2872. byte large_input[1024];
  2873. const char* large_digest =
  2874. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  2875. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  2876. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  2877. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  2878. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  2879. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  2880. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  2881. "\xea\x26";
  2882. a.input = "";
  2883. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  2884. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  2885. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  2886. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  2887. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  2888. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  2889. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  2890. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  2891. a.inLen = XSTRLEN(a.input);
  2892. a.outLen = sizeof(hash);
  2893. b.input = "abc";
  2894. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  2895. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  2896. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  2897. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  2898. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  2899. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  2900. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  2901. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  2902. b.inLen = XSTRLEN(b.input);
  2903. b.outLen = sizeof(hash);
  2904. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2905. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  2906. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  2907. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  2908. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  2909. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  2910. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  2911. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  2912. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  2913. c.inLen = XSTRLEN(c.input);
  2914. c.outLen = sizeof(hash);
  2915. test_sha[0] = a;
  2916. test_sha[1] = b;
  2917. test_sha[2] = c;
  2918. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  2919. if (ret != 0)
  2920. return -3100;
  2921. for (i = 0; i < times; ++i) {
  2922. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  2923. (word32)test_sha[i].inLen);
  2924. if (ret != 0)
  2925. ERROR_OUT(-3101 - i, exit);
  2926. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  2927. if (ret != 0)
  2928. ERROR_OUT(-3102 - i, exit);
  2929. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  2930. ERROR_OUT(-3103 - i, exit);
  2931. }
  2932. /* BEGIN LARGE HASH TEST */ {
  2933. for (i = 0; i < (int)sizeof(large_input); i++) {
  2934. large_input[i] = (byte)(i & 0xFF);
  2935. }
  2936. times = 100;
  2937. for (i = 0; i < times; ++i) {
  2938. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  2939. (word32)sizeof(large_input));
  2940. if (ret != 0)
  2941. ERROR_OUT(-3104, exit);
  2942. }
  2943. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  2944. if (ret != 0)
  2945. ERROR_OUT(-3105, exit);
  2946. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  2947. ERROR_OUT(-3106, exit);
  2948. } /* END LARGE HASH TEST */
  2949. exit:
  2950. wc_Shake256_Free(&sha);
  2951. return ret;
  2952. #else
  2953. return 0;
  2954. #endif
  2955. }
  2956. #endif
  2957. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  2958. {
  2959. wc_HashAlg hash;
  2960. int ret, exp_ret;
  2961. int i, j;
  2962. int digestSz;
  2963. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  2964. byte out[WC_MAX_DIGEST_SIZE];
  2965. byte hashOut[WC_MAX_DIGEST_SIZE];
  2966. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2967. enum wc_HashType hashType;
  2968. #endif
  2969. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  2970. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  2971. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  2972. WC_HASH_TYPE_SHA3_224,
  2973. WC_HASH_TYPE_SHA3_256,
  2974. WC_HASH_TYPE_SHA3_384,
  2975. WC_HASH_TYPE_SHA3_512 };
  2976. enum wc_HashType typesNoImpl[] = {
  2977. #ifdef NO_MD5
  2978. WC_HASH_TYPE_MD5,
  2979. #endif
  2980. #ifdef NO_SHA
  2981. WC_HASH_TYPE_SHA,
  2982. #endif
  2983. #ifndef WOLFSSL_SHA224
  2984. WC_HASH_TYPE_SHA224,
  2985. #endif
  2986. #ifdef NO_SHA256
  2987. WC_HASH_TYPE_SHA256,
  2988. #endif
  2989. #ifndef WOLFSSL_SHA384
  2990. WC_HASH_TYPE_SHA384,
  2991. #endif
  2992. #ifndef WOLFSSL_SHA512
  2993. WC_HASH_TYPE_SHA512,
  2994. #endif
  2995. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  2996. WC_HASH_TYPE_SHA3_224,
  2997. #endif
  2998. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  2999. WC_HASH_TYPE_SHA3_256,
  3000. #endif
  3001. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3002. WC_HASH_TYPE_SHA3_384,
  3003. #endif
  3004. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3005. WC_HASH_TYPE_SHA3_512,
  3006. #endif
  3007. WC_HASH_TYPE_NONE
  3008. };
  3009. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3010. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3011. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3012. WC_HASH_TYPE_BLAKE2B,
  3013. WC_HASH_TYPE_NONE };
  3014. /* Parameter Validation testing. */
  3015. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3016. if (ret != BAD_FUNC_ARG)
  3017. return -3200;
  3018. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3019. if (ret != BAD_FUNC_ARG)
  3020. return -3201;
  3021. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3022. if (ret != BAD_FUNC_ARG)
  3023. return -3202;
  3024. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3025. if (ret != BAD_FUNC_ARG)
  3026. return -3203;
  3027. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3028. if (ret != BAD_FUNC_ARG)
  3029. return -3204;
  3030. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3031. if (ret != BAD_FUNC_ARG)
  3032. return -3205;
  3033. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3034. if (ret != BAD_FUNC_ARG)
  3035. return -3206;
  3036. /* Try invalid hash algorithms. */
  3037. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3038. ret = wc_HashInit(&hash, typesBad[i]);
  3039. if (ret != BAD_FUNC_ARG)
  3040. return -3207 - i;
  3041. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3042. if (ret != BAD_FUNC_ARG)
  3043. return -3217 - i;
  3044. ret = wc_HashFinal(&hash, typesBad[i], out);
  3045. if (ret != BAD_FUNC_ARG)
  3046. return -3227 - i;
  3047. wc_HashFree(&hash, typesBad[i]);
  3048. }
  3049. /* Try valid hash algorithms. */
  3050. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3051. exp_ret = 0;
  3052. if (typesGood[i] == typesNoImpl[j]) {
  3053. /* Recognized but no implementation compiled in. */
  3054. exp_ret = HASH_TYPE_E;
  3055. j++;
  3056. }
  3057. ret = wc_HashInit(&hash, typesGood[i]);
  3058. if (ret != exp_ret)
  3059. return -3237 - i;
  3060. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3061. if (ret != exp_ret)
  3062. return -3247 - i;
  3063. ret = wc_HashFinal(&hash, typesGood[i], out);
  3064. if (ret != exp_ret)
  3065. return -3257 - i;
  3066. wc_HashFree(&hash, typesGood[i]);
  3067. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3068. if (exp_ret < 0 && digestSz != exp_ret)
  3069. return -3267 - i;
  3070. if (exp_ret == 0 && digestSz < 0)
  3071. return -3277 - i;
  3072. if (exp_ret == 0) {
  3073. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3074. digestSz - 1);
  3075. if (ret != BUFFER_E)
  3076. return -3287 - i;
  3077. }
  3078. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3079. if (ret != exp_ret)
  3080. return -3297 - i;
  3081. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3082. return -3307 -i;
  3083. ret = wc_HashGetBlockSize(typesGood[i]);
  3084. if (exp_ret < 0 && ret != exp_ret)
  3085. return -3308 - i;
  3086. if (exp_ret == 0 && ret < 0)
  3087. return -3318 - i;
  3088. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3089. ret = wc_HashGetOID(typesGood[i]);
  3090. if (ret == BAD_FUNC_ARG ||
  3091. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3092. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3093. return -3328 - i;
  3094. }
  3095. hashType = wc_OidGetHash(ret);
  3096. if (exp_ret == 0 && hashType != typesGood[i])
  3097. return -3338 - i;
  3098. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3099. }
  3100. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3101. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3102. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3103. return -3348 - i;
  3104. }
  3105. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3106. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3107. #ifdef WOLFSSL_MD2
  3108. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3109. return -3358;
  3110. #else
  3111. if (ret != HASH_TYPE_E)
  3112. return -3359;
  3113. #endif
  3114. hashType = wc_OidGetHash(646); /* Md2h */
  3115. #ifdef WOLFSSL_MD2
  3116. if (hashType != WC_HASH_TYPE_MD2)
  3117. return -3360;
  3118. #else
  3119. if (hashType != WC_HASH_TYPE_NONE)
  3120. return -3361;
  3121. #endif
  3122. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3123. #ifndef NO_MD5
  3124. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3125. return -3362;
  3126. #else
  3127. if (ret != HASH_TYPE_E)
  3128. return -3363;
  3129. #endif
  3130. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3131. if (ret != BAD_FUNC_ARG)
  3132. return -3364;
  3133. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3134. if (ret != BAD_FUNC_ARG)
  3135. return -3365;
  3136. hashType = wc_OidGetHash(0);
  3137. if (hashType != WC_HASH_TYPE_NONE)
  3138. return -3366;
  3139. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3140. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3141. #ifdef WOLFSSL_MD2
  3142. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3143. return -3367;
  3144. #else
  3145. if (ret != HASH_TYPE_E)
  3146. return -3368;
  3147. #endif
  3148. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3149. #ifdef WOLFSSL_MD2
  3150. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3151. return -3369;
  3152. #else
  3153. if (ret != HASH_TYPE_E)
  3154. return -3370;
  3155. #endif
  3156. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3157. #ifndef NO_MD4
  3158. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3159. return -3371;
  3160. #else
  3161. if (ret != HASH_TYPE_E)
  3162. return -3372;
  3163. #endif
  3164. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3165. #ifndef NO_MD4
  3166. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3167. return -3373;
  3168. #else
  3169. if (ret != HASH_TYPE_E)
  3170. return -3374;
  3171. #endif
  3172. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3173. #if !defined(NO_MD5) && !defined(NO_SHA)
  3174. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3175. return -3375;
  3176. #else
  3177. if (ret != HASH_TYPE_E)
  3178. return -3376;
  3179. #endif
  3180. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3181. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3182. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3183. return -3377;
  3184. #else
  3185. if (ret != HASH_TYPE_E)
  3186. return -3378;
  3187. #endif
  3188. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3189. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3190. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3191. return -3379;
  3192. #else
  3193. if (ret != HASH_TYPE_E)
  3194. return -3380;
  3195. #endif
  3196. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3197. if (ret != BAD_FUNC_ARG)
  3198. return -3381;
  3199. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3200. if (ret != BAD_FUNC_ARG)
  3201. return -3382;
  3202. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3203. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3204. ret = wc_GetCTC_HashOID(MD2);
  3205. if (ret == 0)
  3206. return -3383;
  3207. #endif
  3208. #ifndef NO_MD5
  3209. ret = wc_GetCTC_HashOID(WC_MD5);
  3210. if (ret == 0)
  3211. return -3384;
  3212. #endif
  3213. #ifndef NO_SHA
  3214. ret = wc_GetCTC_HashOID(WC_SHA);
  3215. if (ret == 0)
  3216. return -3385;
  3217. #endif
  3218. #ifdef WOLFSSL_SHA224
  3219. ret = wc_GetCTC_HashOID(WC_SHA224);
  3220. if (ret == 0)
  3221. return -3386;
  3222. #endif
  3223. #ifndef NO_SHA256
  3224. ret = wc_GetCTC_HashOID(WC_SHA256);
  3225. if (ret == 0)
  3226. return -3387;
  3227. #endif
  3228. #ifdef WOLFSSL_SHA384
  3229. ret = wc_GetCTC_HashOID(WC_SHA384);
  3230. if (ret == 0)
  3231. return -3388;
  3232. #endif
  3233. #ifdef WOLFSSL_SHA512
  3234. ret = wc_GetCTC_HashOID(WC_SHA512);
  3235. if (ret == 0)
  3236. return -3389;
  3237. #endif
  3238. ret = wc_GetCTC_HashOID(-1);
  3239. if (ret != 0)
  3240. return -3390;
  3241. #endif
  3242. return 0;
  3243. }
  3244. #if !defined(NO_HMAC) && !defined(NO_MD5)
  3245. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  3246. {
  3247. Hmac hmac;
  3248. byte hash[WC_MD5_DIGEST_SIZE];
  3249. const char* keys[]=
  3250. {
  3251. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3252. "Jefe",
  3253. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3254. };
  3255. testVector a, b, c;
  3256. testVector test_hmac[3];
  3257. int ret;
  3258. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3259. a.input = "Hi There";
  3260. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  3261. "\x9d";
  3262. a.inLen = XSTRLEN(a.input);
  3263. a.outLen = WC_MD5_DIGEST_SIZE;
  3264. b.input = "what do ya want for nothing?";
  3265. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  3266. "\x38";
  3267. b.inLen = XSTRLEN(b.input);
  3268. b.outLen = WC_MD5_DIGEST_SIZE;
  3269. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3270. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3271. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3272. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3273. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  3274. "\xf6";
  3275. c.inLen = XSTRLEN(c.input);
  3276. c.outLen = WC_MD5_DIGEST_SIZE;
  3277. test_hmac[0] = a;
  3278. test_hmac[1] = b;
  3279. test_hmac[2] = c;
  3280. for (i = 0; i < times; ++i) {
  3281. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3282. if (i == 1) {
  3283. continue; /* cavium can't handle short keys, fips not allowed */
  3284. }
  3285. #endif
  3286. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  3287. return -3400;
  3288. }
  3289. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  3290. (word32)XSTRLEN(keys[i]));
  3291. if (ret != 0)
  3292. return -3401;
  3293. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3294. (word32)test_hmac[i].inLen);
  3295. if (ret != 0)
  3296. return -3402;
  3297. ret = wc_HmacFinal(&hmac, hash);
  3298. if (ret != 0)
  3299. return -3403;
  3300. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  3301. return -3404 - i;
  3302. wc_HmacFree(&hmac);
  3303. }
  3304. #ifndef HAVE_FIPS
  3305. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  3306. return -3414;
  3307. #endif
  3308. return 0;
  3309. }
  3310. #endif /* NO_HMAC && NO_MD5 */
  3311. #if !defined(NO_HMAC) && !defined(NO_SHA)
  3312. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  3313. {
  3314. Hmac hmac;
  3315. byte hash[WC_SHA_DIGEST_SIZE];
  3316. const char* keys[]=
  3317. {
  3318. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3319. "\x0b\x0b\x0b",
  3320. "Jefe",
  3321. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3322. "\xAA\xAA\xAA"
  3323. };
  3324. testVector a, b, c;
  3325. testVector test_hmac[3];
  3326. int ret;
  3327. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3328. a.input = "Hi There";
  3329. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  3330. "\x8e\xf1\x46\xbe\x00";
  3331. a.inLen = XSTRLEN(a.input);
  3332. a.outLen = WC_SHA_DIGEST_SIZE;
  3333. b.input = "what do ya want for nothing?";
  3334. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  3335. "\x9c\x25\x9a\x7c\x79";
  3336. b.inLen = XSTRLEN(b.input);
  3337. b.outLen = WC_SHA_DIGEST_SIZE;
  3338. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3339. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3340. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3341. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3342. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  3343. "\x4f\x63\xf1\x75\xd3";
  3344. c.inLen = XSTRLEN(c.input);
  3345. c.outLen = WC_SHA_DIGEST_SIZE;
  3346. test_hmac[0] = a;
  3347. test_hmac[1] = b;
  3348. test_hmac[2] = c;
  3349. for (i = 0; i < times; ++i) {
  3350. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3351. if (i == 1)
  3352. continue; /* cavium can't handle short keys, fips not allowed */
  3353. #endif
  3354. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3355. return -3500;
  3356. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  3357. (word32)XSTRLEN(keys[i]));
  3358. if (ret != 0)
  3359. return -3501;
  3360. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3361. (word32)test_hmac[i].inLen);
  3362. if (ret != 0)
  3363. return -3502;
  3364. ret = wc_HmacFinal(&hmac, hash);
  3365. if (ret != 0)
  3366. return -3503;
  3367. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  3368. return -3504 - i;
  3369. wc_HmacFree(&hmac);
  3370. }
  3371. #ifndef HAVE_FIPS
  3372. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  3373. return -3514;
  3374. #endif
  3375. return 0;
  3376. }
  3377. #endif
  3378. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  3379. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  3380. {
  3381. Hmac hmac;
  3382. byte hash[WC_SHA224_DIGEST_SIZE];
  3383. const char* keys[]=
  3384. {
  3385. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3386. "\x0b\x0b\x0b",
  3387. "Jefe",
  3388. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3389. "\xAA\xAA\xAA",
  3390. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3391. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3392. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3393. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3394. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3395. };
  3396. testVector a, b, c, d;
  3397. testVector test_hmac[4];
  3398. int ret;
  3399. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3400. a.input = "Hi There";
  3401. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  3402. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  3403. a.inLen = XSTRLEN(a.input);
  3404. a.outLen = WC_SHA224_DIGEST_SIZE;
  3405. b.input = "what do ya want for nothing?";
  3406. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  3407. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  3408. b.inLen = XSTRLEN(b.input);
  3409. b.outLen = WC_SHA224_DIGEST_SIZE;
  3410. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3411. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3412. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3413. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3414. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  3415. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  3416. c.inLen = XSTRLEN(c.input);
  3417. c.outLen = WC_SHA224_DIGEST_SIZE;
  3418. d.input = "Big Key Input";
  3419. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  3420. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  3421. d.inLen = XSTRLEN(d.input);
  3422. d.outLen = WC_SHA224_DIGEST_SIZE;
  3423. test_hmac[0] = a;
  3424. test_hmac[1] = b;
  3425. test_hmac[2] = c;
  3426. test_hmac[3] = d;
  3427. for (i = 0; i < times; ++i) {
  3428. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3429. if (i == 1)
  3430. continue; /* cavium can't handle short keys, fips not allowed */
  3431. #endif
  3432. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3433. return -3600;
  3434. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  3435. (word32)XSTRLEN(keys[i]));
  3436. if (ret != 0)
  3437. return -3601;
  3438. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3439. (word32)test_hmac[i].inLen);
  3440. if (ret != 0)
  3441. return -3602;
  3442. ret = wc_HmacFinal(&hmac, hash);
  3443. if (ret != 0)
  3444. return -3603;
  3445. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3446. return -3604 - i;
  3447. wc_HmacFree(&hmac);
  3448. }
  3449. #ifndef HAVE_FIPS
  3450. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  3451. return -3614;
  3452. #endif
  3453. return 0;
  3454. }
  3455. #endif
  3456. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  3457. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  3458. {
  3459. Hmac hmac;
  3460. byte hash[WC_SHA256_DIGEST_SIZE];
  3461. const char* keys[]=
  3462. {
  3463. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3464. "\x0b\x0b\x0b",
  3465. "Jefe",
  3466. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3467. "\xAA\xAA\xAA",
  3468. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3469. "\xAA\xAA\xAA",
  3470. };
  3471. testVector a, b, c, d;
  3472. testVector test_hmac[4];
  3473. int ret;
  3474. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3475. a.input = "Hi There";
  3476. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  3477. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  3478. "\xcf\xf7";
  3479. a.inLen = XSTRLEN(a.input);
  3480. a.outLen = WC_SHA256_DIGEST_SIZE;
  3481. b.input = "what do ya want for nothing?";
  3482. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  3483. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  3484. "\x38\x43";
  3485. b.inLen = XSTRLEN(b.input);
  3486. b.outLen = WC_SHA256_DIGEST_SIZE;
  3487. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3488. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3489. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3490. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3491. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  3492. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  3493. "\x65\xfe";
  3494. c.inLen = XSTRLEN(c.input);
  3495. c.outLen = WC_SHA256_DIGEST_SIZE;
  3496. d.input = 0;
  3497. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  3498. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  3499. "\x3e\x46";
  3500. d.inLen = 0;
  3501. d.outLen = WC_SHA256_DIGEST_SIZE;
  3502. test_hmac[0] = a;
  3503. test_hmac[1] = b;
  3504. test_hmac[2] = c;
  3505. test_hmac[3] = d;
  3506. for (i = 0; i < times; ++i) {
  3507. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3508. if (i == 1)
  3509. continue; /* cavium can't handle short keys, fips not allowed */
  3510. #endif
  3511. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  3512. if (i == 3)
  3513. continue; /* QuickAssist can't handle empty HMAC */
  3514. #endif
  3515. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3516. return -3700 - i;
  3517. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  3518. (word32)XSTRLEN(keys[i]));
  3519. if (ret != 0)
  3520. return -3710 - i;
  3521. if (test_hmac[i].input != NULL) {
  3522. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3523. (word32)test_hmac[i].inLen);
  3524. if (ret != 0)
  3525. return -3720 - i;
  3526. }
  3527. ret = wc_HmacFinal(&hmac, hash);
  3528. if (ret != 0)
  3529. return -3730 - i;
  3530. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  3531. return -3740 - i;
  3532. wc_HmacFree(&hmac);
  3533. }
  3534. #ifndef HAVE_FIPS
  3535. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  3536. return -3750;
  3537. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  3538. return -3751;
  3539. #endif
  3540. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  3541. return -3752;
  3542. return 0;
  3543. }
  3544. #endif
  3545. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  3546. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  3547. {
  3548. Hmac hmac;
  3549. byte hash[WC_SHA384_DIGEST_SIZE];
  3550. const char* keys[]=
  3551. {
  3552. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3553. "\x0b\x0b\x0b",
  3554. "Jefe",
  3555. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3556. "\xAA\xAA\xAA",
  3557. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3558. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3559. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3560. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3561. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3562. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3563. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3564. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3565. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3566. };
  3567. testVector a, b, c, d;
  3568. testVector test_hmac[4];
  3569. int ret;
  3570. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3571. a.input = "Hi There";
  3572. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  3573. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  3574. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  3575. "\xfa\x9c\xb6";
  3576. a.inLen = XSTRLEN(a.input);
  3577. a.outLen = WC_SHA384_DIGEST_SIZE;
  3578. b.input = "what do ya want for nothing?";
  3579. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  3580. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  3581. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  3582. "\xb2\x16\x49";
  3583. b.inLen = XSTRLEN(b.input);
  3584. b.outLen = WC_SHA384_DIGEST_SIZE;
  3585. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3586. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3587. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3588. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3589. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  3590. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  3591. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  3592. "\xa3\x4f\x27";
  3593. c.inLen = XSTRLEN(c.input);
  3594. c.outLen = WC_SHA384_DIGEST_SIZE;
  3595. d.input = "Big Key Input";
  3596. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  3597. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  3598. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  3599. "\x57\x41\x69";
  3600. d.inLen = XSTRLEN(d.input);
  3601. d.outLen = WC_SHA384_DIGEST_SIZE;
  3602. test_hmac[0] = a;
  3603. test_hmac[1] = b;
  3604. test_hmac[2] = c;
  3605. test_hmac[3] = d;
  3606. for (i = 0; i < times; ++i) {
  3607. #if defined(HAVE_FIPS)
  3608. if (i == 1)
  3609. continue; /* fips not allowed */
  3610. #endif
  3611. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3612. return -3800;
  3613. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  3614. (word32)XSTRLEN(keys[i]));
  3615. if (ret != 0)
  3616. return -3801;
  3617. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3618. (word32)test_hmac[i].inLen);
  3619. if (ret != 0)
  3620. return -3802;
  3621. ret = wc_HmacFinal(&hmac, hash);
  3622. if (ret != 0)
  3623. return -3803;
  3624. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3625. return -3804 - i;
  3626. wc_HmacFree(&hmac);
  3627. }
  3628. #ifndef HAVE_FIPS
  3629. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  3630. return -3814;
  3631. #endif
  3632. return 0;
  3633. }
  3634. #endif
  3635. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  3636. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  3637. {
  3638. Hmac hmac;
  3639. byte hash[WC_SHA512_DIGEST_SIZE];
  3640. const char* keys[]=
  3641. {
  3642. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3643. "\x0b\x0b\x0b",
  3644. "Jefe",
  3645. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3646. "\xAA\xAA\xAA",
  3647. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3648. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3649. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3650. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3651. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3652. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3653. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3654. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3655. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3656. };
  3657. testVector a, b, c, d;
  3658. testVector test_hmac[4];
  3659. int ret;
  3660. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3661. a.input = "Hi There";
  3662. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  3663. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  3664. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  3665. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  3666. "\x3a\x12\x68\x54";
  3667. a.inLen = XSTRLEN(a.input);
  3668. a.outLen = WC_SHA512_DIGEST_SIZE;
  3669. b.input = "what do ya want for nothing?";
  3670. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  3671. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  3672. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  3673. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  3674. "\x38\xbc\xe7\x37";
  3675. b.inLen = XSTRLEN(b.input);
  3676. b.outLen = WC_SHA512_DIGEST_SIZE;
  3677. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3678. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3679. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3680. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3681. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  3682. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  3683. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  3684. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  3685. "\xe1\x32\x92\xfb";
  3686. c.inLen = XSTRLEN(c.input);
  3687. c.outLen = WC_SHA512_DIGEST_SIZE;
  3688. d.input = "Big Key Input";
  3689. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  3690. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  3691. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  3692. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  3693. "\x1e\x18\xfe\xfa";
  3694. d.inLen = XSTRLEN(d.input);
  3695. d.outLen = WC_SHA512_DIGEST_SIZE;
  3696. test_hmac[0] = a;
  3697. test_hmac[1] = b;
  3698. test_hmac[2] = c;
  3699. test_hmac[3] = d;
  3700. for (i = 0; i < times; ++i) {
  3701. #if defined(HAVE_FIPS)
  3702. if (i == 1)
  3703. continue; /* fips not allowed */
  3704. #endif
  3705. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3706. return -3900;
  3707. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  3708. (word32)XSTRLEN(keys[i]));
  3709. if (ret != 0)
  3710. return -3901;
  3711. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3712. (word32)test_hmac[i].inLen);
  3713. if (ret != 0)
  3714. return -3902;
  3715. ret = wc_HmacFinal(&hmac, hash);
  3716. if (ret != 0)
  3717. return -3903;
  3718. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3719. return -3904 - i;
  3720. wc_HmacFree(&hmac);
  3721. }
  3722. #ifndef HAVE_FIPS
  3723. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  3724. return -3914;
  3725. #endif
  3726. return 0;
  3727. }
  3728. #endif
  3729. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  3730. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  3731. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  3732. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  3733. {
  3734. Hmac hmac;
  3735. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3736. const char* key[4] =
  3737. {
  3738. "Jefe",
  3739. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3740. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3741. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3742. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3743. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3744. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3745. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3746. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3747. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3748. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3749. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3750. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3751. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3752. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3753. };
  3754. const char* input[4] =
  3755. {
  3756. "what do ya want for nothing?",
  3757. "Hi There",
  3758. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3759. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3760. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3761. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3762. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3763. "Big Key Input"
  3764. };
  3765. const int hashType[4] =
  3766. {
  3767. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  3768. };
  3769. const int hashSz[4] =
  3770. {
  3771. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  3772. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  3773. };
  3774. const char* output[16] =
  3775. {
  3776. /* key = jefe, input = what do ya want for nothing? */
  3777. /* HMAC-SHA3-224 */
  3778. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  3779. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  3780. /* HMAC-SHA3-256 */
  3781. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  3782. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  3783. /* HMAC-SHA3-384 */
  3784. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  3785. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  3786. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  3787. /* HMAC-SHA3-512 */
  3788. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  3789. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  3790. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  3791. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  3792. /* key = 0b..., input = Hi There */
  3793. /* HMAC-SHA3-224 */
  3794. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  3795. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  3796. /* HMAC-SHA3-256 */
  3797. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  3798. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  3799. /* HMAC-SHA3-384 */
  3800. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  3801. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  3802. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  3803. /* HMAC-SHA3-512 */
  3804. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  3805. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  3806. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  3807. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  3808. /* key = aa..., output = dd... */
  3809. /* HMAC-SHA3-224 */
  3810. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  3811. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  3812. /* HMAC-SHA3-256 */
  3813. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  3814. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  3815. /* HMAC-SHA3-384 */
  3816. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  3817. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  3818. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  3819. /* HMAC-SHA3-512 */
  3820. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  3821. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  3822. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  3823. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  3824. /* key = big key, input = Big Key Input */
  3825. /* HMAC-SHA3-224 */
  3826. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  3827. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  3828. /* HMAC-SHA3-256 */
  3829. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  3830. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  3831. /* HMAC-SHA3-384 */
  3832. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  3833. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  3834. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  3835. /* HMAC-SHA3-512 */
  3836. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  3837. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  3838. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  3839. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  3840. };
  3841. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  3842. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  3843. ret;
  3844. #ifdef HAVE_FIPS
  3845. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  3846. * short. Skip it in FIPS builds. */
  3847. i = 1;
  3848. #endif
  3849. for (; i < iMax; i++) {
  3850. for (j = 0; j < jMax; j++) {
  3851. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3852. return -4000;
  3853. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  3854. (word32)XSTRLEN(key[i]));
  3855. if (ret != 0)
  3856. return -4001;
  3857. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  3858. (word32)XSTRLEN(input[i]));
  3859. if (ret != 0)
  3860. return -4002;
  3861. ret = wc_HmacFinal(&hmac, hash);
  3862. if (ret != 0)
  3863. return -4003;
  3864. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  3865. return -4004;
  3866. wc_HmacFree(&hmac);
  3867. if (i > 0)
  3868. continue;
  3869. #ifndef HAVE_FIPS
  3870. ret = wc_HmacSizeByType(hashType[j]);
  3871. if (ret != hashSz[j])
  3872. return -4005;
  3873. #endif
  3874. }
  3875. }
  3876. return 0;
  3877. }
  3878. #endif
  3879. #ifdef WC_RC2
  3880. typedef struct rc2TestVector {
  3881. const char* input;
  3882. const char* output;
  3883. const char* key; /* Key, variable up to 128 bytes */
  3884. const char* iv; /* IV, 8-bytes */
  3885. int inLen;
  3886. int outLen;
  3887. int keyLen;
  3888. int effectiveKeyBits; /* Up to 1024 bits supported */
  3889. } rc2TestVector;
  3890. static int rc2_ecb_test(void)
  3891. {
  3892. int ret = 0;
  3893. byte cipher[RC2_BLOCK_SIZE];
  3894. byte plain[RC2_BLOCK_SIZE];
  3895. rc2TestVector a, b, c, d, e, f, g, h;
  3896. rc2TestVector test_rc2[8];
  3897. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  3898. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3899. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  3900. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3901. a.inLen = RC2_BLOCK_SIZE;
  3902. a.outLen = RC2_BLOCK_SIZE;
  3903. a.keyLen = 8;
  3904. a.effectiveKeyBits = 63;
  3905. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  3906. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  3907. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  3908. b.inLen = RC2_BLOCK_SIZE;
  3909. b.outLen = RC2_BLOCK_SIZE;
  3910. b.keyLen = 8;
  3911. b.effectiveKeyBits = 64;
  3912. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  3913. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  3914. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  3915. c.inLen = RC2_BLOCK_SIZE;
  3916. c.outLen = RC2_BLOCK_SIZE;
  3917. c.keyLen = 8;
  3918. c.effectiveKeyBits = 64;
  3919. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3920. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  3921. d.key = "\x88";
  3922. d.inLen = RC2_BLOCK_SIZE;
  3923. d.outLen = RC2_BLOCK_SIZE;
  3924. d.keyLen = 1;
  3925. d.effectiveKeyBits = 64;
  3926. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3927. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  3928. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  3929. e.inLen = RC2_BLOCK_SIZE;
  3930. e.outLen = RC2_BLOCK_SIZE;
  3931. e.keyLen = 7;
  3932. e.effectiveKeyBits = 64;
  3933. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3934. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  3935. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3936. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  3937. f.inLen = RC2_BLOCK_SIZE;
  3938. f.outLen = RC2_BLOCK_SIZE;
  3939. f.keyLen = 16;
  3940. f.effectiveKeyBits = 64;
  3941. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3942. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  3943. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3944. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  3945. g.inLen = RC2_BLOCK_SIZE;
  3946. g.outLen = RC2_BLOCK_SIZE;
  3947. g.keyLen = 16;
  3948. g.effectiveKeyBits = 128;
  3949. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3950. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  3951. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3952. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  3953. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  3954. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  3955. "\x1e";
  3956. h.inLen = RC2_BLOCK_SIZE;
  3957. h.outLen = RC2_BLOCK_SIZE;
  3958. h.keyLen = 33;
  3959. h.effectiveKeyBits = 129;
  3960. test_rc2[0] = a;
  3961. test_rc2[1] = b;
  3962. test_rc2[2] = c;
  3963. test_rc2[3] = d;
  3964. test_rc2[4] = e;
  3965. test_rc2[5] = f;
  3966. test_rc2[6] = g;
  3967. test_rc2[7] = h;
  3968. for (i = 0; i < times; ++i) {
  3969. Rc2 enc;
  3970. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  3971. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  3972. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  3973. NULL, test_rc2[i].effectiveKeyBits);
  3974. if (ret != 0) {
  3975. return -4100;
  3976. }
  3977. /* ECB encrypt */
  3978. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  3979. (word32)test_rc2[i].outLen);
  3980. if (ret != 0) {
  3981. return -4101;
  3982. }
  3983. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  3984. return -4102;
  3985. }
  3986. /* ECB decrypt */
  3987. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  3988. if (ret != 0) {
  3989. return -4103;
  3990. }
  3991. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  3992. return -4104;
  3993. }
  3994. }
  3995. return 0;
  3996. }
  3997. static int rc2_cbc_test(void)
  3998. {
  3999. int ret = 0;
  4000. byte cipher[128];
  4001. byte plain[128];
  4002. rc2TestVector a, b, c, d, e, f, g, h, i;
  4003. rc2TestVector test_rc2[9];
  4004. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4005. /* key length = 7, effective key bits = 63 */
  4006. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4007. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4008. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4009. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4010. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4011. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4012. a.inLen = RC2_BLOCK_SIZE*2;
  4013. a.outLen = RC2_BLOCK_SIZE*2;
  4014. a.keyLen = 8;
  4015. a.effectiveKeyBits = 63;
  4016. /* key length = 8, effective key bits = 64, all 0xFF */
  4017. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4018. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4019. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4020. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4021. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4022. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4023. b.inLen = RC2_BLOCK_SIZE*2;
  4024. b.outLen = RC2_BLOCK_SIZE*2;
  4025. b.keyLen = 8;
  4026. b.effectiveKeyBits = 64;
  4027. /* key length = 8, effective key bits = 64 */
  4028. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4029. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4030. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4031. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4032. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4033. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4034. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4035. c.inLen = RC2_BLOCK_SIZE*2;
  4036. c.outLen = RC2_BLOCK_SIZE*2;
  4037. c.keyLen = 8;
  4038. c.effectiveKeyBits = 64;
  4039. /* key length = 1, effective key bits = 64 */
  4040. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4041. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4042. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4043. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4044. d.key = "\x88";
  4045. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4046. d.inLen = RC2_BLOCK_SIZE*2;
  4047. d.outLen = RC2_BLOCK_SIZE*2;
  4048. d.keyLen = 1;
  4049. d.effectiveKeyBits = 64;
  4050. /* key length = 7, effective key bits = 64 */
  4051. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4052. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4053. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4054. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4055. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4056. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4057. e.inLen = RC2_BLOCK_SIZE*2;
  4058. e.outLen = RC2_BLOCK_SIZE*2;
  4059. e.keyLen = 7;
  4060. e.effectiveKeyBits = 64;
  4061. /* key length = 16, effective key bits = 64 */
  4062. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4063. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4064. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4065. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4066. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4067. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4068. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4069. f.inLen = RC2_BLOCK_SIZE*2;
  4070. f.outLen = RC2_BLOCK_SIZE*2;
  4071. f.keyLen = 16;
  4072. f.effectiveKeyBits = 64;
  4073. /* key length = 16, effective bits = 128 */
  4074. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4075. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4076. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4077. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4078. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4079. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4080. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4081. g.inLen = RC2_BLOCK_SIZE*2;
  4082. g.outLen = RC2_BLOCK_SIZE*2;
  4083. g.keyLen = 16;
  4084. g.effectiveKeyBits = 128;
  4085. /* key length = 33, effective bits = 129 */
  4086. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4087. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4088. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4089. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  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.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4096. h.inLen = RC2_BLOCK_SIZE*2;
  4097. h.outLen = RC2_BLOCK_SIZE*2;
  4098. h.keyLen = 33;
  4099. h.effectiveKeyBits = 129;
  4100. /* key length = 10, effective bits = 40 */
  4101. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4102. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4103. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4104. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4105. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4106. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4107. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4108. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4109. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4110. "\x3E\x96";
  4111. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4112. i.inLen = RC2_BLOCK_SIZE*4;
  4113. i.outLen = RC2_BLOCK_SIZE*4;
  4114. i.keyLen = 10;
  4115. i.effectiveKeyBits = 40;
  4116. test_rc2[0] = a;
  4117. test_rc2[1] = b;
  4118. test_rc2[2] = c;
  4119. test_rc2[3] = d;
  4120. test_rc2[4] = e;
  4121. test_rc2[5] = f;
  4122. test_rc2[6] = g;
  4123. test_rc2[7] = h;
  4124. test_rc2[8] = i;
  4125. for (j = 0; j < times; ++j) {
  4126. Rc2 rc2;
  4127. XMEMSET(cipher, 0, sizeof(cipher));
  4128. XMEMSET(plain, 0, sizeof(plain));
  4129. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4130. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4131. if (ret != 0) {
  4132. return -4200;
  4133. }
  4134. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4135. test_rc2[j].inLen);
  4136. if (ret != 0) {
  4137. return -4201;
  4138. }
  4139. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4140. return -4202;
  4141. }
  4142. /* reset IV for decrypt, since overriden by encrypt operation */
  4143. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4144. if (ret != 0) {
  4145. return -4203;
  4146. }
  4147. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4148. if (ret != 0) {
  4149. return -4204;
  4150. }
  4151. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4152. return -4205;
  4153. }
  4154. }
  4155. return 0;
  4156. }
  4157. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4158. {
  4159. int ret = 0;
  4160. ret = rc2_ecb_test();
  4161. if (ret != 0) {
  4162. return ret;
  4163. }
  4164. return rc2_cbc_test();
  4165. }
  4166. #endif
  4167. #ifndef NO_RC4
  4168. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4169. {
  4170. byte cipher[16];
  4171. byte plain[16];
  4172. const char* keys[] =
  4173. {
  4174. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4175. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4176. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4177. "\xef\x01\x23\x45"
  4178. };
  4179. testVector a, b, c, d;
  4180. testVector test_arc4[4];
  4181. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4182. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4183. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4184. a.inLen = 8;
  4185. a.outLen = 8;
  4186. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4187. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4188. b.inLen = 8;
  4189. b.outLen = 8;
  4190. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4191. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4192. c.inLen = 8;
  4193. c.outLen = 8;
  4194. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4195. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4196. d.inLen = 10;
  4197. d.outLen = 10;
  4198. test_arc4[0] = a;
  4199. test_arc4[1] = b;
  4200. test_arc4[2] = c;
  4201. test_arc4[3] = d;
  4202. for (i = 0; i < times; ++i) {
  4203. Arc4 enc;
  4204. Arc4 dec;
  4205. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4206. if (i == 3)
  4207. keylen = 4;
  4208. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4209. return -4400;
  4210. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4211. return -4401;
  4212. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4213. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4214. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4215. (word32)test_arc4[i].outLen);
  4216. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4217. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4218. return -4402 - i;
  4219. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4220. return -4412 - i;
  4221. wc_Arc4Free(&enc);
  4222. wc_Arc4Free(&dec);
  4223. }
  4224. return 0;
  4225. }
  4226. #endif
  4227. WOLFSSL_TEST_SUBROUTINE int hc128_test(void)
  4228. {
  4229. #ifdef HAVE_HC128
  4230. byte cipher[16];
  4231. byte plain[16];
  4232. const char* keys[] =
  4233. {
  4234. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4235. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4236. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  4237. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  4238. };
  4239. const char* ivs[] =
  4240. {
  4241. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4242. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4243. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  4244. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  4245. };
  4246. testVector a, b, c, d;
  4247. testVector test_hc128[4];
  4248. int times = sizeof(test_hc128) / sizeof(testVector), i;
  4249. int ret = 0;
  4250. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  4251. HC128 enc[1], dec[1];
  4252. #else
  4253. HC128 *enc = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4254. HC128 *dec = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4255. if ((! enc) || (! dec)) {
  4256. ERROR_OUT(-4500, out);
  4257. }
  4258. #endif
  4259. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4260. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  4261. a.inLen = 8;
  4262. a.outLen = 8;
  4263. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4264. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  4265. b.inLen = 8;
  4266. b.outLen = 8;
  4267. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4268. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  4269. c.inLen = 8;
  4270. c.outLen = 8;
  4271. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4272. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  4273. d.inLen = 15;
  4274. d.outLen = 15;
  4275. test_hc128[0] = a;
  4276. test_hc128[1] = b;
  4277. test_hc128[2] = c;
  4278. test_hc128[3] = d;
  4279. for (i = 0; i < times; ++i) {
  4280. /* align keys/ivs in plain/cipher buffers */
  4281. XMEMCPY(plain, keys[i], 16);
  4282. XMEMCPY(cipher, ivs[i], 16);
  4283. wc_Hc128_SetKey(enc, plain, cipher);
  4284. wc_Hc128_SetKey(dec, plain, cipher);
  4285. /* align input */
  4286. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  4287. if (wc_Hc128_Process(enc, cipher, plain,
  4288. (word32)test_hc128[i].outLen) != 0) {
  4289. ret = -4501;
  4290. goto out;
  4291. }
  4292. if (wc_Hc128_Process(dec, plain, cipher,
  4293. (word32)test_hc128[i].outLen) != 0) {
  4294. ret = -4502;
  4295. goto out;
  4296. }
  4297. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen)) {
  4298. ret = -4503 - i;
  4299. goto out;
  4300. }
  4301. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen)) {
  4302. ret = -4513 - i;
  4303. goto out;
  4304. }
  4305. }
  4306. out:
  4307. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4308. if (enc)
  4309. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4310. if (dec)
  4311. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4312. #endif
  4313. return ret;
  4314. #else
  4315. return 0;
  4316. #endif /* HAVE_HC128 */
  4317. }
  4318. #ifndef NO_RABBIT
  4319. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void)
  4320. {
  4321. byte cipher[16];
  4322. byte plain[16];
  4323. const char* keys[] =
  4324. {
  4325. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4326. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4327. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  4328. };
  4329. const char* ivs[] =
  4330. {
  4331. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4332. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  4333. 0
  4334. };
  4335. testVector a, b, c;
  4336. testVector test_rabbit[3];
  4337. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  4338. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4339. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  4340. a.inLen = 8;
  4341. a.outLen = 8;
  4342. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4343. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  4344. b.inLen = 8;
  4345. b.outLen = 8;
  4346. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4347. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  4348. c.inLen = 8;
  4349. c.outLen = 8;
  4350. test_rabbit[0] = a;
  4351. test_rabbit[1] = b;
  4352. test_rabbit[2] = c;
  4353. for (i = 0; i < times; ++i) {
  4354. Rabbit enc;
  4355. Rabbit dec;
  4356. byte* iv;
  4357. /* align keys/ivs in plain/cipher buffers */
  4358. XMEMCPY(plain, keys[i], 16);
  4359. if (ivs[i]) {
  4360. XMEMCPY(cipher, ivs[i], 8);
  4361. iv = cipher;
  4362. } else
  4363. iv = NULL;
  4364. wc_RabbitSetKey(&enc, plain, iv);
  4365. wc_RabbitSetKey(&dec, plain, iv);
  4366. /* align input */
  4367. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  4368. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  4369. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  4370. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  4371. return -4600 - i;
  4372. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  4373. return -4610 - i;
  4374. }
  4375. return 0;
  4376. }
  4377. #endif /* NO_RABBIT */
  4378. #ifdef HAVE_CHACHA
  4379. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  4380. {
  4381. ChaCha enc;
  4382. ChaCha dec;
  4383. byte cipher[128];
  4384. byte plain[128];
  4385. byte sliver[64];
  4386. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  4387. word32 keySz = 32;
  4388. int ret = 0;
  4389. int i;
  4390. int times = 4;
  4391. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  4392. {
  4393. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4394. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4395. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4396. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4397. };
  4398. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  4399. {
  4400. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4401. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4402. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4403. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  4404. };
  4405. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  4406. {
  4407. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4408. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4409. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4410. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4411. };
  4412. /* 128 bit key */
  4413. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  4414. {
  4415. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4416. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4417. };
  4418. const byte* keys[] = {key1, key2, key3, key4};
  4419. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4420. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4421. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  4422. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4423. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  4424. #ifndef BENCH_EMBEDDED
  4425. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  4426. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  4427. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  4428. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  4429. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  4430. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  4431. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  4432. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  4433. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  4434. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  4435. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  4436. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  4437. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  4438. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  4439. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  4440. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  4441. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  4442. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  4443. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  4444. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  4445. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  4446. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  4447. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  4448. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  4449. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  4450. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  4451. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  4452. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  4453. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  4454. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  4455. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  4456. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  4457. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  4458. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  4459. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  4460. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  4461. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  4462. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  4463. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  4464. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  4465. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  4466. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  4467. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  4468. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  4469. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  4470. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  4471. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  4472. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  4473. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  4474. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  4475. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  4476. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  4477. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  4478. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  4479. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  4480. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  4481. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  4482. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  4483. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  4484. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  4485. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  4486. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  4487. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  4488. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  4489. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  4490. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  4491. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  4492. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  4493. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  4494. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  4495. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  4496. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  4497. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  4498. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  4499. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  4500. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  4501. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  4502. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  4503. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  4504. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  4505. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  4506. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  4507. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  4508. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  4509. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  4510. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  4511. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  4512. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  4513. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  4514. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  4515. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  4516. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  4517. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  4518. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  4519. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  4520. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  4521. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  4522. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  4523. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  4524. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  4525. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  4526. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  4527. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  4528. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  4529. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  4530. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  4531. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  4532. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  4533. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  4534. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  4535. };
  4536. #define CHACHA_BIG_TEST_SIZE 1305
  4537. #ifndef WOLFSSL_SMALL_STACK
  4538. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  4539. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  4540. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  4541. #else
  4542. byte* cipher_big;
  4543. byte* plain_big;
  4544. byte* input_big;
  4545. #endif /* WOLFSSL_SMALL_STACK */
  4546. int block_size;
  4547. #endif /* BENCH_EMBEDDED */
  4548. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  4549. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  4550. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  4551. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  4552. byte* test_chacha[4];
  4553. test_chacha[0] = a;
  4554. test_chacha[1] = b;
  4555. test_chacha[2] = c;
  4556. test_chacha[3] = d;
  4557. #ifndef BENCH_EMBEDDED
  4558. #ifdef WOLFSSL_SMALL_STACK
  4559. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4560. DYNAMIC_TYPE_TMP_BUFFER);
  4561. if (cipher_big == NULL) {
  4562. return MEMORY_E;
  4563. }
  4564. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4565. DYNAMIC_TYPE_TMP_BUFFER);
  4566. if (plain_big == NULL) {
  4567. return MEMORY_E;
  4568. }
  4569. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4570. DYNAMIC_TYPE_TMP_BUFFER);
  4571. if (input_big == NULL) {
  4572. return MEMORY_E;
  4573. }
  4574. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  4575. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  4576. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  4577. #endif /* WOLFSSL_SMALL_STACK */
  4578. #endif /* BENCH_EMBEDDED */
  4579. for (i = 0; i < times; ++i) {
  4580. if (i < 3) {
  4581. keySz = 32;
  4582. }
  4583. else {
  4584. keySz = 16;
  4585. }
  4586. XMEMCPY(plain, keys[i], keySz);
  4587. XMEMSET(cipher, 0, 32);
  4588. XMEMCPY(cipher + 4, ivs[i], 8);
  4589. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  4590. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  4591. if (ret != 0)
  4592. return ret;
  4593. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4594. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  4595. if (ret != 0)
  4596. return ret;
  4597. XMEMCPY(plain, input, 8);
  4598. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  4599. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  4600. if (ret != 0)
  4601. return ret;
  4602. if (XMEMCMP(test_chacha[i], cipher, 8))
  4603. return -4700 - i;
  4604. if (XMEMCMP(plain, input, 8))
  4605. return -4710 - i;
  4606. }
  4607. /* test of starting at a different counter
  4608. encrypts all of the information and decrypts starting at 2nd chunk */
  4609. XMEMSET(plain, 0, sizeof(plain));
  4610. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  4611. XMEMSET(cipher, 0, sizeof(cipher));
  4612. XMEMCPY(cipher + 4, ivs[0], 8);
  4613. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4614. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4615. if (ret != 0)
  4616. return ret;
  4617. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4618. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  4619. if (ret != 0)
  4620. return ret;
  4621. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  4622. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  4623. if (ret != 0)
  4624. return ret;
  4625. if (XMEMCMP(plain + 64, sliver, 64))
  4626. return -4720;
  4627. #ifndef BENCH_EMBEDDED
  4628. /* test of encrypting more data */
  4629. keySz = 32;
  4630. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4631. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4632. if (ret != 0)
  4633. return ret;
  4634. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4635. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4636. if (ret != 0)
  4637. return ret;
  4638. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  4639. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  4640. CHACHA_BIG_TEST_SIZE);
  4641. if (ret != 0)
  4642. return ret;
  4643. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4644. return -4721;
  4645. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4646. return -4722;
  4647. for (i = 0; i < 18; ++i) {
  4648. /* this will test all paths */
  4649. // block sizes: 1 2 3 4 7 8 15 16 31 32 63 64 127 128 255 256 511 512
  4650. block_size = (2 << (i%9)) - (i<9?1:0);
  4651. keySz = 32;
  4652. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4653. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4654. if (ret != 0)
  4655. return ret;
  4656. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4657. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4658. if (ret != 0)
  4659. return ret;
  4660. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, block_size);
  4661. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big, block_size);
  4662. if (ret != 0)
  4663. return ret;
  4664. if (XMEMCMP(plain_big, input_big, block_size))
  4665. return -4723-i;
  4666. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  4667. return -4724-i;
  4668. }
  4669. /* Streaming test */
  4670. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  4671. int j, rem;
  4672. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  4673. if (ret != 0)
  4674. return -4725;
  4675. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  4676. if (ret != 0)
  4677. return -4726;
  4678. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  4679. if (ret != 0)
  4680. return -4727;
  4681. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  4682. if (ret != 0)
  4683. return -4728;
  4684. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  4685. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  4686. if (ret != 0)
  4687. return -4729;
  4688. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  4689. if (ret != 0)
  4690. return -4730;
  4691. }
  4692. rem = CHACHA_BIG_TEST_SIZE - j;
  4693. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  4694. if (ret != 0)
  4695. return -4731;
  4696. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  4697. if (ret != 0)
  4698. return -4732;
  4699. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4700. return -4733;
  4701. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4702. return -4734;
  4703. }
  4704. #ifdef WOLFSSL_SMALL_STACK
  4705. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4706. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4707. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4708. #endif /* WOLFSSL_SMALL_STACK */
  4709. #endif /* BENCH_EMBEDDED */
  4710. return 0;
  4711. }
  4712. #endif /* HAVE_CHACHA */
  4713. #ifdef HAVE_POLY1305
  4714. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  4715. {
  4716. int ret = 0;
  4717. int i;
  4718. byte tag[16];
  4719. Poly1305 enc;
  4720. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  4721. {
  4722. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  4723. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  4724. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  4725. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  4726. 0x75,0x70
  4727. };
  4728. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  4729. {
  4730. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  4731. 0x6c,0x64,0x21
  4732. };
  4733. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  4734. {
  4735. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4736. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4737. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4738. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4739. };
  4740. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  4741. {
  4742. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4743. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4744. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4745. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4746. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4747. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4748. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4749. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4750. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4751. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4752. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4753. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4754. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4755. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4756. 0x61,0x16
  4757. };
  4758. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  4759. {
  4760. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4761. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4762. };
  4763. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  4764. {
  4765. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4766. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4767. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4768. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4769. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4770. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4771. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4772. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4773. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4774. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4775. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4776. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4777. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4778. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4779. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4780. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4781. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4782. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4783. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4784. 0x61,0x16
  4785. };
  4786. byte additional[] =
  4787. {
  4788. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  4789. 0xc4,0xc5,0xc6,0xc7
  4790. };
  4791. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  4792. {
  4793. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4794. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4795. };
  4796. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  4797. {
  4798. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  4799. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  4800. };
  4801. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  4802. {
  4803. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  4804. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  4805. };
  4806. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  4807. {
  4808. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  4809. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  4810. };
  4811. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  4812. {
  4813. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  4814. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  4815. };
  4816. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  4817. {
  4818. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4819. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4820. };
  4821. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  4822. {
  4823. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  4824. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  4825. };
  4826. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  4827. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  4828. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  4829. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4830. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4831. };
  4832. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4833. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  4834. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  4835. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  4836. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  4837. };
  4838. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  4839. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  4840. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  4841. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  4842. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  4843. };
  4844. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  4845. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4846. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4847. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4848. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4849. };
  4850. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  4851. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  4852. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  4853. const byte* keys[] = {key, key, key2, key2, key5, key};
  4854. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  4855. correct6};
  4856. for (i = 0; i < 6; i++) {
  4857. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  4858. if (ret != 0)
  4859. return -4800 - i;
  4860. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  4861. if (ret != 0)
  4862. return -4810 - i;
  4863. ret = wc_Poly1305Final(&enc, tag);
  4864. if (ret != 0)
  4865. return -4820 - i;
  4866. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  4867. return -4830 - i;
  4868. }
  4869. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  4870. XMEMSET(tag, 0, sizeof(tag));
  4871. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  4872. if (ret != 0)
  4873. return -4840;
  4874. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4875. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4876. if (ret != 0)
  4877. return -4841;
  4878. if (XMEMCMP(tag, correct4, sizeof(tag)))
  4879. return -4842;
  4880. /* Check fail of TLS MAC function if altering additional data */
  4881. XMEMSET(tag, 0, sizeof(tag));
  4882. additional[0]++;
  4883. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4884. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4885. if (ret != 0)
  4886. return -4843;
  4887. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  4888. return -4844;
  4889. return 0;
  4890. }
  4891. #endif /* HAVE_POLY1305 */
  4892. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4893. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  4894. {
  4895. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  4896. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4897. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  4898. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  4899. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  4900. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  4901. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  4902. };
  4903. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  4904. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  4905. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  4906. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  4907. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  4908. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  4909. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  4910. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  4911. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  4912. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  4913. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  4914. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  4915. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  4916. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  4917. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  4918. 0x74, 0x2e
  4919. };
  4920. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  4921. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  4922. 0x44, 0x45, 0x46, 0x47
  4923. };
  4924. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  4925. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  4926. 0xc4, 0xc5, 0xc6, 0xc7
  4927. };
  4928. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  4929. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  4930. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  4931. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  4932. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  4933. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  4934. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  4935. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  4936. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  4937. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  4938. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  4939. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  4940. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  4941. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  4942. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  4943. 0x61, 0x16
  4944. };
  4945. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  4946. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  4947. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  4948. };
  4949. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  4950. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4951. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4952. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  4953. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  4954. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  4955. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  4956. };
  4957. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  4958. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  4959. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  4960. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  4961. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4962. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  4963. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  4964. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  4965. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  4966. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  4967. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  4968. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  4969. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  4970. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  4971. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  4972. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  4973. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4974. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  4975. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  4976. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  4977. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  4978. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  4979. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  4980. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  4981. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  4982. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  4983. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  4984. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  4985. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  4986. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  4987. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  4988. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  4989. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  4990. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  4991. 0x9d
  4992. };
  4993. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  4994. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  4995. 0x05, 0x06, 0x07, 0x08
  4996. };
  4997. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  4998. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  4999. 0x00, 0x00, 0x4e, 0x91
  5000. };
  5001. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5002. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5003. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5004. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5005. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5006. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5007. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5008. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5009. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5010. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5011. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5012. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5013. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5014. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5015. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5016. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5017. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5018. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5019. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5020. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5021. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5022. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5023. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5024. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5025. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5026. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5027. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5028. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5029. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5030. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5031. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5032. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5033. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5034. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5035. 0x9b
  5036. };
  5037. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5038. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5039. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5040. };
  5041. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5042. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5043. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5044. int err;
  5045. ChaChaPoly_Aead aead;
  5046. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5047. #define TEST_SMALL_CHACHA_CHUNKS 32
  5048. #else
  5049. #define TEST_SMALL_CHACHA_CHUNKS 64
  5050. #endif
  5051. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5052. word32 testLen;
  5053. #endif
  5054. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5055. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5056. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5057. /* Parameter Validation testing */
  5058. /* Encrypt */
  5059. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5060. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5061. if (err != BAD_FUNC_ARG)
  5062. return -4900;
  5063. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5064. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5065. generatedAuthTag);
  5066. if (err != BAD_FUNC_ARG)
  5067. return -4901;
  5068. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5069. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5070. if (err != BAD_FUNC_ARG)
  5071. return -4902;
  5072. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5073. sizeof(plaintext1), NULL, generatedAuthTag);
  5074. if (err != BAD_FUNC_ARG)
  5075. return -4903;
  5076. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5077. sizeof(plaintext1), generatedCiphertext, NULL);
  5078. if (err != BAD_FUNC_ARG)
  5079. return -4904;
  5080. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5081. 0, generatedCiphertext, generatedAuthTag);
  5082. if (err != BAD_FUNC_ARG)
  5083. return -4905;
  5084. /* Decrypt */
  5085. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5086. sizeof(cipher2), authTag2, generatedPlaintext);
  5087. if (err != BAD_FUNC_ARG)
  5088. return -4906;
  5089. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5090. sizeof(cipher2), authTag2, generatedPlaintext);
  5091. if (err != BAD_FUNC_ARG)
  5092. return -4907;
  5093. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5094. sizeof(cipher2), authTag2, generatedPlaintext);
  5095. if (err != BAD_FUNC_ARG)
  5096. return -4908;
  5097. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5098. sizeof(cipher2), NULL, generatedPlaintext);
  5099. if (err != BAD_FUNC_ARG)
  5100. return -4909;
  5101. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5102. sizeof(cipher2), authTag2, NULL);
  5103. if (err != BAD_FUNC_ARG)
  5104. return -4910;
  5105. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5106. 0, authTag2, generatedPlaintext);
  5107. if (err != BAD_FUNC_ARG)
  5108. return -4911;
  5109. /* Test #1 */
  5110. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5111. aad1, sizeof(aad1),
  5112. plaintext1, sizeof(plaintext1),
  5113. generatedCiphertext, generatedAuthTag);
  5114. if (err) {
  5115. return err;
  5116. }
  5117. /* -- Check the ciphertext and authtag */
  5118. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5119. return -4912;
  5120. }
  5121. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5122. return -4913;
  5123. }
  5124. /* -- Verify decryption works */
  5125. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5126. aad1, sizeof(aad1),
  5127. cipher1, sizeof(cipher1),
  5128. authTag1, generatedPlaintext);
  5129. if (err) {
  5130. return err;
  5131. }
  5132. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5133. return -4914;
  5134. }
  5135. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5136. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5137. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5138. /* Test #2 */
  5139. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5140. aad2, sizeof(aad2),
  5141. plaintext2, sizeof(plaintext2),
  5142. generatedCiphertext, generatedAuthTag);
  5143. if (err) {
  5144. return err;
  5145. }
  5146. /* -- Check the ciphertext and authtag */
  5147. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5148. return -4915;
  5149. }
  5150. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5151. return -4916;
  5152. }
  5153. /* -- Verify decryption works */
  5154. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5155. aad2, sizeof(aad2),
  5156. cipher2, sizeof(cipher2),
  5157. authTag2, generatedPlaintext);
  5158. if (err) {
  5159. return err;
  5160. }
  5161. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5162. return -4917;
  5163. }
  5164. /* AEAD init/update/final - bad argument tests */
  5165. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5166. CHACHA20_POLY1305_AEAD_DECRYPT);
  5167. if (err != BAD_FUNC_ARG)
  5168. return -4918;
  5169. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5170. CHACHA20_POLY1305_AEAD_DECRYPT);
  5171. if (err != BAD_FUNC_ARG)
  5172. return -4919;
  5173. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5174. CHACHA20_POLY1305_AEAD_DECRYPT);
  5175. if (err != BAD_FUNC_ARG)
  5176. return -4920;
  5177. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5178. if (err != BAD_FUNC_ARG)
  5179. return -4921;
  5180. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5181. if (err != BAD_FUNC_ARG)
  5182. return -4922;
  5183. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5184. generatedPlaintext, sizeof(plaintext1));
  5185. if (err != BAD_FUNC_ARG)
  5186. return -4923;
  5187. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5188. sizeof(plaintext1));
  5189. if (err != BAD_FUNC_ARG)
  5190. return -4924;
  5191. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5192. sizeof(plaintext1));
  5193. if (err != BAD_FUNC_ARG)
  5194. return -4925;
  5195. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5196. if (err != BAD_FUNC_ARG)
  5197. return -4926;
  5198. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5199. if (err != BAD_FUNC_ARG)
  5200. return -4927;
  5201. /* AEAD init/update/final - bad state tests */
  5202. /* clear struct - make valgrind happy to resolve
  5203. "Conditional jump or move depends on uninitialised value(s)".
  5204. The enum is "int" size and aead.state is "byte" */
  5205. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5206. XMEMSET(&aead, 0, sizeof(aead));
  5207. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5208. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5209. if (err != BAD_STATE_E)
  5210. return -4928;
  5211. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5212. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5213. if (err != BAD_STATE_E)
  5214. return -4929;
  5215. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5216. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5217. generatedPlaintext, sizeof(plaintext1));
  5218. if (err != BAD_STATE_E)
  5219. return -4930;
  5220. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5221. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5222. if (err != BAD_STATE_E)
  5223. return -4931;
  5224. aead.state = CHACHA20_POLY1305_STATE_READY;
  5225. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5226. if (err != BAD_STATE_E)
  5227. return -4932;
  5228. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5229. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5230. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5231. /* Test 1 - Encrypt */
  5232. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5233. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5234. if (err != 0)
  5235. return -4933;
  5236. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5237. if (err != 0)
  5238. return -4934;
  5239. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5240. /* test doing data in smaller chunks */
  5241. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5242. word32 dataLen = sizeof(plaintext1) - testLen;
  5243. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5244. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5245. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5246. &generatedCiphertext[testLen], dataLen);
  5247. if (err != 0)
  5248. return -4935;
  5249. testLen += dataLen;
  5250. }
  5251. #else
  5252. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5253. generatedCiphertext, sizeof(plaintext1));
  5254. #endif
  5255. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5256. if (err != 0)
  5257. return -4936;
  5258. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5259. if (err != 0)
  5260. return -4937;
  5261. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5262. return -4938;
  5263. }
  5264. /* Test 1 - Decrypt */
  5265. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5266. CHACHA20_POLY1305_AEAD_DECRYPT);
  5267. if (err != 0)
  5268. return -4939;
  5269. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5270. if (err != 0)
  5271. return -4940;
  5272. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5273. /* test doing data in smaller chunks */
  5274. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5275. word32 dataLen = sizeof(plaintext1) - testLen;
  5276. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5277. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5278. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5279. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5280. dataLen);
  5281. if (err != 0)
  5282. return -4941;
  5283. testLen += dataLen;
  5284. }
  5285. #else
  5286. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5287. generatedPlaintext, sizeof(cipher1));
  5288. #endif
  5289. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5290. if (err != 0)
  5291. return -4942;
  5292. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5293. if (err != 0)
  5294. return -4943;
  5295. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5296. return -4944;
  5297. }
  5298. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5299. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5300. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5301. /* Test 2 - Encrypt */
  5302. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5303. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5304. if (err != 0)
  5305. return -4945;
  5306. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5307. if (err != 0)
  5308. return -4946;
  5309. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5310. /* test doing data in smaller chunks */
  5311. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5312. word32 dataLen = sizeof(plaintext2) - testLen;
  5313. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5314. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5315. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5316. &generatedCiphertext[testLen], dataLen);
  5317. if (err != 0)
  5318. return -4947;
  5319. testLen += dataLen;
  5320. }
  5321. #else
  5322. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5323. sizeof(plaintext2));
  5324. #endif
  5325. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5326. if (err != 0)
  5327. return -4948;
  5328. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5329. if (err != 0)
  5330. return -4949;
  5331. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5332. return -4950;
  5333. }
  5334. /* Test 2 - Decrypt */
  5335. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5336. CHACHA20_POLY1305_AEAD_DECRYPT);
  5337. if (err != 0)
  5338. return -4951;
  5339. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5340. if (err != 0)
  5341. return -4952;
  5342. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5343. /* test doing data in smaller chunks */
  5344. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5345. word32 dataLen = sizeof(plaintext2) - testLen;
  5346. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5347. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5348. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5349. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5350. dataLen);
  5351. if (err != 0)
  5352. return -4953;
  5353. testLen += dataLen;
  5354. }
  5355. #else
  5356. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5357. generatedPlaintext, sizeof(cipher2));
  5358. #endif
  5359. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5360. if (err != 0)
  5361. return -4954;
  5362. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5363. if (err != 0)
  5364. return -4955;
  5365. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5366. return -4956;
  5367. }
  5368. return err;
  5369. }
  5370. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5371. #ifndef NO_DES3
  5372. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  5373. {
  5374. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  5375. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5376. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5377. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5378. };
  5379. byte plain[24];
  5380. byte cipher[24];
  5381. Des enc;
  5382. Des dec;
  5383. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  5384. {
  5385. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  5386. };
  5387. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  5388. {
  5389. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  5390. };
  5391. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  5392. {
  5393. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  5394. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  5395. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  5396. };
  5397. int ret;
  5398. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  5399. if (ret != 0)
  5400. return -5000;
  5401. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5402. if (ret != 0)
  5403. return -5001;
  5404. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  5405. if (ret != 0)
  5406. return -5002;
  5407. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5408. if (ret != 0)
  5409. return -5003;
  5410. if (XMEMCMP(plain, vector, sizeof(plain)))
  5411. return -5004;
  5412. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  5413. return -5005;
  5414. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  5415. if (ret != 0)
  5416. return -5006;
  5417. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5418. {
  5419. EncryptedInfo info;
  5420. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5421. XMEMCPY(info.iv, iv, sizeof(iv));
  5422. info.ivSz = sizeof(iv);
  5423. info.keySz = sizeof(key);
  5424. info.cipherType = WC_CIPHER_DES;
  5425. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5426. sizeof(key), WC_HASH_TYPE_SHA);
  5427. if (ret != 0)
  5428. return -5007;
  5429. /* Test invalid info ptr */
  5430. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  5431. sizeof(key), WC_HASH_TYPE_SHA);
  5432. if (ret != BAD_FUNC_ARG)
  5433. return -5008;
  5434. /* Test invalid hash type */
  5435. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5436. sizeof(key), WC_HASH_TYPE_NONE);
  5437. if (ret == 0)
  5438. return -5009;
  5439. }
  5440. #endif
  5441. return 0;
  5442. }
  5443. #endif /* NO_DES3 */
  5444. #ifndef NO_DES3
  5445. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  5446. {
  5447. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  5448. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5449. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5450. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5451. };
  5452. byte plain[24];
  5453. byte cipher[24];
  5454. Des3 enc;
  5455. Des3 dec;
  5456. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5457. {
  5458. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  5459. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  5460. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  5461. };
  5462. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5463. {
  5464. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  5465. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  5466. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  5467. };
  5468. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  5469. {
  5470. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  5471. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  5472. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  5473. };
  5474. int ret;
  5475. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  5476. return -5100;
  5477. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  5478. return -5101;
  5479. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  5480. if (ret != 0)
  5481. return -5102;
  5482. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  5483. if (ret != 0)
  5484. return -5103;
  5485. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5486. #if defined(WOLFSSL_ASYNC_CRYPT)
  5487. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5488. #endif
  5489. if (ret != 0)
  5490. return -5104;
  5491. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5492. #if defined(WOLFSSL_ASYNC_CRYPT)
  5493. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5494. #endif
  5495. if (ret != 0)
  5496. return -5105;
  5497. if (XMEMCMP(plain, vector, sizeof(plain)))
  5498. return -5106;
  5499. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5500. return -5107;
  5501. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  5502. /* test the same vectors with using compatibility layer */
  5503. {
  5504. DES_key_schedule ks1;
  5505. DES_key_schedule ks2;
  5506. DES_key_schedule ks3;
  5507. DES_cblock iv4;
  5508. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  5509. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  5510. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  5511. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  5512. XMEMSET(plain, 0, sizeof(plain));
  5513. XMEMSET(cipher, 0, sizeof(cipher));
  5514. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  5515. &iv4, DES_ENCRYPT);
  5516. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  5517. &iv4, DES_DECRYPT);
  5518. if (XMEMCMP(plain, vector, sizeof(plain)))
  5519. return -5108;
  5520. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5521. return -5109;
  5522. }
  5523. #endif /* OPENSSL_EXTRA */
  5524. wc_Des3Free(&enc);
  5525. wc_Des3Free(&dec);
  5526. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5527. {
  5528. EncryptedInfo info;
  5529. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5530. XMEMCPY(info.iv, iv3, sizeof(iv3));
  5531. info.ivSz = sizeof(iv3);
  5532. info.keySz = sizeof(key3);
  5533. info.cipherType = WC_CIPHER_DES3;
  5534. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  5535. sizeof(key3), WC_HASH_TYPE_SHA);
  5536. if (ret != 0)
  5537. return -5110;
  5538. }
  5539. #endif
  5540. return 0;
  5541. }
  5542. #endif /* NO_DES */
  5543. #ifndef NO_AES
  5544. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  5545. defined(WOLFSSL_AES_XTS)
  5546. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5547. /* pass in the function, key, iv, plain text and expected and this function
  5548. * tests that the encryption and decryption is successful */
  5549. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  5550. const byte* iv, const byte* plain, int plainSz,
  5551. const byte* expected, int expectedSz)
  5552. {
  5553. #ifdef WOLFSSL_SMALL_STACK
  5554. EVP_CIPHER_CTX *ctx = NULL;
  5555. #else
  5556. EVP_CIPHER_CTX ctx[1];
  5557. #endif
  5558. int idx, ret = 0, cipherSz;
  5559. byte* cipher;
  5560. #ifdef WOLFSSL_SMALL_STACK
  5561. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  5562. return MEMORY_E;
  5563. #endif
  5564. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5565. if (cipher == NULL) {
  5566. ret = -5120;
  5567. goto EVP_TEST_END;
  5568. }
  5569. /* test encrypt */
  5570. EVP_CIPHER_CTX_init(ctx);
  5571. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  5572. ret = -5121;
  5573. goto EVP_TEST_END;
  5574. }
  5575. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  5576. ret = -5122;
  5577. goto EVP_TEST_END;
  5578. }
  5579. cipherSz = idx;
  5580. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5581. ret = -5123;
  5582. goto EVP_TEST_END;
  5583. }
  5584. cipherSz += idx;
  5585. if (XMEMCMP(cipher, expected, plainSz)) {
  5586. ret = -5124;
  5587. goto EVP_TEST_END;
  5588. }
  5589. /* test decrypt */
  5590. EVP_CIPHER_CTX_init(ctx);
  5591. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  5592. ret = -5125;
  5593. goto EVP_TEST_END;
  5594. }
  5595. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  5596. ret = -5126;
  5597. goto EVP_TEST_END;
  5598. }
  5599. cipherSz = idx;
  5600. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5601. ret = -5127;
  5602. goto EVP_TEST_END;
  5603. }
  5604. cipherSz += idx;
  5605. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  5606. ret = -5128;
  5607. goto EVP_TEST_END;
  5608. }
  5609. EVP_TEST_END:
  5610. if (cipher)
  5611. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5612. (void)cipherSz;
  5613. #ifdef WOLFSSL_SMALL_STACK
  5614. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  5615. #endif
  5616. return ret;
  5617. }
  5618. #endif /* OPENSSL_EXTRA */
  5619. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  5620. #ifdef WOLFSSL_AES_OFB
  5621. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  5622. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  5623. {
  5624. #ifdef WOLFSSL_AES_256
  5625. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5626. {
  5627. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  5628. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  5629. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  5630. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  5631. };
  5632. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  5633. {
  5634. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  5635. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  5636. };
  5637. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  5638. {
  5639. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  5640. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  5641. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  5642. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  5643. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  5644. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  5645. };
  5646. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5647. {
  5648. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  5649. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  5650. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  5651. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  5652. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  5653. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  5654. };
  5655. #endif /* WOLFSSL_AES_256 */
  5656. #ifdef WOLFSSL_AES_128
  5657. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5658. {
  5659. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  5660. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  5661. };
  5662. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  5663. {
  5664. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5665. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5666. };
  5667. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  5668. {
  5669. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5670. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5671. };
  5672. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  5673. {
  5674. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  5675. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  5676. };
  5677. #endif /* WOLFSSL_AES_128 */
  5678. #ifdef WOLFSSL_AES_192
  5679. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  5680. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  5681. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  5682. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  5683. };
  5684. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5685. {
  5686. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5687. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5688. };
  5689. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  5690. {
  5691. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  5692. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  5693. };
  5694. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  5695. {
  5696. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5698. };
  5699. #endif /* WOLFSSL_AES_192 */
  5700. #ifdef WOLFSSL_SMALL_STACK
  5701. Aes *enc = NULL;
  5702. #else
  5703. Aes enc[1];
  5704. #endif
  5705. byte cipher[AES_BLOCK_SIZE * 4];
  5706. #ifdef HAVE_AES_DECRYPT
  5707. #ifdef WOLFSSL_SMALL_STACK
  5708. Aes *dec = NULL;
  5709. #else
  5710. Aes dec[1];
  5711. #endif
  5712. byte plain [AES_BLOCK_SIZE * 4];
  5713. #endif
  5714. int ret = 0;
  5715. #ifdef WOLFSSL_SMALL_STACK
  5716. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5717. ERROR_OUT(-1, out);
  5718. #ifdef HAVE_AES_DECRYPT
  5719. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5720. ERROR_OUT(-1, out);
  5721. #endif
  5722. #endif
  5723. XMEMSET(enc, 0, sizeof *enc);
  5724. #ifdef HAVE_AES_DECRYPT
  5725. XMEMSET(dec, 0, sizeof *dec);
  5726. #endif
  5727. #ifdef WOLFSSL_AES_128
  5728. /* 128 key size test */
  5729. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5730. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  5731. cipher2, sizeof(cipher2));
  5732. if (ret != 0) {
  5733. goto out;
  5734. }
  5735. #endif
  5736. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5737. if (ret != 0)
  5738. ERROR_OUT(-5129, out);
  5739. #ifdef HAVE_AES_DECRYPT
  5740. /* decrypt uses AES_ENCRYPTION */
  5741. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5742. if (ret != 0)
  5743. ERROR_OUT(-5130, out);
  5744. #endif
  5745. XMEMSET(cipher, 0, sizeof(cipher));
  5746. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  5747. if (ret != 0)
  5748. ERROR_OUT(-5131, out);
  5749. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  5750. ERROR_OUT(-5132, out);
  5751. #ifdef HAVE_AES_DECRYPT
  5752. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  5753. if (ret != 0)
  5754. ERROR_OUT(-5133, out);
  5755. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  5756. ERROR_OUT(-5134, out);
  5757. #endif /* HAVE_AES_DECRYPT */
  5758. #endif /* WOLFSSL_AES_128 */
  5759. #ifdef WOLFSSL_AES_192
  5760. /* 192 key size test */
  5761. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5762. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  5763. cipher3, sizeof(cipher3));
  5764. if (ret != 0) {
  5765. goto out;
  5766. }
  5767. #endif
  5768. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5769. if (ret != 0)
  5770. ERROR_OUT(-5135, out);
  5771. #ifdef HAVE_AES_DECRYPT
  5772. /* decrypt uses AES_ENCRYPTION */
  5773. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5774. if (ret != 0)
  5775. ERROR_OUT(-5136, out);
  5776. #endif
  5777. XMEMSET(cipher, 0, sizeof(cipher));
  5778. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  5779. if (ret != 0)
  5780. ERROR_OUT(-5137, out);
  5781. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  5782. ERROR_OUT(-5138, out);
  5783. #ifdef HAVE_AES_DECRYPT
  5784. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  5785. if (ret != 0)
  5786. ERROR_OUT(-5139, out);
  5787. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  5788. ERROR_OUT(-5140, out);
  5789. #endif /* HAVE_AES_DECRYPT */
  5790. #endif /* WOLFSSL_AES_192 */
  5791. #ifdef WOLFSSL_AES_256
  5792. /* 256 key size test */
  5793. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5794. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  5795. cipher1, sizeof(cipher1));
  5796. if (ret != 0) {
  5797. goto out;
  5798. }
  5799. #endif
  5800. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5801. if (ret != 0)
  5802. ERROR_OUT(-5141, out);
  5803. #ifdef HAVE_AES_DECRYPT
  5804. /* decrypt uses AES_ENCRYPTION */
  5805. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5806. if (ret != 0)
  5807. ERROR_OUT(-5142, out);
  5808. #endif
  5809. XMEMSET(cipher, 0, sizeof(cipher));
  5810. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  5811. if (ret != 0)
  5812. ERROR_OUT(-5143, out);
  5813. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  5814. ERROR_OUT(-5144, out);
  5815. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  5816. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5817. if (ret != 0)
  5818. ERROR_OUT(-5145, out);
  5819. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  5820. AES_BLOCK_SIZE))
  5821. ERROR_OUT(-5146, out);
  5822. #ifdef HAVE_AES_DECRYPT
  5823. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  5824. if (ret != 0)
  5825. ERROR_OUT(-5147, out);
  5826. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  5827. ERROR_OUT(-5148, out);
  5828. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  5829. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5830. if (ret != 0)
  5831. ERROR_OUT(-5149, out);
  5832. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  5833. AES_BLOCK_SIZE))
  5834. ERROR_OUT(-5150, out);
  5835. #endif /* HAVE_AES_DECRYPT */
  5836. /* multiple blocks at once */
  5837. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5838. if (ret != 0)
  5839. ERROR_OUT(-5151, out);
  5840. #ifdef HAVE_AES_DECRYPT
  5841. /* decrypt uses AES_ENCRYPTION */
  5842. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5843. if (ret != 0)
  5844. ERROR_OUT(-5152, out);
  5845. #endif
  5846. XMEMSET(cipher, 0, sizeof(cipher));
  5847. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  5848. if (ret != 0)
  5849. ERROR_OUT(-5153, out);
  5850. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  5851. ERROR_OUT(-5154, out);
  5852. #ifdef HAVE_AES_DECRYPT
  5853. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  5854. if (ret != 0)
  5855. ERROR_OUT(-5155, out);
  5856. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  5857. ERROR_OUT(-5156, out);
  5858. #endif /* HAVE_AES_DECRYPT */
  5859. /* inline decrypt/encrypt*/
  5860. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5861. if (ret != 0)
  5862. ERROR_OUT(-5157, out);
  5863. #ifdef HAVE_AES_DECRYPT
  5864. /* decrypt uses AES_ENCRYPTION */
  5865. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5866. if (ret != 0)
  5867. ERROR_OUT(-5158, out);
  5868. #endif
  5869. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  5870. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  5871. if (ret != 0)
  5872. ERROR_OUT(-5159, out);
  5873. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  5874. ERROR_OUT(-5160, out);
  5875. #ifdef HAVE_AES_DECRYPT
  5876. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  5877. if (ret != 0)
  5878. ERROR_OUT(-5161, out);
  5879. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  5880. ERROR_OUT(-5162, out);
  5881. #endif /* HAVE_AES_DECRYPT */
  5882. /* 256 key size test leftover support */
  5883. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5884. if (ret != 0)
  5885. ERROR_OUT(-5163, out);
  5886. #ifdef HAVE_AES_DECRYPT
  5887. /* decrypt uses AES_ENCRYPTION */
  5888. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5889. if (ret != 0)
  5890. ERROR_OUT(-5164, out);
  5891. #endif
  5892. XMEMSET(cipher, 0, sizeof(cipher));
  5893. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  5894. if (ret != 0)
  5895. ERROR_OUT(-5165, out);
  5896. if (XMEMCMP(cipher, cipher1, 3))
  5897. ERROR_OUT(-5166, out);
  5898. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  5899. if (ret != 0)
  5900. ERROR_OUT(-5167, out);
  5901. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  5902. ERROR_OUT(-5168, out);
  5903. #ifdef HAVE_AES_DECRYPT
  5904. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  5905. if (ret != 0)
  5906. ERROR_OUT(-5169, out);
  5907. if (XMEMCMP(plain, plain1, 6))
  5908. ERROR_OUT(-5170, out);
  5909. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  5910. if (ret != 0)
  5911. ERROR_OUT(-5171, out);
  5912. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  5913. ERROR_OUT(-5172, out);
  5914. #endif /* HAVE_AES_DECRYPT */
  5915. out:
  5916. #ifdef WOLFSSL_SMALL_STACK
  5917. if (enc)
  5918. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  5919. #ifdef HAVE_AES_DECRYPT
  5920. if (dec)
  5921. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  5922. #endif
  5923. #endif
  5924. #endif /* WOLFSSL_AES_256 */
  5925. return ret;
  5926. }
  5927. #endif /* WOLFSSL_AES_OFB */
  5928. #if defined(WOLFSSL_AES_CFB)
  5929. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  5930. static int aescfb_test(void)
  5931. {
  5932. #ifdef WOLFSSL_SMALL_STACK
  5933. Aes *enc = NULL;
  5934. #else
  5935. Aes enc[1];
  5936. #endif
  5937. int enc_inited = 0;
  5938. byte cipher[AES_BLOCK_SIZE * 4];
  5939. #ifdef HAVE_AES_DECRYPT
  5940. #ifdef WOLFSSL_SMALL_STACK
  5941. Aes *dec = NULL;
  5942. #else
  5943. Aes dec[1];
  5944. #endif
  5945. int dec_inited = 0;
  5946. byte plain [AES_BLOCK_SIZE * 4];
  5947. #endif
  5948. int ret = 0;
  5949. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  5950. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  5951. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  5952. };
  5953. #ifdef WOLFSSL_AES_128
  5954. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5955. {
  5956. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  5957. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  5958. };
  5959. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5960. {
  5961. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  5962. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  5963. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  5964. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  5965. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  5966. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  5967. };
  5968. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5969. {
  5970. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5971. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5972. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5973. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5974. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5975. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  5976. };
  5977. #endif /* WOLFSSL_AES_128 */
  5978. #ifdef WOLFSSL_AES_192
  5979. /* 192 size key test */
  5980. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5981. {
  5982. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  5983. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  5984. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  5985. };
  5986. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  5987. {
  5988. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  5989. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  5990. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  5991. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  5992. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  5993. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  5994. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  5995. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  5996. };
  5997. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5998. {
  5999. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6000. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6001. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6002. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6003. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6004. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6005. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6006. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6007. };
  6008. #endif /* WOLFSSL_AES_192 */
  6009. #ifdef WOLFSSL_AES_256
  6010. /* 256 size key simple test */
  6011. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6012. {
  6013. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6014. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6015. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6016. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6017. };
  6018. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6019. {
  6020. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6021. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6022. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6023. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6024. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6025. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6026. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6027. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6028. };
  6029. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6030. {
  6031. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6032. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6033. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6034. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6035. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6036. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6037. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6038. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6039. };
  6040. #endif /* WOLFSSL_AES_256 */
  6041. #ifdef WOLFSSL_SMALL_STACK
  6042. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6043. ERROR_OUT(-1, out);
  6044. #ifdef HAVE_AES_DECRYPT
  6045. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6046. ERROR_OUT(-1, out);
  6047. #endif
  6048. #endif
  6049. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6050. ERROR_OUT(-5173, out);
  6051. else
  6052. enc_inited = 1;
  6053. #ifdef HAVE_AES_DECRYPT
  6054. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6055. ERROR_OUT(-5174, out);
  6056. else
  6057. dec_inited = 1;
  6058. #endif
  6059. #ifdef WOLFSSL_AES_128
  6060. /* 128 key tests */
  6061. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6062. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6063. cipher1, sizeof(cipher1));
  6064. if (ret != 0) {
  6065. return ret;
  6066. }
  6067. #endif
  6068. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6069. if (ret != 0)
  6070. ERROR_OUT(-5175, out);
  6071. #ifdef HAVE_AES_DECRYPT
  6072. /* decrypt uses AES_ENCRYPTION */
  6073. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6074. if (ret != 0)
  6075. ERROR_OUT(-5176, out);
  6076. #endif
  6077. XMEMSET(cipher, 0, sizeof(cipher));
  6078. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6079. if (ret != 0)
  6080. ERROR_OUT(-5177, out);
  6081. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6082. ERROR_OUT(-5178, out);
  6083. /* test restarting encryption process */
  6084. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6085. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6086. if (ret != 0)
  6087. ERROR_OUT(-5179, out);
  6088. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6089. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6090. ERROR_OUT(-5180, out);
  6091. #ifdef HAVE_AES_DECRYPT
  6092. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6093. if (ret != 0)
  6094. ERROR_OUT(-5181, out);
  6095. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6096. ERROR_OUT(-5182, out);
  6097. #endif /* HAVE_AES_DECRYPT */
  6098. #endif /* WOLFSSL_AES_128 */
  6099. #ifdef WOLFSSL_AES_192
  6100. /* 192 key size test */
  6101. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6102. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6103. cipher2, sizeof(cipher2));
  6104. if (ret != 0) {
  6105. return ret;
  6106. }
  6107. #endif
  6108. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6109. if (ret != 0)
  6110. ERROR_OUT(-5183, out);
  6111. #ifdef HAVE_AES_DECRYPT
  6112. /* decrypt uses AES_ENCRYPTION */
  6113. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6114. if (ret != 0)
  6115. ERROR_OUT(-5184, out);
  6116. #endif
  6117. XMEMSET(cipher, 0, sizeof(cipher));
  6118. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6119. if (ret != 0)
  6120. ERROR_OUT(-5185, out);
  6121. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6122. ERROR_OUT(-5186, out);
  6123. #ifdef HAVE_AES_DECRYPT
  6124. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6125. if (ret != 0)
  6126. ERROR_OUT(-5187, out);
  6127. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6128. ERROR_OUT(-5188, out);
  6129. #endif /* HAVE_AES_DECRYPT */
  6130. #endif /* WOLFSSL_AES_192 */
  6131. #ifdef WOLFSSL_AES_256
  6132. /* 256 key size test */
  6133. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6134. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6135. cipher3, sizeof(cipher3));
  6136. if (ret != 0) {
  6137. return ret;
  6138. }
  6139. #endif
  6140. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6141. if (ret != 0)
  6142. ERROR_OUT(-5189, out);
  6143. #ifdef HAVE_AES_DECRYPT
  6144. /* decrypt uses AES_ENCRYPTION */
  6145. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6146. if (ret != 0)
  6147. ERROR_OUT(-5190, out);
  6148. #endif
  6149. /* test with data left overs, magic lengths are checking near edges */
  6150. XMEMSET(cipher, 0, sizeof(cipher));
  6151. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6152. if (ret != 0)
  6153. ERROR_OUT(-5191, out);
  6154. if (XMEMCMP(cipher, cipher3, 4))
  6155. ERROR_OUT(-5192, out);
  6156. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6157. if (ret != 0)
  6158. ERROR_OUT(-5193, out);
  6159. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6160. ERROR_OUT(-5194, out);
  6161. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6162. (AES_BLOCK_SIZE * 4) - 31);
  6163. if (ret != 0)
  6164. ERROR_OUT(-5195, out);
  6165. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6166. ERROR_OUT(-5196, out);
  6167. #ifdef HAVE_AES_DECRYPT
  6168. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6169. if (ret != 0)
  6170. ERROR_OUT(-5197, out);
  6171. if (XMEMCMP(plain, msg3, 4))
  6172. ERROR_OUT(-5198, out);
  6173. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6174. if (ret != 0)
  6175. ERROR_OUT(-5199, out);
  6176. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6177. if (ret != 0)
  6178. ERROR_OUT(-5200, out);
  6179. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6180. ERROR_OUT(-5201, out);
  6181. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6182. (AES_BLOCK_SIZE * 4) - 31);
  6183. if (ret != 0)
  6184. ERROR_OUT(-5202, out);
  6185. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6186. ERROR_OUT(-5203, out);
  6187. #endif /* HAVE_AES_DECRYPT */
  6188. #endif /* WOLFSSL_AES_256 */
  6189. out:
  6190. if (enc_inited)
  6191. wc_AesFree(enc);
  6192. if (dec_inited)
  6193. wc_AesFree(dec);
  6194. #ifdef WOLFSSL_SMALL_STACK
  6195. if (enc)
  6196. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6197. #ifdef HAVE_AES_DECRYPT
  6198. if (dec)
  6199. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6200. #endif
  6201. #endif
  6202. return ret;
  6203. }
  6204. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6205. static int aescfb1_test(void)
  6206. {
  6207. #ifdef WOLFSSL_SMALL_STACK
  6208. Aes *enc = NULL;
  6209. #else
  6210. Aes enc[1];
  6211. #endif
  6212. int enc_inited = 0;
  6213. byte cipher[AES_BLOCK_SIZE];
  6214. #ifdef HAVE_AES_DECRYPT
  6215. #ifdef WOLFSSL_SMALL_STACK
  6216. Aes *dec = NULL;
  6217. #else
  6218. Aes dec[1];
  6219. #endif
  6220. int dec_inited = 0;
  6221. byte plain [AES_BLOCK_SIZE];
  6222. #endif
  6223. int ret = 0;
  6224. #ifdef WOLFSSL_AES_128
  6225. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6226. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6227. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6228. };
  6229. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6230. {
  6231. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6232. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6233. };
  6234. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6235. {
  6236. 0x00
  6237. };
  6238. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6239. {
  6240. 0xC0
  6241. };
  6242. #endif /* WOLFSSL_AES_128 */
  6243. #ifdef WOLFSSL_AES_192
  6244. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6245. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6246. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6247. };
  6248. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6249. {
  6250. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6251. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6252. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6253. };
  6254. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6255. {
  6256. 0x30
  6257. };
  6258. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6259. {
  6260. 0x80
  6261. };
  6262. #endif /* WOLFSSL_AES_192 */
  6263. #ifdef WOLFSSL_AES_256
  6264. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6265. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6266. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6267. };
  6268. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6269. {
  6270. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6271. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6272. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6273. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6274. };
  6275. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6276. {
  6277. 0xF7, 0x00
  6278. };
  6279. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6280. {
  6281. 0x41, 0xC0
  6282. };
  6283. #endif /* WOLFSSL_AES_256 */
  6284. #ifdef WOLFSSL_SMALL_STACK
  6285. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6286. ERROR_OUT(-1, out);
  6287. #ifdef HAVE_AES_DECRYPT
  6288. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6289. ERROR_OUT(-1, out);
  6290. #endif
  6291. #endif
  6292. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6293. ERROR_OUT(-5204, out);
  6294. else
  6295. enc_inited = 1;
  6296. #ifdef HAVE_AES_DECRYPT
  6297. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6298. ERROR_OUT(-5205, out);
  6299. else
  6300. dec_inited = 1;
  6301. #endif
  6302. #ifdef WOLFSSL_AES_128
  6303. /* 128 key tests */
  6304. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6305. if (ret != 0)
  6306. ERROR_OUT(-5206, out);
  6307. #ifdef HAVE_AES_DECRYPT
  6308. /* decrypt uses AES_ENCRYPTION */
  6309. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6310. if (ret != 0)
  6311. ERROR_OUT(-5207, out);
  6312. #endif
  6313. XMEMSET(cipher, 0, sizeof(cipher));
  6314. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6315. if (ret != 0)
  6316. ERROR_OUT(-5208, out);
  6317. if (cipher[0] != cipher1[0])
  6318. ERROR_OUT(-5209, out);
  6319. #ifdef HAVE_AES_DECRYPT
  6320. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6321. if (ret != 0)
  6322. ERROR_OUT(-5210, out);
  6323. if (plain[0] != msg1[0])
  6324. ERROR_OUT(-5211, out);
  6325. #endif /* HAVE_AES_DECRYPT */
  6326. #ifdef OPENSSL_EXTRA
  6327. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6328. if (ret != 0)
  6329. ERROR_OUT(-5212, out);
  6330. XMEMSET(cipher, 0, sizeof(cipher));
  6331. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6332. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6333. if (ret != 0)
  6334. ERROR_OUT(-5213, out);
  6335. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6336. cipher, sizeof(msg1));
  6337. if (ret != 0) {
  6338. goto out;
  6339. }
  6340. #endif
  6341. #endif /* WOLFSSL_AES_128 */
  6342. #ifdef WOLFSSL_AES_192
  6343. /* 192 key tests */
  6344. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6345. if (ret != 0)
  6346. ERROR_OUT(-5214, out);
  6347. XMEMSET(cipher, 0, sizeof(cipher));
  6348. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6349. if (ret != 0)
  6350. ERROR_OUT(-5215, out);
  6351. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6352. ERROR_OUT(-5216, out);
  6353. #ifdef OPENSSL_EXTRA
  6354. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6355. if (ret != 0)
  6356. ERROR_OUT(-5217, out);
  6357. XMEMSET(cipher, 0, sizeof(cipher));
  6358. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  6359. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  6360. if (ret != 0)
  6361. ERROR_OUT(-5218, out);
  6362. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  6363. cipher, sizeof(msg2));
  6364. if (ret != 0) {
  6365. goto out;
  6366. }
  6367. #endif
  6368. #endif /* WOLFSSL_AES_192 */
  6369. #ifdef WOLFSSL_AES_256
  6370. /* 256 key tests */
  6371. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6372. if (ret != 0)
  6373. ERROR_OUT(-5219, out);
  6374. XMEMSET(cipher, 0, sizeof(cipher));
  6375. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  6376. if (ret != 0)
  6377. ERROR_OUT(-5220, out);
  6378. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6379. ERROR_OUT(-5221, out);
  6380. #ifdef OPENSSL_EXTRA
  6381. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6382. if (ret != 0)
  6383. ERROR_OUT(-5222, out);
  6384. XMEMSET(cipher, 0, sizeof(cipher));
  6385. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  6386. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  6387. if (ret != 0)
  6388. ERROR_OUT(-5223, out);
  6389. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  6390. cipher, sizeof(msg3));
  6391. if (ret != 0) {
  6392. goto out;
  6393. }
  6394. #endif
  6395. out:
  6396. if (enc_inited)
  6397. wc_AesFree(enc);
  6398. #ifdef HAVE_AES_DECRYPT
  6399. if (dec_inited)
  6400. wc_AesFree(dec);
  6401. #endif
  6402. #ifdef WOLFSSL_SMALL_STACK
  6403. if (enc)
  6404. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6405. #ifdef HAVE_AES_DECRYPT
  6406. if (dec)
  6407. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6408. #endif
  6409. #endif
  6410. #endif /* WOLFSSL_AES_256 */
  6411. return ret;
  6412. }
  6413. static int aescfb8_test(void)
  6414. {
  6415. #ifdef WOLFSSL_SMALL_STACK
  6416. Aes *enc = NULL;
  6417. #else
  6418. Aes enc[1];
  6419. #endif
  6420. int enc_inited = 0;
  6421. byte cipher[AES_BLOCK_SIZE];
  6422. #ifdef HAVE_AES_DECRYPT
  6423. #ifdef WOLFSSL_SMALL_STACK
  6424. Aes *dec = NULL;
  6425. #else
  6426. Aes dec[1];
  6427. #endif
  6428. int dec_inited = 0;
  6429. byte plain [AES_BLOCK_SIZE];
  6430. #endif
  6431. int ret = 0;
  6432. #ifdef WOLFSSL_AES_128
  6433. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6434. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  6435. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  6436. };
  6437. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6438. {
  6439. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  6440. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  6441. };
  6442. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6443. {
  6444. 0xd2,0x76,0x91
  6445. };
  6446. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6447. {
  6448. 0xc9,0x06,0x35
  6449. };
  6450. #endif /* WOLFSSL_AES_128 */
  6451. #ifdef WOLFSSL_AES_192
  6452. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6453. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  6454. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  6455. };
  6456. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6457. {
  6458. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  6459. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  6460. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  6461. };
  6462. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6463. {
  6464. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  6465. 0xb5,0xe8
  6466. };
  6467. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6468. {
  6469. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  6470. 0x3a,0x60
  6471. };
  6472. #endif
  6473. #ifdef WOLFSSL_AES_256
  6474. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6475. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  6476. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  6477. };
  6478. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6479. {
  6480. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  6481. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  6482. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  6483. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  6484. };
  6485. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6486. {
  6487. 0x1c,0xff,0x95
  6488. };
  6489. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6490. {
  6491. 0xb9,0x74,0xfa
  6492. };
  6493. #endif
  6494. #ifdef WOLFSSL_SMALL_STACK
  6495. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6496. ERROR_OUT(-5238, out);
  6497. #ifdef HAVE_AES_DECRYPT
  6498. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6499. ERROR_OUT(-5239, out);
  6500. #endif
  6501. #endif
  6502. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6503. ERROR_OUT(-5224, out);
  6504. else
  6505. enc_inited = 1;
  6506. #ifdef HAVE_AES_DECRYPT
  6507. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6508. ERROR_OUT(-5225, out);
  6509. else
  6510. dec_inited = 1;
  6511. #endif
  6512. #ifdef WOLFSSL_AES_128
  6513. /* 128 key tests */
  6514. #ifdef OPENSSL_EXTRA
  6515. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  6516. cipher1, sizeof(cipher1));
  6517. if (ret != 0) {
  6518. return ret;
  6519. }
  6520. #endif
  6521. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6522. if (ret != 0)
  6523. ERROR_OUT(-5226, out);
  6524. #ifdef HAVE_AES_DECRYPT
  6525. /* decrypt uses AES_ENCRYPTION */
  6526. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6527. if (ret != 0)
  6528. ERROR_OUT(-5227, out);
  6529. #endif
  6530. XMEMSET(cipher, 0, sizeof(cipher));
  6531. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  6532. if (ret != 0)
  6533. ERROR_OUT(-5228, out);
  6534. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  6535. ERROR_OUT(-5229, out);
  6536. #ifdef HAVE_AES_DECRYPT
  6537. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  6538. if (ret != 0)
  6539. ERROR_OUT(-5230, out);
  6540. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  6541. ERROR_OUT(-5231, out);
  6542. #endif /* HAVE_AES_DECRYPT */
  6543. #endif /* WOLFSSL_AES_128 */
  6544. #ifdef WOLFSSL_AES_192
  6545. /* 192 key tests */
  6546. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6547. if (ret != 0)
  6548. ERROR_OUT(-5232, out);
  6549. XMEMSET(cipher, 0, sizeof(cipher));
  6550. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  6551. if (ret != 0)
  6552. ERROR_OUT(-5233, out);
  6553. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  6554. ERROR_OUT(-5234, out);
  6555. #ifdef OPENSSL_EXTRA
  6556. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  6557. cipher2, sizeof(msg2));
  6558. if (ret != 0) {
  6559. return ret;
  6560. }
  6561. #endif
  6562. #endif /* WOLFSSL_AES_192 */
  6563. #ifdef WOLFSSL_AES_256
  6564. /* 256 key tests */
  6565. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6566. if (ret != 0)
  6567. ERROR_OUT(-5235, out);
  6568. XMEMSET(cipher, 0, sizeof(cipher));
  6569. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  6570. if (ret != 0)
  6571. ERROR_OUT(-5236, out);
  6572. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6573. ERROR_OUT(-5237, out);
  6574. #ifdef OPENSSL_EXTRA
  6575. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  6576. cipher3, sizeof(msg3));
  6577. if (ret != 0) {
  6578. goto out;
  6579. }
  6580. #endif
  6581. out:
  6582. if (enc_inited)
  6583. wc_AesFree(enc);
  6584. #ifdef HAVE_AES_DECRYPT
  6585. if (dec_inited)
  6586. wc_AesFree(dec);
  6587. #endif
  6588. #ifdef WOLFSSL_SMALL_STACK
  6589. if (enc)
  6590. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6591. #ifdef HAVE_AES_DECRYPT
  6592. if (dec)
  6593. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6594. #endif
  6595. #endif
  6596. #endif /* WOLFSSL_AES_256 */
  6597. return ret;
  6598. }
  6599. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  6600. #endif /* WOLFSSL_AES_CFB */
  6601. static int aes_key_size_test(void)
  6602. {
  6603. int ret;
  6604. #ifdef WOLFSSL_SMALL_STACK
  6605. Aes *aes;
  6606. #else
  6607. Aes aes[1];
  6608. #endif
  6609. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6610. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6611. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6612. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6613. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  6614. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6615. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6616. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6617. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6618. byte iv[] = "1234567890abcdef";
  6619. #ifndef HAVE_FIPS
  6620. word32 keySize;
  6621. #endif
  6622. #ifdef WOLFSSL_SMALL_STACK
  6623. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6624. return -5315;
  6625. #endif
  6626. #if !defined(HAVE_FIPS) || \
  6627. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  6628. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  6629. * supported with that FIPS version */
  6630. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  6631. if (ret != BAD_FUNC_ARG)
  6632. ERROR_OUT(-5300, out);
  6633. #endif
  6634. ret = wc_AesInit(aes, HEAP_HINT, devId);
  6635. /* 0 check OK for FIPSv1 */
  6636. if (ret != 0)
  6637. ERROR_OUT(-5301, out);
  6638. #ifndef HAVE_FIPS
  6639. /* Parameter Validation testing. */
  6640. ret = wc_AesGetKeySize(NULL, NULL);
  6641. if (ret != BAD_FUNC_ARG)
  6642. ERROR_OUT(-5302, out);
  6643. ret = wc_AesGetKeySize(aes, NULL);
  6644. if (ret != BAD_FUNC_ARG)
  6645. ERROR_OUT(-5303, out);
  6646. ret = wc_AesGetKeySize(NULL, &keySize);
  6647. if (ret != BAD_FUNC_ARG)
  6648. ERROR_OUT(-5304, out);
  6649. /* Crashes in FIPS */
  6650. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6651. if (ret != BAD_FUNC_ARG)
  6652. ERROR_OUT(-5305, out);
  6653. #endif
  6654. /* NULL IV indicates to use all zeros IV. */
  6655. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  6656. #ifdef WOLFSSL_AES_128
  6657. if (ret != 0)
  6658. #else
  6659. if (ret != BAD_FUNC_ARG)
  6660. #endif
  6661. ERROR_OUT(-5306, out);
  6662. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  6663. if (ret != BAD_FUNC_ARG)
  6664. ERROR_OUT(-5307, out);
  6665. /* CryptoCell handles rounds internally */
  6666. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  6667. /* Force invalid rounds */
  6668. aes->rounds = 16;
  6669. ret = wc_AesGetKeySize(aes, &keySize);
  6670. if (ret != BAD_FUNC_ARG)
  6671. ERROR_OUT(-5308, out);
  6672. #endif
  6673. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6674. #ifdef WOLFSSL_AES_128
  6675. if (ret != 0)
  6676. #else
  6677. if (ret != BAD_FUNC_ARG)
  6678. #endif
  6679. ERROR_OUT(-5309, out);
  6680. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  6681. ret = wc_AesGetKeySize(aes, &keySize);
  6682. if (ret != 0 || keySize != sizeof(key16))
  6683. ERROR_OUT(-5310, out);
  6684. #endif
  6685. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  6686. #ifdef WOLFSSL_AES_192
  6687. if (ret != 0)
  6688. #else
  6689. if (ret != BAD_FUNC_ARG)
  6690. #endif
  6691. ERROR_OUT(-5311, out);
  6692. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  6693. ret = wc_AesGetKeySize(aes, &keySize);
  6694. if (ret != 0 || keySize != sizeof(key24))
  6695. ERROR_OUT(-5312, out);
  6696. #endif
  6697. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  6698. #ifdef WOLFSSL_AES_256
  6699. if (ret != 0)
  6700. #else
  6701. if (ret != BAD_FUNC_ARG)
  6702. #endif
  6703. ERROR_OUT(-5313, out);
  6704. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  6705. ret = wc_AesGetKeySize(aes, &keySize);
  6706. if (ret != 0 || keySize != sizeof(key32))
  6707. ERROR_OUT(-5314, out);
  6708. #endif
  6709. out:
  6710. #ifdef WOLFSSL_SMALL_STACK
  6711. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6712. #endif
  6713. return ret;
  6714. }
  6715. #if defined(WOLFSSL_AES_XTS)
  6716. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  6717. #ifdef WOLFSSL_AES_128
  6718. static int aes_xts_128_test(void)
  6719. {
  6720. #ifdef WOLFSSL_SMALL_STACK
  6721. XtsAes *aes = NULL;
  6722. #else
  6723. XtsAes aes[1];
  6724. #endif
  6725. int aes_inited = 0;
  6726. int ret = 0;
  6727. unsigned char buf[AES_BLOCK_SIZE * 2];
  6728. unsigned char cipher[AES_BLOCK_SIZE * 2];
  6729. /* 128 key tests */
  6730. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6731. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  6732. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  6733. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  6734. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  6735. };
  6736. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6737. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  6738. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6739. };
  6740. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6741. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6742. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  6743. };
  6744. /* plain text test of partial block is not from NIST test vector list */
  6745. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6746. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6747. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6748. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6749. };
  6750. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6751. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  6752. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  6753. };
  6754. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6755. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  6756. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  6757. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  6758. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  6759. };
  6760. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6761. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  6762. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  6763. };
  6764. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6765. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  6766. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  6767. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  6768. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  6769. };
  6770. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6771. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  6772. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  6773. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  6774. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  6775. };
  6776. #ifdef WOLFSSL_SMALL_STACK
  6777. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6778. ERROR_OUT(-5417, out);
  6779. #endif
  6780. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6781. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6782. if (ret != 0) {
  6783. printf("EVP_aes_128_xts failed!\n");
  6784. goto out;
  6785. }
  6786. #endif
  6787. XMEMSET(buf, 0, sizeof(buf));
  6788. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6789. HEAP_HINT, devId) != 0)
  6790. ERROR_OUT(-5400, out);
  6791. else
  6792. aes_inited = 1;
  6793. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6794. #if defined(WOLFSSL_ASYNC_CRYPT)
  6795. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6796. #endif
  6797. if (ret != 0)
  6798. ERROR_OUT(-5401, out);
  6799. if (XMEMCMP(c2, buf, sizeof(c2)))
  6800. ERROR_OUT(-5402, out);
  6801. XMEMSET(buf, 0, sizeof(buf));
  6802. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6803. HEAP_HINT, devId) != 0)
  6804. ERROR_OUT(-5403, out);
  6805. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6806. #if defined(WOLFSSL_ASYNC_CRYPT)
  6807. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6808. #endif
  6809. if (ret != 0)
  6810. ERROR_OUT(-5404, out);
  6811. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6812. ERROR_OUT(-5405, out);
  6813. /* partial block encryption test */
  6814. XMEMSET(cipher, 0, sizeof(cipher));
  6815. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6816. #if defined(WOLFSSL_ASYNC_CRYPT)
  6817. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6818. #endif
  6819. if (ret != 0)
  6820. ERROR_OUT(-5406, out);
  6821. wc_AesXtsFree(aes);
  6822. /* partial block decrypt test */
  6823. XMEMSET(buf, 0, sizeof(buf));
  6824. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  6825. HEAP_HINT, devId) != 0)
  6826. ERROR_OUT(-5407, out);
  6827. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6828. #if defined(WOLFSSL_ASYNC_CRYPT)
  6829. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6830. #endif
  6831. if (ret != 0)
  6832. ERROR_OUT(-5408, out);
  6833. if (XMEMCMP(pp, buf, sizeof(pp)))
  6834. ERROR_OUT(-5409, out);
  6835. /* NIST decrypt test vector */
  6836. XMEMSET(buf, 0, sizeof(buf));
  6837. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  6838. #if defined(WOLFSSL_ASYNC_CRYPT)
  6839. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6840. #endif
  6841. if (ret != 0)
  6842. ERROR_OUT(-5410, out);
  6843. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6844. ERROR_OUT(-5411, out);
  6845. /* fail case with decrypting using wrong key */
  6846. XMEMSET(buf, 0, sizeof(buf));
  6847. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6848. #if defined(WOLFSSL_ASYNC_CRYPT)
  6849. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6850. #endif
  6851. if (ret != 0)
  6852. ERROR_OUT(-5412, out);
  6853. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  6854. ERROR_OUT(-5413, out);
  6855. /* set correct key and retest */
  6856. XMEMSET(buf, 0, sizeof(buf));
  6857. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  6858. HEAP_HINT, devId) != 0)
  6859. ERROR_OUT(-5414, out);
  6860. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6861. #if defined(WOLFSSL_ASYNC_CRYPT)
  6862. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6863. #endif
  6864. if (ret != 0)
  6865. ERROR_OUT(-5415, out);
  6866. if (XMEMCMP(p2, buf, sizeof(p2)))
  6867. ERROR_OUT(-5416, out);
  6868. out:
  6869. if (aes_inited)
  6870. wc_AesXtsFree(aes);
  6871. #ifdef WOLFSSL_SMALL_STACK
  6872. if (aes)
  6873. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6874. #endif
  6875. return ret;
  6876. }
  6877. #endif /* WOLFSSL_AES_128 */
  6878. #ifdef WOLFSSL_AES_256
  6879. static int aes_xts_256_test(void)
  6880. {
  6881. #ifdef WOLFSSL_SMALL_STACK
  6882. XtsAes *aes = NULL;
  6883. #else
  6884. XtsAes aes[1];
  6885. #endif
  6886. int aes_inited = 0;
  6887. int ret = 0;
  6888. unsigned char buf[AES_BLOCK_SIZE * 3];
  6889. unsigned char cipher[AES_BLOCK_SIZE * 3];
  6890. /* 256 key tests */
  6891. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6892. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  6893. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  6894. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  6895. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  6896. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  6897. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  6898. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  6899. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  6900. };
  6901. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6902. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  6903. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  6904. };
  6905. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6906. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  6907. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  6908. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  6909. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  6910. };
  6911. /* plain text test of partial block is not from NIST test vector list */
  6912. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6913. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6914. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6915. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6916. };
  6917. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6918. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  6919. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  6920. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  6921. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  6922. };
  6923. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6924. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  6925. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  6926. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  6927. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  6928. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  6929. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  6930. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  6931. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  6932. };
  6933. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6934. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  6935. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  6936. };
  6937. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6938. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  6939. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  6940. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  6941. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  6942. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  6943. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  6944. };
  6945. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6946. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  6947. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  6948. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  6949. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  6950. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  6951. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  6952. };
  6953. #ifdef WOLFSSL_SMALL_STACK
  6954. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6955. ERROR_OUT(-5515, out);
  6956. #endif
  6957. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6958. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6959. if (ret != 0) {
  6960. printf("EVP_aes_256_xts failed\n");
  6961. goto out;
  6962. }
  6963. #endif
  6964. XMEMSET(buf, 0, sizeof(buf));
  6965. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6966. HEAP_HINT, devId) != 0)
  6967. ERROR_OUT(-5500, out);
  6968. else
  6969. aes_inited = 1;
  6970. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6971. #if defined(WOLFSSL_ASYNC_CRYPT)
  6972. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6973. #endif
  6974. if (ret != 0)
  6975. ERROR_OUT(-5501, out);
  6976. if (XMEMCMP(c2, buf, sizeof(c2)))
  6977. ERROR_OUT(-5502, out);
  6978. XMEMSET(buf, 0, sizeof(buf));
  6979. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6980. HEAP_HINT, devId) != 0)
  6981. ERROR_OUT(-5503, out);
  6982. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6983. #if defined(WOLFSSL_ASYNC_CRYPT)
  6984. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6985. #endif
  6986. if (ret != 0)
  6987. ERROR_OUT(-5504, out);
  6988. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6989. ERROR_OUT(-5505, out);
  6990. /* partial block encryption test */
  6991. XMEMSET(cipher, 0, sizeof(cipher));
  6992. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6993. #if defined(WOLFSSL_ASYNC_CRYPT)
  6994. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6995. #endif
  6996. if (ret != 0)
  6997. ERROR_OUT(-5506, out);
  6998. wc_AesXtsFree(aes);
  6999. /* partial block decrypt test */
  7000. XMEMSET(buf, 0, sizeof(buf));
  7001. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7002. HEAP_HINT, devId) != 0)
  7003. ERROR_OUT(-5507, out);
  7004. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7005. #if defined(WOLFSSL_ASYNC_CRYPT)
  7006. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7007. #endif
  7008. if (ret != 0)
  7009. ERROR_OUT(-5508, out);
  7010. if (XMEMCMP(pp, buf, sizeof(pp)))
  7011. ERROR_OUT(-5509, out);
  7012. /* NIST decrypt test vector */
  7013. XMEMSET(buf, 0, sizeof(buf));
  7014. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7015. #if defined(WOLFSSL_ASYNC_CRYPT)
  7016. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7017. #endif
  7018. if (ret != 0)
  7019. ERROR_OUT(-5510, out);
  7020. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7021. ERROR_OUT(-5511, out);
  7022. XMEMSET(buf, 0, sizeof(buf));
  7023. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7024. HEAP_HINT, devId) != 0)
  7025. ERROR_OUT(-5512, out);
  7026. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7027. #if defined(WOLFSSL_ASYNC_CRYPT)
  7028. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7029. #endif
  7030. if (ret != 0)
  7031. ERROR_OUT(-5513, out);
  7032. if (XMEMCMP(p2, buf, sizeof(p2)))
  7033. ERROR_OUT(-5514, out);
  7034. out:
  7035. if (aes_inited)
  7036. wc_AesXtsFree(aes);
  7037. #ifdef WOLFSSL_SMALL_STACK
  7038. if (aes)
  7039. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7040. #endif
  7041. return ret;
  7042. }
  7043. #endif /* WOLFSSL_AES_256 */
  7044. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7045. /* both 128 and 256 bit key test */
  7046. static int aes_xts_sector_test(void)
  7047. {
  7048. #ifdef WOLFSSL_SMALL_STACK
  7049. XtsAes *aes = NULL;
  7050. #else
  7051. XtsAes aes[1];
  7052. #endif
  7053. int aes_inited = 0;
  7054. int ret = 0;
  7055. unsigned char buf[AES_BLOCK_SIZE * 2];
  7056. /* 128 key tests */
  7057. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7058. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7059. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7060. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7061. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7062. };
  7063. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7064. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7065. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7066. };
  7067. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7068. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7069. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7070. };
  7071. word64 s1 = 141;
  7072. /* 256 key tests */
  7073. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7074. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7075. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7076. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7077. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7078. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7079. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7080. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7081. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7082. };
  7083. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7084. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7085. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7086. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7087. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7088. };
  7089. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7090. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7091. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7092. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7093. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7094. };
  7095. word64 s2 = 187;
  7096. #ifdef WOLFSSL_SMALL_STACK
  7097. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7098. ERROR_OUT(-5612, out);
  7099. #endif
  7100. XMEMSET(buf, 0, sizeof(buf));
  7101. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7102. HEAP_HINT, devId) != 0)
  7103. ERROR_OUT(-5600, out);
  7104. else
  7105. aes_inited = 1;
  7106. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7107. #if defined(WOLFSSL_ASYNC_CRYPT)
  7108. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7109. #endif
  7110. if (ret != 0)
  7111. ERROR_OUT(-5601, out);
  7112. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7113. ERROR_OUT(-5602, out);
  7114. /* decrypt test */
  7115. XMEMSET(buf, 0, sizeof(buf));
  7116. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7117. HEAP_HINT, devId) != 0)
  7118. ERROR_OUT(-5603, out);
  7119. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7120. #if defined(WOLFSSL_ASYNC_CRYPT)
  7121. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7122. #endif
  7123. if (ret != 0)
  7124. ERROR_OUT(-5604, out);
  7125. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7126. ERROR_OUT(-5605, out);
  7127. wc_AesXtsFree(aes);
  7128. /* 256 bit key tests */
  7129. XMEMSET(buf, 0, sizeof(buf));
  7130. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7131. HEAP_HINT, devId) != 0)
  7132. ERROR_OUT(-5606, out);
  7133. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7134. #if defined(WOLFSSL_ASYNC_CRYPT)
  7135. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7136. #endif
  7137. if (ret != 0)
  7138. ERROR_OUT(-5607, out);
  7139. if (XMEMCMP(c2, buf, sizeof(c2)))
  7140. ERROR_OUT(-5608, out);
  7141. /* decrypt test */
  7142. XMEMSET(buf, 0, sizeof(buf));
  7143. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7144. HEAP_HINT, devId) != 0)
  7145. ERROR_OUT(-5609, out);
  7146. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7147. #if defined(WOLFSSL_ASYNC_CRYPT)
  7148. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7149. #endif
  7150. if (ret != 0)
  7151. ERROR_OUT(-5610, out);
  7152. if (XMEMCMP(p2, buf, sizeof(p2)))
  7153. ERROR_OUT(-5611, out);
  7154. out:
  7155. if (aes_inited)
  7156. wc_AesXtsFree(aes);
  7157. #ifdef WOLFSSL_SMALL_STACK
  7158. if (aes)
  7159. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7160. #endif
  7161. return ret;
  7162. }
  7163. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7164. #ifdef WOLFSSL_AES_128
  7165. /* testing of bad arguments */
  7166. static int aes_xts_args_test(void)
  7167. {
  7168. #ifdef WOLFSSL_SMALL_STACK
  7169. XtsAes *aes = NULL;
  7170. #else
  7171. XtsAes aes[1];
  7172. #endif
  7173. int aes_inited = 0;
  7174. int ret;
  7175. unsigned char buf[AES_BLOCK_SIZE * 2];
  7176. /* 128 key tests */
  7177. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7178. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7179. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7180. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7181. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7182. };
  7183. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7184. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7185. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7186. };
  7187. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7188. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7189. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7190. };
  7191. word64 s1 = 141;
  7192. #ifdef WOLFSSL_SMALL_STACK
  7193. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7194. ERROR_OUT(-5708, out);
  7195. #endif
  7196. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7197. HEAP_HINT, devId) == 0)
  7198. ERROR_OUT(-5700, out);
  7199. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7200. HEAP_HINT, devId) == 0)
  7201. ERROR_OUT(-5701, out);
  7202. /* encryption operations */
  7203. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7204. HEAP_HINT, devId) != 0)
  7205. ERROR_OUT(-5702, out);
  7206. else
  7207. aes_inited = 1;
  7208. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7209. #if defined(WOLFSSL_ASYNC_CRYPT)
  7210. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7211. #endif
  7212. if (ret == 0)
  7213. ERROR_OUT(-5703, out);
  7214. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7215. #if defined(WOLFSSL_ASYNC_CRYPT)
  7216. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7217. #endif
  7218. if (ret == 0)
  7219. ERROR_OUT(-5704, out);
  7220. wc_AesXtsFree(aes);
  7221. /* decryption operations */
  7222. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7223. HEAP_HINT, devId) != 0)
  7224. ERROR_OUT(-5705, out);
  7225. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7226. #if defined(WOLFSSL_ASYNC_CRYPT)
  7227. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7228. #endif
  7229. if (ret == 0)
  7230. ERROR_OUT(-5706, out);
  7231. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7232. #if defined(WOLFSSL_ASYNC_CRYPT)
  7233. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7234. #endif
  7235. if (ret == 0)
  7236. ERROR_OUT(-5707, out);
  7237. ret = 0;
  7238. out:
  7239. if (aes_inited)
  7240. wc_AesXtsFree(aes);
  7241. #ifdef WOLFSSL_SMALL_STACK
  7242. if (aes)
  7243. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7244. #endif
  7245. return ret;
  7246. }
  7247. #endif /* WOLFSSL_AES_128 */
  7248. #endif /* WOLFSSL_AES_XTS */
  7249. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7250. static int aes_cbc_test(void)
  7251. {
  7252. byte cipher[AES_BLOCK_SIZE];
  7253. byte plain[AES_BLOCK_SIZE];
  7254. int ret;
  7255. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7256. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7257. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7258. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7259. };
  7260. byte key[] = "0123456789abcdef "; /* align */
  7261. byte iv[] = "1234567890abcdef "; /* align */
  7262. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7263. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7264. /* Parameter Validation testing. */
  7265. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7266. if (ret != BAD_FUNC_ARG)
  7267. return -5800;
  7268. #ifdef HAVE_AES_DECRYPT
  7269. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7270. if (ret != BAD_FUNC_ARG)
  7271. return -5801;
  7272. #endif
  7273. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7274. AES_BLOCK_SIZE, iv);
  7275. if (ret != 0)
  7276. return -5802;
  7277. #ifdef HAVE_AES_DECRYPT
  7278. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7279. AES_BLOCK_SIZE, iv);
  7280. if (ret != 0)
  7281. return -5803;
  7282. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7283. return -5804;
  7284. #endif /* HAVE_AES_DECRYPT */
  7285. (void)plain;
  7286. return 0;
  7287. }
  7288. #endif
  7289. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  7290. {
  7291. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7292. #ifdef WOLFSSL_SMALL_STACK
  7293. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7294. #else
  7295. Aes enc[1];
  7296. #endif
  7297. byte cipher[AES_BLOCK_SIZE * 4];
  7298. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7299. #ifdef WOLFSSL_SMALL_STACK
  7300. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7301. #else
  7302. Aes dec[1];
  7303. #endif
  7304. byte plain [AES_BLOCK_SIZE * 4];
  7305. #endif
  7306. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER */
  7307. int ret = 0;
  7308. #ifdef HAVE_AES_CBC
  7309. #ifdef WOLFSSL_AES_128
  7310. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7311. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7312. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7313. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7314. };
  7315. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7316. {
  7317. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  7318. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  7319. };
  7320. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  7321. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  7322. #ifdef WOLFSSL_SMALL_STACK
  7323. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7324. if (enc == NULL)
  7325. ERROR_OUT(-5948, out);
  7326. #endif
  7327. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7328. if (dec == NULL)
  7329. ERROR_OUT(-5949, out);
  7330. #endif
  7331. #endif
  7332. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7333. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  7334. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7335. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7336. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  7337. #endif
  7338. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7339. if (ret != 0)
  7340. ERROR_OUT(-5902, out);
  7341. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7342. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  7343. if (ret != 0)
  7344. ERROR_OUT(-5903, out);
  7345. #endif
  7346. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  7347. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  7348. #if defined(WOLFSSL_ASYNC_CRYPT)
  7349. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7350. #endif
  7351. if (ret != 0)
  7352. ERROR_OUT(-5904, out);
  7353. #ifdef HAVE_AES_DECRYPT
  7354. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  7355. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  7356. #if defined(WOLFSSL_ASYNC_CRYPT)
  7357. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7358. #endif
  7359. if (ret != 0)
  7360. ERROR_OUT(-5905, out);
  7361. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  7362. ERROR_OUT(-5906, out);
  7363. #endif /* HAVE_AES_DECRYPT */
  7364. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  7365. ERROR_OUT(-5907, out);
  7366. #endif /* WOLFSSL_AES_128 */
  7367. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  7368. {
  7369. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  7370. /* "All work and no play makes Jack a dull boy. " */
  7371. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7372. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7373. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7374. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7375. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7376. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7377. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7378. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7379. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7380. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7381. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7382. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7383. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7384. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7385. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7386. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7387. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7388. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7389. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7390. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7391. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7392. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7393. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7394. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7395. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7396. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7397. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7398. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7399. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7400. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7401. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7402. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7403. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7404. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7405. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7406. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7407. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7408. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7409. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7410. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7411. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7412. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7413. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7414. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7415. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7416. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7417. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7418. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  7419. };
  7420. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  7421. word32 keySz, msgSz;
  7422. #ifdef WOLFSSL_SMALL_STACK
  7423. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7424. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7425. if ((bigCipher == NULL) ||
  7426. (bigPlain == NULL)) {
  7427. if (bigCipher != NULL)
  7428. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7429. ERROR_OUT(-5947, out);
  7430. }
  7431. #else
  7432. byte bigCipher[sizeof(bigMsg)];
  7433. byte bigPlain[sizeof(bigMsg)];
  7434. #endif
  7435. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  7436. * message by AES_BLOCK_SIZE for each size of AES key. */
  7437. for (keySz = 16; keySz <= 32; keySz += 8) {
  7438. for (msgSz = AES_BLOCK_SIZE;
  7439. msgSz <= sizeof(bigMsg);
  7440. msgSz += AES_BLOCK_SIZE) {
  7441. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  7442. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  7443. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  7444. if (ret != 0) {
  7445. ret = -5908;
  7446. break;
  7447. }
  7448. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  7449. if (ret != 0) {
  7450. ret = -5909;
  7451. break;
  7452. }
  7453. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  7454. #if defined(WOLFSSL_ASYNC_CRYPT)
  7455. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7456. #endif
  7457. if (ret != 0) {
  7458. ret = -5910;
  7459. break;
  7460. }
  7461. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  7462. #if defined(WOLFSSL_ASYNC_CRYPT)
  7463. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7464. #endif
  7465. if (ret != 0) {
  7466. ret = -5911;
  7467. break;
  7468. }
  7469. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  7470. ret = -5912;
  7471. break;
  7472. }
  7473. }
  7474. if (ret != 0)
  7475. break;
  7476. }
  7477. #ifdef WOLFSSL_SMALL_STACK
  7478. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7479. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7480. #endif
  7481. if (ret != 0)
  7482. goto out;
  7483. }
  7484. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  7485. /* Test of AES IV state with encrypt/decrypt */
  7486. #ifdef WOLFSSL_AES_128
  7487. {
  7488. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  7489. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  7490. */
  7491. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7492. {
  7493. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  7494. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  7495. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  7496. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  7497. };
  7498. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  7499. {
  7500. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  7501. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  7502. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  7503. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  7504. };
  7505. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7506. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  7507. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  7508. };
  7509. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7510. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7511. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  7512. };
  7513. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7514. if (ret != 0)
  7515. ERROR_OUT(-5913, out);
  7516. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  7517. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  7518. #if defined(WOLFSSL_ASYNC_CRYPT)
  7519. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7520. #endif
  7521. if (ret != 0)
  7522. ERROR_OUT(-5914, out);
  7523. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  7524. ERROR_OUT(-5915, out);
  7525. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7526. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7527. #if defined(WOLFSSL_ASYNC_CRYPT)
  7528. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7529. #endif
  7530. if (ret != 0)
  7531. ERROR_OUT(-5916, out);
  7532. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  7533. AES_BLOCK_SIZE))
  7534. ERROR_OUT(-5917, out);
  7535. #if defined(HAVE_AES_DECRYPT)
  7536. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  7537. if (ret != 0)
  7538. ERROR_OUT(-5918, out);
  7539. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  7540. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  7541. #if defined(WOLFSSL_ASYNC_CRYPT)
  7542. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7543. #endif
  7544. if (ret != 0)
  7545. ERROR_OUT(-5919, out);
  7546. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  7547. ERROR_OUT(-5920, out);
  7548. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  7549. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7550. #if defined(WOLFSSL_ASYNC_CRYPT)
  7551. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7552. #endif
  7553. if (ret != 0)
  7554. ERROR_OUT(-5921, out);
  7555. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  7556. AES_BLOCK_SIZE))
  7557. ERROR_OUT(-5922, out);
  7558. #endif /* HAVE_AES_DECRYPT */
  7559. }
  7560. #endif /* WOLFSSL_AES_128 */
  7561. #endif /* HAVE_AES_CBC */
  7562. #ifdef WOLFSSL_AES_COUNTER
  7563. {
  7564. /* test vectors from "Recommendation for Block Cipher Modes of
  7565. * Operation" NIST Special Publication 800-38A */
  7566. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  7567. {
  7568. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  7569. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  7570. };
  7571. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  7572. {
  7573. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7574. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7575. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7576. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7577. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7578. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7579. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7580. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7581. };
  7582. #ifdef WOLFSSL_AES_128
  7583. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  7584. {
  7585. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  7586. 0xc2
  7587. };
  7588. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  7589. {
  7590. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7591. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7592. };
  7593. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  7594. {
  7595. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  7596. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  7597. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  7598. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  7599. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  7600. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  7601. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  7602. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  7603. };
  7604. #endif /* WOLFSSL_AES_128 */
  7605. #ifdef WOLFSSL_AES_192
  7606. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  7607. {
  7608. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7609. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7610. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7611. };
  7612. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  7613. {
  7614. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  7615. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  7616. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  7617. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  7618. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  7619. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  7620. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  7621. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  7622. };
  7623. #endif
  7624. #ifdef WOLFSSL_AES_256
  7625. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  7626. {
  7627. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7628. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7629. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7630. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7631. };
  7632. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  7633. {
  7634. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  7635. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  7636. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  7637. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  7638. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  7639. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  7640. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  7641. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  7642. };
  7643. #endif
  7644. #ifdef WOLFSSL_AES_128
  7645. wc_AesSetKeyDirect(enc, ctr128Key, sizeof(ctr128Key),
  7646. ctrIv, AES_ENCRYPTION);
  7647. /* Ctr only uses encrypt, even on key setup */
  7648. wc_AesSetKeyDirect(dec, ctr128Key, sizeof(ctr128Key),
  7649. ctrIv, AES_ENCRYPTION);
  7650. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(ctrPlain));
  7651. if (ret != 0) {
  7652. ERROR_OUT(-5923, out);
  7653. }
  7654. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(ctrPlain));
  7655. if (ret != 0) {
  7656. ERROR_OUT(-5924, out);
  7657. }
  7658. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7659. ERROR_OUT(-5925, out);
  7660. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  7661. ERROR_OUT(-5926, out);
  7662. /* let's try with just 9 bytes, non block size test */
  7663. wc_AesSetKeyDirect(enc, ctr128Key, AES_BLOCK_SIZE,
  7664. ctrIv, AES_ENCRYPTION);
  7665. /* Ctr only uses encrypt, even on key setup */
  7666. wc_AesSetKeyDirect(dec, ctr128Key, AES_BLOCK_SIZE,
  7667. ctrIv, AES_ENCRYPTION);
  7668. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7669. if (ret != 0) {
  7670. ERROR_OUT(-5927, out);
  7671. }
  7672. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7673. if (ret != 0) {
  7674. ERROR_OUT(-5928, out);
  7675. }
  7676. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7677. ERROR_OUT(-5929, out);
  7678. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  7679. ERROR_OUT(-5930, out);
  7680. /* and an additional 9 bytes to reuse tmp left buffer */
  7681. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7682. if (ret != 0) {
  7683. ERROR_OUT(-5931, out);
  7684. }
  7685. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7686. if (ret != 0) {
  7687. ERROR_OUT(-5932, out);
  7688. }
  7689. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7690. ERROR_OUT(-5933, out);
  7691. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  7692. ERROR_OUT(-5934, out);
  7693. #endif /* WOLFSSL_AES_128 */
  7694. #ifdef WOLFSSL_AES_192
  7695. /* 192 bit key */
  7696. wc_AesSetKeyDirect(enc, ctr192Key, sizeof(ctr192Key),
  7697. ctrIv, AES_ENCRYPTION);
  7698. /* Ctr only uses encrypt, even on key setup */
  7699. wc_AesSetKeyDirect(dec, ctr192Key, sizeof(ctr192Key),
  7700. ctrIv, AES_ENCRYPTION);
  7701. XMEMSET(plain, 0, sizeof(plain));
  7702. ret = wc_AesCtrEncrypt(enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  7703. if (ret != 0) {
  7704. ERROR_OUT(-5935, out);
  7705. }
  7706. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  7707. ERROR_OUT(-5936, out);
  7708. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7709. if (ret != 0) {
  7710. ERROR_OUT(-5937, out);
  7711. }
  7712. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  7713. ERROR_OUT(-5938, out);
  7714. #endif /* WOLFSSL_AES_192 */
  7715. #ifdef WOLFSSL_AES_256
  7716. /* 256 bit key */
  7717. wc_AesSetKeyDirect(enc, ctr256Key, sizeof(ctr256Key),
  7718. ctrIv, AES_ENCRYPTION);
  7719. /* Ctr only uses encrypt, even on key setup */
  7720. wc_AesSetKeyDirect(dec, ctr256Key, sizeof(ctr256Key),
  7721. ctrIv, AES_ENCRYPTION);
  7722. XMEMSET(plain, 0, sizeof(plain));
  7723. ret = wc_AesCtrEncrypt(enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  7724. if (ret != 0) {
  7725. ERROR_OUT(-5939, out);
  7726. }
  7727. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7728. ERROR_OUT(-5940, out);
  7729. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7730. if (ret != 0) {
  7731. ERROR_OUT(-5941, out);
  7732. }
  7733. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  7734. ERROR_OUT(-5942, out);
  7735. #endif /* WOLFSSL_AES_256 */
  7736. }
  7737. #endif /* WOLFSSL_AES_COUNTER */
  7738. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  7739. {
  7740. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7741. {
  7742. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7743. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7744. };
  7745. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7746. {
  7747. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7748. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7749. };
  7750. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7751. {
  7752. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7753. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7754. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7755. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7756. };
  7757. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7758. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7759. if (ret != 0)
  7760. ERROR_OUT(-5943, out);
  7761. wc_AesEncryptDirect(enc, cipher, niPlain);
  7762. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  7763. ERROR_OUT(-5944, out);
  7764. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7765. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  7766. if (ret != 0)
  7767. ERROR_OUT(-5945, out);
  7768. wc_AesDecryptDirect(dec, plain, niCipher);
  7769. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  7770. ERROR_OUT(-5946, out);
  7771. }
  7772. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  7773. ret = aes_key_size_test();
  7774. if (ret != 0)
  7775. goto out;
  7776. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7777. ret = aes_cbc_test();
  7778. if (ret != 0)
  7779. goto out;
  7780. #endif
  7781. #if defined(WOLFSSL_AES_XTS)
  7782. #ifdef WOLFSSL_AES_128
  7783. ret = aes_xts_128_test();
  7784. if (ret != 0)
  7785. goto out;
  7786. #endif
  7787. #ifdef WOLFSSL_AES_256
  7788. ret = aes_xts_256_test();
  7789. if (ret != 0)
  7790. goto out;
  7791. #endif
  7792. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7793. ret = aes_xts_sector_test();
  7794. if (ret != 0)
  7795. goto out;
  7796. #endif
  7797. #ifdef WOLFSSL_AES_128
  7798. ret = aes_xts_args_test();
  7799. if (ret != 0)
  7800. goto out;
  7801. #endif
  7802. #endif
  7803. #if defined(WOLFSSL_AES_CFB)
  7804. ret = aescfb_test();
  7805. if (ret != 0)
  7806. goto out;
  7807. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7808. ret = aescfb1_test();
  7809. if (ret != 0)
  7810. goto out;
  7811. ret = aescfb8_test();
  7812. if (ret != 0)
  7813. goto out;
  7814. #endif
  7815. #endif
  7816. out:
  7817. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  7818. #ifdef WOLFSSL_SMALL_STACK
  7819. if (enc) {
  7820. if (ret != -5900) /* note this must match ERRROR_OUT() code
  7821. * for wc_AesInit(enc, ...) failure above.
  7822. */
  7823. wc_AesFree(enc);
  7824. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7825. }
  7826. #else
  7827. if (ret != -5900)
  7828. wc_AesFree(enc);
  7829. #endif
  7830. (void)cipher;
  7831. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7832. #ifdef WOLFSSL_SMALL_STACK
  7833. if (dec) {
  7834. if ((ret != -5900) && (ret != -5901))
  7835. /* note these codes must match the ERRROR_OUT() codes for
  7836. * wc_AesInit() failures above.
  7837. */
  7838. wc_AesFree(dec);
  7839. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7840. }
  7841. #else
  7842. if ((ret != -5900) && (ret != -5901))
  7843. wc_AesFree(dec);
  7844. #endif
  7845. (void)plain;
  7846. #endif
  7847. #endif
  7848. return ret;
  7849. }
  7850. #ifdef WOLFSSL_AES_192
  7851. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  7852. {
  7853. #ifdef HAVE_AES_CBC
  7854. #ifdef WOLFSSL_SMALL_STACK
  7855. Aes *enc = NULL;
  7856. #else
  7857. Aes enc[1];
  7858. #endif
  7859. byte cipher[AES_BLOCK_SIZE];
  7860. #ifdef HAVE_AES_DECRYPT
  7861. #ifdef WOLFSSL_SMALL_STACK
  7862. Aes *dec = NULL;
  7863. #else
  7864. Aes dec[1];
  7865. #endif
  7866. byte plain[AES_BLOCK_SIZE];
  7867. #endif
  7868. #endif /* HAVE_AES_CBC */
  7869. int ret = 0;
  7870. #ifdef HAVE_AES_CBC
  7871. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  7872. * Appendix F.2.3 */
  7873. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  7874. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7875. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7876. };
  7877. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7878. {
  7879. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  7880. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  7881. };
  7882. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  7883. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7884. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7885. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7886. };
  7887. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  7888. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7889. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7890. };
  7891. #ifdef WOLFSSL_SMALL_STACK
  7892. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7893. ERROR_OUT(-6008, out);
  7894. #ifdef HAVE_AES_DECRYPT
  7895. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7896. ERROR_OUT(-6009, out);
  7897. #endif
  7898. #endif
  7899. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7900. ERROR_OUT(-6000, out);
  7901. #ifdef HAVE_AES_DECRYPT
  7902. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7903. ERROR_OUT(-6001, out);
  7904. #endif
  7905. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  7906. if (ret != 0)
  7907. ERROR_OUT(-6002, out);
  7908. #ifdef HAVE_AES_DECRYPT
  7909. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  7910. if (ret != 0)
  7911. ERROR_OUT(-6003, out);
  7912. #endif
  7913. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7914. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  7915. #if defined(WOLFSSL_ASYNC_CRYPT)
  7916. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7917. #endif
  7918. if (ret != 0)
  7919. ERROR_OUT(-6004, out);
  7920. #ifdef HAVE_AES_DECRYPT
  7921. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7922. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  7923. #if defined(WOLFSSL_ASYNC_CRYPT)
  7924. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7925. #endif
  7926. if (ret != 0)
  7927. ERROR_OUT(-6005, out);
  7928. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  7929. ERROR_OUT(-6006, out);
  7930. }
  7931. #endif
  7932. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  7933. ERROR_OUT(-6007, out);
  7934. wc_AesFree(enc);
  7935. #ifdef HAVE_AES_DECRYPT
  7936. wc_AesFree(dec);
  7937. #endif
  7938. out:
  7939. #ifdef WOLFSSL_SMALL_STACK
  7940. if (enc)
  7941. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7942. #ifdef HAVE_AES_DECRYPT
  7943. if (dec)
  7944. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7945. #endif
  7946. #endif
  7947. #endif /* HAVE_AES_CBC */
  7948. return ret;
  7949. }
  7950. #endif /* WOLFSSL_AES_192 */
  7951. #ifdef WOLFSSL_AES_256
  7952. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  7953. {
  7954. #ifdef HAVE_AES_CBC
  7955. #ifdef WOLFSSL_SMALL_STACK
  7956. Aes *enc = NULL;
  7957. #else
  7958. Aes enc[1];
  7959. #endif
  7960. byte cipher[AES_BLOCK_SIZE];
  7961. #ifdef HAVE_AES_DECRYPT
  7962. #ifdef WOLFSSL_SMALL_STACK
  7963. Aes *dec = NULL;
  7964. #else
  7965. Aes dec[1];
  7966. #endif
  7967. byte plain[AES_BLOCK_SIZE];
  7968. #endif
  7969. #endif /* HAVE_AES_CBC */
  7970. int ret = 0;
  7971. #ifdef HAVE_AES_CBC
  7972. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  7973. * Appendix F.2.5 */
  7974. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  7975. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7976. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7977. };
  7978. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7979. {
  7980. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  7981. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  7982. };
  7983. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  7984. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7985. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7986. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7987. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7988. };
  7989. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  7990. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7991. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7992. };
  7993. #ifdef WOLFSSL_SMALL_STACK
  7994. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7995. ERROR_OUT(-6108, out);
  7996. #ifdef HAVE_AES_DECRYPT
  7997. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7998. ERROR_OUT(-6109, out);
  7999. #endif
  8000. #endif
  8001. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8002. ERROR_OUT(-6100, out);
  8003. #ifdef HAVE_AES_DECRYPT
  8004. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8005. ERROR_OUT(-6101, out);
  8006. #endif
  8007. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  8008. if (ret != 0)
  8009. ERROR_OUT(-6102, out);
  8010. #ifdef HAVE_AES_DECRYPT
  8011. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  8012. if (ret != 0)
  8013. ERROR_OUT(-6103, out);
  8014. #endif
  8015. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8016. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  8017. #if defined(WOLFSSL_ASYNC_CRYPT)
  8018. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8019. #endif
  8020. if (ret != 0)
  8021. ERROR_OUT(-6104, out);
  8022. #ifdef HAVE_AES_DECRYPT
  8023. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8024. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  8025. #if defined(WOLFSSL_ASYNC_CRYPT)
  8026. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8027. #endif
  8028. if (ret != 0)
  8029. ERROR_OUT(-6105, out);
  8030. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  8031. ERROR_OUT(-6106, out);
  8032. }
  8033. #endif
  8034. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  8035. ERROR_OUT(-6107, out);
  8036. wc_AesFree(enc);
  8037. #ifdef HAVE_AES_DECRYPT
  8038. wc_AesFree(dec);
  8039. #endif
  8040. out:
  8041. #ifdef WOLFSSL_SMALL_STACK
  8042. if (enc)
  8043. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8044. #ifdef HAVE_AES_DECRYPT
  8045. if (dec)
  8046. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8047. #endif
  8048. #endif
  8049. #endif /* HAVE_AES_CBC */
  8050. return ret;
  8051. }
  8052. #endif /* WOLFSSL_AES_256 */
  8053. #ifdef HAVE_AESGCM
  8054. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  8055. byte* plain, int plainSz, byte* cipher, int cipherSz,
  8056. byte* aad, int aadSz, byte* tag, int tagSz)
  8057. {
  8058. int ret, enc_inited = 0, dec_inited = 0;
  8059. #ifdef WOLFSSL_SMALL_STACK
  8060. Aes *enc = NULL;
  8061. Aes *dec = NULL;
  8062. #else
  8063. Aes enc[1];
  8064. Aes dec[1];
  8065. #endif
  8066. byte resultT[AES_BLOCK_SIZE];
  8067. byte resultP[AES_BLOCK_SIZE * 3];
  8068. byte resultC[AES_BLOCK_SIZE * 3];
  8069. int result;
  8070. #ifdef WOLFSSL_SMALL_STACK
  8071. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8072. ERROR_OUT(-6118, out);
  8073. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8074. ERROR_OUT(-6119, out);
  8075. #endif
  8076. XMEMSET(resultT, 0, sizeof(resultT));
  8077. XMEMSET(resultC, 0, sizeof(resultC));
  8078. XMEMSET(resultP, 0, sizeof(resultP));
  8079. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8080. ERROR_OUT(-6110, out);
  8081. else
  8082. enc_inited = 1;
  8083. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8084. ERROR_OUT(-6111, out);
  8085. else
  8086. dec_inited = 1;
  8087. result = wc_AesGcmSetKey(enc, key, keySz);
  8088. if (result != 0)
  8089. ERROR_OUT(-6112, out);
  8090. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8091. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  8092. resultT, tagSz, aad, aadSz);
  8093. #if defined(WOLFSSL_ASYNC_CRYPT)
  8094. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8095. #endif
  8096. if (result != 0)
  8097. ERROR_OUT(-6113, out);
  8098. if (cipher != NULL) {
  8099. if (XMEMCMP(cipher, resultC, cipherSz))
  8100. ERROR_OUT(-6114, out);
  8101. }
  8102. if (XMEMCMP(tag, resultT, tagSz))
  8103. ERROR_OUT(-6115, out);
  8104. #ifdef HAVE_AES_DECRYPT
  8105. result = wc_AesGcmSetKey(dec, key, keySz);
  8106. if (result != 0)
  8107. ERROR_OUT(-6116, out);
  8108. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  8109. iv, ivSz, resultT, tagSz, aad, aadSz);
  8110. #if defined(WOLFSSL_ASYNC_CRYPT)
  8111. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8112. #endif
  8113. if (result != 0)
  8114. ERROR_OUT(-6117, out);
  8115. if (plain != NULL) {
  8116. if (XMEMCMP(plain, resultP, plainSz))
  8117. ERROR_OUT(-6118, out);
  8118. }
  8119. #endif /* HAVE_AES_DECRYPT */
  8120. ret = 0;
  8121. out:
  8122. if (enc_inited)
  8123. wc_AesFree(enc);
  8124. if (dec_inited)
  8125. wc_AesFree(dec);
  8126. #ifdef WOLFSSL_SMALL_STACK
  8127. if (enc)
  8128. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8129. if (dec)
  8130. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8131. #endif
  8132. return ret;
  8133. }
  8134. /* tests that only use 12 byte IV and 16 or less byte AAD
  8135. * test vectors are from NIST SP 800-38D
  8136. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  8137. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  8138. {
  8139. byte key1[] = {
  8140. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  8141. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  8142. };
  8143. byte iv1[] = {
  8144. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  8145. 0xe4, 0xed, 0x2f, 0x6d
  8146. };
  8147. ALIGN64 byte plain1[] = {
  8148. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  8149. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  8150. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  8151. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  8152. };
  8153. byte aad1[] = {
  8154. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  8155. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  8156. };
  8157. ALIGN64 byte cipher1[] = {
  8158. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  8159. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  8160. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  8161. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  8162. };
  8163. byte tag1[] = {
  8164. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  8165. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  8166. };
  8167. byte key2[] = {
  8168. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  8169. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  8170. };
  8171. byte iv2[] = {
  8172. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  8173. 0xc9, 0x8a, 0xff, 0xe3
  8174. };
  8175. ALIGN64 byte plain2[] = {
  8176. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  8177. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  8178. };
  8179. ALIGN64 byte cipher2[] = {
  8180. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  8181. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  8182. };
  8183. byte tag2[] = {
  8184. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  8185. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  8186. };
  8187. byte key3[] = {
  8188. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  8189. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  8190. };
  8191. byte iv3[] = {
  8192. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  8193. 0x6a, 0x4b, 0xd5, 0xe1
  8194. };
  8195. byte tag3[] = {
  8196. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  8197. 0x11, 0x64, 0xb2, 0xff
  8198. };
  8199. int ret;
  8200. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  8201. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  8202. aad1, sizeof(aad1), tag1, sizeof(tag1));
  8203. if (ret != 0) {
  8204. return ret;
  8205. }
  8206. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  8207. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  8208. NULL, 0, tag2, sizeof(tag2));
  8209. if (ret != 0) {
  8210. return ret;
  8211. }
  8212. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  8213. NULL, 0, NULL, 0,
  8214. NULL, 0, tag3, sizeof(tag3));
  8215. if (ret != 0) {
  8216. return ret;
  8217. }
  8218. return 0;
  8219. }
  8220. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  8221. {
  8222. #ifdef WOLFSSL_SMALL_STACK
  8223. Aes *enc = NULL;
  8224. Aes *dec = NULL;
  8225. #else
  8226. Aes enc[1];
  8227. Aes dec[1];
  8228. #endif
  8229. /*
  8230. * This is Test Case 16 from the document Galois/
  8231. * Counter Mode of Operation (GCM) by McGrew and
  8232. * Viega.
  8233. */
  8234. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8235. {
  8236. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  8237. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  8238. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  8239. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  8240. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  8241. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  8242. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  8243. 0xba, 0x63, 0x7b, 0x39
  8244. };
  8245. #if defined(WOLFSSL_AES_256)
  8246. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8247. {
  8248. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8249. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8250. 0xab, 0xad, 0xda, 0xd2
  8251. };
  8252. #endif
  8253. #ifdef WOLFSSL_AES_256
  8254. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8255. {
  8256. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8257. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8258. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8259. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  8260. };
  8261. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8262. {
  8263. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  8264. 0xde, 0xca, 0xf8, 0x88
  8265. };
  8266. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  8267. {
  8268. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  8269. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  8270. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  8271. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  8272. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  8273. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  8274. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  8275. 0xbc, 0xc9, 0xf6, 0x62
  8276. };
  8277. #endif /* WOLFSSL_AES_256 */
  8278. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8279. {
  8280. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  8281. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  8282. };
  8283. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  8284. #if !defined(HAVE_FIPS) && \
  8285. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  8286. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  8287. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8288. !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  8289. !(defined(WOLF_CRYPTO_CB) && \
  8290. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  8291. #define ENABLE_NON_12BYTE_IV_TEST
  8292. #ifdef WOLFSSL_AES_192
  8293. /* Test Case 12, uses same plaintext and AAD data. */
  8294. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8295. {
  8296. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8297. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8298. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  8299. };
  8300. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8301. {
  8302. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  8303. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  8304. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  8305. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  8306. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  8307. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  8308. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  8309. 0xa6, 0x37, 0xb3, 0x9b
  8310. };
  8311. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  8312. {
  8313. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  8314. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  8315. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  8316. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  8317. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  8318. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  8319. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  8320. 0xe9, 0xb7, 0x37, 0x3b
  8321. };
  8322. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8323. {
  8324. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  8325. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  8326. };
  8327. #endif /* WOLFSSL_AES_192 */
  8328. #ifdef WOLFSSL_AES_128
  8329. /* The following is an interesting test case from the example
  8330. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  8331. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  8332. {
  8333. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  8334. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  8335. };
  8336. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  8337. {
  8338. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  8339. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  8340. };
  8341. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  8342. {
  8343. 0xca
  8344. };
  8345. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  8346. {
  8347. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  8348. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  8349. };
  8350. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  8351. {
  8352. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  8353. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  8354. };
  8355. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  8356. {
  8357. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  8358. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  8359. };
  8360. #endif /* WOLFSSL_AES_128 */
  8361. #ifdef WOLFSSL_AES_256
  8362. int ivlen;
  8363. #endif
  8364. #endif
  8365. byte resultT[sizeof(t1)];
  8366. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  8367. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  8368. int result = 0;
  8369. int ret;
  8370. #ifdef WOLFSSL_AES_256
  8371. int alen;
  8372. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8373. int plen;
  8374. #endif
  8375. #endif
  8376. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8377. #if !defined(BENCH_AESGCM_LARGE)
  8378. #define BENCH_AESGCM_LARGE 1024
  8379. #endif
  8380. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8381. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8382. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8383. if ((! large_input) || (! large_output) || (! large_outdec))
  8384. ERROR_OUT(MEMORY_E, out);
  8385. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  8386. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  8387. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  8388. #endif
  8389. #ifdef WOLFSSL_SMALL_STACK
  8390. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8391. ERROR_OUT(-6342, out);
  8392. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8393. ERROR_OUT(-6343, out);
  8394. #endif
  8395. (void)result;
  8396. XMEMSET(resultT, 0, sizeof(resultT));
  8397. XMEMSET(resultC, 0, sizeof(resultC));
  8398. XMEMSET(resultP, 0, sizeof(resultP));
  8399. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  8400. ERROR_OUT(-6300, out);
  8401. }
  8402. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  8403. ERROR_OUT(-6301, out);
  8404. }
  8405. #ifdef WOLFSSL_AES_256
  8406. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8407. if (result != 0)
  8408. ERROR_OUT(-6302, out);
  8409. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8410. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8411. resultT, sizeof(resultT), a, sizeof(a));
  8412. #if defined(WOLFSSL_ASYNC_CRYPT)
  8413. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8414. #endif
  8415. if (result != 0)
  8416. ERROR_OUT(-6303, out);
  8417. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8418. ERROR_OUT(-6304, out);
  8419. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  8420. ERROR_OUT(-6305, out);
  8421. #ifdef HAVE_AES_DECRYPT
  8422. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8423. if (result != 0)
  8424. ERROR_OUT(-6306, out);
  8425. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  8426. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8427. #if defined(WOLFSSL_ASYNC_CRYPT)
  8428. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8429. #endif
  8430. if (result != 0)
  8431. ERROR_OUT(-6307, out);
  8432. if (XMEMCMP(p, resultP, sizeof(p)))
  8433. ERROR_OUT(-6308, out);
  8434. #endif /* HAVE_AES_DECRYPT */
  8435. /* Large buffer test */
  8436. #ifdef BENCH_AESGCM_LARGE
  8437. /* setup test buffer */
  8438. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  8439. large_input[alen] = (byte)alen;
  8440. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8441. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8442. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  8443. resultT, sizeof(resultT), a, sizeof(a));
  8444. #if defined(WOLFSSL_ASYNC_CRYPT)
  8445. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8446. #endif
  8447. if (result != 0)
  8448. ERROR_OUT(-6309, out);
  8449. #ifdef HAVE_AES_DECRYPT
  8450. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8451. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  8452. sizeof(resultT), a, sizeof(a));
  8453. #if defined(WOLFSSL_ASYNC_CRYPT)
  8454. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8455. #endif
  8456. if (result != 0)
  8457. ERROR_OUT(-6310, out);
  8458. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  8459. ERROR_OUT(-6311, out);
  8460. #endif /* HAVE_AES_DECRYPT */
  8461. #endif /* BENCH_AESGCM_LARGE */
  8462. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  8463. /* Variable IV length test */
  8464. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  8465. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8466. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  8467. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8468. #if defined(WOLFSSL_ASYNC_CRYPT)
  8469. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8470. #endif
  8471. if (result != 0)
  8472. ERROR_OUT(-6312, out);
  8473. #ifdef HAVE_AES_DECRYPT
  8474. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  8475. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8476. #if defined(WOLFSSL_ASYNC_CRYPT)
  8477. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8478. #endif
  8479. if (result != 0)
  8480. ERROR_OUT(-6313, out);
  8481. #endif /* HAVE_AES_DECRYPT */
  8482. }
  8483. #endif
  8484. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  8485. /* Variable authenticated data length test */
  8486. for (alen=0; alen<(int)sizeof(p); alen++) {
  8487. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8488. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  8489. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8490. #if defined(WOLFSSL_ASYNC_CRYPT)
  8491. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8492. #endif
  8493. if (result != 0)
  8494. ERROR_OUT(-6314, out);
  8495. #ifdef HAVE_AES_DECRYPT
  8496. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  8497. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8498. #if defined(WOLFSSL_ASYNC_CRYPT)
  8499. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8500. #endif
  8501. if (result != 0)
  8502. ERROR_OUT(-6315, out);
  8503. #endif /* HAVE_AES_DECRYPT */
  8504. }
  8505. #endif
  8506. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8507. #ifdef BENCH_AESGCM_LARGE
  8508. /* Variable plain text length test */
  8509. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  8510. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8511. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8512. plen, iv1, sizeof(iv1), resultT,
  8513. sizeof(resultT), a, sizeof(a));
  8514. #if defined(WOLFSSL_ASYNC_CRYPT)
  8515. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8516. #endif
  8517. if (result != 0)
  8518. ERROR_OUT(-6316, out);
  8519. #ifdef HAVE_AES_DECRYPT
  8520. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8521. plen, iv1, sizeof(iv1), resultT,
  8522. sizeof(resultT), a, sizeof(a));
  8523. #if defined(WOLFSSL_ASYNC_CRYPT)
  8524. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8525. #endif
  8526. if (result != 0)
  8527. ERROR_OUT(-6317, out);
  8528. #endif /* HAVE_AES_DECRYPT */
  8529. }
  8530. #else /* BENCH_AESGCM_LARGE */
  8531. /* Variable plain text length test */
  8532. for (plen=1; plen<(int)sizeof(p); plen++) {
  8533. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8534. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  8535. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8536. #if defined(WOLFSSL_ASYNC_CRYPT)
  8537. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8538. #endif
  8539. if (result != 0)
  8540. ERROR_OUT(-6318, out);
  8541. #ifdef HAVE_AES_DECRYPT
  8542. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  8543. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8544. #if defined(WOLFSSL_ASYNC_CRYPT)
  8545. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8546. #endif
  8547. if (result != 0)
  8548. ERROR_OUT(-6319, out);
  8549. #endif /* HAVE_AES_DECRYPT */
  8550. }
  8551. #endif /* BENCH_AESGCM_LARGE */
  8552. #endif
  8553. #endif /* WOLFSSL_AES_256 */
  8554. /* test with IV != 12 bytes */
  8555. #ifdef ENABLE_NON_12BYTE_IV_TEST
  8556. XMEMSET(resultT, 0, sizeof(resultT));
  8557. XMEMSET(resultC, 0, sizeof(resultC));
  8558. XMEMSET(resultP, 0, sizeof(resultP));
  8559. #ifdef WOLFSSL_AES_192
  8560. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  8561. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8562. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  8563. resultT, sizeof(resultT), a, sizeof(a));
  8564. #if defined(WOLFSSL_ASYNC_CRYPT)
  8565. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8566. #endif
  8567. if (result != 0)
  8568. ERROR_OUT(-6320, out);
  8569. if (XMEMCMP(c2, resultC, sizeof(c2)))
  8570. ERROR_OUT(-6321, out);
  8571. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  8572. ERROR_OUT(-6322, out);
  8573. #ifdef HAVE_AES_DECRYPT
  8574. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  8575. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  8576. #if defined(WOLFSSL_ASYNC_CRYPT)
  8577. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8578. #endif
  8579. if (result != 0)
  8580. ERROR_OUT(-6323, out);
  8581. if (XMEMCMP(p, resultP, sizeof(p)))
  8582. ERROR_OUT(-6324, out);
  8583. #endif /* HAVE_AES_DECRYPT */
  8584. XMEMSET(resultT, 0, sizeof(resultT));
  8585. XMEMSET(resultC, 0, sizeof(resultC));
  8586. XMEMSET(resultP, 0, sizeof(resultP));
  8587. #endif /* WOLFSSL_AES_192 */
  8588. #ifdef WOLFSSL_AES_128
  8589. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  8590. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8591. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  8592. resultT, sizeof(t3), a3, sizeof(a3));
  8593. #if defined(WOLFSSL_ASYNC_CRYPT)
  8594. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8595. #endif
  8596. if (result != 0)
  8597. ERROR_OUT(-6325, out);
  8598. if (XMEMCMP(c3, resultC, sizeof(c3)))
  8599. ERROR_OUT(-6326, out);
  8600. if (XMEMCMP(t3, resultT, sizeof(t3)))
  8601. ERROR_OUT(-6327, out);
  8602. #ifdef HAVE_AES_DECRYPT
  8603. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  8604. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  8605. #if defined(WOLFSSL_ASYNC_CRYPT)
  8606. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8607. #endif
  8608. if (result != 0)
  8609. ERROR_OUT(-6328, out);
  8610. if (XMEMCMP(p3, resultP, sizeof(p3)))
  8611. ERROR_OUT(-6329, out);
  8612. #endif /* HAVE_AES_DECRYPT */
  8613. #endif /* WOLFSSL_AES_128 */
  8614. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  8615. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8616. !defined(WOLFSSL_XILINX_CRYPT) && \
  8617. !(defined(WOLF_CRYPTO_CB) && \
  8618. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  8619. XMEMSET(resultT, 0, sizeof(resultT));
  8620. XMEMSET(resultC, 0, sizeof(resultC));
  8621. XMEMSET(resultP, 0, sizeof(resultP));
  8622. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8623. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8624. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8625. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8626. #if defined(WOLFSSL_ASYNC_CRYPT)
  8627. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8628. #endif
  8629. if (result != 0)
  8630. ERROR_OUT(-6330, out);
  8631. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8632. ERROR_OUT(-6331, out);
  8633. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  8634. ERROR_OUT(-6332, out);
  8635. #ifdef HAVE_AES_DECRYPT
  8636. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  8637. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8638. #if defined(WOLFSSL_ASYNC_CRYPT)
  8639. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8640. #endif
  8641. if (result != 0)
  8642. ERROR_OUT(-6333, out);
  8643. if (XMEMCMP(p, resultP, sizeof(p)))
  8644. ERROR_OUT(-6334, out);
  8645. #endif /* HAVE_AES_DECRYPT */
  8646. #endif /* WOLFSSL_AES_256 */
  8647. #if !defined(HAVE_FIPS) || \
  8648. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  8649. /* Test encrypt with internally generated IV */
  8650. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  8651. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  8652. {
  8653. WC_RNG rng;
  8654. byte randIV[12];
  8655. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  8656. if (result != 0)
  8657. ERROR_OUT(-6335, out);
  8658. XMEMSET(randIV, 0, sizeof(randIV));
  8659. XMEMSET(resultT, 0, sizeof(resultT));
  8660. XMEMSET(resultC, 0, sizeof(resultC));
  8661. XMEMSET(resultP, 0, sizeof(resultP));
  8662. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8663. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  8664. if (result != 0)
  8665. ERROR_OUT(-6336, out);
  8666. result = wc_AesGcmEncrypt_ex(enc,
  8667. resultC, p, sizeof(p),
  8668. randIV, sizeof(randIV),
  8669. resultT, sizeof(resultT),
  8670. a, sizeof(a));
  8671. #if defined(WOLFSSL_ASYNC_CRYPT)
  8672. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8673. #endif
  8674. if (result != 0)
  8675. ERROR_OUT(-6337, out);
  8676. /* Check the IV has been set. */
  8677. {
  8678. word32 i, ivSum = 0;
  8679. for (i = 0; i < sizeof(randIV); i++)
  8680. ivSum += randIV[i];
  8681. if (ivSum == 0)
  8682. ERROR_OUT(-6338, out);
  8683. }
  8684. #ifdef HAVE_AES_DECRYPT
  8685. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8686. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  8687. if (result != 0)
  8688. ERROR_OUT(-6339, out);
  8689. result = wc_AesGcmDecrypt(dec,
  8690. resultP, resultC, sizeof(c1),
  8691. randIV, sizeof(randIV),
  8692. resultT, sizeof(resultT),
  8693. a, sizeof(a));
  8694. #if defined(WOLFSSL_ASYNC_CRYPT)
  8695. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8696. #endif
  8697. if (result != 0)
  8698. ERROR_OUT(-6340, out);
  8699. if (XMEMCMP(p, resultP, sizeof(p)))
  8700. ERROR_OUT(-6341, out);
  8701. #endif /* HAVE_AES_DECRYPT */
  8702. wc_FreeRng(&rng);
  8703. }
  8704. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  8705. #endif /* HAVE_FIPS_VERSION >= 2 */
  8706. wc_AesFree(enc);
  8707. wc_AesFree(dec);
  8708. ret = 0;
  8709. out:
  8710. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8711. if (large_input)
  8712. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8713. if (large_output)
  8714. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8715. if (large_outdec)
  8716. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8717. #endif
  8718. #ifdef WOLFSSL_SMALL_STACK
  8719. if (enc)
  8720. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8721. if (dec)
  8722. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8723. #endif
  8724. return ret;
  8725. }
  8726. #ifdef WOLFSSL_AES_128
  8727. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  8728. {
  8729. int ret;
  8730. #ifdef WOLFSSL_SMALL_STACK
  8731. Gmac *gmac;
  8732. #else
  8733. Gmac gmac[1];
  8734. #endif
  8735. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8736. {
  8737. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  8738. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  8739. };
  8740. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8741. {
  8742. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  8743. 0xe2, 0x8c, 0x8f, 0x16
  8744. };
  8745. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  8746. {
  8747. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  8748. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  8749. };
  8750. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8751. {
  8752. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  8753. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8754. };
  8755. #if (!defined(HAVE_FIPS) || \
  8756. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  8757. /* FIPS builds only allow 16-byte auth tags. */
  8758. /* This sample uses a 15-byte auth tag. */
  8759. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8760. {
  8761. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  8762. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  8763. };
  8764. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8765. {
  8766. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  8767. 0x1a, 0x60, 0x24, 0xa7
  8768. };
  8769. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  8770. {
  8771. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  8772. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  8773. };
  8774. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8775. {
  8776. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  8777. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  8778. };
  8779. #endif
  8780. byte tag[16];
  8781. #ifdef WOLFSSL_SMALL_STACK
  8782. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8783. return -6409;
  8784. #endif
  8785. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  8786. (void)wc_AesInit((Aes*)gmac, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  8787. XMEMSET(tag, 0, sizeof(tag));
  8788. wc_GmacSetKey(gmac, k1, sizeof(k1));
  8789. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  8790. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  8791. ERROR_OUT(-6400, out);
  8792. #if (!defined(HAVE_FIPS) || \
  8793. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  8794. XMEMSET(tag, 0, sizeof(tag));
  8795. wc_GmacSetKey(gmac, k2, sizeof(k2));
  8796. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  8797. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  8798. ERROR_OUT(-6401, out);
  8799. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  8800. {
  8801. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  8802. {
  8803. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  8804. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8805. };
  8806. WC_RNG rng;
  8807. byte iv[12];
  8808. #ifndef HAVE_FIPS
  8809. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  8810. ERROR_OUT(-6402, out);
  8811. #else
  8812. if (wc_InitRng(&rng) != 0)
  8813. ERROR_OUT(-6403, out);
  8814. #endif
  8815. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  8816. t1, sizeof(t1)) != 0)
  8817. ERROR_OUT(-6404, out);
  8818. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  8819. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  8820. ERROR_OUT(-6405, out);
  8821. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  8822. t2, sizeof(t2)) != 0)
  8823. ERROR_OUT(-6406, out);
  8824. XMEMSET(tag, 0, sizeof(tag));
  8825. XMEMSET(iv, 0, sizeof(iv));
  8826. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  8827. tag, sizeof(tag), &rng) != 0)
  8828. ERROR_OUT(-6407, out);
  8829. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  8830. tag, sizeof(tag)) != 0)
  8831. ERROR_OUT(-6408, out);
  8832. wc_FreeRng(&rng);
  8833. }
  8834. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  8835. #endif /* HAVE_FIPS */
  8836. ret = 0;
  8837. out:
  8838. #ifdef WOLFSSL_SMALL_STACK
  8839. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  8840. #endif
  8841. return ret;
  8842. }
  8843. #endif /* WOLFSSL_AES_128 */
  8844. #endif /* HAVE_AESGCM */
  8845. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  8846. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  8847. {
  8848. int ret;
  8849. #ifdef WOLFSSL_SMALL_STACK
  8850. Aes *enc;
  8851. #else
  8852. Aes enc[1];
  8853. #endif
  8854. /* key */
  8855. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  8856. {
  8857. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  8858. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  8859. };
  8860. /* nonce */
  8861. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  8862. {
  8863. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  8864. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  8865. };
  8866. /* plaintext */
  8867. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8868. {
  8869. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  8870. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8871. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  8872. };
  8873. /* plaintext - long */
  8874. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  8875. {
  8876. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  8877. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8878. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  8879. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  8880. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  8881. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8882. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  8883. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  8884. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  8885. 0x50
  8886. };
  8887. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8888. {
  8889. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  8890. };
  8891. /* ciphertext */
  8892. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  8893. {
  8894. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  8895. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  8896. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  8897. };
  8898. /* tag - authentication */
  8899. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  8900. {
  8901. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  8902. };
  8903. /* ciphertext - long */
  8904. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  8905. {
  8906. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  8907. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  8908. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  8909. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  8910. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  8911. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  8912. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  8913. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  8914. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  8915. 0x0b
  8916. };
  8917. /* tag - authentication - long */
  8918. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  8919. {
  8920. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  8921. };
  8922. byte t2[sizeof(t)];
  8923. byte p2[sizeof(p)];
  8924. byte c2[sizeof(c)];
  8925. byte iv2[sizeof(iv)];
  8926. byte pl2[sizeof(pl)];
  8927. byte cl2[sizeof(cl)];
  8928. byte tl2[sizeof(tl)];
  8929. int result;
  8930. #ifdef WOLFSSL_SMALL_STACK
  8931. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8932. return -6521;
  8933. #endif
  8934. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  8935. XMEMSET(t2, 0, sizeof(t2));
  8936. XMEMSET(c2, 0, sizeof(c2));
  8937. XMEMSET(p2, 0, sizeof(p2));
  8938. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  8939. if (result != 0)
  8940. ERROR_OUT(-6500, out);
  8941. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8942. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  8943. t2, sizeof(t2), a, sizeof(a));
  8944. if (result != 0)
  8945. ERROR_OUT(-6501, out);
  8946. if (XMEMCMP(c, c2, sizeof(c2)))
  8947. ERROR_OUT(-6502, out);
  8948. if (XMEMCMP(t, t2, sizeof(t2)))
  8949. ERROR_OUT(-6503, out);
  8950. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  8951. t2, sizeof(t2), a, sizeof(a));
  8952. if (result != 0)
  8953. ERROR_OUT(-6504, out);
  8954. if (XMEMCMP(p, p2, sizeof(p2)))
  8955. ERROR_OUT(-6505, out);
  8956. /* Test the authentication failure */
  8957. t2[0]++; /* Corrupt the authentication tag. */
  8958. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  8959. t2, sizeof(t2), a, sizeof(a));
  8960. if (result == 0)
  8961. ERROR_OUT(-6506, out);
  8962. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  8963. * authentication fail. */
  8964. XMEMSET(c2, 0, sizeof(c2));
  8965. if (XMEMCMP(p2, c2, sizeof(p2)))
  8966. ERROR_OUT(-6507, out);
  8967. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  8968. XMEMSET(t2, 0, sizeof(t2));
  8969. XMEMSET(c2, 0, sizeof(c2));
  8970. XMEMSET(p2, 0, sizeof(p2));
  8971. XMEMSET(iv2, 0, sizeof(iv2));
  8972. #ifndef HAVE_SELFTEST
  8973. /* selftest build does not have wc_AesCcmSetNonce() or
  8974. * wc_AesCcmEncrypt_ex() */
  8975. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  8976. ERROR_OUT(-6508, out);
  8977. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  8978. ERROR_OUT(-6509, out);
  8979. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  8980. t2, sizeof(t2), a, sizeof(a)) != 0)
  8981. ERROR_OUT(-6510, out);
  8982. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  8983. ERROR_OUT(-6511, out);
  8984. if (XMEMCMP(c, c2, sizeof(c2)))
  8985. ERROR_OUT(-6512, out);
  8986. if (XMEMCMP(t, t2, sizeof(t2)))
  8987. ERROR_OUT(-6513, out);
  8988. #endif
  8989. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8990. /* test fail on invalid IV sizes */
  8991. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  8992. if (result != 0)
  8993. ERROR_OUT(-6514, out);
  8994. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8995. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  8996. t2, 1, a, sizeof(a));
  8997. if (result == 0) {
  8998. ERROR_OUT(-6515, out);
  8999. }
  9000. #endif
  9001. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  9002. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  9003. tl2, sizeof(tl2), a, sizeof(a));
  9004. if (result != 0)
  9005. ERROR_OUT(-6516, out);
  9006. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  9007. ERROR_OUT(-6517, out);
  9008. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  9009. ERROR_OUT(-6518, out);
  9010. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  9011. tl2, sizeof(tl2), a, sizeof(a));
  9012. if (result != 0)
  9013. ERROR_OUT(-6519, out);
  9014. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  9015. ERROR_OUT(-6520, out);
  9016. ret = 0;
  9017. out:
  9018. #ifdef WOLFSSL_SMALL_STACK
  9019. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9020. #endif
  9021. return ret;
  9022. }
  9023. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  9024. #ifdef HAVE_AES_KEYWRAP
  9025. #define MAX_KEYWRAP_TEST_OUTLEN 40
  9026. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  9027. typedef struct keywrapVector {
  9028. const byte* kek;
  9029. const byte* data;
  9030. const byte* verify;
  9031. word32 kekLen;
  9032. word32 dataLen;
  9033. word32 verifyLen;
  9034. } keywrapVector;
  9035. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  9036. {
  9037. int wrapSz, plainSz, testSz, i;
  9038. /* test vectors from RFC 3394 (kek, data, verify) */
  9039. #ifdef WOLFSSL_AES_128
  9040. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  9041. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  9042. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9043. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9044. };
  9045. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  9046. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9047. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9048. };
  9049. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  9050. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  9051. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  9052. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  9053. };
  9054. #endif /* WOLFSSL_AES_128 */
  9055. #ifdef WOLFSSL_AES_192
  9056. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  9057. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  9058. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9059. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9060. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9061. };
  9062. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  9063. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9064. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9065. };
  9066. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  9067. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  9068. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  9069. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  9070. };
  9071. #endif
  9072. #ifdef WOLFSSL_AES_256
  9073. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  9074. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  9075. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9076. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9077. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9078. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9079. };
  9080. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  9081. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9082. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9083. };
  9084. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  9085. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  9086. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  9087. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  9088. };
  9089. #endif
  9090. #ifdef WOLFSSL_AES_192
  9091. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  9092. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  9093. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9094. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9095. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9096. };
  9097. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  9098. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9099. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9101. };
  9102. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  9103. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  9104. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  9105. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  9106. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  9107. };
  9108. #endif
  9109. #ifdef WOLFSSL_AES_256
  9110. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  9111. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  9112. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9113. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9114. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9115. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9116. };
  9117. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  9118. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9119. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9120. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9121. };
  9122. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  9123. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  9124. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  9125. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  9126. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  9127. };
  9128. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  9129. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  9130. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9131. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9132. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9133. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9134. };
  9135. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  9136. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9137. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9138. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9139. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9140. };
  9141. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  9142. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  9143. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  9144. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  9145. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  9146. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  9147. };
  9148. #endif /* WOLFSSL_AES_256 */
  9149. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  9150. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  9151. const keywrapVector test_wrap[] =
  9152. {
  9153. #ifdef WOLFSSL_AES_128
  9154. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  9155. #endif
  9156. #ifdef WOLFSSL_AES_192
  9157. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  9158. #endif
  9159. #ifdef WOLFSSL_AES_256
  9160. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  9161. #endif
  9162. #ifdef WOLFSSL_AES_192
  9163. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  9164. #endif
  9165. #ifdef WOLFSSL_AES_256
  9166. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  9167. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  9168. #endif
  9169. };
  9170. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  9171. XMEMSET(output, 0, sizeof(output));
  9172. XMEMSET(plain, 0, sizeof(plain));
  9173. for (i = 0; i < testSz; i++) {
  9174. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  9175. test_wrap[i].data, test_wrap[i].dataLen,
  9176. output, sizeof(output), NULL);
  9177. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  9178. return -6600;
  9179. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  9180. return -6601;
  9181. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  9182. output, wrapSz,
  9183. plain, sizeof(plain), NULL);
  9184. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  9185. return -6602;
  9186. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  9187. return -6603 - i;
  9188. }
  9189. return 0;
  9190. }
  9191. #endif /* HAVE_AES_KEYWRAP */
  9192. #endif /* NO_AES */
  9193. #ifdef HAVE_CAMELLIA
  9194. enum {
  9195. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  9196. };
  9197. typedef struct {
  9198. int type;
  9199. const byte* plaintext;
  9200. const byte* iv;
  9201. const byte* ciphertext;
  9202. const byte* key;
  9203. word32 keySz;
  9204. int errorCode;
  9205. } test_vector_t;
  9206. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  9207. {
  9208. /* Camellia ECB Test Plaintext */
  9209. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  9210. {
  9211. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9212. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9213. };
  9214. /* Camellia ECB Test Initialization Vector */
  9215. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  9216. /* Test 1: Camellia ECB 128-bit key */
  9217. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9218. {
  9219. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9220. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9221. };
  9222. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9223. {
  9224. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  9225. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  9226. };
  9227. /* Test 2: Camellia ECB 192-bit key */
  9228. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9229. {
  9230. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9231. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9232. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  9233. };
  9234. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9235. {
  9236. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  9237. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  9238. };
  9239. /* Test 3: Camellia ECB 256-bit key */
  9240. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9241. {
  9242. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9243. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9244. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9245. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  9246. };
  9247. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9248. {
  9249. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  9250. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  9251. };
  9252. /* Camellia CBC Test Plaintext */
  9253. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  9254. {
  9255. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  9256. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  9257. };
  9258. /* Camellia CBC Test Initialization Vector */
  9259. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  9260. {
  9261. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9262. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9263. };
  9264. /* Test 4: Camellia-CBC 128-bit key */
  9265. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  9266. {
  9267. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  9268. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  9269. };
  9270. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  9271. {
  9272. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  9273. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  9274. };
  9275. /* Test 5: Camellia-CBC 192-bit key */
  9276. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  9277. {
  9278. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  9279. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  9280. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  9281. };
  9282. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  9283. {
  9284. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  9285. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  9286. };
  9287. /* Test 6: CBC 256-bit key */
  9288. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  9289. {
  9290. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  9291. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  9292. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  9293. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  9294. };
  9295. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  9296. {
  9297. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  9298. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  9299. };
  9300. byte out[CAMELLIA_BLOCK_SIZE];
  9301. Camellia cam;
  9302. int i, testsSz, ret;
  9303. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  9304. {
  9305. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  9306. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  9307. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  9308. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  9309. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  9310. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  9311. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  9312. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  9313. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  9314. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  9315. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  9316. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  9317. };
  9318. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  9319. for (i = 0; i < testsSz; i++) {
  9320. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  9321. testVectors[i].iv) != 0)
  9322. return testVectors[i].errorCode;
  9323. switch (testVectors[i].type) {
  9324. case CAM_ECB_ENC:
  9325. ret = wc_CamelliaEncryptDirect(&cam, out,
  9326. testVectors[i].plaintext);
  9327. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9328. CAMELLIA_BLOCK_SIZE))
  9329. return testVectors[i].errorCode;
  9330. break;
  9331. case CAM_ECB_DEC:
  9332. ret = wc_CamelliaDecryptDirect(&cam, out,
  9333. testVectors[i].ciphertext);
  9334. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9335. CAMELLIA_BLOCK_SIZE))
  9336. return testVectors[i].errorCode;
  9337. break;
  9338. case CAM_CBC_ENC:
  9339. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  9340. CAMELLIA_BLOCK_SIZE);
  9341. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9342. CAMELLIA_BLOCK_SIZE))
  9343. return testVectors[i].errorCode;
  9344. break;
  9345. case CAM_CBC_DEC:
  9346. ret = wc_CamelliaCbcDecrypt(&cam, out,
  9347. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  9348. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9349. CAMELLIA_BLOCK_SIZE))
  9350. return testVectors[i].errorCode;
  9351. break;
  9352. default:
  9353. break;
  9354. }
  9355. }
  9356. /* Setting the IV and checking it was actually set. */
  9357. ret = wc_CamelliaSetIV(&cam, ivc);
  9358. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  9359. return -6700;
  9360. /* Setting the IV to NULL should be same as all zeros IV */
  9361. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  9362. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  9363. return -6701;
  9364. /* First parameter should never be null */
  9365. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  9366. return -6702;
  9367. /* First parameter should never be null, check it fails */
  9368. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  9369. return -6703;
  9370. /* Key should have a size of 16, 24, or 32 */
  9371. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  9372. return -6704;
  9373. return 0;
  9374. }
  9375. #endif /* HAVE_CAMELLIA */
  9376. #ifdef HAVE_IDEA
  9377. WOLFSSL_TEST_SUBROUTINE int idea_test(void)
  9378. {
  9379. int ret;
  9380. word16 i, j;
  9381. Idea idea;
  9382. byte data[IDEA_BLOCK_SIZE];
  9383. /* Project NESSIE test vectors */
  9384. #define IDEA_NB_TESTS 6
  9385. #define IDEA_NB_TESTS_EXTRA 4
  9386. WOLFSSL_SMALL_STACK_STATIC const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  9387. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  9388. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9389. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  9390. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9391. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9392. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9393. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9394. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9395. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9396. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9397. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9398. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9399. };
  9400. WOLFSSL_SMALL_STACK_STATIC const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9401. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9402. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9403. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9404. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9405. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  9406. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  9407. };
  9408. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9409. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  9410. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  9411. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  9412. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  9413. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9414. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9415. };
  9416. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9417. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  9418. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  9419. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  9420. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  9421. };
  9422. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9423. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  9424. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  9425. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  9426. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  9427. };
  9428. /* CBC test */
  9429. const char *message = "International Data Encryption Algorithm";
  9430. byte msg_enc[40], msg_dec[40];
  9431. for (i = 0; i < IDEA_NB_TESTS; i++) {
  9432. /* Set encryption key */
  9433. XMEMSET(&idea, 0, sizeof(Idea));
  9434. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9435. NULL, IDEA_ENCRYPTION);
  9436. if (ret != 0) {
  9437. printf("wc_IdeaSetKey (enc) failed\n");
  9438. return -6800;
  9439. }
  9440. /* Data encryption */
  9441. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  9442. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  9443. printf("Bad encryption\n");
  9444. return -6801;
  9445. }
  9446. /* Set decryption key */
  9447. XMEMSET(&idea, 0, sizeof(Idea));
  9448. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9449. NULL, IDEA_DECRYPTION);
  9450. if (ret != 0) {
  9451. printf("wc_IdeaSetKey (dec) failed\n");
  9452. return -6802;
  9453. }
  9454. /* Data decryption */
  9455. ret = wc_IdeaCipher(&idea, data, data);
  9456. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  9457. printf("Bad decryption\n");
  9458. return -6803;
  9459. }
  9460. /* Set encryption key */
  9461. XMEMSET(&idea, 0, sizeof(Idea));
  9462. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9463. v_key[i], IDEA_ENCRYPTION);
  9464. if (ret != 0) {
  9465. printf("wc_IdeaSetKey (enc) failed\n");
  9466. return -6804;
  9467. }
  9468. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  9469. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  9470. (word32)XSTRLEN(message)+1);
  9471. if (ret != 0) {
  9472. printf("wc_IdeaCbcEncrypt failed\n");
  9473. return -6805;
  9474. }
  9475. /* Set decryption key */
  9476. XMEMSET(&idea, 0, sizeof(Idea));
  9477. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9478. v_key[i], IDEA_DECRYPTION);
  9479. if (ret != 0) {
  9480. printf("wc_IdeaSetKey (dec) failed\n");
  9481. return -6806;
  9482. }
  9483. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  9484. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  9485. (word32)XSTRLEN(message)+1);
  9486. if (ret != 0) {
  9487. printf("wc_IdeaCbcDecrypt failed\n");
  9488. return -6807;
  9489. }
  9490. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  9491. printf("Bad CBC decryption\n");
  9492. return -6808;
  9493. }
  9494. }
  9495. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  9496. /* Set encryption key */
  9497. XMEMSET(&idea, 0, sizeof(Idea));
  9498. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9499. NULL, IDEA_ENCRYPTION);
  9500. if (ret != 0) {
  9501. printf("wc_IdeaSetKey (enc) failed\n");
  9502. return -6809;
  9503. }
  9504. /* 100 times data encryption */
  9505. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9506. for (j = 0; j < 100; j++) {
  9507. ret = wc_IdeaCipher(&idea, data, data);
  9508. if (ret != 0) {
  9509. return -6810;
  9510. }
  9511. }
  9512. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  9513. printf("Bad encryption (100 times)\n");
  9514. return -6811;
  9515. }
  9516. /* 1000 times data encryption */
  9517. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9518. for (j = 0; j < 1000; j++) {
  9519. ret = wc_IdeaCipher(&idea, data, data);
  9520. if (ret != 0) {
  9521. return -6812;
  9522. }
  9523. }
  9524. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  9525. printf("Bad encryption (100 times)\n");
  9526. return -6813;
  9527. }
  9528. }
  9529. #ifndef WC_NO_RNG
  9530. /* random test for CBC */
  9531. {
  9532. WC_RNG rng;
  9533. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  9534. rnd[1000], enc[1000], dec[1000];
  9535. /* random values */
  9536. #ifndef HAVE_FIPS
  9537. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9538. #else
  9539. ret = wc_InitRng(&rng);
  9540. #endif
  9541. if (ret != 0)
  9542. return -6814;
  9543. for (i = 0; i < 1000; i++) {
  9544. /* random key */
  9545. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  9546. if (ret != 0)
  9547. return -6815;
  9548. /* random iv */
  9549. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  9550. if (ret != 0)
  9551. return -6816;
  9552. /* random data */
  9553. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  9554. if (ret != 0)
  9555. return -6817;
  9556. /* Set encryption key */
  9557. XMEMSET(&idea, 0, sizeof(Idea));
  9558. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  9559. if (ret != 0) {
  9560. printf("wc_IdeaSetKey (enc) failed\n");
  9561. return -6818;
  9562. }
  9563. /* Data encryption */
  9564. XMEMSET(enc, 0, sizeof(enc));
  9565. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  9566. if (ret != 0) {
  9567. printf("wc_IdeaCbcEncrypt failed\n");
  9568. return -6819;
  9569. }
  9570. /* Set decryption key */
  9571. XMEMSET(&idea, 0, sizeof(Idea));
  9572. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  9573. if (ret != 0) {
  9574. printf("wc_IdeaSetKey (enc) failed\n");
  9575. return -6820;
  9576. }
  9577. /* Data decryption */
  9578. XMEMSET(dec, 0, sizeof(dec));
  9579. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  9580. if (ret != 0) {
  9581. printf("wc_IdeaCbcDecrypt failed\n");
  9582. return -6821;
  9583. }
  9584. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  9585. printf("Bad CBC decryption\n");
  9586. return -6822;
  9587. }
  9588. }
  9589. wc_FreeRng(&rng);
  9590. }
  9591. #endif /* WC_NO_RNG */
  9592. return 0;
  9593. }
  9594. #endif /* HAVE_IDEA */
  9595. #ifdef HAVE_XCHACHA
  9596. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  9597. int ret = -6830;
  9598. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9599. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  9600. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  9601. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  9602. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  9603. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  9604. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  9605. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  9606. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  9607. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  9608. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  9609. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  9610. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  9611. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  9612. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  9613. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  9614. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  9615. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  9616. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  9617. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  9618. };
  9619. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9620. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9621. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9622. };
  9623. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9624. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9625. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  9626. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9627. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  9628. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  9629. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  9630. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  9631. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  9632. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  9633. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  9634. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  9635. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  9636. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  9637. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  9638. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  9639. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  9640. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  9641. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  9642. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  9643. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  9644. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  9645. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  9646. };
  9647. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9648. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9649. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9650. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9651. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  9652. ERROR_OUT(MEMORY_E, out);
  9653. #else
  9654. struct ChaCha chacha[1];
  9655. byte buf1[sizeof Plaintext];
  9656. byte buf2[sizeof Plaintext];
  9657. #endif
  9658. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9659. if (ret < 0)
  9660. ERROR_OUT(-6831, out);
  9661. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  9662. if (ret < 0)
  9663. ERROR_OUT(-6832, out);
  9664. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9665. ERROR_OUT(-6833, out);
  9666. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9667. if (ret < 0)
  9668. ERROR_OUT(-6834, out);
  9669. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  9670. if (ret < 0)
  9671. ERROR_OUT(-6835, out);
  9672. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9673. ERROR_OUT(-6836, out);
  9674. out:
  9675. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9676. if (chacha)
  9677. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9678. if (buf1)
  9679. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9680. if (buf2)
  9681. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9682. #endif
  9683. return ret;
  9684. }
  9685. #endif /* HAVE_XCHACHA */
  9686. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  9687. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  9688. int ret;
  9689. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9690. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  9691. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  9692. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  9693. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  9694. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  9695. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  9696. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  9697. 0x74, 0x2e }; /* t. */
  9698. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  9699. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9700. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9701. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9702. };
  9703. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9704. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9705. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  9706. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9707. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  9708. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  9709. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  9710. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  9711. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  9712. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  9713. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  9714. 0xb5, 0x2e };
  9715. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  9716. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  9717. };
  9718. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9719. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9720. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9721. #else
  9722. byte buf1[sizeof Ciphertext + sizeof Tag];
  9723. byte buf2[sizeof Plaintext];
  9724. #endif
  9725. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  9726. Plaintext, sizeof Plaintext,
  9727. AAD, sizeof AAD,
  9728. IV, sizeof IV,
  9729. Key, sizeof Key);
  9730. if (ret < 0)
  9731. ERROR_OUT(-6840, out);
  9732. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9733. ERROR_OUT(-6841, out);
  9734. if (XMEMCMP(buf1 + sizeof Plaintext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  9735. ERROR_OUT(-6842, out);
  9736. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  9737. buf1, sizeof Plaintext + sizeof Tag,
  9738. AAD, sizeof AAD,
  9739. IV, sizeof IV,
  9740. Key, sizeof Key);
  9741. if (ret < 0)
  9742. ERROR_OUT(-6843, out);
  9743. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9744. ERROR_OUT(-6844, out);
  9745. out:
  9746. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9747. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9748. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9749. #endif
  9750. return ret;
  9751. }
  9752. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  9753. #ifndef WC_NO_RNG
  9754. static int _rng_test(WC_RNG* rng, int errorOffset)
  9755. {
  9756. byte block[32];
  9757. int ret, i;
  9758. XMEMSET(block, 0, sizeof(block));
  9759. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  9760. if (ret != 0) {
  9761. ret = -6850;
  9762. goto exit;
  9763. }
  9764. /* Check for 0's */
  9765. for (i=0; i<(int)sizeof(block); i++) {
  9766. if (block[i] == 0) {
  9767. ret++;
  9768. }
  9769. }
  9770. /* All zeros count check */
  9771. if (ret >= (int)sizeof(block)) {
  9772. ret = -6851;
  9773. goto exit;
  9774. }
  9775. ret = wc_RNG_GenerateByte(rng, block);
  9776. if (ret != 0) {
  9777. ret = -6852;
  9778. goto exit;
  9779. }
  9780. /* Parameter validation testing. */
  9781. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  9782. if (ret != BAD_FUNC_ARG) {
  9783. ret = -6853;
  9784. goto exit;
  9785. }
  9786. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  9787. if (ret != BAD_FUNC_ARG) {
  9788. ret = -6854;
  9789. goto exit;
  9790. }
  9791. ret = wc_RNG_GenerateByte(NULL, block);
  9792. if (ret != BAD_FUNC_ARG) {
  9793. ret = -6855;
  9794. goto exit;
  9795. }
  9796. ret = wc_RNG_GenerateByte(rng, NULL);
  9797. if (ret != BAD_FUNC_ARG) {
  9798. ret = -6856;
  9799. goto exit;
  9800. }
  9801. ret = 0;
  9802. exit:
  9803. if (ret != 0)
  9804. ret += errorOffset;
  9805. return ret;
  9806. }
  9807. static int random_rng_test(void)
  9808. {
  9809. WC_RNG localRng;
  9810. WC_RNG* rng;
  9811. int ret;
  9812. rng = &localRng;
  9813. /* Test stack based RNG. */
  9814. #ifndef HAVE_FIPS
  9815. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  9816. #else
  9817. ret = wc_InitRng(rng);
  9818. #endif
  9819. if (ret != 0) return -6900;
  9820. ret = _rng_test(rng, -6300);
  9821. /* Make sure and free RNG */
  9822. wc_FreeRng(rng);
  9823. if (ret != 0) return ret;
  9824. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  9825. {
  9826. byte nonce[8] = { 0 };
  9827. /* Test dynamic RNG. */
  9828. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  9829. if (rng == NULL) return -6901;
  9830. ret = _rng_test(rng, -6310);
  9831. wc_rng_free(rng);
  9832. }
  9833. #endif
  9834. return ret;
  9835. }
  9836. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  9837. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  9838. {
  9839. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  9840. {
  9841. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  9842. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  9843. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  9844. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  9845. };
  9846. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  9847. {
  9848. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  9849. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  9850. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  9851. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  9852. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  9853. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  9854. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  9855. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  9856. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  9857. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  9858. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  9859. };
  9860. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  9861. {
  9862. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  9863. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  9864. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  9865. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  9866. };
  9867. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  9868. {
  9869. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  9870. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  9871. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  9872. };
  9873. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  9874. {
  9875. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  9876. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  9877. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  9878. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  9879. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  9880. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  9881. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  9882. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  9883. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  9884. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  9885. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  9886. };
  9887. byte output[WC_SHA256_DIGEST_SIZE * 4];
  9888. int ret;
  9889. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  9890. output, sizeof(output));
  9891. if (ret != 0)
  9892. return -7000;
  9893. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  9894. return -7001;
  9895. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  9896. test2EntropyB, sizeof(test2EntropyB),
  9897. output, sizeof(output));
  9898. if (ret != 0)
  9899. return -7002;
  9900. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  9901. return -7003;
  9902. /* Basic RNG generate block test */
  9903. if ((ret = random_rng_test()) != 0)
  9904. return ret;
  9905. /* Test the seed check function. */
  9906. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  9907. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9908. {
  9909. word32 i, outputSz;
  9910. /* Repeat the same byte over and over. Should fail. */
  9911. outputSz = sizeof(output);
  9912. XMEMSET(output, 1, outputSz);
  9913. ret = wc_RNG_TestSeed(output, outputSz);
  9914. if (ret == 0)
  9915. return -7004;
  9916. /* Every byte of the entropy scratch is different,
  9917. * entropy is a single byte that shouldn't match. */
  9918. outputSz = (sizeof(word32) * 2) + 1;
  9919. for (i = 0; i < outputSz; i++)
  9920. output[i] = (byte)i;
  9921. ret = wc_RNG_TestSeed(output, outputSz);
  9922. if (ret != 0)
  9923. return -7005;
  9924. outputSz = sizeof(output);
  9925. for (i = 0; i < outputSz; i++)
  9926. output[i] = (byte)i;
  9927. ret = wc_RNG_TestSeed(output, outputSz);
  9928. if (ret != 0)
  9929. return -7006;
  9930. }
  9931. #endif
  9932. return 0;
  9933. }
  9934. #else
  9935. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  9936. {
  9937. /* Basic RNG generate block test */
  9938. return random_rng_test();
  9939. }
  9940. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  9941. #endif /* WC_NO_RNG */
  9942. #ifndef MEM_TEST_SZ
  9943. #define MEM_TEST_SZ 1024
  9944. #endif
  9945. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  9946. static int simple_mem_test(int sz)
  9947. {
  9948. int ret = 0;
  9949. byte* b;
  9950. int i;
  9951. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9952. if (b == NULL) {
  9953. return -7110;
  9954. }
  9955. /* utilize memory */
  9956. for (i = 0; i < sz; i++) {
  9957. b[i] = (byte)i;
  9958. }
  9959. /* read back and verify */
  9960. for (i = 0; i < sz; i++) {
  9961. if (b[i] != (byte)i) {
  9962. ret = -7111;
  9963. break;
  9964. }
  9965. }
  9966. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9967. return ret;
  9968. }
  9969. #endif
  9970. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  9971. {
  9972. int ret = 0;
  9973. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  9974. byte* b = NULL;
  9975. #endif
  9976. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  9977. int i;
  9978. #endif
  9979. #ifdef WOLFSSL_STATIC_MEMORY
  9980. word32 size[] = { WOLFMEM_BUCKETS };
  9981. word32 dist[] = { WOLFMEM_DIST };
  9982. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  9983. int pad = -(int)((wolfssl_word)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  9984. /* pad to account for if head of buffer is not at set memory
  9985. * alignment when tests are ran */
  9986. #endif
  9987. #ifdef WOLFSSL_STATIC_MEMORY
  9988. /* check macro settings */
  9989. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  9990. return -7200;
  9991. }
  9992. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  9993. return -7201;
  9994. }
  9995. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  9996. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  9997. /* each element in array should be divisible by alignment size */
  9998. return -7202;
  9999. }
  10000. }
  10001. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  10002. if (size[i - 1] >= size[i]) {
  10003. return -7203; /* sizes should be in increasing order */
  10004. }
  10005. }
  10006. /* check that padding size returned is possible */
  10007. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  10008. return -7204; /* no room for wc_Memory struct */
  10009. }
  10010. if (wolfSSL_MemoryPaddingSz() < 0) {
  10011. return -7205;
  10012. }
  10013. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  10014. return -7206; /* not aligned! */
  10015. }
  10016. /* check function to return optimum buffer size (rounded down) */
  10017. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  10018. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  10019. return -7207; /* not aligned! */
  10020. }
  10021. if (ret < 0) {
  10022. return -7208;
  10023. }
  10024. if ((unsigned int)ret > sizeof(buffer)) {
  10025. return -7209; /* did not round down as expected */
  10026. }
  10027. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  10028. return -7210; /* return value changed when using suggested value */
  10029. }
  10030. ret = wolfSSL_MemoryPaddingSz();
  10031. ret += pad; /* add space that is going to be needed if buffer not aligned */
  10032. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  10033. (ret + (int)size[0])) {
  10034. return -7211; /* did not round down to nearest bucket value */
  10035. }
  10036. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  10037. if ((ret - pad) < 0) {
  10038. return -7212;
  10039. }
  10040. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  10041. return -7213; /* not even chunks of memory for IO size */
  10042. }
  10043. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  10044. return -7214; /* memory not aligned */
  10045. }
  10046. /* check for passing bad or unknown arguments to functions */
  10047. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  10048. return -7215;
  10049. }
  10050. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  10051. return -7216; /* should round to 0 since struct + bucket will not fit */
  10052. }
  10053. (void)dist; /* avoid static analysis warning of variable not used */
  10054. #endif
  10055. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  10056. /* simple test */
  10057. ret = simple_mem_test(MEM_TEST_SZ);
  10058. if (ret != 0)
  10059. return ret;
  10060. #endif
  10061. #ifdef COMPLEX_MEM_TEST
  10062. /* test various size blocks */
  10063. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  10064. ret = simple_mem_test(i);
  10065. if (ret != 0)
  10066. return ret;
  10067. }
  10068. #endif
  10069. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  10070. /* realloc test */
  10071. b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10072. if (b) {
  10073. b = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  10074. DYNAMIC_TYPE_TMP_BUFFER);
  10075. }
  10076. if (b == NULL) {
  10077. return -7217;
  10078. }
  10079. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10080. #endif
  10081. return ret;
  10082. }
  10083. #ifdef HAVE_NTRU
  10084. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  10085. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  10086. {
  10087. static WC_RNG rng;
  10088. if (cmd == INIT)
  10089. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  10090. if (out == NULL)
  10091. return 0;
  10092. if (cmd == GET_BYTE_OF_ENTROPY)
  10093. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  10094. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  10095. *out = 1;
  10096. return 1;
  10097. }
  10098. return 0;
  10099. }
  10100. #endif /* HAVE_NTRU */
  10101. #ifndef NO_FILESYSTEM
  10102. /* Cert Paths */
  10103. #ifdef FREESCALE_MQX
  10104. #define CERT_PREFIX "a:\\"
  10105. #define CERT_PATH_SEP "\\"
  10106. #elif defined(WOLFSSL_uTKERNEL2)
  10107. #define CERT_PREFIX "/uda/"
  10108. #define CERT_PATH_SEP "/"
  10109. #endif
  10110. #ifndef CERT_PREFIX
  10111. #define CERT_PREFIX "./"
  10112. #endif
  10113. #ifndef CERT_PATH_SEP
  10114. #define CERT_PATH_SEP "/"
  10115. #endif
  10116. #ifndef CERT_WRITE_TEMP_DIR
  10117. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  10118. #endif
  10119. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  10120. /* Generated Test Certs */
  10121. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10122. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  10123. #if !defined(NO_RSA) && !defined(NO_ASN)
  10124. static const char* clientKey = CERT_ROOT "client-key.der";
  10125. static const char* clientCert = CERT_ROOT "client-cert.der";
  10126. #ifdef WOLFSSL_CERT_EXT
  10127. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  10128. #endif
  10129. #endif /* !NO_RSA && !NO_ASN */
  10130. #endif
  10131. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  10132. #if !defined(NO_RSA) && !defined(NO_ASN)
  10133. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  10134. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  10135. #ifdef WOLFSSL_CERT_GEN
  10136. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  10137. #endif
  10138. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  10139. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  10140. #endif
  10141. #ifdef HAVE_PKCS7
  10142. static const char* rsaServerCertDerFile =
  10143. CERT_ROOT "server-cert.der";
  10144. static const char* rsaServerKeyDerFile =
  10145. CERT_ROOT "server-key.der";
  10146. #endif
  10147. #endif
  10148. #endif /* !NO_RSA && !NO_ASN */
  10149. #endif /* !USE_CERT_BUFFER_* */
  10150. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10151. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  10152. !defined(NO_ASN)
  10153. #ifndef NO_DH
  10154. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  10155. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  10156. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  10157. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  10158. #endif
  10159. #endif
  10160. #ifndef NO_DSA
  10161. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  10162. #endif
  10163. #endif /* !USE_CERT_BUFFER_* */
  10164. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  10165. #ifdef HAVE_ECC
  10166. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  10167. #ifdef HAVE_ECC_KEY_IMPORT
  10168. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  10169. #endif
  10170. #endif
  10171. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  10172. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  10173. #ifndef NO_RSA
  10174. /* eccKeyPubFile is used in a test that requires RSA. */
  10175. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  10176. #endif
  10177. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  10178. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  10179. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10180. static const char* eccCaKey384File =
  10181. CERT_ROOT "ca-ecc384-key.der";
  10182. static const char* eccCaCert384File =
  10183. CERT_ROOT "ca-ecc384-cert.pem";
  10184. #endif
  10185. #endif
  10186. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  10187. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  10188. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  10189. #endif
  10190. #endif /* HAVE_ECC */
  10191. #ifdef HAVE_ED25519
  10192. #ifdef WOLFSSL_TEST_CERT
  10193. static const char* serverEd25519Cert =
  10194. CERT_ROOT "ed25519/server-ed25519.der";
  10195. static const char* caEd25519Cert =
  10196. CERT_ROOT "ed25519/ca-ed25519.der";
  10197. #endif
  10198. #endif
  10199. #ifdef HAVE_ED448
  10200. #ifdef WOLFSSL_TEST_CERT
  10201. static const char* serverEd448Cert =
  10202. CERT_ROOT "ed448/server-ed448.der";
  10203. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  10204. #endif
  10205. #endif
  10206. #endif /* !USE_CERT_BUFFER_* */
  10207. #ifndef NO_WRITE_TEMP_FILES
  10208. #ifdef HAVE_ECC
  10209. #ifdef WOLFSSL_CERT_GEN
  10210. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  10211. #endif
  10212. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  10213. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  10214. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  10215. #endif
  10216. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  10217. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  10218. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  10219. #ifdef HAVE_PKCS8
  10220. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  10221. #endif
  10222. #if defined(WOLFSSL_CERT_GEN) || \
  10223. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10224. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  10225. #endif
  10226. #endif /* HAVE_ECC */
  10227. #ifndef NO_RSA
  10228. #if defined(WOLFSSL_CERT_GEN) || \
  10229. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10230. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  10231. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  10232. #endif
  10233. #ifdef WOLFSSL_CERT_GEN
  10234. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  10235. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  10236. #endif
  10237. #ifdef WOLFSSL_CERT_REQ
  10238. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  10239. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  10240. #endif
  10241. #endif /* !NO_RSA */
  10242. #if !defined(NO_RSA) || !defined(NO_DSA)
  10243. #ifdef WOLFSSL_KEY_GEN
  10244. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  10245. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  10246. #endif
  10247. #endif
  10248. #endif /* !NO_WRITE_TEMP_FILES */
  10249. #endif /* !NO_FILESYSTEM */
  10250. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  10251. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  10252. #ifdef WOLFSSL_MULTI_ATTRIB
  10253. static CertName certDefaultName;
  10254. static void initDefaultName(void)
  10255. {
  10256. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  10257. certDefaultName.countryEnc = CTC_PRINTABLE;
  10258. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  10259. certDefaultName.stateEnc = CTC_UTF8;
  10260. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  10261. certDefaultName.localityEnc = CTC_UTF8;
  10262. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  10263. certDefaultName.surEnc = CTC_UTF8;
  10264. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  10265. certDefaultName.orgEnc = CTC_UTF8;
  10266. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  10267. certDefaultName.unitEnc = CTC_UTF8;
  10268. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  10269. certDefaultName.commonNameEnc = CTC_UTF8;
  10270. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  10271. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  10272. #ifdef WOLFSSL_CERT_EXT
  10273. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  10274. certDefaultName.busCatEnc = CTC_UTF8;
  10275. #endif
  10276. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  10277. #ifdef WOLFSSL_TEST_CERT
  10278. {
  10279. NameAttrib* n;
  10280. /* test having additional OUs and setting DC */
  10281. n = &certDefaultName.name[0];
  10282. n->id = ASN_ORGUNIT_NAME;
  10283. n->type = CTC_UTF8;
  10284. n->sz = sizeof("Development-2");
  10285. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  10286. #if CTC_MAX_ATTRIB > 3
  10287. n = &certDefaultName.name[1];
  10288. n->id = ASN_DOMAIN_COMPONENT;
  10289. n->type = CTC_UTF8;
  10290. n->sz = sizeof("com");
  10291. XMEMCPY(n->value, "com", sizeof("com"));
  10292. n = &certDefaultName.name[2];
  10293. n->id = ASN_DOMAIN_COMPONENT;
  10294. n->type = CTC_UTF8;
  10295. n->sz = sizeof("wolfssl");
  10296. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  10297. #endif
  10298. }
  10299. #endif /* WOLFSSL_TEST_CERT */
  10300. }
  10301. #else
  10302. static const CertName certDefaultName = {
  10303. "US", CTC_PRINTABLE, /* country */
  10304. "Oregon", CTC_UTF8, /* state */
  10305. "Portland", CTC_UTF8, /* locality */
  10306. "Test", CTC_UTF8, /* sur */
  10307. "wolfSSL", CTC_UTF8, /* org */
  10308. "Development", CTC_UTF8, /* unit */
  10309. "www.wolfssl.com", CTC_UTF8, /* commonName */
  10310. "wolfSSL12345", CTC_PRINTABLE, /* serial number of device */
  10311. #ifdef WOLFSSL_CERT_EXT
  10312. "Private Organization", CTC_UTF8, /* businessCategory */
  10313. "US", CTC_PRINTABLE, /* jurisdiction country */
  10314. "Oregon", CTC_PRINTABLE, /* jurisdiction state */
  10315. #endif
  10316. "info@wolfssl.com" /* email */
  10317. };
  10318. #endif /* WOLFSSL_MULTI_ATTRIB */
  10319. #ifdef WOLFSSL_CERT_EXT
  10320. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  10321. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  10322. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  10323. "digitalSignature,nonRepudiation";
  10324. #endif
  10325. #if (defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)) && !defined(NO_RSA)
  10326. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  10327. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  10328. #endif
  10329. #endif /* WOLFSSL_CERT_EXT */
  10330. #endif /* WOLFSSL_CERT_GEN */
  10331. #ifndef NO_RSA
  10332. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  10333. !defined(NO_FILESYSTEM)
  10334. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  10335. static byte minName[] = { 0x30, 0x00 };
  10336. static byte nameBad[] = {
  10337. 0x30, 0x08,
  10338. 0x31, 0x06,
  10339. 0x30, 0x04,
  10340. 0x06, 0x02,
  10341. 0x55, 0x04,
  10342. };
  10343. static byte minDates[] = {
  10344. 0x30, 0x1e,
  10345. 0x17, 0x0d,
  10346. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  10347. 0x32, 0x33, 0x31, 0x30, 0x5a,
  10348. 0x17, 0x0d,
  10349. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  10350. 0x32, 0x33, 0x31, 0x30, 0x5a
  10351. };
  10352. static byte minPubKey[] = {
  10353. 0x30, 0x1b,
  10354. 0x30, 0x0d,
  10355. 0x06, 0x09,
  10356. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10357. 0x01,
  10358. 0x05, 0x00,
  10359. 0x03, 0x0b,
  10360. 0x00, 0x30, 0x08,
  10361. 0x02, 0x01,
  10362. 0x03,
  10363. 0x02, 0x03,
  10364. 0x01, 0x00, 0x01
  10365. };
  10366. static byte minSigAlg[] = {
  10367. 0x30, 0x0d,
  10368. 0x06, 0x09,
  10369. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10370. 0x0b,
  10371. 0x05, 0x00
  10372. };
  10373. static byte minSig[] = {
  10374. 0x03, 0x01,
  10375. 0x00
  10376. };
  10377. static int add_seq(byte* certData, int offset, byte* data, byte length)
  10378. {
  10379. XMEMMOVE(certData + offset + 2, data, length);
  10380. certData[offset++] = 0x30;
  10381. certData[offset++] = length;
  10382. return offset + length;
  10383. }
  10384. static int add_data(byte* certData, int offset, byte* data, byte length)
  10385. {
  10386. XMEMCPY(certData + offset, data, length);
  10387. return offset + length;
  10388. }
  10389. static int cert_asn1_test(void)
  10390. {
  10391. int ret;
  10392. int len[3];
  10393. DecodedCert cert;
  10394. byte certData[106];
  10395. byte* badCert = NULL;
  10396. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10397. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10398. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10399. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  10400. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10401. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  10402. len[1] = add_seq(certData, 0, certData, len[2]);
  10403. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  10404. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  10405. len[0] = add_seq(certData, 0, certData, len[1]);
  10406. /* Minimal good certificate */
  10407. InitDecodedCert(&cert, certData, len[0], 0);
  10408. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10409. FreeDecodedCert(&cert);
  10410. if (ret != 0) {
  10411. ERROR_OUT(-7300, done);
  10412. }
  10413. /* Bad issuer name */
  10414. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10415. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10416. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  10417. len[1] = add_seq(certData, 0, certData, len[2]);
  10418. len[0] = add_seq(certData, 0, certData, len[1]);
  10419. /* Put data into allocated buffer to allow access error checking. */
  10420. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10421. XMEMCPY(badCert, certData, len[0]);
  10422. InitDecodedCert(&cert, badCert, len[0], 0);
  10423. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10424. FreeDecodedCert(&cert);
  10425. if (ret != ASN_PARSE_E) {
  10426. ERROR_OUT(-7301, done);
  10427. }
  10428. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10429. badCert = NULL;
  10430. ret = 0;
  10431. done:
  10432. if (badCert != NULL)
  10433. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10434. return ret;
  10435. }
  10436. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  10437. {
  10438. #if !defined(NO_FILESYSTEM)
  10439. DecodedCert cert;
  10440. byte* tmp;
  10441. size_t bytes;
  10442. XFILE file;
  10443. int ret;
  10444. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10445. if (tmp == NULL)
  10446. return -7400;
  10447. /* Certificate with Name Constraints extension. */
  10448. #ifdef FREESCALE_MQX
  10449. file = XFOPEN(".\\certs\\test\\cert-ext-nc.der", "rb");
  10450. #else
  10451. file = XFOPEN("./certs/test/cert-ext-nc.der", "rb");
  10452. #endif
  10453. if (!file) {
  10454. ERROR_OUT(-7401, done);
  10455. }
  10456. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10457. XFCLOSE(file);
  10458. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10459. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10460. if (ret != 0) {
  10461. ERROR_OUT(-7402, done);
  10462. }
  10463. FreeDecodedCert(&cert);
  10464. /* Certificate with Inhibit Any Policy extension. */
  10465. #ifdef FREESCALE_MQX
  10466. file = XFOPEN(".\\certs\\test\\cert-ext-ia.der", "rb");
  10467. #else
  10468. file = XFOPEN("./certs/test/cert-ext-ia.der", "rb");
  10469. #endif
  10470. if (!file) {
  10471. ERROR_OUT(-7403, done);
  10472. }
  10473. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10474. XFCLOSE(file);
  10475. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10476. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10477. if (ret != 0) {
  10478. ERROR_OUT(-7404, done);
  10479. }
  10480. FreeDecodedCert(&cert);
  10481. /* Certificate with Netscape Certificate Type extension. */
  10482. #ifdef FREESCALE_MQX
  10483. file = XFOPEN(".\\certs\\test\\cert-ext-nct.der", "rb");
  10484. #else
  10485. file = XFOPEN("./certs/test/cert-ext-nct.der", "rb");
  10486. #endif
  10487. if (!file) {
  10488. ERROR_OUT(-7405, done);
  10489. }
  10490. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10491. XFCLOSE(file);
  10492. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10493. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10494. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  10495. if (ret != 0) {
  10496. ERROR_OUT(-7406, done);
  10497. }
  10498. #else
  10499. if (ret != ASN_CRIT_EXT_E) {
  10500. ERROR_OUT(-7407, done);
  10501. }
  10502. ret = 0;
  10503. #endif
  10504. done:
  10505. FreeDecodedCert(&cert);
  10506. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10507. #endif /* !NO_FILESYSTEM */
  10508. if (ret == 0)
  10509. ret = cert_asn1_test();
  10510. return ret;
  10511. }
  10512. #endif /* WOLFSSL_TEST_CERT */
  10513. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  10514. !defined(NO_FILESYSTEM)
  10515. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  10516. {
  10517. DecodedCert cert;
  10518. byte* tmp;
  10519. size_t bytes;
  10520. XFILE file;
  10521. int ret;
  10522. /* created from rsa_test : othercert.der */
  10523. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10524. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10525. /* created from rsa_test : othercert.der */
  10526. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  10527. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  10528. #ifdef HAVE_ECC
  10529. /* created from ecc_test_cert_gen : certecc.der */
  10530. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10531. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  10532. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  10533. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  10534. #else
  10535. /* Authority key id from ./certs/ca-ecc-cert.pem */
  10536. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  10537. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  10538. #endif
  10539. #endif /* HAVE_ECC */
  10540. /* created from rsa_test : cert.der */
  10541. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10542. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10543. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10544. if (tmp == NULL)
  10545. return -7500;
  10546. /* load othercert.der (Cert signed by an authority) */
  10547. file = XFOPEN(otherCertDerFile, "rb");
  10548. if (!file) {
  10549. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10550. return -7501;
  10551. }
  10552. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10553. XFCLOSE(file);
  10554. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10555. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10556. if (ret != 0)
  10557. return -7502;
  10558. /* check the SKID from a RSA certificate */
  10559. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10560. return -7503;
  10561. /* check the AKID from an RSA certificate */
  10562. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10563. return -7504;
  10564. /* check the Key Usage from an RSA certificate */
  10565. if (!cert.extKeyUsageSet)
  10566. return -7505;
  10567. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  10568. return -7506;
  10569. /* check the CA Basic Constraints from an RSA certificate */
  10570. if (cert.isCA)
  10571. return -7507;
  10572. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10573. /* check the Certificate Policies Id */
  10574. if (cert.extCertPoliciesNb != 1)
  10575. return -7508;
  10576. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10577. return -7509;
  10578. #endif
  10579. FreeDecodedCert(&cert);
  10580. #ifdef HAVE_ECC
  10581. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  10582. file = XFOPEN(certEccDerFile, "rb");
  10583. if (!file) {
  10584. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10585. return -7510;
  10586. }
  10587. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10588. XFCLOSE(file);
  10589. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10590. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10591. if (ret != 0)
  10592. return -7511;
  10593. /* check the SKID from a ECC certificate - generated dynamically */
  10594. /* check the AKID from an ECC certificate */
  10595. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10596. return -7512;
  10597. /* check the Key Usage from an ECC certificate */
  10598. if (!cert.extKeyUsageSet)
  10599. return -7513;
  10600. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  10601. return -7514;
  10602. /* check the CA Basic Constraints from an ECC certificate */
  10603. if (cert.isCA)
  10604. return -7515;
  10605. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10606. /* check the Certificate Policies Id */
  10607. if (cert.extCertPoliciesNb != 2)
  10608. return -7516;
  10609. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  10610. return -7517;
  10611. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  10612. return -7518;
  10613. #endif
  10614. FreeDecodedCert(&cert);
  10615. #endif /* HAVE_ECC */
  10616. /* load cert.der (self signed certificate) */
  10617. file = XFOPEN(certDerFile, "rb");
  10618. if (!file) {
  10619. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10620. return -7519;
  10621. }
  10622. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10623. XFCLOSE(file);
  10624. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10625. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10626. if (ret != 0)
  10627. return -7520;
  10628. /* check the SKID from a CA certificate */
  10629. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10630. return -7521;
  10631. /* check the AKID from an CA certificate */
  10632. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10633. return -7522;
  10634. /* check the Key Usage from CA certificate */
  10635. if (!cert.extKeyUsageSet)
  10636. return -7523;
  10637. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  10638. return -7524;
  10639. /* check the CA Basic Constraints CA certificate */
  10640. if (!cert.isCA)
  10641. return -7525;
  10642. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10643. /* check the Certificate Policies Id */
  10644. if (cert.extCertPoliciesNb != 2)
  10645. return -7526;
  10646. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10647. return -7527;
  10648. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  10649. return -7528;
  10650. #endif
  10651. FreeDecodedCert(&cert);
  10652. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10653. return 0;
  10654. }
  10655. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  10656. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  10657. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  10658. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  10659. {
  10660. int ret = 0;
  10661. Cert cert;
  10662. FILE* file;
  10663. byte* der;
  10664. word32 derSz;
  10665. derSz = FOURK_BUF;
  10666. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10667. if (der == NULL)
  10668. ret = -7600;
  10669. if (ret == 0) {
  10670. /* load cert.der */
  10671. file = XFOPEN(certDerFile, "rb");
  10672. if (file != NULL) {
  10673. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  10674. XFCLOSE(file);
  10675. }
  10676. else
  10677. ret = -7601;
  10678. }
  10679. if (ret == 0) {
  10680. if (wc_InitCert(&cert)) {
  10681. ret = -7602;
  10682. }
  10683. }
  10684. if (ret == 0) {
  10685. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  10686. }
  10687. if (ret == 0) {
  10688. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10689. ret = -7603;
  10690. }
  10691. if (ret == 0) {
  10692. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  10693. ret = -7604;
  10694. }
  10695. if (ret == 0) {
  10696. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  10697. ret = -7605;
  10698. }
  10699. if (ret == 0) {
  10700. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  10701. ret = -7606;
  10702. }
  10703. if (ret == 0) {
  10704. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10705. ret = -7607;
  10706. }
  10707. if (ret == 0) {
  10708. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  10709. ret = -7608;
  10710. }
  10711. if (ret == 0) {
  10712. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  10713. ret = -7609;
  10714. }
  10715. #ifdef WOLFSSL_ALT_NAMES
  10716. if (ret == 0) {
  10717. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  10718. ret = -7610;
  10719. }
  10720. if (ret == 0) {
  10721. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10722. ret = -7611;
  10723. }
  10724. if (ret == 0) {
  10725. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  10726. ret = -7612;
  10727. }
  10728. if (ret == 0) {
  10729. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10730. ret = -7613;
  10731. }
  10732. #endif
  10733. if (ret == 0) {
  10734. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  10735. ret = -7614;
  10736. }
  10737. if (ret == 0) {
  10738. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  10739. ret = -7615;
  10740. }
  10741. wc_SetCert_Free(&cert);
  10742. if (ret == 0) {
  10743. if(cert.decodedCert != NULL)
  10744. ret = -7616;
  10745. }
  10746. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10747. return ret;
  10748. }
  10749. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  10750. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  10751. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  10752. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  10753. static int rsa_flatten_test(RsaKey* key)
  10754. {
  10755. int ret;
  10756. byte e[RSA_TEST_BYTES];
  10757. byte n[RSA_TEST_BYTES];
  10758. word32 eSz = sizeof(e);
  10759. word32 nSz = sizeof(n);
  10760. /* Parameter Validation testing. */
  10761. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  10762. #ifdef HAVE_USER_RSA
  10763. /* Implementation using IPP Libraries returns:
  10764. * -101 = USER_CRYPTO_ERROR
  10765. */
  10766. if (ret == 0)
  10767. #else
  10768. if (ret != BAD_FUNC_ARG)
  10769. #endif
  10770. return -7620;
  10771. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  10772. #ifdef HAVE_USER_RSA
  10773. /* Implementation using IPP Libraries returns:
  10774. * -101 = USER_CRYPTO_ERROR
  10775. */
  10776. if (ret == 0)
  10777. #else
  10778. if (ret != BAD_FUNC_ARG)
  10779. #endif
  10780. return -7621;
  10781. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  10782. #ifdef HAVE_USER_RSA
  10783. /* Implementation using IPP Libraries returns:
  10784. * -101 = USER_CRYPTO_ERROR
  10785. */
  10786. if (ret == 0)
  10787. #else
  10788. if (ret != BAD_FUNC_ARG)
  10789. #endif
  10790. return -7622;
  10791. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  10792. #ifdef HAVE_USER_RSA
  10793. /* Implementation using IPP Libraries returns:
  10794. * -101 = USER_CRYPTO_ERROR
  10795. */
  10796. if (ret == 0)
  10797. #else
  10798. if (ret != BAD_FUNC_ARG)
  10799. #endif
  10800. return -7623;
  10801. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  10802. #ifdef HAVE_USER_RSA
  10803. /* Implementation using IPP Libraries returns:
  10804. * -101 = USER_CRYPTO_ERROR
  10805. */
  10806. if (ret == 0)
  10807. #else
  10808. if (ret != BAD_FUNC_ARG)
  10809. #endif
  10810. return -7624;
  10811. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10812. if (ret != 0)
  10813. return -7625;
  10814. eSz = 0;
  10815. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10816. #ifdef HAVE_USER_RSA
  10817. /* Implementation using IPP Libraries returns:
  10818. * -101 = USER_CRYPTO_ERROR
  10819. */
  10820. if (ret == 0)
  10821. #elif defined(HAVE_FIPS) && \
  10822. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  10823. if (ret != 0)
  10824. #else
  10825. if (ret != RSA_BUFFER_E)
  10826. #endif
  10827. return -7626;
  10828. eSz = sizeof(e);
  10829. nSz = 0;
  10830. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10831. #ifdef HAVE_USER_RSA
  10832. /* Implementation using IPP Libraries returns:
  10833. * -101 = USER_CRYPTO_ERROR
  10834. */
  10835. if (ret == 0)
  10836. #else
  10837. if (ret != RSA_BUFFER_E)
  10838. #endif
  10839. return -7627;
  10840. return 0;
  10841. }
  10842. #endif /* NO_ASN */
  10843. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  10844. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  10845. static int rsa_export_key_test(RsaKey* key)
  10846. {
  10847. int ret;
  10848. byte e[3];
  10849. word32 eSz = sizeof(e);
  10850. byte n[RSA_TEST_BYTES];
  10851. word32 nSz = sizeof(n);
  10852. byte d[RSA_TEST_BYTES];
  10853. word32 dSz = sizeof(d);
  10854. byte p[RSA_TEST_BYTES/2];
  10855. word32 pSz = sizeof(p);
  10856. byte q[RSA_TEST_BYTES/2];
  10857. word32 qSz = sizeof(q);
  10858. word32 zero = 0;
  10859. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10860. if (ret != BAD_FUNC_ARG)
  10861. return -7630;
  10862. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10863. if (ret != BAD_FUNC_ARG)
  10864. return -7631;
  10865. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10866. if (ret != BAD_FUNC_ARG)
  10867. return -7632;
  10868. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10869. if (ret != BAD_FUNC_ARG)
  10870. return -7633;
  10871. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  10872. if (ret != BAD_FUNC_ARG)
  10873. return -7634;
  10874. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  10875. if (ret != BAD_FUNC_ARG)
  10876. return -7635;
  10877. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  10878. if (ret != BAD_FUNC_ARG)
  10879. return -7636;
  10880. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  10881. if (ret != BAD_FUNC_ARG)
  10882. return -7637;
  10883. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  10884. if (ret != BAD_FUNC_ARG)
  10885. return -7638;
  10886. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  10887. if (ret != BAD_FUNC_ARG)
  10888. return -7639;
  10889. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  10890. if (ret != BAD_FUNC_ARG)
  10891. return -7640;
  10892. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10893. if (ret != RSA_BUFFER_E)
  10894. return -7641;
  10895. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  10896. if (ret != RSA_BUFFER_E)
  10897. return -7642;
  10898. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  10899. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  10900. if (ret != RSA_BUFFER_E)
  10901. return -7643;
  10902. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  10903. if (ret != RSA_BUFFER_E)
  10904. return -7644;
  10905. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  10906. if (ret != RSA_BUFFER_E)
  10907. return -7645;
  10908. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  10909. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10910. if (ret != 0)
  10911. return -7646;
  10912. return 0;
  10913. }
  10914. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  10915. #ifndef NO_SIG_WRAPPER
  10916. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  10917. {
  10918. int ret;
  10919. word32 sigSz;
  10920. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  10921. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  10922. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  10923. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  10924. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  10925. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  10926. };
  10927. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  10928. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  10929. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  10930. 0x00, 0x04, 0x20,
  10931. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  10932. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  10933. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  10934. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  10935. };
  10936. word32 inLen = (word32)XSTRLEN((char*)in);
  10937. byte out[RSA_TEST_BYTES];
  10938. /* Parameter Validation testing. */
  10939. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  10940. if (ret != BAD_FUNC_ARG)
  10941. return -7650;
  10942. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  10943. if (ret != BAD_FUNC_ARG)
  10944. return -7651;
  10945. sigSz = (word32)modLen;
  10946. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  10947. inLen, out, &sigSz, key, keyLen, rng);
  10948. if (ret != BAD_FUNC_ARG)
  10949. return -7652;
  10950. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10951. 0, out, &sigSz, key, keyLen, rng);
  10952. if (ret != BAD_FUNC_ARG)
  10953. return -7653;
  10954. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10955. inLen, NULL, &sigSz, key, keyLen, rng);
  10956. if (ret != BAD_FUNC_ARG)
  10957. return -7654;
  10958. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10959. inLen, out, NULL, key, keyLen, rng);
  10960. if (ret != BAD_FUNC_ARG)
  10961. return -7655;
  10962. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10963. inLen, out, &sigSz, NULL, keyLen, rng);
  10964. if (ret != BAD_FUNC_ARG)
  10965. return -7656;
  10966. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10967. inLen, out, &sigSz, key, 0, rng);
  10968. if (ret != BAD_FUNC_ARG)
  10969. return -7657;
  10970. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10971. inLen, out, &sigSz, key, keyLen, NULL);
  10972. #ifdef HAVE_USER_RSA
  10973. /* Implementation using IPP Libraries returns:
  10974. * -101 = USER_CRYPTO_ERROR
  10975. */
  10976. if (ret == 0)
  10977. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  10978. /* blinding / rng handled with hardware acceleration */
  10979. if (ret != 0)
  10980. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  10981. /* async may not require RNG */
  10982. if (ret != 0 && ret != MISSING_RNG_E)
  10983. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  10984. /* FIPS140 implementation does not do blinding */
  10985. if (ret != 0)
  10986. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY)
  10987. if (ret != SIG_TYPE_E)
  10988. #elif defined(WOLFSSL_CRYPTOCELL)
  10989. /* RNG is handled with the cryptocell */
  10990. if (ret != 0)
  10991. #else
  10992. if (ret != MISSING_RNG_E)
  10993. #endif
  10994. return -7658;
  10995. sigSz = 0;
  10996. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  10997. inLen, out, &sigSz, key, keyLen, rng);
  10998. if (ret != BAD_FUNC_ARG)
  10999. return -7659;
  11000. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  11001. inLen, out, (word32)modLen, key, keyLen);
  11002. if (ret != BAD_FUNC_ARG)
  11003. return -7660;
  11004. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11005. 0, out, (word32)modLen, key, keyLen);
  11006. if (ret != BAD_FUNC_ARG)
  11007. return -7661;
  11008. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11009. inLen, NULL, (word32)modLen, key, keyLen);
  11010. if (ret != BAD_FUNC_ARG)
  11011. return -7662;
  11012. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11013. inLen, out, 0, key, keyLen);
  11014. if (ret != BAD_FUNC_ARG)
  11015. return -7663;
  11016. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11017. inLen, out, (word32)modLen, NULL, keyLen);
  11018. if (ret != BAD_FUNC_ARG)
  11019. return -7664;
  11020. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11021. inLen, out, (word32)modLen, key, 0);
  11022. if (ret != BAD_FUNC_ARG)
  11023. return -7665;
  11024. #ifndef HAVE_ECC
  11025. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  11026. if (ret != SIG_TYPE_E)
  11027. return -7666;
  11028. #endif
  11029. /* Use APIs. */
  11030. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  11031. if (ret != modLen)
  11032. return -7667;
  11033. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  11034. if (ret != modLen)
  11035. return -7668;
  11036. sigSz = (word32)ret;
  11037. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11038. XMEMSET(out, 0, sizeof(out));
  11039. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11040. inLen, out, &sigSz, key, keyLen, rng);
  11041. if (ret != 0)
  11042. return -7669;
  11043. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11044. inLen, out, (word32)modLen, key, keyLen);
  11045. if (ret != 0)
  11046. return -7670;
  11047. sigSz = (word32)sizeof(out);
  11048. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11049. in, inLen, out, &sigSz, key, keyLen, rng);
  11050. if (ret != 0)
  11051. return -7671;
  11052. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11053. in, inLen, out, (word32)modLen, key, keyLen);
  11054. if (ret != 0)
  11055. return -7672;
  11056. /* Wrong signature type. */
  11057. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11058. inLen, out, (word32)modLen, key, keyLen);
  11059. if (ret == 0)
  11060. return -7673;
  11061. /* check hash functions */
  11062. sigSz = (word32)sizeof(out);
  11063. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11064. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  11065. if (ret != 0)
  11066. return -7674;
  11067. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11068. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  11069. if (ret != 0)
  11070. return -7675;
  11071. sigSz = (word32)sizeof(out);
  11072. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11073. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  11074. if (ret != 0)
  11075. return -7676;
  11076. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11077. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  11078. if (ret != 0)
  11079. return -7677;
  11080. #else
  11081. (void)hash;
  11082. (void)hashEnc;
  11083. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11084. return 0;
  11085. }
  11086. #endif /* !NO_SIG_WRAPPER */
  11087. #ifdef WC_RSA_NONBLOCK
  11088. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  11089. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  11090. {
  11091. int ret = 0, count;
  11092. int signSz = 0;
  11093. RsaNb nb;
  11094. byte* inlinePlain = NULL;
  11095. /* Enable non-blocking RSA mode - provide context */
  11096. ret = wc_RsaSetNonBlock(key, &nb);
  11097. if (ret != 0)
  11098. return ret;
  11099. #ifdef WC_RSA_NONBLOCK_TIME
  11100. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  11101. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  11102. if (ret != 0)
  11103. return ret;
  11104. #endif
  11105. count = 0;
  11106. do {
  11107. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  11108. count++; /* track number of would blocks */
  11109. if (ret == FP_WOULDBLOCK) {
  11110. /* do "other" work here */
  11111. }
  11112. } while (ret == FP_WOULDBLOCK);
  11113. if (ret < 0) {
  11114. return ret;
  11115. }
  11116. #ifdef DEBUG_WOLFSSL
  11117. printf("RSA non-block sign: %d times\n", count);
  11118. #endif
  11119. signSz = ret;
  11120. /* Test non-blocking verify */
  11121. XMEMSET(plain, 0, plainSz);
  11122. count = 0;
  11123. do {
  11124. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  11125. count++; /* track number of would blocks */
  11126. if (ret == FP_WOULDBLOCK) {
  11127. /* do "other" work here */
  11128. }
  11129. } while (ret == FP_WOULDBLOCK);
  11130. if (ret < 0) {
  11131. return ret;
  11132. }
  11133. #ifdef DEBUG_WOLFSSL
  11134. printf("RSA non-block verify: %d times\n", count);
  11135. #endif
  11136. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  11137. return SIG_VERIFY_E;
  11138. }
  11139. /* Test inline non-blocking verify */
  11140. count = 0;
  11141. do {
  11142. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  11143. count++; /* track number of would blocks */
  11144. if (ret == FP_WOULDBLOCK) {
  11145. /* do "other" work here */
  11146. }
  11147. } while (ret == FP_WOULDBLOCK);
  11148. if (ret < 0) {
  11149. return ret;
  11150. }
  11151. #ifdef DEBUG_WOLFSSL
  11152. printf("RSA non-block inline verify: %d times\n", count);
  11153. #endif
  11154. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  11155. return SIG_VERIFY_E;
  11156. }
  11157. /* Disabling non-block RSA mode */
  11158. ret = wc_RsaSetNonBlock(key, NULL);
  11159. (void)count;
  11160. return 0;
  11161. }
  11162. #endif
  11163. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  11164. static int rsa_decode_test(RsaKey* keyPub)
  11165. {
  11166. int ret;
  11167. word32 inSz;
  11168. word32 inOutIdx;
  11169. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  11170. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  11171. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  11172. 0x03 };
  11173. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11174. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11175. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00,
  11176. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11177. 0x02, 0x1, 0x03 };
  11178. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00,
  11179. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11180. 0x02, 0x1, 0x03 };
  11181. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11182. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11183. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11184. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11185. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03,
  11186. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11187. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  11188. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  11189. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11190. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1,
  11191. 0x03 };
  11192. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1,
  11193. 0x03 };
  11194. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1,
  11195. 0x03 };
  11196. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00,
  11197. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11198. ret = wc_InitRsaKey(keyPub, NULL);
  11199. if (ret != 0)
  11200. return -7690;
  11201. /* Parameter Validation testing. */
  11202. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  11203. if (ret != BAD_FUNC_ARG) {
  11204. ret = -7691;
  11205. goto done;
  11206. }
  11207. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  11208. if (ret != BAD_FUNC_ARG) {
  11209. ret = -7692;
  11210. goto done;
  11211. }
  11212. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  11213. if (ret != BAD_FUNC_ARG) {
  11214. ret = -7693;
  11215. goto done;
  11216. }
  11217. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  11218. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11219. if (ret != 0) {
  11220. #else
  11221. if (ret != ASN_GETINT_E) {
  11222. #endif
  11223. ret = -7694;
  11224. goto done;
  11225. }
  11226. wc_FreeRsaKey(keyPub);
  11227. ret = wc_InitRsaKey(keyPub, NULL);
  11228. if (ret != 0)
  11229. return -7695;
  11230. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  11231. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11232. if (ret != 0) {
  11233. #else
  11234. if (ret != ASN_GETINT_E) {
  11235. #endif
  11236. ret = -7696;
  11237. goto done;
  11238. }
  11239. wc_FreeRsaKey(keyPub);
  11240. ret = wc_InitRsaKey(keyPub, NULL);
  11241. if (ret != 0)
  11242. return -7697;
  11243. /* Use API. */
  11244. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  11245. if (ret != 0) {
  11246. ret = -7698;
  11247. goto done;
  11248. }
  11249. wc_FreeRsaKey(keyPub);
  11250. ret = wc_InitRsaKey(keyPub, NULL);
  11251. if (ret != 0)
  11252. return -7699;
  11253. /* Parameter Validation testing. */
  11254. inSz = sizeof(good);
  11255. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  11256. if (ret != BAD_FUNC_ARG) {
  11257. ret = -7700;
  11258. goto done;
  11259. }
  11260. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  11261. if (ret != BAD_FUNC_ARG) {
  11262. ret = -7701;
  11263. goto done;
  11264. }
  11265. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  11266. if (ret != BAD_FUNC_ARG) {
  11267. ret = -7702;
  11268. goto done;
  11269. }
  11270. /* Use good data and offset to bad data. */
  11271. inOutIdx = 2;
  11272. inSz = sizeof(good) - inOutIdx;
  11273. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11274. if (ret != ASN_PARSE_E) {
  11275. ret = -7703;
  11276. goto done;
  11277. }
  11278. inOutIdx = 2;
  11279. inSz = sizeof(goodAlgId) - inOutIdx;
  11280. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11281. if (ret != ASN_PARSE_E) {
  11282. ret = -7704;
  11283. goto done;
  11284. }
  11285. inOutIdx = 2;
  11286. inSz = sizeof(goodAlgId);
  11287. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11288. #ifndef WOLFSSL_NO_DECODE_EXTRA
  11289. if (ret != ASN_PARSE_E)
  11290. #else
  11291. if (ret != ASN_RSA_KEY_E)
  11292. #endif
  11293. {
  11294. ret = -7705;
  11295. goto done;
  11296. }
  11297. /* Try different bad data. */
  11298. inSz = sizeof(badAlgIdNull);
  11299. inOutIdx = 0;
  11300. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  11301. if (ret != ASN_EXPECT_0_E) {
  11302. ret = -7706;
  11303. goto done;
  11304. }
  11305. inSz = sizeof(badNotBitString);
  11306. inOutIdx = 0;
  11307. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  11308. if (ret != ASN_BITSTR_E) {
  11309. ret = -7707;
  11310. goto done;
  11311. }
  11312. inSz = sizeof(badBitStringLen);
  11313. inOutIdx = 0;
  11314. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  11315. if (ret != ASN_PARSE_E) {
  11316. ret = -7708;
  11317. goto done;
  11318. }
  11319. inSz = sizeof(badNoSeq);
  11320. inOutIdx = 0;
  11321. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  11322. if (ret != ASN_PARSE_E) {
  11323. ret = -7709;
  11324. goto done;
  11325. }
  11326. inSz = sizeof(badNoObj);
  11327. inOutIdx = 0;
  11328. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  11329. if (ret != ASN_PARSE_E) {
  11330. ret = -7710;
  11331. goto done;
  11332. }
  11333. inSz = sizeof(badIntN);
  11334. inOutIdx = 0;
  11335. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  11336. if (ret != ASN_RSA_KEY_E) {
  11337. ret = -7711;
  11338. goto done;
  11339. }
  11340. inSz = sizeof(badNotIntE);
  11341. inOutIdx = 0;
  11342. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  11343. if (ret != ASN_RSA_KEY_E) {
  11344. ret = -7712;
  11345. goto done;
  11346. }
  11347. /* TODO: Shouldn't pass as the sequence length is too small. */
  11348. inSz = sizeof(badLength);
  11349. inOutIdx = 0;
  11350. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  11351. if (ret != 0) {
  11352. ret = -7713;
  11353. goto done;
  11354. }
  11355. /* TODO: Shouldn't ignore object id's data. */
  11356. wc_FreeRsaKey(keyPub);
  11357. ret = wc_InitRsaKey(keyPub, NULL);
  11358. if (ret != 0)
  11359. return -7714;
  11360. inSz = sizeof(badBitStrNoZero);
  11361. inOutIdx = 0;
  11362. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  11363. if (ret != ASN_EXPECT_0_E) {
  11364. ret = -7715;
  11365. goto done;
  11366. }
  11367. wc_FreeRsaKey(keyPub);
  11368. ret = wc_InitRsaKey(keyPub, NULL);
  11369. if (ret != 0)
  11370. return -7716;
  11371. /* Valid data cases. */
  11372. inSz = sizeof(good);
  11373. inOutIdx = 0;
  11374. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11375. if (ret != 0) {
  11376. ret = -7717;
  11377. goto done;
  11378. }
  11379. if (inOutIdx != inSz) {
  11380. ret = -7718;
  11381. goto done;
  11382. }
  11383. wc_FreeRsaKey(keyPub);
  11384. ret = wc_InitRsaKey(keyPub, NULL);
  11385. if (ret != 0)
  11386. return -7719;
  11387. inSz = sizeof(goodAlgId);
  11388. inOutIdx = 0;
  11389. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11390. if (ret != 0) {
  11391. ret = -7720;
  11392. goto done;
  11393. }
  11394. if (inOutIdx != inSz) {
  11395. ret = -7721;
  11396. goto done;
  11397. }
  11398. wc_FreeRsaKey(keyPub);
  11399. ret = wc_InitRsaKey(keyPub, NULL);
  11400. if (ret != 0)
  11401. return -7722;
  11402. inSz = sizeof(goodAlgIdNull);
  11403. inOutIdx = 0;
  11404. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  11405. if (ret != 0) {
  11406. ret = -7723;
  11407. goto done;
  11408. }
  11409. if (inOutIdx != inSz) {
  11410. ret = -7724;
  11411. goto done;
  11412. }
  11413. done:
  11414. wc_FreeRsaKey(keyPub);
  11415. return ret;
  11416. }
  11417. #endif
  11418. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  11419. /* Need to create known good signatures to test with this. */
  11420. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  11421. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  11422. {
  11423. byte digest[WC_MAX_DIGEST_SIZE];
  11424. int ret = 0;
  11425. const char inStr[] = TEST_STRING;
  11426. word32 inLen = (word32)TEST_STRING_SZ;
  11427. word32 outSz;
  11428. word32 plainSz;
  11429. word32 digestSz;
  11430. int i, j;
  11431. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11432. int k, l;
  11433. #endif
  11434. int len;
  11435. byte* plain;
  11436. int mgf[] = {
  11437. #ifndef NO_SHA
  11438. WC_MGF1SHA1,
  11439. #endif
  11440. #ifdef WOLFSSL_SHA224
  11441. WC_MGF1SHA224,
  11442. #endif
  11443. WC_MGF1SHA256,
  11444. #ifdef WOLFSSL_SHA384
  11445. WC_MGF1SHA384,
  11446. #endif
  11447. #ifdef WOLFSSL_SHA512
  11448. WC_MGF1SHA512
  11449. #endif
  11450. };
  11451. enum wc_HashType hash[] = {
  11452. #ifndef NO_SHA
  11453. WC_HASH_TYPE_SHA,
  11454. #endif
  11455. #ifdef WOLFSSL_SHA224
  11456. WC_HASH_TYPE_SHA224,
  11457. #endif
  11458. WC_HASH_TYPE_SHA256,
  11459. #ifdef WOLFSSL_SHA384
  11460. WC_HASH_TYPE_SHA384,
  11461. #endif
  11462. #ifdef WOLFSSL_SHA512
  11463. WC_HASH_TYPE_SHA512,
  11464. #endif
  11465. };
  11466. DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  11467. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11468. DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  11469. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11470. if (in == NULL || out == NULL || sig == NULL)
  11471. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  11472. #endif
  11473. XMEMCPY(in, inStr, inLen);
  11474. /* Test all combinations of hash and MGF. */
  11475. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  11476. /* Calculate hash of message. */
  11477. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  11478. if (ret != 0)
  11479. ERROR_OUT(-7730, exit_rsa_pss);
  11480. digestSz = wc_HashGetDigestSize(hash[j]);
  11481. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  11482. outSz = RSA_TEST_BYTES;
  11483. do {
  11484. #if defined(WOLFSSL_ASYNC_CRYPT)
  11485. ret = wc_AsyncWait(ret, &key->asyncDev,
  11486. WC_ASYNC_FLAG_CALL_AGAIN);
  11487. #endif
  11488. if (ret >= 0) {
  11489. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  11490. hash[j], mgf[i], -1, key, rng);
  11491. }
  11492. } while (ret == WC_PENDING_E);
  11493. if (ret <= 0)
  11494. ERROR_OUT(-7731, exit_rsa_pss);
  11495. outSz = ret;
  11496. XMEMCPY(sig, out, outSz);
  11497. plain = NULL;
  11498. TEST_SLEEP();
  11499. do {
  11500. #if defined(WOLFSSL_ASYNC_CRYPT)
  11501. ret = wc_AsyncWait(ret, &key->asyncDev,
  11502. WC_ASYNC_FLAG_CALL_AGAIN);
  11503. #endif
  11504. if (ret >= 0) {
  11505. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  11506. mgf[i], -1, key);
  11507. }
  11508. } while (ret == WC_PENDING_E);
  11509. if (ret <= 0)
  11510. ERROR_OUT(-7732, exit_rsa_pss);
  11511. plainSz = ret;
  11512. TEST_SLEEP();
  11513. #if defined(HAVE_SELFTEST) && \
  11514. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11515. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11516. hash[j], -1);
  11517. #else
  11518. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11519. hash[j], -1, wc_RsaEncryptSize(key)*8);
  11520. #endif
  11521. if (ret != 0)
  11522. ERROR_OUT(-7733, exit_rsa_pss);
  11523. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11524. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  11525. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  11526. if (i == k && j == l)
  11527. continue;
  11528. XMEMCPY(sig, out, outSz);
  11529. do {
  11530. #if defined(WOLFSSL_ASYNC_CRYPT)
  11531. ret = wc_AsyncWait(ret, &key->asyncDev,
  11532. WC_ASYNC_FLAG_CALL_AGAIN);
  11533. #endif
  11534. if (ret >= 0) {
  11535. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  11536. (byte**)&plain, hash[l], mgf[k], -1, key);
  11537. }
  11538. } while (ret == WC_PENDING_E);
  11539. if (ret >= 0)
  11540. ERROR_OUT(-7734, exit_rsa_pss);
  11541. }
  11542. }
  11543. #endif
  11544. }
  11545. }
  11546. /* Test that a salt length of zero works. */
  11547. digestSz = wc_HashGetDigestSize(hash[0]);
  11548. outSz = RSA_TEST_BYTES;
  11549. do {
  11550. #if defined(WOLFSSL_ASYNC_CRYPT)
  11551. ret = wc_AsyncWait(ret, &key->asyncDev,
  11552. WC_ASYNC_FLAG_CALL_AGAIN);
  11553. #endif
  11554. if (ret >= 0) {
  11555. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11556. mgf[0], 0, key, rng);
  11557. }
  11558. } while (ret == WC_PENDING_E);
  11559. if (ret <= 0)
  11560. ERROR_OUT(-7735, exit_rsa_pss);
  11561. outSz = ret;
  11562. TEST_SLEEP();
  11563. do {
  11564. #if defined(WOLFSSL_ASYNC_CRYPT)
  11565. ret = wc_AsyncWait(ret, &key->asyncDev,
  11566. WC_ASYNC_FLAG_CALL_AGAIN);
  11567. #endif
  11568. if (ret >= 0) {
  11569. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  11570. 0, key);
  11571. }
  11572. } while (ret == WC_PENDING_E);
  11573. if (ret <= 0)
  11574. ERROR_OUT(-7736, exit_rsa_pss);
  11575. plainSz = ret;
  11576. TEST_SLEEP();
  11577. do {
  11578. #if defined(WOLFSSL_ASYNC_CRYPT)
  11579. ret = wc_AsyncWait(ret, &key->asyncDev,
  11580. WC_ASYNC_FLAG_CALL_AGAIN);
  11581. #endif
  11582. if (ret >= 0) {
  11583. #if defined(HAVE_SELFTEST) && \
  11584. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11585. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11586. hash[0], 0);
  11587. #else
  11588. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11589. hash[0], 0, 0);
  11590. #endif
  11591. }
  11592. } while (ret == WC_PENDING_E);
  11593. if (ret != 0)
  11594. ERROR_OUT(-7737, exit_rsa_pss);
  11595. XMEMCPY(sig, out, outSz);
  11596. plain = NULL;
  11597. do {
  11598. #if defined(WOLFSSL_ASYNC_CRYPT)
  11599. ret = wc_AsyncWait(ret, &key->asyncDev,
  11600. WC_ASYNC_FLAG_CALL_AGAIN);
  11601. #endif
  11602. if (ret >= 0) {
  11603. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11604. 0, key);
  11605. }
  11606. } while (ret == WC_PENDING_E);
  11607. if (ret <= 0)
  11608. ERROR_OUT(-7738, exit_rsa_pss);
  11609. plainSz = ret;
  11610. TEST_SLEEP();
  11611. #if defined(HAVE_SELFTEST) && \
  11612. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11613. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11614. 0);
  11615. #else
  11616. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11617. 0, 0);
  11618. #endif
  11619. if (ret != 0)
  11620. ERROR_OUT(-7739, exit_rsa_pss);
  11621. /* Test bad salt lengths in various APIs. */
  11622. digestSz = wc_HashGetDigestSize(hash[0]);
  11623. outSz = RSA_TEST_BYTES;
  11624. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11625. len = -2;
  11626. #else
  11627. len = -3;
  11628. #endif
  11629. do {
  11630. #if defined(WOLFSSL_ASYNC_CRYPT)
  11631. ret = wc_AsyncWait(ret, &key->asyncDev,
  11632. WC_ASYNC_FLAG_CALL_AGAIN);
  11633. #endif
  11634. if (ret >= 0) {
  11635. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11636. mgf[0], len, key, rng);
  11637. }
  11638. } while (ret == WC_PENDING_E);
  11639. if (ret != PSS_SALTLEN_E)
  11640. ERROR_OUT(-7740, exit_rsa_pss);
  11641. do {
  11642. #if defined(WOLFSSL_ASYNC_CRYPT)
  11643. ret = wc_AsyncWait(ret, &key->asyncDev,
  11644. WC_ASYNC_FLAG_CALL_AGAIN);
  11645. #endif
  11646. if (ret >= 0) {
  11647. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11648. mgf[0], digestSz + 1, key, rng);
  11649. }
  11650. } while (ret == WC_PENDING_E);
  11651. if (ret != PSS_SALTLEN_E)
  11652. ERROR_OUT(-7741, exit_rsa_pss);
  11653. TEST_SLEEP();
  11654. do {
  11655. #if defined(WOLFSSL_ASYNC_CRYPT)
  11656. ret = wc_AsyncWait(ret, &key->asyncDev,
  11657. WC_ASYNC_FLAG_CALL_AGAIN);
  11658. #endif
  11659. if (ret >= 0) {
  11660. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  11661. mgf[0], -2, key);
  11662. }
  11663. } while (ret == WC_PENDING_E);
  11664. if (ret != PSS_SALTLEN_E)
  11665. ERROR_OUT(-7742, exit_rsa_pss);
  11666. TEST_SLEEP();
  11667. do {
  11668. #if defined(WOLFSSL_ASYNC_CRYPT)
  11669. ret = wc_AsyncWait(ret, &key->asyncDev,
  11670. WC_ASYNC_FLAG_CALL_AGAIN);
  11671. #endif
  11672. if (ret >= 0) {
  11673. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11674. digestSz + 1, key);
  11675. }
  11676. } while (ret == WC_PENDING_E);
  11677. if (ret != PSS_SALTLEN_E)
  11678. ERROR_OUT(-7743, exit_rsa_pss);
  11679. TEST_SLEEP();
  11680. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11681. len = -2;
  11682. #else
  11683. len = -3;
  11684. #endif
  11685. #if defined(HAVE_SELFTEST) && \
  11686. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11687. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11688. len);
  11689. #else
  11690. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11691. len, 0);
  11692. #endif
  11693. if (ret != PSS_SALTLEN_E)
  11694. ERROR_OUT(-7744, exit_rsa_pss);
  11695. #ifndef WOLFSSL_PSS_LONG_SALT
  11696. len = digestSz + 1;
  11697. #else
  11698. len = plainSz - digestSz - 1;
  11699. #endif
  11700. #if defined(HAVE_SELFTEST) && \
  11701. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11702. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11703. len);
  11704. #else
  11705. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11706. len, 0);
  11707. #endif
  11708. if (ret != PSS_SALTLEN_E)
  11709. ERROR_OUT(-7745, exit_rsa_pss);
  11710. ret = 0;
  11711. exit_rsa_pss:
  11712. FREE_VAR(sig, HEAP_HINT);
  11713. FREE_VAR(in, HEAP_HINT);
  11714. FREE_VAR(out, HEAP_HINT);
  11715. return ret;
  11716. }
  11717. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11718. #endif
  11719. #ifdef WC_RSA_NO_PADDING
  11720. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  11721. {
  11722. WC_RNG rng;
  11723. byte* tmp = NULL;
  11724. size_t bytes;
  11725. int ret;
  11726. word32 inLen = 0;
  11727. word32 idx = 0;
  11728. word32 outSz = RSA_TEST_BYTES;
  11729. word32 plainSz = RSA_TEST_BYTES;
  11730. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11731. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  11732. !defined(NO_FILESYSTEM)
  11733. XFILE file;
  11734. #endif
  11735. DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  11736. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11737. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  11738. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11739. if (key == NULL || out == NULL || plain == NULL)
  11740. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  11741. #endif
  11742. /* initialize stack structures */
  11743. XMEMSET(&rng, 0, sizeof(rng));
  11744. XMEMSET(key, 0, sizeof(RsaKey));
  11745. #ifdef USE_CERT_BUFFERS_1024
  11746. bytes = (size_t)sizeof_client_key_der_1024;
  11747. if (bytes < (size_t)sizeof_client_cert_der_1024)
  11748. bytes = (size_t)sizeof_client_cert_der_1024;
  11749. #elif defined(USE_CERT_BUFFERS_2048)
  11750. bytes = (size_t)sizeof_client_key_der_2048;
  11751. if (bytes < (size_t)sizeof_client_cert_der_2048)
  11752. bytes = (size_t)sizeof_client_cert_der_2048;
  11753. #else
  11754. bytes = FOURK_BUF;
  11755. #endif
  11756. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11757. if (tmp == NULL
  11758. #ifdef WOLFSSL_ASYNC_CRYPT
  11759. || out == NULL || plain == NULL
  11760. #endif
  11761. ) {
  11762. ERROR_OUT(-7800, exit_rsa_nopadding);
  11763. }
  11764. #ifdef USE_CERT_BUFFERS_1024
  11765. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  11766. #elif defined(USE_CERT_BUFFERS_2048)
  11767. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  11768. #elif defined(USE_CERT_BUFFERS_3072)
  11769. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  11770. #elif defined(USE_CERT_BUFFERS_4096)
  11771. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  11772. #elif !defined(NO_FILESYSTEM)
  11773. file = XFOPEN(clientKey, "rb");
  11774. if (!file) {
  11775. err_sys("can't open ./certs/client-key.der, "
  11776. "Please run from wolfSSL home dir", -40);
  11777. ERROR_OUT(-7801, exit_rsa_nopadding);
  11778. }
  11779. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11780. XFCLOSE(file);
  11781. #else
  11782. /* No key to use. */
  11783. ERROR_OUT(-7802, exit_rsa_nopadding);
  11784. #endif /* USE_CERT_BUFFERS */
  11785. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  11786. if (ret != 0) {
  11787. ERROR_OUT(-7803, exit_rsa_nopadding);
  11788. }
  11789. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  11790. if (ret != 0) {
  11791. ERROR_OUT(-7804, exit_rsa_nopadding);
  11792. }
  11793. /* after loading in key use tmp as the test buffer */
  11794. #ifndef HAVE_FIPS
  11795. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11796. #else
  11797. ret = wc_InitRng(&rng);
  11798. #endif
  11799. if (ret != 0) {
  11800. ERROR_OUT(-7805, exit_rsa_nopadding);
  11801. }
  11802. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11803. inLen = wc_RsaEncryptSize(key);
  11804. outSz = inLen;
  11805. plainSz = inLen;
  11806. XMEMSET(tmp, 7, inLen);
  11807. do {
  11808. #if defined(WOLFSSL_ASYNC_CRYPT)
  11809. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11810. #endif
  11811. if (ret >= 0) {
  11812. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  11813. RSA_PRIVATE_ENCRYPT, &rng);
  11814. }
  11815. } while (ret == WC_PENDING_E);
  11816. if (ret <= 0) {
  11817. ERROR_OUT(-7806, exit_rsa_nopadding);
  11818. }
  11819. /* encrypted result should not be the same as input */
  11820. if (XMEMCMP(out, tmp, inLen) == 0) {
  11821. ERROR_OUT(-7807, exit_rsa_nopadding);
  11822. }
  11823. TEST_SLEEP();
  11824. /* decrypt with public key and compare result */
  11825. do {
  11826. #if defined(WOLFSSL_ASYNC_CRYPT)
  11827. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11828. #endif
  11829. if (ret >= 0) {
  11830. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  11831. RSA_PUBLIC_DECRYPT, &rng);
  11832. }
  11833. } while (ret == WC_PENDING_E);
  11834. if (ret <= 0) {
  11835. ERROR_OUT(-7808, exit_rsa_nopadding);
  11836. }
  11837. if (XMEMCMP(plain, tmp, inLen) != 0) {
  11838. ERROR_OUT(-7809, exit_rsa_nopadding);
  11839. }
  11840. TEST_SLEEP();
  11841. #endif
  11842. #ifdef WC_RSA_BLINDING
  11843. ret = wc_RsaSetRNG(NULL, &rng);
  11844. if (ret != BAD_FUNC_ARG) {
  11845. ERROR_OUT(-7810, exit_rsa_nopadding);
  11846. }
  11847. ret = wc_RsaSetRNG(key, &rng);
  11848. if (ret < 0) {
  11849. ERROR_OUT(-7811, exit_rsa_nopadding);
  11850. }
  11851. #endif
  11852. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  11853. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11854. do {
  11855. #if defined(WOLFSSL_ASYNC_CRYPT)
  11856. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11857. #endif
  11858. if (ret >= 0) {
  11859. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  11860. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  11861. }
  11862. } while (ret == WC_PENDING_E);
  11863. if (ret < 0) {
  11864. ERROR_OUT(-7812, exit_rsa_nopadding);
  11865. }
  11866. TEST_SLEEP();
  11867. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11868. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11869. do {
  11870. #if defined(WOLFSSL_ASYNC_CRYPT)
  11871. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11872. #endif
  11873. if (ret >= 0) {
  11874. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  11875. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  11876. }
  11877. } while (ret == WC_PENDING_E);
  11878. if (ret < 0) {
  11879. ERROR_OUT(-7813, exit_rsa_nopadding);
  11880. }
  11881. if (XMEMCMP(plain, tmp, inLen) != 0) {
  11882. ERROR_OUT(-7814, exit_rsa_nopadding);
  11883. }
  11884. TEST_SLEEP();
  11885. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11886. /* test some bad arguments */
  11887. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  11888. &rng);
  11889. if (ret != BAD_FUNC_ARG) {
  11890. ERROR_OUT(-7815, exit_rsa_nopadding);
  11891. }
  11892. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  11893. &rng);
  11894. if (ret != BAD_FUNC_ARG) {
  11895. ERROR_OUT(-7816, exit_rsa_nopadding);
  11896. }
  11897. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  11898. &rng);
  11899. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  11900. ERROR_OUT(-7817, exit_rsa_nopadding);
  11901. }
  11902. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  11903. RSA_PUBLIC_DECRYPT, &rng);
  11904. if (ret != BAD_FUNC_ARG) {
  11905. ERROR_OUT(-7818, exit_rsa_nopadding);
  11906. }
  11907. /* if making it to this point of code without hitting an ERROR_OUT then
  11908. * all tests have passed */
  11909. ret = 0;
  11910. exit_rsa_nopadding:
  11911. wc_FreeRsaKey(key);
  11912. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11913. FREE_VAR(key, HEAP_HINT);
  11914. FREE_VAR(out, HEAP_HINT);
  11915. FREE_VAR(plain, HEAP_HINT);
  11916. wc_FreeRng(&rng);
  11917. return ret;
  11918. }
  11919. #endif /* WC_RSA_NO_PADDING */
  11920. #ifdef WOLFSSL_HAVE_SP_RSA
  11921. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  11922. {
  11923. byte* tmp = NULL;
  11924. size_t bytes;
  11925. int ret;
  11926. word32 inLen = 0;
  11927. #ifndef NO_ASN
  11928. word32 idx = 0;
  11929. #endif
  11930. word32 outSz = RSA_TEST_BYTES;
  11931. word32 plainSz = RSA_TEST_BYTES;
  11932. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  11933. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  11934. XFILE file;
  11935. #endif
  11936. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11937. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  11938. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11939. if (out == NULL || plain == NULL)
  11940. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  11941. #endif
  11942. #if defined(USE_CERT_BUFFERS_2048)
  11943. bytes = (size_t)sizeof_client_key_der_2048;
  11944. if (bytes < (size_t)sizeof_client_cert_der_2048)
  11945. bytes = (size_t)sizeof_client_cert_der_2048;
  11946. #else
  11947. bytes = FOURK_BUF;
  11948. #endif
  11949. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11950. if (tmp == NULL
  11951. #ifdef WOLFSSL_ASYNC_CRYPT
  11952. || out == NULL || plain == NULL
  11953. #endif
  11954. ) {
  11955. ERROR_OUT(-7800, exit_rsa_even_mod);
  11956. }
  11957. #if defined(USE_CERT_BUFFERS_2048)
  11958. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  11959. #elif defined(USE_CERT_BUFFERS_3072)
  11960. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  11961. #elif defined(USE_CERT_BUFFERS_4096)
  11962. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  11963. #elif !defined(NO_FILESYSTEM)
  11964. file = XFOPEN(clientKey, "rb");
  11965. if (!file) {
  11966. err_sys("can't open ./certs/client-key.der, "
  11967. "Please run from wolfSSL home dir", -40);
  11968. ERROR_OUT(-7801, exit_rsa_even_mod);
  11969. }
  11970. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11971. XFCLOSE(file);
  11972. #else
  11973. /* No key to use. */
  11974. ERROR_OUT(-7802, exit_rsa_even_mod);
  11975. #endif /* USE_CERT_BUFFERS */
  11976. #ifndef NO_ASN
  11977. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  11978. if (ret != 0) {
  11979. ERROR_OUT(-7804, exit_rsa_even_mod);
  11980. }
  11981. #else
  11982. #ifdef USE_CERT_BUFFERS_2048
  11983. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  11984. if (ret != 0) {
  11985. ERROR_OUT(-7804, exit_rsa_even_mod);
  11986. }
  11987. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  11988. if (ret != 0) {
  11989. ERROR_OUT(-7804, exit_rsa_even_mod);
  11990. }
  11991. #ifndef NO_SIG_WRAPPER
  11992. modLen = 2048;
  11993. #endif
  11994. #else
  11995. #error Not supported yet!
  11996. #endif
  11997. #endif
  11998. key->n.dp[0] &= (mp_digit)-2;
  11999. if (ret != 0) {
  12000. ERROR_OUT(-7804, exit_rsa_even_mod);
  12001. }
  12002. /* after loading in key use tmp as the test buffer */
  12003. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12004. inLen = 32;
  12005. outSz = wc_RsaEncryptSize(key);
  12006. XMEMSET(tmp, 7, plainSz);
  12007. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  12008. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12009. ERROR_OUT(-7806, exit_rsa_even_mod);
  12010. }
  12011. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  12012. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12013. ERROR_OUT(-7808, exit_rsa_even_mod);
  12014. }
  12015. #endif
  12016. #ifdef WC_RSA_BLINDING
  12017. ret = wc_RsaSetRNG(key, rng);
  12018. if (ret < 0) {
  12019. ERROR_OUT(-7811, exit_rsa_even_mod);
  12020. }
  12021. #endif
  12022. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  12023. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12024. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  12025. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12026. ERROR_OUT(-7812, exit_rsa_even_mod);
  12027. }
  12028. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  12029. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12030. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  12031. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12032. ERROR_OUT(-7813, exit_rsa_even_mod);
  12033. }
  12034. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12035. /* if making it to this point of code without hitting an ERROR_OUT then
  12036. * all tests have passed */
  12037. ret = 0;
  12038. exit_rsa_even_mod:
  12039. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12040. FREE_VAR(out, HEAP_HINT);
  12041. FREE_VAR(plain, HEAP_HINT);
  12042. (void)out;
  12043. (void)outSz;
  12044. (void)plain;
  12045. (void)plainSz;
  12046. (void)inLen;
  12047. (void)rng;
  12048. return ret;
  12049. }
  12050. #endif /* WOLFSSL_HAVE_SP_RSA */
  12051. #ifdef WOLFSSL_CERT_GEN
  12052. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  12053. {
  12054. #ifdef WOLFSSL_SMALL_STACK
  12055. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12056. #ifdef WOLFSSL_TEST_CERT
  12057. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12058. #endif
  12059. #else
  12060. RsaKey caKey[1];
  12061. #ifdef WOLFSSL_TEST_CERT
  12062. DecodedCert decode[1];
  12063. #endif
  12064. #endif
  12065. byte* der = NULL;
  12066. int ret;
  12067. Cert* myCert = NULL;
  12068. int certSz;
  12069. size_t bytes3;
  12070. word32 idx3 = 0;
  12071. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12072. XFILE file3;
  12073. #endif
  12074. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  12075. struct tm beforeTime;
  12076. struct tm afterTime;
  12077. #endif
  12078. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  12079. (void)keypub;
  12080. #ifdef WOLFSSL_SMALL_STACK
  12081. if (caKey == NULL)
  12082. ERROR_OUT(MEMORY_E, exit_rsa);
  12083. #ifdef WOLFSSL_TEST_CERT
  12084. if (decode == NULL)
  12085. ERROR_OUT(MEMORY_E, exit_rsa);
  12086. #endif
  12087. #endif
  12088. XMEMSET(caKey, 0, sizeof *caKey);
  12089. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12090. if (der == NULL) {
  12091. ERROR_OUT(-7820, exit_rsa);
  12092. }
  12093. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12094. if (myCert == NULL) {
  12095. ERROR_OUT(-7821, exit_rsa);
  12096. }
  12097. /* self signed */
  12098. if (wc_InitCert(myCert)) {
  12099. ERROR_OUT(-7822, exit_rsa);
  12100. }
  12101. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12102. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  12103. myCert->serialSz = (int)sizeof(mySerial);
  12104. myCert->isCA = 1;
  12105. #ifndef NO_SHA256
  12106. myCert->sigType = CTC_SHA256wRSA;
  12107. #else
  12108. myCert->sigType = CTC_SHAwRSA;
  12109. #endif
  12110. #ifdef WOLFSSL_CERT_EXT
  12111. /* add Policies */
  12112. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12113. CTC_MAX_CERTPOL_SZ);
  12114. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  12115. CTC_MAX_CERTPOL_SZ);
  12116. myCert->certPoliciesNb = 2;
  12117. /* add SKID from the Public Key */
  12118. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12119. ERROR_OUT(-7823, exit_rsa);
  12120. }
  12121. /* add AKID from the Public Key */
  12122. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12123. ERROR_OUT(-7824, exit_rsa);
  12124. }
  12125. /* add Key Usage */
  12126. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  12127. ERROR_OUT(-7825, exit_rsa);
  12128. }
  12129. #ifdef WOLFSSL_EKU_OID
  12130. {
  12131. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  12132. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  12133. HEAP_HINT) != 0) {
  12134. ERROR_OUT(-7826, exit_rsa);
  12135. }
  12136. }
  12137. #endif /* WOLFSSL_EKU_OID */
  12138. #endif /* WOLFSSL_CERT_EXT */
  12139. ret = 0;
  12140. do {
  12141. #if defined(WOLFSSL_ASYNC_CRYPT)
  12142. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12143. #endif
  12144. if (ret >= 0) {
  12145. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  12146. }
  12147. } while (ret == WC_PENDING_E);
  12148. if (ret < 0) {
  12149. ERROR_OUT(-7827, exit_rsa);
  12150. }
  12151. certSz = ret;
  12152. #ifdef WOLFSSL_TEST_CERT
  12153. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12154. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12155. if (ret != 0) {
  12156. FreeDecodedCert(decode);
  12157. ERROR_OUT(-7828, exit_rsa);
  12158. }
  12159. FreeDecodedCert(decode);
  12160. #endif
  12161. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  12162. CERT_TYPE, -5578);
  12163. if (ret != 0) {
  12164. goto exit_rsa;
  12165. }
  12166. /* Setup Certificate */
  12167. if (wc_InitCert(myCert)) {
  12168. ERROR_OUT(-7829, exit_rsa);
  12169. }
  12170. #ifdef WOLFSSL_ALT_NAMES
  12171. /* Get CA Cert for testing */
  12172. #ifdef USE_CERT_BUFFERS_1024
  12173. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  12174. bytes3 = sizeof_ca_cert_der_1024;
  12175. #elif defined(USE_CERT_BUFFERS_2048)
  12176. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  12177. bytes3 = sizeof_ca_cert_der_2048;
  12178. #else
  12179. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  12180. if (!file3) {
  12181. ERROR_OUT(-7830, exit_rsa);
  12182. }
  12183. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12184. XFCLOSE(file3);
  12185. #endif /* USE_CERT_BUFFERS */
  12186. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  12187. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  12188. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  12189. if (ret != 0) {
  12190. ERROR_OUT(-7831, exit_rsa);
  12191. }
  12192. #endif
  12193. /* get alt names from der */
  12194. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  12195. if (ret != 0) {
  12196. ERROR_OUT(-7832, exit_rsa);
  12197. }
  12198. /* get dates from der */
  12199. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  12200. if (ret != 0) {
  12201. ERROR_OUT(-7833, exit_rsa);
  12202. }
  12203. #ifndef NO_ASN_TIME
  12204. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  12205. if (ret < 0) {
  12206. ERROR_OUT(-7834, exit_rsa);
  12207. }
  12208. #endif
  12209. #endif /* WOLFSSL_ALT_NAMES */
  12210. /* Get CA Key */
  12211. #ifdef USE_CERT_BUFFERS_1024
  12212. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12213. bytes3 = sizeof_ca_key_der_1024;
  12214. #elif defined(USE_CERT_BUFFERS_2048)
  12215. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12216. bytes3 = sizeof_ca_key_der_2048;
  12217. #else
  12218. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12219. if (!file3) {
  12220. ERROR_OUT(-7835, exit_rsa);
  12221. }
  12222. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12223. XFCLOSE(file3);
  12224. #endif /* USE_CERT_BUFFERS */
  12225. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12226. if (ret != 0) {
  12227. ERROR_OUT(-7836, exit_rsa);
  12228. }
  12229. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12230. if (ret != 0) {
  12231. ERROR_OUT(-7837, exit_rsa);
  12232. }
  12233. #ifndef NO_SHA256
  12234. myCert->sigType = CTC_SHA256wRSA;
  12235. #else
  12236. myCert->sigType = CTC_SHAwRSA;
  12237. #endif
  12238. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12239. #ifdef WOLFSSL_CERT_EXT
  12240. /* add Policies */
  12241. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12242. CTC_MAX_CERTPOL_SZ);
  12243. myCert->certPoliciesNb =1;
  12244. /* add SKID from the Public Key */
  12245. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  12246. ERROR_OUT(-7838, exit_rsa);
  12247. }
  12248. /* add AKID from the CA certificate */
  12249. #if defined(USE_CERT_BUFFERS_2048)
  12250. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12251. sizeof_ca_cert_der_2048);
  12252. #elif defined(USE_CERT_BUFFERS_1024)
  12253. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12254. sizeof_ca_cert_der_1024);
  12255. #else
  12256. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12257. #endif
  12258. if (ret != 0) {
  12259. ERROR_OUT(-7839, exit_rsa);
  12260. }
  12261. /* add Key Usage */
  12262. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  12263. ERROR_OUT(-7840, exit_rsa);
  12264. }
  12265. #endif /* WOLFSSL_CERT_EXT */
  12266. #if defined(USE_CERT_BUFFERS_2048)
  12267. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12268. sizeof_ca_cert_der_2048);
  12269. #elif defined(USE_CERT_BUFFERS_1024)
  12270. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12271. sizeof_ca_cert_der_1024);
  12272. #else
  12273. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12274. #endif
  12275. if (ret < 0) {
  12276. ERROR_OUT(-7841, exit_rsa);
  12277. }
  12278. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  12279. if (certSz < 0) {
  12280. ERROR_OUT(-7842, exit_rsa);
  12281. }
  12282. ret = 0;
  12283. do {
  12284. #if defined(WOLFSSL_ASYNC_CRYPT)
  12285. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12286. #endif
  12287. if (ret >= 0) {
  12288. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  12289. caKey, NULL, rng);
  12290. }
  12291. } while (ret == WC_PENDING_E);
  12292. if (ret < 0) {
  12293. ERROR_OUT(-7843, exit_rsa);
  12294. }
  12295. certSz = ret;
  12296. #ifdef WOLFSSL_TEST_CERT
  12297. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12298. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12299. if (ret != 0) {
  12300. FreeDecodedCert(decode);
  12301. ERROR_OUT(-7844, exit_rsa);
  12302. }
  12303. FreeDecodedCert(decode);
  12304. #endif
  12305. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  12306. CERT_TYPE, -5598);
  12307. if (ret != 0) {
  12308. goto exit_rsa;
  12309. }
  12310. exit_rsa:
  12311. #ifdef WOLFSSL_SMALL_STACK
  12312. if (caKey != NULL) {
  12313. wc_FreeRsaKey(caKey);
  12314. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12315. }
  12316. #ifdef WOLFSSL_TEST_CERT
  12317. if (decode != NULL)
  12318. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12319. #endif
  12320. #else
  12321. wc_FreeRsaKey(caKey);
  12322. #endif
  12323. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12324. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12325. return ret;
  12326. }
  12327. #endif
  12328. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  12329. /* Make Cert / Sign example for ECC cert and RSA CA */
  12330. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  12331. {
  12332. #ifdef WOLFSSL_SMALL_STACK
  12333. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12334. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12335. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12336. #ifdef WOLFSSL_TEST_CERT
  12337. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12338. #endif
  12339. #else
  12340. RsaKey caKey[1];
  12341. ecc_key caEccKey[1];
  12342. ecc_key caEccKeyPub[1];
  12343. #ifdef WOLFSSL_TEST_CERT
  12344. DecodedCert decode[1];
  12345. #endif
  12346. #endif
  12347. byte* der = NULL;
  12348. Cert* myCert = NULL;
  12349. int certSz;
  12350. size_t bytes3;
  12351. word32 idx3 = 0;
  12352. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  12353. || !defined(USE_CERT_BUFFERS_256)
  12354. XFILE file3;
  12355. #endif
  12356. int ret;
  12357. #ifdef WOLFSSL_SMALL_STACK
  12358. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  12359. #ifdef WOLFSSL_TEST_CERT
  12360. || (decode == NULL)
  12361. #endif
  12362. )
  12363. ERROR_OUT(MEMORY_E, exit_rsa);
  12364. #endif
  12365. XMEMSET(caKey, 0, sizeof *caKey);
  12366. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  12367. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  12368. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12369. if (der == NULL) {
  12370. ERROR_OUT(-7850, exit_rsa);
  12371. }
  12372. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12373. if (myCert == NULL) {
  12374. ERROR_OUT(-7851, exit_rsa);
  12375. }
  12376. /* Get CA Key */
  12377. #ifdef USE_CERT_BUFFERS_1024
  12378. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12379. bytes3 = sizeof_ca_key_der_1024;
  12380. #elif defined(USE_CERT_BUFFERS_2048)
  12381. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12382. bytes3 = sizeof_ca_key_der_2048;
  12383. #else
  12384. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12385. if (!file3) {
  12386. ERROR_OUT(-7852, exit_rsa);
  12387. }
  12388. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12389. XFCLOSE(file3);
  12390. #endif /* USE_CERT_BUFFERS */
  12391. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12392. if (ret != 0) {
  12393. ERROR_OUT(-7853, exit_rsa);
  12394. }
  12395. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12396. if (ret != 0) {
  12397. ERROR_OUT(-7854, exit_rsa);
  12398. }
  12399. /* Get Cert Key */
  12400. #ifdef USE_CERT_BUFFERS_256
  12401. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  12402. bytes3 = sizeof_ecc_key_pub_der_256;
  12403. #else
  12404. file3 = XFOPEN(eccKeyPubFile, "rb");
  12405. if (!file3) {
  12406. ERROR_OUT(-7855, exit_rsa);
  12407. }
  12408. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12409. XFCLOSE(file3);
  12410. #endif
  12411. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  12412. if (ret != 0) {
  12413. ERROR_OUT(-7856, exit_rsa);
  12414. }
  12415. idx3 = 0;
  12416. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  12417. if (ret != 0) {
  12418. ERROR_OUT(-7857, exit_rsa);
  12419. }
  12420. /* Setup Certificate */
  12421. if (wc_InitCert(myCert)) {
  12422. ERROR_OUT(-7858, exit_rsa);
  12423. }
  12424. #ifndef NO_SHA256
  12425. myCert->sigType = CTC_SHA256wRSA;
  12426. #else
  12427. myCert->sigType = CTC_SHAwRSA;
  12428. #endif
  12429. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12430. #ifdef WOLFSSL_CERT_EXT
  12431. /* add Policies */
  12432. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  12433. CTC_MAX_CERTPOL_SZ);
  12434. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  12435. CTC_MAX_CERTPOL_SZ);
  12436. myCert->certPoliciesNb = 2;
  12437. /* add SKID from the Public Key */
  12438. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  12439. ERROR_OUT(-7859, exit_rsa);
  12440. }
  12441. /* add AKID from the CA certificate */
  12442. #if defined(USE_CERT_BUFFERS_2048)
  12443. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12444. sizeof_ca_cert_der_2048);
  12445. #elif defined(USE_CERT_BUFFERS_1024)
  12446. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12447. sizeof_ca_cert_der_1024);
  12448. #else
  12449. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12450. #endif
  12451. if (ret != 0) {
  12452. ERROR_OUT(-7860, exit_rsa);
  12453. }
  12454. /* add Key Usage */
  12455. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  12456. ERROR_OUT(-7861, exit_rsa);
  12457. }
  12458. #endif /* WOLFSSL_CERT_EXT */
  12459. #if defined(USE_CERT_BUFFERS_2048)
  12460. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12461. sizeof_ca_cert_der_2048);
  12462. #elif defined(USE_CERT_BUFFERS_1024)
  12463. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12464. sizeof_ca_cert_der_1024);
  12465. #else
  12466. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12467. #endif
  12468. if (ret < 0) {
  12469. ERROR_OUT(-7862, exit_rsa);
  12470. }
  12471. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  12472. if (certSz < 0) {
  12473. ERROR_OUT(-7863, exit_rsa);
  12474. }
  12475. ret = 0;
  12476. do {
  12477. #if defined(WOLFSSL_ASYNC_CRYPT)
  12478. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12479. #endif
  12480. if (ret >= 0) {
  12481. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  12482. FOURK_BUF, caKey, NULL, rng);
  12483. }
  12484. } while (ret == WC_PENDING_E);
  12485. if (ret < 0) {
  12486. ERROR_OUT(-7864, exit_rsa);
  12487. }
  12488. certSz = ret;
  12489. #ifdef WOLFSSL_TEST_CERT
  12490. InitDecodedCert(decode, der, certSz, 0);
  12491. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12492. if (ret != 0) {
  12493. FreeDecodedCert(decode);
  12494. ERROR_OUT(-7865, exit_rsa);
  12495. }
  12496. FreeDecodedCert(decode);
  12497. #endif
  12498. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  12499. CERT_TYPE, -5616);
  12500. if (ret != 0) {
  12501. goto exit_rsa;
  12502. }
  12503. exit_rsa:
  12504. #ifdef WOLFSSL_SMALL_STACK
  12505. if (caKey != NULL) {
  12506. wc_FreeRsaKey(caKey);
  12507. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12508. }
  12509. if (caEccKey != NULL) {
  12510. wc_ecc_free(caEccKey);
  12511. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12512. }
  12513. if (caEccKeyPub != NULL) {
  12514. wc_ecc_free(caEccKeyPub);
  12515. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12516. }
  12517. #ifdef WOLFSSL_TEST_CERT
  12518. if (decode != NULL)
  12519. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12520. #endif
  12521. #else
  12522. wc_FreeRsaKey(caKey);
  12523. wc_ecc_free(caEccKey);
  12524. wc_ecc_free(caEccKeyPub);
  12525. #endif
  12526. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12527. myCert = NULL;
  12528. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12529. der = NULL;
  12530. if (ret >= 0)
  12531. ret = 0;
  12532. return ret;
  12533. }
  12534. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  12535. #ifdef WOLFSSL_KEY_GEN
  12536. static int rsa_keygen_test(WC_RNG* rng)
  12537. {
  12538. #ifdef WOLFSSL_SMALL_STACK
  12539. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12540. #else
  12541. RsaKey genKey[1];
  12542. #endif
  12543. int ret;
  12544. byte* der = NULL;
  12545. word32 idx = 0;
  12546. int derSz = 0;
  12547. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  12548. int keySz = 1024;
  12549. #else
  12550. int keySz = 2048;
  12551. #endif
  12552. #ifdef WOLFSSL_SMALL_STACK
  12553. if (! genKey)
  12554. ERROR_OUT(MEMORY_E, exit_rsa);
  12555. #endif
  12556. XMEMSET(genKey, 0, sizeof *genKey);
  12557. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  12558. if (ret != 0) {
  12559. ERROR_OUT(-7870, exit_rsa);
  12560. }
  12561. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  12562. #if defined(WOLFSSL_ASYNC_CRYPT)
  12563. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  12564. #endif
  12565. if (ret != 0) {
  12566. ERROR_OUT(-7871, exit_rsa);
  12567. }
  12568. TEST_SLEEP();
  12569. /* If not using old FIPS, or not using FAST or USER RSA... */
  12570. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  12571. (!defined(HAVE_FIPS) || \
  12572. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  12573. !defined(HAVE_SELFTEST) && !defined(HAVE_INTEL_QA)
  12574. ret = wc_CheckRsaKey(genKey);
  12575. if (ret != 0) {
  12576. ERROR_OUT(-7872, exit_rsa);
  12577. }
  12578. #endif
  12579. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12580. if (der == NULL) {
  12581. ERROR_OUT(-7873, exit_rsa);
  12582. }
  12583. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  12584. if (derSz < 0) {
  12585. ERROR_OUT(-7874, exit_rsa);
  12586. }
  12587. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  12588. PRIVATEKEY_TYPE, -5555);
  12589. if (ret != 0) {
  12590. goto exit_rsa;
  12591. }
  12592. wc_FreeRsaKey(genKey);
  12593. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  12594. if (ret != 0) {
  12595. ERROR_OUT(-7875, exit_rsa);
  12596. }
  12597. idx = 0;
  12598. #if !defined(WOLFSSL_CRYPTOCELL)
  12599. /* The private key part of the key gen pairs from cryptocell can't be exported */
  12600. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  12601. if (ret != 0) {
  12602. ERROR_OUT(-7876, exit_rsa);
  12603. }
  12604. #endif /* WOLFSSL_CRYPTOCELL */
  12605. exit_rsa:
  12606. #ifdef WOLFSSL_SMALL_STACK
  12607. if (genKey) {
  12608. wc_FreeRsaKey(genKey);
  12609. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12610. }
  12611. #else
  12612. wc_FreeRsaKey(genKey);
  12613. #endif
  12614. if (der != NULL) {
  12615. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12616. der = NULL;
  12617. }
  12618. return ret;
  12619. }
  12620. #endif
  12621. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  12622. {
  12623. int ret;
  12624. byte* tmp = NULL;
  12625. byte* der = NULL;
  12626. size_t bytes;
  12627. WC_RNG rng;
  12628. #ifdef WOLFSSL_SMALL_STACK
  12629. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12630. #else
  12631. RsaKey key[1];
  12632. #endif
  12633. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12634. #ifdef WOLFSSL_SMALL_STACK
  12635. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12636. #else
  12637. RsaKey keypub[1];
  12638. #endif
  12639. #endif
  12640. #if defined(HAVE_NTRU)
  12641. #ifdef WOLFSSL_SMALL_STACK
  12642. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12643. #else
  12644. RsaKey caKey[1];
  12645. #endif
  12646. #endif
  12647. #if !defined(NO_ASN) || !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  12648. || defined(WOLFSSL_PUBLIC_MP)
  12649. word32 idx = 0;
  12650. #endif
  12651. #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
  12652. !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  12653. const char inStr[] = TEST_STRING;
  12654. const word32 inLen = (word32)TEST_STRING_SZ;
  12655. const word32 outSz = RSA_TEST_BYTES;
  12656. const word32 plainSz = RSA_TEST_BYTES;
  12657. #endif
  12658. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  12659. byte* res;
  12660. #endif
  12661. #ifndef NO_SIG_WRAPPER
  12662. int modLen;
  12663. #endif
  12664. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12665. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  12666. !defined(NO_FILESYSTEM)
  12667. XFILE file;
  12668. XFILE file2;
  12669. #endif
  12670. #ifdef WOLFSSL_TEST_CERT
  12671. #ifdef WOLFSSL_SMALL_STACK
  12672. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12673. #else
  12674. DecodedCert cert[1];
  12675. #endif
  12676. #endif
  12677. #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
  12678. !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  12679. DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  12680. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12681. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12682. #endif
  12683. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  12684. if (in == NULL || out == NULL || plain == NULL)
  12685. ERROR_OUT(MEMORY_E, exit_rsa);
  12686. #endif
  12687. XMEMCPY(in, inStr, inLen);
  12688. #ifdef WOLFSSL_SMALL_STACK
  12689. if (key == NULL)
  12690. ERROR_OUT(MEMORY_E, exit_rsa);
  12691. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12692. if (keypub == NULL)
  12693. ERROR_OUT(MEMORY_E, exit_rsa);
  12694. #endif
  12695. #if defined(HAVE_NTRU)
  12696. if (caKey == NULL)
  12697. ERROR_OUT(MEMORY_E, exit_rsa);
  12698. #endif
  12699. #ifdef WOLFSSL_TEST_CERT
  12700. if (cert == NULL)
  12701. ERROR_OUT(MEMORY_E, exit_rsa);
  12702. #endif
  12703. #endif /* WOLFSSL_SMALL_STACK */
  12704. /* initialize stack structures */
  12705. XMEMSET(&rng, 0, sizeof(rng));
  12706. XMEMSET(key, 0, sizeof *key);
  12707. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12708. XMEMSET(keypub, 0, sizeof *keypub);
  12709. #endif
  12710. #if defined(HAVE_NTRU)
  12711. XMEMSET(caKey, 0, sizeof *caKey);
  12712. #endif
  12713. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12714. ret = rsa_decode_test(key);
  12715. if (ret != 0)
  12716. ERROR_OUT(ret, exit_rsa);
  12717. #endif
  12718. #ifdef USE_CERT_BUFFERS_1024
  12719. bytes = (size_t)sizeof_client_key_der_1024;
  12720. if (bytes < (size_t)sizeof_client_cert_der_1024)
  12721. bytes = (size_t)sizeof_client_cert_der_1024;
  12722. #elif defined(USE_CERT_BUFFERS_2048)
  12723. bytes = (size_t)sizeof_client_key_der_2048;
  12724. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12725. bytes = (size_t)sizeof_client_cert_der_2048;
  12726. #elif defined(USE_CERT_BUFFERS_3072)
  12727. bytes = (size_t)sizeof_client_key_der_3072;
  12728. if (bytes < (size_t)sizeof_client_cert_der_3072)
  12729. bytes = (size_t)sizeof_client_cert_der_3072;
  12730. #elif defined(USE_CERT_BUFFERS_4096)
  12731. bytes = (size_t)sizeof_client_key_der_4096;
  12732. if (bytes < (size_t)sizeof_client_cert_der_4096)
  12733. bytes = (size_t)sizeof_client_cert_der_4096;
  12734. #else
  12735. bytes = FOURK_BUF;
  12736. #endif
  12737. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12738. if (tmp == NULL)
  12739. ERROR_OUT(-7900, exit_rsa);
  12740. #ifdef USE_CERT_BUFFERS_1024
  12741. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  12742. #elif defined(USE_CERT_BUFFERS_2048)
  12743. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12744. #elif defined(USE_CERT_BUFFERS_3072)
  12745. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12746. #elif defined(USE_CERT_BUFFERS_4096)
  12747. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12748. #elif !defined(NO_FILESYSTEM)
  12749. file = XFOPEN(clientKey, "rb");
  12750. if (!file) {
  12751. err_sys("can't open ./certs/client-key.der, "
  12752. "Please run from wolfSSL home dir", -40);
  12753. ERROR_OUT(-7901, exit_rsa);
  12754. }
  12755. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12756. XFCLOSE(file);
  12757. #else
  12758. /* No key to use. */
  12759. ERROR_OUT(-7902, exit_rsa);
  12760. #endif /* USE_CERT_BUFFERS */
  12761. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  12762. if (ret != 0) {
  12763. ERROR_OUT(-7903, exit_rsa);
  12764. }
  12765. #ifndef NO_ASN
  12766. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12767. if (ret != 0) {
  12768. ERROR_OUT(-7904, exit_rsa);
  12769. }
  12770. #ifndef NO_SIG_WRAPPER
  12771. modLen = wc_RsaEncryptSize(key);
  12772. #endif
  12773. #else
  12774. #ifdef USE_CERT_BUFFERS_2048
  12775. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  12776. if (ret != 0) {
  12777. ERROR_OUT(-7905, exit_rsa);
  12778. }
  12779. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  12780. if (ret != 0) {
  12781. ERROR_OUT(-7906, exit_rsa);
  12782. }
  12783. #ifndef NO_SIG_WRAPPER
  12784. modLen = 2048;
  12785. #endif
  12786. #else
  12787. #error Not supported yet!
  12788. #endif
  12789. #endif
  12790. #ifndef WC_NO_RNG
  12791. #ifndef HAVE_FIPS
  12792. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12793. #else
  12794. ret = wc_InitRng(&rng);
  12795. #endif
  12796. if (ret != 0) {
  12797. ERROR_OUT(-7907, exit_rsa);
  12798. }
  12799. #endif
  12800. #ifndef NO_SIG_WRAPPER
  12801. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  12802. if (ret != 0)
  12803. goto exit_rsa;
  12804. #endif
  12805. #ifdef WC_RSA_NONBLOCK
  12806. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  12807. if (ret != 0)
  12808. goto exit_rsa;
  12809. #endif
  12810. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  12811. do {
  12812. #if defined(WOLFSSL_ASYNC_CRYPT)
  12813. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12814. #endif
  12815. if (ret >= 0) {
  12816. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  12817. }
  12818. } while (ret == WC_PENDING_E);
  12819. if (ret < 0) {
  12820. ERROR_OUT(-7908, exit_rsa);
  12821. }
  12822. TEST_SLEEP();
  12823. #ifdef WC_RSA_BLINDING
  12824. {
  12825. int tmpret = ret;
  12826. ret = wc_RsaSetRNG(key, &rng);
  12827. if (ret < 0) {
  12828. ERROR_OUT(-7909, exit_rsa);
  12829. }
  12830. ret = tmpret;
  12831. }
  12832. #endif
  12833. idx = (word32)ret; /* save off encrypted length */
  12834. do {
  12835. #if defined(WOLFSSL_ASYNC_CRYPT)
  12836. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12837. #endif
  12838. if (ret >= 0) {
  12839. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  12840. }
  12841. } while (ret == WC_PENDING_E);
  12842. if (ret < 0) {
  12843. ERROR_OUT(-7910, exit_rsa);
  12844. }
  12845. if (XMEMCMP(plain, in, inLen)) {
  12846. ERROR_OUT(-7911, exit_rsa);
  12847. }
  12848. TEST_SLEEP();
  12849. do {
  12850. #if defined(WOLFSSL_ASYNC_CRYPT)
  12851. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12852. #endif
  12853. if (ret >= 0) {
  12854. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  12855. }
  12856. } while (ret == WC_PENDING_E);
  12857. if (ret < 0) {
  12858. ERROR_OUT(-7912, exit_rsa);
  12859. }
  12860. if (ret != (int)inLen) {
  12861. ERROR_OUT(-7913, exit_rsa);
  12862. }
  12863. if (XMEMCMP(res, in, inLen)) {
  12864. ERROR_OUT(-7914, exit_rsa);
  12865. }
  12866. TEST_SLEEP();
  12867. do {
  12868. #if defined(WOLFSSL_ASYNC_CRYPT)
  12869. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12870. #endif
  12871. if (ret >= 0) {
  12872. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  12873. }
  12874. } while (ret == WC_PENDING_E);
  12875. if (ret < 0) {
  12876. ERROR_OUT(-7915, exit_rsa);
  12877. }
  12878. TEST_SLEEP();
  12879. #elif defined(WOLFSSL_PUBLIC_MP)
  12880. (void)outSz;
  12881. (void)inLen;
  12882. (void)res;
  12883. {
  12884. static byte signature_2048[] = {
  12885. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  12886. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  12887. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  12888. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  12889. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  12890. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  12891. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  12892. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  12893. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  12894. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  12895. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  12896. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  12897. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  12898. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  12899. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  12900. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  12901. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  12902. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  12903. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  12904. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  12905. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  12906. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  12907. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  12908. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  12909. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  12910. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  12911. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  12912. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  12913. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  12914. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  12915. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  12916. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  12917. };
  12918. ret = sizeof(signature_2048);
  12919. XMEMCPY(out, signature_2048, ret);
  12920. }
  12921. #endif
  12922. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  12923. idx = (word32)ret;
  12924. XMEMSET(plain, 0, plainSz);
  12925. do {
  12926. #if defined(WOLFSSL_ASYNC_CRYPT)
  12927. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12928. #endif
  12929. if (ret >= 0) {
  12930. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  12931. #if defined(WOLFSSL_CRYPTOCELL)
  12932. /*
  12933. Cryptocell requires the input data and signature byte array to verify.
  12934. first argument must be the input data
  12935. second argument must be the length of input data
  12936. third argument must be the signature byte array or the output from
  12937. wc_RsaSSL_Sign()
  12938. fourth argument must be the length of the signature byte array
  12939. */
  12940. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  12941. #else
  12942. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  12943. #endif /* WOLFSSL_CRYPTOCELL */
  12944. #else
  12945. byte* dec = NULL;
  12946. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  12947. if (ret > 0) {
  12948. XMEMCPY(plain, dec, ret);
  12949. }
  12950. #endif
  12951. }
  12952. } while (ret == WC_PENDING_E);
  12953. if (ret < 0) {
  12954. ERROR_OUT(-7916, exit_rsa);
  12955. }
  12956. if (XMEMCMP(plain, in, (size_t)ret)) {
  12957. ERROR_OUT(-7917, exit_rsa);
  12958. }
  12959. TEST_SLEEP();
  12960. #endif
  12961. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12962. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  12963. /* OAEP padding testing */
  12964. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  12965. (!defined(HAVE_FIPS) || \
  12966. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  12967. #ifndef NO_SHA
  12968. XMEMSET(plain, 0, plainSz);
  12969. do {
  12970. #if defined(WOLFSSL_ASYNC_CRYPT)
  12971. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12972. #endif
  12973. if (ret >= 0) {
  12974. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  12975. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  12976. }
  12977. } while (ret == WC_PENDING_E);
  12978. if (ret < 0) {
  12979. ERROR_OUT(-7918, exit_rsa);
  12980. }
  12981. TEST_SLEEP();
  12982. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12983. idx = (word32)ret;
  12984. do {
  12985. #if defined(WOLFSSL_ASYNC_CRYPT)
  12986. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12987. #endif
  12988. if (ret >= 0) {
  12989. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  12990. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  12991. }
  12992. } while (ret == WC_PENDING_E);
  12993. if (ret < 0) {
  12994. ERROR_OUT(-7919, exit_rsa);
  12995. }
  12996. if (XMEMCMP(plain, in, inLen)) {
  12997. ERROR_OUT(-7920, exit_rsa);
  12998. }
  12999. TEST_SLEEP();
  13000. #endif /* NO_SHA */
  13001. #endif
  13002. #ifndef NO_SHA256
  13003. XMEMSET(plain, 0, plainSz);
  13004. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13005. do {
  13006. #if defined(WOLFSSL_ASYNC_CRYPT)
  13007. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13008. #endif
  13009. if (ret >= 0) {
  13010. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13011. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13012. }
  13013. } while (ret == WC_PENDING_E);
  13014. if (ret < 0) {
  13015. ERROR_OUT(-7921, exit_rsa);
  13016. }
  13017. TEST_SLEEP();
  13018. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13019. idx = (word32)ret;
  13020. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13021. do {
  13022. #if defined(WOLFSSL_ASYNC_CRYPT)
  13023. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13024. #endif
  13025. if (ret >= 0) {
  13026. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13027. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13028. }
  13029. } while (ret == WC_PENDING_E);
  13030. if (ret < 0) {
  13031. ERROR_OUT(-7922, exit_rsa);
  13032. }
  13033. if (XMEMCMP(plain, in, inLen)) {
  13034. ERROR_OUT(-7923, exit_rsa);
  13035. }
  13036. TEST_SLEEP();
  13037. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13038. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13039. do {
  13040. #if defined(WOLFSSL_ASYNC_CRYPT)
  13041. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13042. #endif
  13043. if (ret >= 0) {
  13044. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  13045. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13046. }
  13047. } while (ret == WC_PENDING_E);
  13048. if (ret < 0) {
  13049. ERROR_OUT(-7924, exit_rsa);
  13050. }
  13051. if (ret != (int)inLen) {
  13052. ERROR_OUT(-7925, exit_rsa);
  13053. }
  13054. if (XMEMCMP(res, in, inLen)) {
  13055. ERROR_OUT(-7926, exit_rsa);
  13056. }
  13057. TEST_SLEEP();
  13058. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13059. /* check fails if not using the same optional label */
  13060. XMEMSET(plain, 0, plainSz);
  13061. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13062. do {
  13063. #if defined(WOLFSSL_ASYNC_CRYPT)
  13064. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13065. #endif
  13066. if (ret >= 0) {
  13067. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13068. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13069. }
  13070. } while (ret == WC_PENDING_E);
  13071. if (ret < 0) {
  13072. ERROR_OUT(-7927, exit_rsa);
  13073. }
  13074. TEST_SLEEP();
  13075. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13076. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13077. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13078. !defined(WOLFSSL_CRYPTOCELL)
  13079. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  13080. idx = (word32)ret;
  13081. do {
  13082. #if defined(WOLFSSL_ASYNC_CRYPT)
  13083. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13084. #endif
  13085. if (ret >= 0) {
  13086. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13087. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13088. }
  13089. } while (ret == WC_PENDING_E);
  13090. if (ret > 0) { /* in this case decrypt should fail */
  13091. ERROR_OUT(-7928, exit_rsa);
  13092. }
  13093. ret = 0;
  13094. TEST_SLEEP();
  13095. #endif /* !HAVE_CAVIUM */
  13096. /* check using optional label with encrypt/decrypt */
  13097. XMEMSET(plain, 0, plainSz);
  13098. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13099. do {
  13100. #if defined(WOLFSSL_ASYNC_CRYPT)
  13101. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13102. #endif
  13103. if (ret >= 0) {
  13104. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13105. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13106. }
  13107. } while (ret == WC_PENDING_E);
  13108. if (ret < 0) {
  13109. ERROR_OUT(-7929, exit_rsa);
  13110. }
  13111. TEST_SLEEP();
  13112. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13113. idx = (word32)ret;
  13114. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13115. do {
  13116. #if defined(WOLFSSL_ASYNC_CRYPT)
  13117. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13118. #endif
  13119. if (ret >= 0) {
  13120. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13121. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13122. }
  13123. } while (ret == WC_PENDING_E);
  13124. if (ret < 0) {
  13125. ERROR_OUT(-7930, exit_rsa);
  13126. }
  13127. if (XMEMCMP(plain, in, inLen)) {
  13128. ERROR_OUT(-7931, exit_rsa);
  13129. }
  13130. TEST_SLEEP();
  13131. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13132. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13133. #ifndef NO_SHA
  13134. /* check fail using mismatch hash algorithms */
  13135. XMEMSET(plain, 0, plainSz);
  13136. do {
  13137. #if defined(WOLFSSL_ASYNC_CRYPT)
  13138. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13139. #endif
  13140. if (ret >= 0) {
  13141. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13142. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  13143. }
  13144. } while (ret == WC_PENDING_E);
  13145. if (ret < 0) {
  13146. ERROR_OUT(-7932, exit_rsa);
  13147. }
  13148. TEST_SLEEP();
  13149. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13150. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13151. !defined(WOLFSSL_CRYPTOCELL)
  13152. idx = (word32)ret;
  13153. do {
  13154. #if defined(WOLFSSL_ASYNC_CRYPT)
  13155. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13156. #endif
  13157. if (ret >= 0) {
  13158. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13159. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  13160. in, inLen);
  13161. }
  13162. } while (ret == WC_PENDING_E);
  13163. if (ret > 0) { /* should fail */
  13164. ERROR_OUT(-7933, exit_rsa);
  13165. }
  13166. ret = 0;
  13167. TEST_SLEEP();
  13168. #endif /* !HAVE_CAVIUM */
  13169. #endif /* NO_SHA */
  13170. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13171. #endif /* NO_SHA256 */
  13172. #ifdef WOLFSSL_SHA512
  13173. /* Check valid RSA key size is used while using hash length of SHA512
  13174. If key size is less than (hash length * 2) + 2 then is invalid use
  13175. and test, since OAEP padding requires this.
  13176. BAD_FUNC_ARG is returned when this case is not met */
  13177. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  13178. XMEMSET(plain, 0, plainSz);
  13179. do {
  13180. #if defined(WOLFSSL_ASYNC_CRYPT)
  13181. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13182. #endif
  13183. if (ret >= 0) {
  13184. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13185. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13186. }
  13187. } while (ret == WC_PENDING_E);
  13188. if (ret < 0) {
  13189. ERROR_OUT(-7934, exit_rsa);
  13190. }
  13191. TEST_SLEEP();
  13192. idx = ret;
  13193. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13194. do {
  13195. #if defined(WOLFSSL_ASYNC_CRYPT)
  13196. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13197. #endif
  13198. if (ret >= 0) {
  13199. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13200. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13201. }
  13202. } while (ret == WC_PENDING_E);
  13203. if (ret < 0) {
  13204. ERROR_OUT(-7935, exit_rsa);
  13205. }
  13206. if (XMEMCMP(plain, in, inLen)) {
  13207. ERROR_OUT(-7936, exit_rsa);
  13208. }
  13209. TEST_SLEEP();
  13210. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13211. }
  13212. #endif /* WOLFSSL_SHA512 */
  13213. /* check using pkcsv15 padding with _ex API */
  13214. XMEMSET(plain, 0, plainSz);
  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_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13221. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13222. }
  13223. } while (ret == WC_PENDING_E);
  13224. if (ret < 0) {
  13225. ERROR_OUT(-7937, exit_rsa);
  13226. }
  13227. TEST_SLEEP();
  13228. idx = (word32)ret;
  13229. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13230. do {
  13231. #if defined(WOLFSSL_ASYNC_CRYPT)
  13232. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13233. #endif
  13234. if (ret >= 0) {
  13235. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13236. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13237. }
  13238. } while (ret == WC_PENDING_E);
  13239. if (ret < 0) {
  13240. ERROR_OUT(-7938, exit_rsa);
  13241. }
  13242. if (XMEMCMP(plain, in, inLen)) {
  13243. ERROR_OUT(-7939, exit_rsa);
  13244. }
  13245. TEST_SLEEP();
  13246. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13247. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  13248. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  13249. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13250. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  13251. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13252. ret = rsa_export_key_test(key);
  13253. if (ret != 0)
  13254. return ret;
  13255. #endif
  13256. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13257. ret = rsa_flatten_test(key);
  13258. if (ret != 0)
  13259. return ret;
  13260. #endif
  13261. #if defined(WOLFSSL_MDK_ARM)
  13262. #define sizeof(s) XSTRLEN((char *)(s))
  13263. #endif
  13264. #ifdef USE_CERT_BUFFERS_1024
  13265. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  13266. bytes = (size_t)sizeof_client_cert_der_1024;
  13267. #elif defined(USE_CERT_BUFFERS_2048)
  13268. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  13269. bytes = (size_t)sizeof_client_cert_der_2048;
  13270. #elif defined(USE_CERT_BUFFERS_3072)
  13271. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  13272. bytes = (size_t)sizeof_client_cert_der_3072;
  13273. #elif defined(USE_CERT_BUFFERS_4096)
  13274. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  13275. bytes = (size_t)sizeof_client_cert_der_4096;
  13276. #elif !defined(NO_FILESYSTEM)
  13277. file2 = XFOPEN(clientCert, "rb");
  13278. if (!file2) {
  13279. ERROR_OUT(-7940, exit_rsa);
  13280. }
  13281. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  13282. XFCLOSE(file2);
  13283. #else
  13284. /* No certificate to use. */
  13285. ERROR_OUT(-7941, exit_rsa);
  13286. #endif
  13287. #ifdef sizeof
  13288. #undef sizeof
  13289. #endif
  13290. #ifdef WOLFSSL_TEST_CERT
  13291. InitDecodedCert(cert, tmp, (word32)bytes, 0);
  13292. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, 0);
  13293. if (ret != 0) {
  13294. FreeDecodedCert(cert);
  13295. ERROR_OUT(-7942, exit_rsa);
  13296. }
  13297. FreeDecodedCert(cert);
  13298. #else
  13299. (void)bytes;
  13300. #endif
  13301. #ifdef WOLFSSL_CERT_EXT
  13302. #ifdef USE_CERT_BUFFERS_1024
  13303. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  13304. bytes = sizeof_client_keypub_der_1024;
  13305. #elif defined(USE_CERT_BUFFERS_2048)
  13306. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  13307. bytes = sizeof_client_keypub_der_2048;
  13308. #elif defined(USE_CERT_BUFFERS_3072)
  13309. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  13310. bytes = sizeof_client_keypub_der_3072;
  13311. #elif defined(USE_CERT_BUFFERS_4096)
  13312. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  13313. bytes = sizeof_client_keypub_der_4096;
  13314. #else
  13315. file = XFOPEN(clientKeyPub, "rb");
  13316. if (!file) {
  13317. err_sys("can't open ./certs/client-keyPub.der, "
  13318. "Please run from wolfSSL home dir", -40);
  13319. ERROR_OUT(-7943, exit_rsa);
  13320. }
  13321. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13322. XFCLOSE(file);
  13323. #endif /* USE_CERT_BUFFERS */
  13324. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  13325. if (ret != 0) {
  13326. ERROR_OUT(-7944, exit_rsa);
  13327. }
  13328. idx = 0;
  13329. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  13330. if (ret != 0) {
  13331. ERROR_OUT(-7945, exit_rsa);
  13332. }
  13333. #endif /* WOLFSSL_CERT_EXT */
  13334. #ifdef WOLFSSL_KEY_GEN
  13335. ret = rsa_keygen_test(&rng);
  13336. if (ret != 0)
  13337. goto exit_rsa;
  13338. #endif
  13339. #ifdef WOLFSSL_CERT_GEN
  13340. /* Make Cert / Sign example for RSA cert and RSA CA */
  13341. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  13342. if (ret != 0)
  13343. goto exit_rsa;
  13344. #if !defined(NO_RSA) && defined(HAVE_ECC)
  13345. ret = rsa_ecc_certgen_test(&rng, tmp);
  13346. if (ret != 0)
  13347. goto exit_rsa;
  13348. #endif
  13349. #ifdef HAVE_NTRU
  13350. {
  13351. Cert myCert;
  13352. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13353. XFILE caFile;
  13354. #endif
  13355. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13356. XFILE ntruPrivFile;
  13357. #endif
  13358. int certSz;
  13359. word32 idx3 = 0;
  13360. #ifdef WOLFSSL_TEST_CERT
  13361. DecodedCert decode;
  13362. #endif
  13363. byte public_key[557]; /* sized for EES401EP2 */
  13364. word16 public_key_len; /* no. of octets in public key */
  13365. byte private_key[607]; /* sized for EES401EP2 */
  13366. word16 private_key_len; /* no. of octets in private key */
  13367. DRBG_HANDLE drbg;
  13368. static uint8_t const pers_str[] = {
  13369. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  13370. };
  13371. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  13372. sizeof(pers_str), GetEntropy, &drbg);
  13373. if (rc != DRBG_OK) {
  13374. ERROR_OUT(-7946, exit_rsa);
  13375. }
  13376. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13377. &public_key_len, NULL,
  13378. &private_key_len, NULL);
  13379. if (rc != NTRU_OK) {
  13380. ERROR_OUT(-7947, exit_rsa);
  13381. }
  13382. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13383. &public_key_len, public_key,
  13384. &private_key_len, private_key);
  13385. if (rc != NTRU_OK) {
  13386. ERROR_OUT(-7948, exit_rsa);
  13387. }
  13388. rc = ntru_crypto_drbg_uninstantiate(drbg);
  13389. if (rc != NTRU_OK) {
  13390. ERROR_OUT(-7949, exit_rsa);
  13391. }
  13392. #ifdef USE_CERT_BUFFERS_1024
  13393. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13394. bytes = sizeof_ca_key_der_1024;
  13395. #elif defined(USE_CERT_BUFFERS_2048)
  13396. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13397. bytes = sizeof_ca_key_der_2048;
  13398. #else
  13399. caFile = XFOPEN(rsaCaKeyFile, "rb");
  13400. if (!caFile) {
  13401. ERROR_OUT(-7950, exit_rsa);
  13402. }
  13403. bytes = XFREAD(tmp, 1, FOURK_BUF, caFile);
  13404. XFCLOSE(caFile);
  13405. #endif /* USE_CERT_BUFFERS */
  13406. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13407. if (ret != 0) {
  13408. ERROR_OUT(-7951, exit_rsa);
  13409. }
  13410. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes);
  13411. if (ret != 0) {
  13412. ERROR_OUT(-7952, exit_rsa);
  13413. }
  13414. if (wc_InitCert(&myCert)) {
  13415. ERROR_OUT(-7953, exit_rsa);
  13416. }
  13417. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  13418. myCert.daysValid = 1000;
  13419. #ifdef WOLFSSL_CERT_EXT
  13420. /* add SKID from the Public Key */
  13421. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  13422. public_key_len) != 0) {
  13423. ERROR_OUT(-7954, exit_rsa);
  13424. }
  13425. /* add AKID from the CA certificate */
  13426. #if defined(USE_CERT_BUFFERS_2048)
  13427. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  13428. sizeof_ca_cert_der_2048);
  13429. #elif defined(USE_CERT_BUFFERS_1024)
  13430. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  13431. sizeof_ca_cert_der_1024);
  13432. #else
  13433. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  13434. #endif
  13435. if (ret != 0) {
  13436. ERROR_OUT(-7955, exit_rsa);
  13437. }
  13438. /* add Key Usage */
  13439. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  13440. ERROR_OUT(-7956, exit_rsa);
  13441. }
  13442. #endif /* WOLFSSL_CERT_EXT */
  13443. #if defined(USE_CERT_BUFFERS_2048)
  13444. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  13445. sizeof_ca_cert_der_2048);
  13446. #elif defined(USE_CERT_BUFFERS_1024)
  13447. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  13448. sizeof_ca_cert_der_1024);
  13449. #else
  13450. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  13451. #endif
  13452. if (ret < 0) {
  13453. ERROR_OUT(-7957, exit_rsa);
  13454. }
  13455. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13456. if (der == NULL) {
  13457. ERROR_OUT(-7958, exit_rsa);
  13458. }
  13459. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  13460. public_key_len, &rng);
  13461. if (certSz < 0) {
  13462. ERROR_OUT(-7959, exit_rsa);
  13463. }
  13464. ret = 0;
  13465. do {
  13466. #if defined(WOLFSSL_ASYNC_CRYPT)
  13467. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13468. #endif
  13469. if (ret >= 0) {
  13470. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  13471. caKey, NULL, &rng);
  13472. }
  13473. } while (ret == WC_PENDING_E);
  13474. wc_FreeRsaKey(caKey);
  13475. if (ret < 0) {
  13476. ERROR_OUT(-7960, exit_rsa);
  13477. }
  13478. certSz = ret;
  13479. #ifdef WOLFSSL_TEST_CERT
  13480. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  13481. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  13482. if (ret != 0) {
  13483. FreeDecodedCert(&decode);
  13484. ERROR_OUT(-7961, exit_rsa);
  13485. }
  13486. FreeDecodedCert(&decode);
  13487. #endif
  13488. ret = SaveDerAndPem(der, certSz, "./ntru-cert.der", "./ntru-cert.pem",
  13489. CERT_TYPE, -5637);
  13490. if (ret != 0) {
  13491. goto exit_rsa;
  13492. }
  13493. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13494. ntruPrivFile = XFOPEN("./ntru-key.raw", "wb");
  13495. if (!ntruPrivFile) {
  13496. ERROR_OUT(-7962, exit_rsa);
  13497. }
  13498. ret = (int)XFWRITE(private_key, 1, private_key_len, ntruPrivFile);
  13499. XFCLOSE(ntruPrivFile);
  13500. if (ret != private_key_len) {
  13501. ERROR_OUT(-7963, exit_rsa);
  13502. }
  13503. #endif
  13504. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13505. der = NULL;
  13506. }
  13507. #endif /* HAVE_NTRU */
  13508. #ifdef WOLFSSL_CERT_REQ
  13509. {
  13510. Cert *req;
  13511. int derSz;
  13512. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13513. if (! req)
  13514. ERROR_OUT(MEMORY_E, exit_rsa);
  13515. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  13516. if (der == NULL) {
  13517. ERROR_OUT(-7964, exit_rsa);
  13518. }
  13519. if (wc_InitCert(req)) {
  13520. ERROR_OUT(-7965, exit_rsa);
  13521. }
  13522. req->version = 0;
  13523. req->isCA = 1;
  13524. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  13525. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  13526. #ifndef NO_SHA256
  13527. req->sigType = CTC_SHA256wRSA;
  13528. #else
  13529. req->sigType = CTC_SHAwRSA;
  13530. #endif
  13531. #ifdef WOLFSSL_CERT_EXT
  13532. /* add SKID from the Public Key */
  13533. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  13534. ERROR_OUT(-7966, exit_rsa);
  13535. }
  13536. /* add Key Usage */
  13537. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  13538. ERROR_OUT(-7967, exit_rsa);
  13539. }
  13540. /* add Extended Key Usage */
  13541. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  13542. "emailProtection,timeStamping,OCSPSigning") != 0) {
  13543. ERROR_OUT(-7968, exit_rsa);
  13544. }
  13545. #ifdef WOLFSSL_EKU_OID
  13546. {
  13547. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13548. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  13549. HEAP_HINT) != 0) {
  13550. ERROR_OUT(-7969, exit_rsa);
  13551. }
  13552. }
  13553. #endif /* WOLFSSL_EKU_OID */
  13554. #endif /* WOLFSSL_CERT_EXT */
  13555. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13556. if (derSz < 0) {
  13557. ERROR_OUT(-7970, exit_rsa);
  13558. }
  13559. #ifdef WOLFSSL_CERT_EXT
  13560. /* Try again with "any" flag set, will override all others */
  13561. if (wc_SetExtKeyUsage(req, "any") != 0) {
  13562. ERROR_OUT(-7971, exit_rsa);
  13563. }
  13564. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13565. if (derSz < 0) {
  13566. ERROR_OUT(-7972, exit_rsa);
  13567. }
  13568. #endif /* WOLFSSL_CERT_EXT */
  13569. ret = 0;
  13570. do {
  13571. #if defined(WOLFSSL_ASYNC_CRYPT)
  13572. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13573. #endif
  13574. if (ret >= 0) {
  13575. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  13576. key, NULL, &rng);
  13577. }
  13578. } while (ret == WC_PENDING_E);
  13579. if (ret < 0) {
  13580. ERROR_OUT(-7973, exit_rsa);
  13581. }
  13582. derSz = ret;
  13583. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  13584. CERTREQ_TYPE, -5650);
  13585. if (ret != 0) {
  13586. goto exit_rsa;
  13587. }
  13588. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  13589. if (derSz < 0) {
  13590. ERROR_OUT(-7974, exit_rsa);
  13591. }
  13592. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13593. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13594. der = NULL;
  13595. }
  13596. #endif /* WOLFSSL_CERT_REQ */
  13597. #endif /* WOLFSSL_CERT_GEN */
  13598. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  13599. /* Need to create known good signatures to test with this. */
  13600. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13601. ret = rsa_pss_test(&rng, key);
  13602. if (ret != 0)
  13603. goto exit_rsa;
  13604. #endif
  13605. #endif
  13606. #ifdef WOLFSSL_HAVE_SP_RSA
  13607. /* New key to be loaded in rsa_even_mod_test(). */
  13608. if (key != NULL)
  13609. wc_FreeRsaKey(key);
  13610. ret = rsa_even_mod_test(&rng, key);
  13611. #endif
  13612. exit_rsa:
  13613. #ifdef WOLFSSL_SMALL_STACK
  13614. if (key != NULL) {
  13615. wc_FreeRsaKey(key);
  13616. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13617. }
  13618. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13619. if (keypub != NULL) {
  13620. wc_FreeRsaKey(keypub);
  13621. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13622. }
  13623. #endif
  13624. #if defined(HAVE_NTRU)
  13625. if (caKey != NULL) {
  13626. wc_FreeRsaKey(caKey);
  13627. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13628. }
  13629. #endif
  13630. #ifdef WOLFSSL_TEST_CERT
  13631. if (cert != NULL)
  13632. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13633. #endif
  13634. #else
  13635. wc_FreeRsaKey(key);
  13636. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13637. wc_FreeRsaKey(keypub);
  13638. #endif
  13639. #if defined(HAVE_NTRU)
  13640. wc_FreeRsaKey(caKey);
  13641. #endif
  13642. #endif
  13643. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13644. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13645. wc_FreeRng(&rng);
  13646. FREE_VAR(in, HEAP_HINT);
  13647. FREE_VAR(out, HEAP_HINT);
  13648. FREE_VAR(plain, HEAP_HINT);
  13649. /* ret can be greater then 0 with certgen but all negative values should
  13650. * be returned and treated as an error */
  13651. if (ret >= 0) {
  13652. return 0;
  13653. }
  13654. else {
  13655. return ret;
  13656. }
  13657. }
  13658. #endif /* !NO_RSA */
  13659. #ifndef NO_DH
  13660. static int dh_fips_generate_test(WC_RNG *rng)
  13661. {
  13662. int ret = 0;
  13663. #ifdef WOLFSSL_SMALL_STACK
  13664. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  13665. #else
  13666. DhKey key[1];
  13667. #endif
  13668. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  13669. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  13670. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  13671. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  13672. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  13673. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  13674. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  13675. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  13676. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  13677. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  13678. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  13679. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  13680. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  13681. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  13682. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  13683. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  13684. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  13685. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  13686. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  13687. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  13688. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  13689. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  13690. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  13691. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  13692. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  13693. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  13694. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  13695. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  13696. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  13697. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  13698. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  13699. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  13700. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  13701. };
  13702. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  13703. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  13704. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  13705. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  13706. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  13707. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  13708. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  13709. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  13710. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  13711. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  13712. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  13713. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  13714. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  13715. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  13716. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  13717. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  13718. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  13719. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  13720. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  13721. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  13722. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  13723. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  13724. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  13725. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  13726. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  13727. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  13728. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  13729. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  13730. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  13731. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  13732. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  13733. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  13734. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  13735. };
  13736. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  13737. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  13738. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  13739. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  13740. 0x40, 0x52, 0xed, 0x41
  13741. };
  13742. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  13743. 0x00,
  13744. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  13745. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  13746. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  13747. 0x40, 0x52, 0xed, 0x41
  13748. };
  13749. byte priv[256];
  13750. byte pub[256];
  13751. word32 privSz = sizeof(priv);
  13752. word32 pubSz = sizeof(pub);
  13753. #ifdef WOLFSSL_SMALL_STACK
  13754. if (key == NULL)
  13755. ERROR_OUT(MEMORY_E, exit_gen_test);
  13756. #endif
  13757. /* Parameter Validation testing. */
  13758. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  13759. if (ret != BAD_FUNC_ARG)
  13760. ERROR_OUT(-7980, exit_gen_test);
  13761. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  13762. if (ret != BAD_FUNC_ARG)
  13763. ERROR_OUT(-7981, exit_gen_test);
  13764. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  13765. if (ret != BAD_FUNC_ARG)
  13766. ERROR_OUT(-7982, exit_gen_test);
  13767. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  13768. if (ret != BAD_FUNC_ARG)
  13769. ERROR_OUT(-7983, exit_gen_test);
  13770. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  13771. if (ret != BAD_FUNC_ARG)
  13772. ERROR_OUT(-7984, exit_gen_test);
  13773. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  13774. if (ret != BAD_FUNC_ARG)
  13775. ERROR_OUT(-7985, exit_gen_test);
  13776. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13777. if (ret != 0)
  13778. ERROR_OUT(-7986, exit_gen_test);
  13779. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  13780. if (ret != 0) {
  13781. ERROR_OUT(-7987, exit_gen_test);
  13782. }
  13783. wc_FreeDhKey(key);
  13784. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13785. if (ret != 0)
  13786. ERROR_OUT(-7988, exit_gen_test);
  13787. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  13788. if (ret != 0) {
  13789. ERROR_OUT(-7989, exit_gen_test);
  13790. }
  13791. /* Use API. */
  13792. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  13793. #if defined(WOLFSSL_ASYNC_CRYPT)
  13794. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  13795. #endif
  13796. if (ret != 0) {
  13797. ERROR_OUT(-7990, exit_gen_test);
  13798. }
  13799. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  13800. if (ret != 0) {
  13801. ERROR_OUT(-7991, exit_gen_test);
  13802. }
  13803. wc_FreeDhKey(key);
  13804. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13805. if (ret != 0)
  13806. ERROR_OUT(-7992, exit_gen_test);
  13807. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  13808. if (ret != 0) {
  13809. ERROR_OUT(-7993, exit_gen_test);
  13810. }
  13811. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  13812. if (ret != 0) {
  13813. ERROR_OUT(-7994, exit_gen_test);
  13814. }
  13815. #ifndef HAVE_SELFTEST
  13816. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  13817. if (ret != 0) {
  13818. ERROR_OUT(-7995, exit_gen_test);
  13819. }
  13820. /* Taint the public key so the check fails. */
  13821. pub[0]++;
  13822. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  13823. if (ret != MP_CMP_E) {
  13824. ERROR_OUT(-7996, exit_gen_test);
  13825. }
  13826. #ifdef WOLFSSL_KEY_GEN
  13827. wc_FreeDhKey(key);
  13828. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13829. if (ret != 0)
  13830. ERROR_OUT(-7997, exit_gen_test);
  13831. ret = wc_DhGenerateParams(rng, 2048, key);
  13832. if (ret != 0) {
  13833. ERROR_OUT(-7998, exit_gen_test);
  13834. }
  13835. privSz = sizeof(priv);
  13836. pubSz = sizeof(pub);
  13837. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  13838. #if defined(WOLFSSL_ASYNC_CRYPT)
  13839. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  13840. #endif
  13841. if (ret != 0) {
  13842. ERROR_OUT(-7999, exit_gen_test);
  13843. }
  13844. #endif /* WOLFSSL_KEY_GEN */
  13845. #endif /* HAVE_SELFTEST */
  13846. ret = 0;
  13847. exit_gen_test:
  13848. #ifdef WOLFSSL_SMALL_STACK
  13849. if (key) {
  13850. wc_FreeDhKey(key);
  13851. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13852. }
  13853. #else
  13854. wc_FreeDhKey(key);
  13855. #endif
  13856. return ret;
  13857. }
  13858. static int dh_generate_test(WC_RNG *rng)
  13859. {
  13860. int ret = 0;
  13861. DhKey smallKey;
  13862. byte p[2] = { 0, 5 };
  13863. byte g[2] = { 0, 2 };
  13864. #if !defined(WOLFSSL_SP_MATH)
  13865. #ifdef WOLFSSL_DH_CONST
  13866. /* the table for constant DH lookup will round to the lowest byte size 21 */
  13867. byte priv[21];
  13868. byte pub[21];
  13869. #else
  13870. byte priv[2];
  13871. byte pub[2];
  13872. #endif
  13873. word32 privSz = sizeof(priv);
  13874. word32 pubSz = sizeof(pub);
  13875. #endif
  13876. ret = wc_InitDhKey_ex(&smallKey, HEAP_HINT, devId);
  13877. if (ret != 0)
  13878. return -8010;
  13879. /* Parameter Validation testing. */
  13880. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  13881. if (ret != BAD_FUNC_ARG)
  13882. return -8011;
  13883. wc_FreeDhKey(NULL);
  13884. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  13885. if (ret != BAD_FUNC_ARG) {
  13886. ERROR_OUT(-8012, exit_gen_test);
  13887. }
  13888. ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g));
  13889. if (ret != BAD_FUNC_ARG) {
  13890. ERROR_OUT(-8013, exit_gen_test);
  13891. }
  13892. ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g));
  13893. if (ret != BAD_FUNC_ARG) {
  13894. ERROR_OUT(-8014, exit_gen_test);
  13895. }
  13896. ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g));
  13897. if (ret != BAD_FUNC_ARG) {
  13898. ERROR_OUT(-8015, exit_gen_test);
  13899. }
  13900. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0);
  13901. if (ret != BAD_FUNC_ARG) {
  13902. ERROR_OUT(-8016, exit_gen_test);
  13903. }
  13904. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
  13905. if (ret != 0) {
  13906. ERROR_OUT(-8017, exit_gen_test);
  13907. }
  13908. #if !defined(WOLFSSL_SP_MATH)
  13909. /* Use API. */
  13910. ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz);
  13911. #if defined(WOLFSSL_ASYNC_CRYPT)
  13912. ret = wc_AsyncWait(ret, &smallKey.asyncDev, WC_ASYNC_FLAG_NONE);
  13913. #endif
  13914. if (ret != 0) {
  13915. ret = -8018;
  13916. }
  13917. #else
  13918. (void)rng;
  13919. ret = 0;
  13920. #endif
  13921. exit_gen_test:
  13922. wc_FreeDhKey(&smallKey);
  13923. return ret;
  13924. }
  13925. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  13926. typedef struct dh_pubvalue_test {
  13927. const byte* data;
  13928. word32 len;
  13929. } dh_pubvalue_test;
  13930. static int dh_test_check_pubvalue(void)
  13931. {
  13932. int ret;
  13933. word32 i;
  13934. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  13935. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  13936. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  13937. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  13938. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  13939. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  13940. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  13941. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  13942. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  13943. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  13944. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  13945. const dh_pubvalue_test dh_pubval_fail[] = {
  13946. { prime, sizeof(prime) },
  13947. { pubValZero, sizeof(pubValZero) },
  13948. { pubValZeroLong, sizeof(pubValZeroLong) },
  13949. { pubValOne, sizeof(pubValOne) },
  13950. { pubValOneLong, sizeof(pubValOneLong) },
  13951. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  13952. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  13953. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  13954. { pubValTooBig0, sizeof(pubValTooBig0) },
  13955. { pubValTooBig1, sizeof(pubValTooBig1) },
  13956. { pubValTooLong, sizeof(pubValTooLong) },
  13957. };
  13958. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  13959. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  13960. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  13961. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  13962. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  13963. const dh_pubvalue_test dh_pubval_pass[] = {
  13964. { pubValTwo, sizeof(pubValTwo) },
  13965. { pubValTwoLong, sizeof(pubValTwoLong) },
  13966. { pubValGood, sizeof(pubValGood) },
  13967. { pubValGoodLen, sizeof(pubValGoodLen) },
  13968. { pubValGoodLong, sizeof(pubValGoodLong) },
  13969. };
  13970. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  13971. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  13972. dh_pubval_fail[i].len);
  13973. if (ret != MP_VAL)
  13974. return -8020 - (int)i;
  13975. }
  13976. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  13977. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  13978. dh_pubval_pass[i].len);
  13979. if (ret != 0)
  13980. return -8030 - (int)i;
  13981. }
  13982. return 0;
  13983. }
  13984. #endif
  13985. #if defined(HAVE_FFDHE)
  13986. #ifdef HAVE_FFDHE_3072
  13987. #define FFDHE_KEY_SIZE (3072/8)
  13988. #else
  13989. #define FFDHE_KEY_SIZE (2048/8)
  13990. #endif
  13991. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  13992. {
  13993. int ret;
  13994. word32 privSz, pubSz, privSz2, pubSz2;
  13995. #ifdef WOLFSSL_SMALL_STACK
  13996. byte *priv = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13997. byte *pub = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13998. byte *priv2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13999. byte *pub2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14000. byte *agree = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14001. byte *agree2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14002. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14003. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14004. #else
  14005. byte priv[FFDHE_KEY_SIZE];
  14006. byte pub[FFDHE_KEY_SIZE];
  14007. byte priv2[FFDHE_KEY_SIZE];
  14008. byte pub2[FFDHE_KEY_SIZE];
  14009. byte agree[FFDHE_KEY_SIZE];
  14010. byte agree2[FFDHE_KEY_SIZE];
  14011. DhKey key[1];
  14012. DhKey key2[1];
  14013. #endif
  14014. word32 agreeSz = FFDHE_KEY_SIZE;
  14015. word32 agreeSz2 = FFDHE_KEY_SIZE;
  14016. #ifdef WOLFSSL_SMALL_STACK
  14017. if ((priv == NULL) ||
  14018. (pub == NULL) ||
  14019. (priv2 == NULL) ||
  14020. (pub2 == NULL) ||
  14021. (agree == NULL) ||
  14022. (agree2 == NULL) ||
  14023. (key == NULL) ||
  14024. (key2 == NULL))
  14025. ERROR_OUT(-8050, done);
  14026. #endif
  14027. pubSz = FFDHE_KEY_SIZE;
  14028. pubSz2 = FFDHE_KEY_SIZE;
  14029. privSz = FFDHE_KEY_SIZE;
  14030. privSz2 = FFDHE_KEY_SIZE;
  14031. XMEMSET(key, 0, sizeof *key);
  14032. XMEMSET(key2, 0, sizeof *key2);
  14033. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14034. if (ret != 0) {
  14035. ERROR_OUT(-8051, done);
  14036. }
  14037. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14038. if (ret != 0) {
  14039. ERROR_OUT(-8052, done);
  14040. }
  14041. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  14042. if (ret != 0) {
  14043. ERROR_OUT(-8053, done);
  14044. }
  14045. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  14046. params->g_len);
  14047. if (ret != 0) {
  14048. ERROR_OUT(-8054, done);
  14049. }
  14050. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14051. #if defined(WOLFSSL_ASYNC_CRYPT)
  14052. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14053. #endif
  14054. if (ret != 0) {
  14055. ERROR_OUT(-8055, done);
  14056. }
  14057. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  14058. #if defined(WOLFSSL_ASYNC_CRYPT)
  14059. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14060. #endif
  14061. if (ret != 0) {
  14062. ERROR_OUT(-8056, done);
  14063. }
  14064. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14065. #if defined(WOLFSSL_ASYNC_CRYPT)
  14066. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14067. #endif
  14068. if (ret != 0) {
  14069. ERROR_OUT(-8057, done);
  14070. }
  14071. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14072. #if defined(WOLFSSL_ASYNC_CRYPT)
  14073. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14074. #endif
  14075. if (ret != 0) {
  14076. ERROR_OUT(-8058, done);
  14077. }
  14078. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14079. ERROR_OUT(-8059, done);
  14080. }
  14081. #ifdef WOLFSSL_HAVE_SP_DH
  14082. /* Make p even */
  14083. key->p.dp[0] &= (mp_digit)-2;
  14084. if (ret != 0) {
  14085. ERROR_OUT(-8058, done);
  14086. }
  14087. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14088. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14089. ERROR_OUT(-8058, done);
  14090. }
  14091. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14092. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14093. ERROR_OUT(-8057, done);
  14094. }
  14095. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14096. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14097. ERROR_OUT(-8057, done);
  14098. }
  14099. /* Getting here means success - set ret to 0. */
  14100. ret = 0;
  14101. #endif
  14102. done:
  14103. #ifdef WOLFSSL_SMALL_STACK
  14104. if (priv)
  14105. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14106. if (pub)
  14107. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14108. if (priv2)
  14109. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14110. if (pub2)
  14111. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14112. if (agree)
  14113. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14114. if (agree2)
  14115. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14116. if (key) {
  14117. wc_FreeDhKey(key);
  14118. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14119. }
  14120. if (key2) {
  14121. wc_FreeDhKey(key2);
  14122. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14123. }
  14124. #else
  14125. wc_FreeDhKey(key);
  14126. wc_FreeDhKey(key2);
  14127. #endif
  14128. return ret;
  14129. }
  14130. #endif /* HAVE_FFDHE */
  14131. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  14132. {
  14133. int ret;
  14134. word32 bytes;
  14135. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  14136. WC_RNG rng;
  14137. int keyInit = 0;
  14138. #define DH_TEST_TMP_SIZE 1024
  14139. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14140. #define DH_TEST_BUF_SIZE 256
  14141. #else
  14142. #define DH_TEST_BUF_SIZE 512
  14143. #endif
  14144. word32 agreeSz = DH_TEST_BUF_SIZE;
  14145. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  14146. #ifdef WOLFSSL_SMALL_STACK
  14147. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14148. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14149. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14150. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14151. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14152. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14153. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14154. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14155. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14156. if ((tmp == NULL) || (priv == NULL) || (pub == NULL) ||
  14157. (priv2 == NULL) || (pub2 == NULL) || (agree == NULL) ||
  14158. (agree2 == NULL))
  14159. ERROR_OUT(-8100, done);
  14160. #else
  14161. DhKey key[1];
  14162. DhKey key2[1];
  14163. byte tmp[DH_TEST_TMP_SIZE];
  14164. byte priv[DH_TEST_BUF_SIZE];
  14165. byte pub[DH_TEST_BUF_SIZE];
  14166. byte priv2[DH_TEST_BUF_SIZE];
  14167. byte pub2[DH_TEST_BUF_SIZE];
  14168. byte agree[DH_TEST_BUF_SIZE];
  14169. byte agree2[DH_TEST_BUF_SIZE];
  14170. #endif
  14171. #ifdef USE_CERT_BUFFERS_1024
  14172. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  14173. bytes = (size_t)sizeof_dh_key_der_1024;
  14174. #elif defined(USE_CERT_BUFFERS_2048)
  14175. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  14176. bytes = (size_t)sizeof_dh_key_der_2048;
  14177. #elif defined(USE_CERT_BUFFERS_3072)
  14178. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  14179. bytes = (size_t)sizeof_dh_key_der_3072;
  14180. #elif defined(USE_CERT_BUFFERS_4096)
  14181. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  14182. bytes = (size_t)sizeof_dh_key_der_4096;
  14183. #elif defined(NO_ASN)
  14184. /* don't use file, no DER parsing */
  14185. #elif !defined(NO_FILESYSTEM)
  14186. {
  14187. XFILE file = XFOPEN(dhParamsFile, "rb");
  14188. if (! file)
  14189. ERROR_OUT(-8101, done);
  14190. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14191. XFCLOSE(file);
  14192. }
  14193. #else
  14194. /* No DH key to use. */
  14195. ERROR_OUT(-8102, done);
  14196. #endif /* USE_CERT_BUFFERS */
  14197. (void)idx;
  14198. (void)tmp;
  14199. (void)bytes;
  14200. pubSz = DH_TEST_BUF_SIZE;
  14201. pubSz2 = DH_TEST_BUF_SIZE;
  14202. privSz = DH_TEST_BUF_SIZE;
  14203. privSz2 = DH_TEST_BUF_SIZE;
  14204. XMEMSET(&rng, 0, sizeof(rng));
  14205. /* Use API for coverage. */
  14206. ret = wc_InitDhKey(key);
  14207. if (ret != 0) {
  14208. ERROR_OUT(-8103, done);
  14209. }
  14210. wc_FreeDhKey(key);
  14211. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14212. if (ret != 0) {
  14213. ERROR_OUT(-8104, done);
  14214. }
  14215. keyInit = 1;
  14216. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14217. if (ret != 0) {
  14218. ERROR_OUT(-8105, done);
  14219. }
  14220. #ifdef NO_ASN
  14221. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14222. if (ret != 0) {
  14223. ERROR_OUT(-8106, done);
  14224. }
  14225. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14226. if (ret != 0) {
  14227. ERROR_OUT(-8107, done);
  14228. }
  14229. #else
  14230. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14231. if (ret != 0) {
  14232. ERROR_OUT(-8108, done);
  14233. }
  14234. idx = 0;
  14235. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  14236. if (ret != 0) {
  14237. ERROR_OUT(-8109, done);
  14238. }
  14239. #endif
  14240. #ifndef HAVE_FIPS
  14241. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14242. #else
  14243. ret = wc_InitRng(&rng);
  14244. #endif
  14245. if (ret != 0) {
  14246. ERROR_OUT(-8110, done);
  14247. }
  14248. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  14249. #if defined(WOLFSSL_ASYNC_CRYPT)
  14250. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14251. #endif
  14252. if (ret != 0) {
  14253. ERROR_OUT(-8111, done);
  14254. }
  14255. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  14256. #if defined(WOLFSSL_ASYNC_CRYPT)
  14257. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14258. #endif
  14259. if (ret != 0) {
  14260. ERROR_OUT(-8112, done);
  14261. }
  14262. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14263. #if defined(WOLFSSL_ASYNC_CRYPT)
  14264. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14265. #endif
  14266. if (ret != 0) {
  14267. ERROR_OUT(-8113, done);
  14268. }
  14269. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14270. #if defined(WOLFSSL_ASYNC_CRYPT)
  14271. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14272. #endif
  14273. if (ret != 0) {
  14274. ERROR_OUT(-8114, done);
  14275. }
  14276. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14277. ERROR_OUT(-8115, done);
  14278. }
  14279. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14280. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  14281. ERROR_OUT(-8116, done);
  14282. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  14283. ERROR_OUT(-8117, done);
  14284. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  14285. ERROR_OUT(-8118, done);
  14286. {
  14287. word32 pSz, qSz, gSz;
  14288. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  14289. ERROR_OUT(-8119, done);
  14290. }
  14291. #endif
  14292. /* Test DH key import / export */
  14293. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  14294. (!defined(HAVE_FIPS) || \
  14295. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  14296. wc_FreeDhKey(key);
  14297. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14298. if (ret != 0) {
  14299. ERROR_OUT(-8120, done);
  14300. }
  14301. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM)
  14302. {
  14303. XFILE file = XFOPEN(dhKeyFile, "rb");
  14304. if (!file)
  14305. ERROR_OUT(-8121, done);
  14306. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14307. XFCLOSE(file);
  14308. }
  14309. idx = 0;
  14310. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14311. if (ret != 0) {
  14312. ERROR_OUT(-8122, done);
  14313. }
  14314. #else
  14315. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14316. if (ret != 0) {
  14317. ERROR_OUT(-8123, done);
  14318. }
  14319. #endif
  14320. privSz = DH_TEST_BUF_SIZE;
  14321. pubSz = DH_TEST_BUF_SIZE;
  14322. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  14323. if (ret != 0) {
  14324. ERROR_OUT(-8124, done);
  14325. }
  14326. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  14327. if (ret != 0) {
  14328. ERROR_OUT(-8125, done);
  14329. }
  14330. #endif /* WOLFSSL_DH_EXTRA */
  14331. ret = dh_generate_test(&rng);
  14332. if (ret != 0)
  14333. ERROR_OUT(-8126, done);
  14334. ret = dh_fips_generate_test(&rng);
  14335. if (ret != 0)
  14336. ERROR_OUT(-8127, done);
  14337. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14338. ret = dh_test_check_pubvalue();
  14339. if (ret != 0)
  14340. ERROR_OUT(-8128, done);
  14341. #endif
  14342. /* Specialized code for key gen when using FFDHE-2048 and FFDHE-3072. */
  14343. #ifdef HAVE_FFDHE_2048
  14344. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  14345. if (ret != 0)
  14346. ERROR_OUT(-8129, done);
  14347. #endif
  14348. #ifdef HAVE_FFDHE_3072
  14349. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  14350. if (ret != 0)
  14351. ERROR_OUT(-8130, done);
  14352. #endif
  14353. wc_FreeDhKey(key);
  14354. keyInit = 0;
  14355. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  14356. !defined(WOLFSSL_OLD_PRIME_CHECK)
  14357. /* Test Check Key */
  14358. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  14359. NULL, 0, 0, &rng);
  14360. if (ret != 0)
  14361. ERROR_OUT(-8131, done);
  14362. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  14363. #endif
  14364. done:
  14365. wc_FreeRng(&rng);
  14366. #ifdef WOLFSSL_SMALL_STACK
  14367. if (key) {
  14368. if (keyInit)
  14369. wc_FreeDhKey(key);
  14370. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14371. }
  14372. if (key2) {
  14373. wc_FreeDhKey(key2);
  14374. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14375. }
  14376. if (tmp)
  14377. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14378. if (priv)
  14379. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14380. if (pub)
  14381. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14382. if (priv2)
  14383. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14384. if (pub2)
  14385. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14386. if (agree)
  14387. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14388. if (agree2)
  14389. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14390. #else
  14391. if (keyInit)
  14392. wc_FreeDhKey(key);
  14393. wc_FreeDhKey(key2);
  14394. #endif
  14395. return ret;
  14396. #undef DH_TEST_BUF_SIZE
  14397. #undef DH_TEST_TMP_SIZE
  14398. }
  14399. #endif /* NO_DH */
  14400. #ifndef NO_DSA
  14401. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  14402. {
  14403. int ret = 0, answer;
  14404. word32 bytes;
  14405. word32 idx = 0;
  14406. WC_RNG rng;
  14407. wc_Sha sha;
  14408. byte hash[WC_SHA_DIGEST_SIZE];
  14409. byte signature[40];
  14410. #ifdef WOLFSSL_KEY_GEN
  14411. byte* der = 0;
  14412. #endif
  14413. #define DSA_TEST_TMP_SIZE 1024
  14414. #ifdef WOLFSSL_SMALL_STACK
  14415. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14416. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14417. #ifdef WOLFSSL_KEY_GEN
  14418. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14419. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14420. #endif
  14421. if ((tmp == NULL) ||
  14422. (key == NULL)
  14423. #ifdef WOLFSSL_KEY_GEN
  14424. || (derIn == NULL)
  14425. || (genKey == NULL)
  14426. #endif
  14427. ) {
  14428. ret = -8216;
  14429. goto out;
  14430. }
  14431. #else
  14432. byte tmp[1024];
  14433. DsaKey key[1];
  14434. #ifdef WOLFSSL_KEY_GEN
  14435. DsaKey derIn[1];
  14436. DsaKey genKey[1];
  14437. #endif
  14438. #endif
  14439. #ifdef USE_CERT_BUFFERS_1024
  14440. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  14441. bytes = sizeof_dsa_key_der_1024;
  14442. #elif defined(USE_CERT_BUFFERS_2048)
  14443. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  14444. bytes = sizeof_dsa_key_der_2048;
  14445. #else
  14446. {
  14447. XFILE file = XFOPEN(dsaKey, "rb");
  14448. if (!file)
  14449. ERROR_OUT(-8200, out);
  14450. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  14451. XFCLOSE(file);
  14452. }
  14453. #endif /* USE_CERT_BUFFERS */
  14454. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  14455. if (ret != 0)
  14456. ERROR_OUT(-8201, out);
  14457. wc_ShaUpdate(&sha, tmp, bytes);
  14458. wc_ShaFinal(&sha, hash);
  14459. wc_ShaFree(&sha);
  14460. ret = wc_InitDsaKey(key);
  14461. if (ret != 0)
  14462. ERROR_OUT(-8202, out);
  14463. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  14464. if (ret != 0)
  14465. ERROR_OUT(-8203, out);
  14466. #ifndef HAVE_FIPS
  14467. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14468. #else
  14469. ret = wc_InitRng(&rng);
  14470. #endif
  14471. if (ret != 0)
  14472. ERROR_OUT(-8204, out);
  14473. ret = wc_DsaSign(hash, signature, key, &rng);
  14474. if (ret != 0)
  14475. ERROR_OUT(-8205, out);
  14476. ret = wc_DsaVerify(hash, signature, key, &answer);
  14477. if (ret != 0)
  14478. ERROR_OUT(-8206, out);
  14479. if (answer != 1)
  14480. ERROR_OUT(-8207, out);
  14481. wc_FreeDsaKey(key);
  14482. #ifdef WOLFSSL_KEY_GEN
  14483. {
  14484. int derSz = 0;
  14485. ret = wc_InitDsaKey(genKey);
  14486. if (ret != 0)
  14487. ERROR_OUT(-8208, out);
  14488. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  14489. if (ret != 0) {
  14490. wc_FreeDsaKey(genKey);
  14491. ERROR_OUT(-8209, out);
  14492. }
  14493. ret = wc_MakeDsaKey(&rng, genKey);
  14494. if (ret != 0) {
  14495. wc_FreeDsaKey(genKey);
  14496. ERROR_OUT(-8210, out);
  14497. }
  14498. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14499. if (der == NULL) {
  14500. wc_FreeDsaKey(genKey);
  14501. ERROR_OUT(-8211, out);
  14502. }
  14503. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  14504. if (derSz < 0) {
  14505. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14506. ERROR_OUT(-8212, out);
  14507. }
  14508. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14509. DSA_PRIVATEKEY_TYPE, -5814);
  14510. if (ret != 0) {
  14511. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14512. wc_FreeDsaKey(genKey);
  14513. goto out;
  14514. }
  14515. ret = wc_InitDsaKey(derIn);
  14516. if (ret != 0) {
  14517. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14518. wc_FreeDsaKey(genKey);
  14519. ERROR_OUT(-8213, out);
  14520. }
  14521. idx = 0;
  14522. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  14523. if (ret != 0) {
  14524. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14525. wc_FreeDsaKey(derIn);
  14526. wc_FreeDsaKey(genKey);
  14527. ERROR_OUT(-8214, out);
  14528. }
  14529. }
  14530. #endif /* WOLFSSL_KEY_GEN */
  14531. out:
  14532. #ifdef WOLFSSL_SMALL_STACK
  14533. if (key) {
  14534. #endif
  14535. if (wc_InitDsaKey_h(key, NULL) != 0)
  14536. ret = -8215;
  14537. #ifdef WOLFSSL_SMALL_STACK
  14538. }
  14539. #endif
  14540. #ifdef WOLFSSL_KEY_GEN
  14541. if (der)
  14542. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14543. #endif
  14544. #ifdef WOLFSSL_SMALL_STACK
  14545. if (tmp)
  14546. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14547. if (key)
  14548. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14549. #ifdef WOLFSSL_KEY_GEN
  14550. if (derIn) {
  14551. wc_FreeDsaKey(derIn);
  14552. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14553. }
  14554. if (genKey) {
  14555. wc_FreeDsaKey(genKey);
  14556. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14557. }
  14558. #endif
  14559. #else /* !WOLFSSL_SMALL_STACK */
  14560. #ifdef WOLFSSL_KEY_GEN
  14561. wc_FreeDsaKey(derIn);
  14562. wc_FreeDsaKey(genKey);
  14563. #endif
  14564. #endif
  14565. wc_FreeRng(&rng);
  14566. return ret;
  14567. }
  14568. #endif /* NO_DSA */
  14569. #ifdef WOLFCRYPT_HAVE_SRP
  14570. static int generate_random_salt(byte *buf, word32 size)
  14571. {
  14572. int ret = -8220;
  14573. WC_RNG rng;
  14574. if(NULL == buf || !size)
  14575. return -8221;
  14576. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  14577. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  14578. wc_FreeRng(&rng);
  14579. }
  14580. return ret;
  14581. }
  14582. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  14583. {
  14584. int r;
  14585. byte clientPubKey[80]; /* A */
  14586. byte serverPubKey[80]; /* B */
  14587. word32 clientPubKeySz = 80;
  14588. word32 serverPubKeySz = 80;
  14589. byte username[] = "user";
  14590. word32 usernameSz = 4;
  14591. byte password[] = "password";
  14592. word32 passwordSz = 8;
  14593. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  14594. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  14595. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  14596. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  14597. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  14598. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  14599. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  14600. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  14601. };
  14602. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  14603. 0x02
  14604. };
  14605. byte salt[10];
  14606. byte verifier[80];
  14607. word32 v_size = sizeof(verifier);
  14608. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  14609. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  14610. #ifdef WOLFSSL_SMALL_STACK
  14611. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14612. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14613. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  14614. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  14615. if ((cli == NULL) ||
  14616. (srv == NULL) ||
  14617. (clientProof == NULL) ||
  14618. (serverProof == NULL)) {
  14619. r = -8222;
  14620. goto out;
  14621. }
  14622. #else
  14623. Srp cli[1], srv[1];
  14624. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  14625. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  14626. #endif
  14627. /* set as 0's so if second init on srv not called SrpTerm is not on
  14628. * garbage values */
  14629. XMEMSET(srv, 0, sizeof *srv);
  14630. XMEMSET(cli, 0, sizeof *cli);
  14631. /* generating random salt */
  14632. r = generate_random_salt(salt, sizeof(salt));
  14633. /* client knows username and password. */
  14634. /* server knows N, g, salt and verifier. */
  14635. if (!r) r = wc_SrpInit(cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  14636. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  14637. /* loading N, g and salt in advance to generate the verifier. */
  14638. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  14639. g, sizeof(g),
  14640. salt, sizeof(salt));
  14641. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  14642. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  14643. /* client sends username to server */
  14644. if (!r) r = wc_SrpInit(srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  14645. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  14646. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  14647. g, sizeof(g),
  14648. salt, sizeof(salt));
  14649. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  14650. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  14651. /* server sends N, g, salt and B to client */
  14652. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  14653. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  14654. serverPubKey, serverPubKeySz);
  14655. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  14656. /* client sends A and M1 to server */
  14657. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  14658. serverPubKey, serverPubKeySz);
  14659. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  14660. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  14661. /* server sends M2 to client */
  14662. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  14663. wc_SrpTerm(cli);
  14664. wc_SrpTerm(srv);
  14665. #ifdef WOLFSSL_SMALL_STACK
  14666. out:
  14667. if (cli)
  14668. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14669. if (srv)
  14670. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14671. if (clientProof)
  14672. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14673. if (serverProof)
  14674. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14675. #endif
  14676. return r;
  14677. }
  14678. #endif /* WOLFCRYPT_HAVE_SRP */
  14679. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  14680. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  14681. static int openssl_aes_test(void)
  14682. {
  14683. #ifdef HAVE_AES_CBC
  14684. #ifdef WOLFSSL_AES_128
  14685. {
  14686. /* EVP_CipherUpdate test */
  14687. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  14688. {
  14689. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14690. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14691. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14692. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14693. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14694. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14695. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14696. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14697. };
  14698. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  14699. "0123456789abcdef "; /* align */
  14700. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  14701. "1234567890abcdef "; /* align */
  14702. byte cipher[AES_BLOCK_SIZE * 4];
  14703. byte plain [AES_BLOCK_SIZE * 4];
  14704. #ifdef WOLFSSL_SMALL_STACK
  14705. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  14706. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  14707. #else
  14708. EVP_CIPHER_CTX en[1];
  14709. EVP_CIPHER_CTX de[1];
  14710. #endif
  14711. int outlen ;
  14712. int total = 0;
  14713. int i;
  14714. #ifdef WOLFSSL_SMALL_STACK
  14715. if ((en == NULL) || (de == NULL))
  14716. return MEMORY_E;
  14717. #endif
  14718. EVP_CIPHER_CTX_init(en);
  14719. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14720. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14721. return -8400;
  14722. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14723. (byte*)cbcPlain, 9) == 0)
  14724. return -8401;
  14725. if (outlen != 0)
  14726. return -8402;
  14727. total += outlen;
  14728. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  14729. (byte*)&cbcPlain[9] , 9) == 0)
  14730. return -8403;
  14731. if (outlen != 16)
  14732. return -8404;
  14733. total += outlen;
  14734. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14735. return -8405;
  14736. if (outlen != 16)
  14737. return -8406;
  14738. total += outlen;
  14739. if (total != 32)
  14740. return 3408;
  14741. total = 0;
  14742. EVP_CIPHER_CTX_init(de);
  14743. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14744. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14745. return -8407;
  14746. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  14747. return -8408;
  14748. if (outlen != 0)
  14749. return -8409;
  14750. total += outlen;
  14751. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14752. (byte*)&cipher[6], 12) == 0)
  14753. return -8410;
  14754. if (outlen != 0)
  14755. total += outlen;
  14756. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14757. (byte*)&cipher[6+12], 14) == 0)
  14758. return -8411;
  14759. if (outlen != 16)
  14760. return -8412;
  14761. total += outlen;
  14762. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14763. return -8413;
  14764. if (outlen != 2)
  14765. return -8414;
  14766. total += outlen;
  14767. if (total != 18)
  14768. return 3427;
  14769. if (XMEMCMP(plain, cbcPlain, 18))
  14770. return -8415;
  14771. /* test with encrypting/decrypting more than 16 bytes at once */
  14772. total = 0;
  14773. EVP_CIPHER_CTX_init(en);
  14774. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14775. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14776. return -8416;
  14777. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14778. (byte*)cbcPlain, 17) == 0)
  14779. return -8417;
  14780. if (outlen != 16)
  14781. return -8418;
  14782. total += outlen;
  14783. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  14784. (byte*)&cbcPlain[17] , 1) == 0)
  14785. return -8419;
  14786. if (outlen != 0)
  14787. return -8420;
  14788. total += outlen;
  14789. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14790. return -8421;
  14791. if (outlen != 16)
  14792. return -8422;
  14793. total += outlen;
  14794. if (total != 32)
  14795. return -8423;
  14796. total = 0;
  14797. EVP_CIPHER_CTX_init(de);
  14798. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14799. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14800. return -8424;
  14801. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  14802. return -8425;
  14803. if (outlen != 16)
  14804. return -8426;
  14805. total += outlen;
  14806. /* final call on non block size should fail */
  14807. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  14808. return -8427;
  14809. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14810. (byte*)&cipher[17], 1) == 0)
  14811. return -8428;
  14812. if (outlen != 0)
  14813. total += outlen;
  14814. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14815. (byte*)&cipher[17+1], 14) == 0)
  14816. return -8429;
  14817. if (outlen != 0)
  14818. return -8430;
  14819. total += outlen;
  14820. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14821. return -8431;
  14822. if (outlen != 2)
  14823. return -8432;
  14824. total += outlen;
  14825. if (total != 18)
  14826. return -8433;
  14827. if (XMEMCMP(plain, cbcPlain, 18))
  14828. return -8434;
  14829. /* test byte by byte decrypt */
  14830. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  14831. plain[i] = i;
  14832. }
  14833. total = 0;
  14834. EVP_CIPHER_CTX_init(en);
  14835. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14836. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14837. return -8435;
  14838. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14839. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  14840. return -8436;
  14841. if (outlen != AES_BLOCK_SIZE * 3)
  14842. return -8437;
  14843. total += outlen;
  14844. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14845. return -8438;
  14846. if (outlen != AES_BLOCK_SIZE)
  14847. return -8439;
  14848. total += outlen;
  14849. if (total != sizeof(plain))
  14850. return -8440;
  14851. total = 0;
  14852. EVP_CIPHER_CTX_init(de);
  14853. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14854. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14855. return -8441;
  14856. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  14857. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  14858. (byte*)cipher + i, 1) == 0)
  14859. return -8442;
  14860. if (outlen > 0) {
  14861. int j;
  14862. total += outlen;
  14863. for (j = 0; j < total; j++) {
  14864. if (plain[j] != j) {
  14865. return -8443;
  14866. }
  14867. }
  14868. }
  14869. }
  14870. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14871. return -8444;
  14872. total += outlen;
  14873. if (total != AES_BLOCK_SIZE * 3) {
  14874. return -8445;
  14875. }
  14876. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  14877. if (plain[i] != i) {
  14878. return -8446;
  14879. }
  14880. }
  14881. #ifdef WOLFSSL_SMALL_STACK
  14882. wolfSSL_EVP_CIPHER_CTX_free(en);
  14883. wolfSSL_EVP_CIPHER_CTX_free(de);
  14884. #endif
  14885. }
  14886. /* set buffers to be exact size to catch potential over read/write */
  14887. {
  14888. /* EVP_CipherUpdate test */
  14889. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  14890. {
  14891. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14892. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14893. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14894. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14895. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14896. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14897. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14898. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14899. };
  14900. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  14901. "0123456789abcdef "; /* align */
  14902. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  14903. "1234567890abcdef "; /* align */
  14904. #define EVP_TEST_BUF_SZ 18
  14905. #define EVP_TEST_BUF_PAD 32
  14906. byte cipher[EVP_TEST_BUF_SZ];
  14907. byte plain [EVP_TEST_BUF_SZ];
  14908. byte padded[EVP_TEST_BUF_PAD];
  14909. #ifdef WOLFSSL_SMALL_STACK
  14910. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  14911. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  14912. #else
  14913. EVP_CIPHER_CTX en[1];
  14914. EVP_CIPHER_CTX de[1];
  14915. #endif
  14916. int outlen ;
  14917. int total = 0;
  14918. #ifdef WOLFSSL_SMALL_STACK
  14919. if ((en == NULL) || (de == NULL))
  14920. return MEMORY_E;
  14921. #endif
  14922. EVP_CIPHER_CTX_init(en);
  14923. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14924. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14925. return -8447;
  14926. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  14927. return -8448;
  14928. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14929. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  14930. return -8449;
  14931. if (outlen != 16)
  14932. return -8450;
  14933. total += outlen;
  14934. /* should fail here */
  14935. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  14936. return -8451;
  14937. /* turn padding back on and do successful encrypt */
  14938. total = 0;
  14939. EVP_CIPHER_CTX_init(en);
  14940. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14941. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14942. return -8452;
  14943. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  14944. return -8453;
  14945. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  14946. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  14947. return -8454;
  14948. if (outlen != 16)
  14949. return -8455;
  14950. total += outlen;
  14951. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  14952. return -8456;
  14953. total += outlen;
  14954. if (total != 32)
  14955. return -8457;
  14956. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  14957. /* test out of bounds read on buffers w/o padding during decryption */
  14958. total = 0;
  14959. EVP_CIPHER_CTX_init(de);
  14960. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14961. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14962. return -8458;
  14963. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  14964. return -8459;
  14965. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  14966. EVP_TEST_BUF_SZ) == 0)
  14967. return -8460;
  14968. if (outlen != 16)
  14969. return -8461;
  14970. total += outlen;
  14971. /* should fail since not using padding */
  14972. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  14973. return -8462;
  14974. total = 0;
  14975. EVP_CIPHER_CTX_init(de);
  14976. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14977. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14978. return -8463;
  14979. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  14980. return -8464;
  14981. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  14982. EVP_TEST_BUF_PAD) == 0)
  14983. return -8465;
  14984. if (outlen != 16)
  14985. return -8466;
  14986. total += outlen;
  14987. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  14988. return -8467;
  14989. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  14990. return -8468;
  14991. #ifdef WOLFSSL_SMALL_STACK
  14992. wolfSSL_EVP_CIPHER_CTX_free(en);
  14993. wolfSSL_EVP_CIPHER_CTX_free(de);
  14994. #endif
  14995. }
  14996. { /* evp_cipher test: EVP_aes_128_cbc */
  14997. #ifdef WOLFSSL_SMALL_STACK
  14998. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  14999. #else
  15000. EVP_CIPHER_CTX ctx[1];
  15001. #endif
  15002. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  15003. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15004. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15005. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15006. };
  15007. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15008. {
  15009. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15010. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  15011. };
  15012. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15013. "0123456789abcdef "; /* align */
  15014. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15015. "1234567890abcdef "; /* align */
  15016. byte cipher[AES_BLOCK_SIZE * 4];
  15017. byte plain [AES_BLOCK_SIZE * 4];
  15018. #ifdef WOLFSSL_SMALL_STACK
  15019. if (ctx == NULL)
  15020. return MEMORY_E;
  15021. #endif
  15022. EVP_CIPHER_CTX_init(ctx);
  15023. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  15024. return -8469;
  15025. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15026. return -8470;
  15027. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15028. return -8471;
  15029. EVP_CIPHER_CTX_init(ctx);
  15030. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  15031. return -8472;
  15032. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15033. return -8473;
  15034. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15035. return -8474;
  15036. #ifdef WOLFSSL_SMALL_STACK
  15037. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15038. #endif
  15039. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15040. #endif /* WOLFSSL_AES_128 */
  15041. #endif /* HAVE_AES_CBC */
  15042. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15043. { /* evp_cipher test: EVP_aes_256_ecb*/
  15044. #ifdef WOLFSSL_SMALL_STACK
  15045. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15046. #else
  15047. EVP_CIPHER_CTX ctx[1];
  15048. #endif
  15049. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15050. {
  15051. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15052. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15053. };
  15054. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15055. {
  15056. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15057. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15058. };
  15059. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15060. {
  15061. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15062. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15063. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15064. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15065. };
  15066. byte cipher[AES_BLOCK_SIZE * 4];
  15067. byte plain [AES_BLOCK_SIZE * 4];
  15068. #ifdef WOLFSSL_SMALL_STACK
  15069. if (ctx == NULL)
  15070. return MEMORY_E;
  15071. #endif
  15072. EVP_CIPHER_CTX_init(ctx);
  15073. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  15074. return -8475;
  15075. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15076. return -8476;
  15077. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15078. return -8477;
  15079. EVP_CIPHER_CTX_init(ctx);
  15080. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  15081. return -8478;
  15082. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15083. return -8479;
  15084. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15085. return -8480;
  15086. #ifdef WOLFSSL_SMALL_STACK
  15087. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15088. #endif
  15089. } /* end evp_cipher test */
  15090. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  15091. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  15092. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  15093. {
  15094. /* Test: AES_encrypt/decrypt/set Key */
  15095. #ifdef WOLFSSL_SMALL_STACK
  15096. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15097. #ifdef HAVE_AES_DECRYPT
  15098. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15099. #endif
  15100. #else
  15101. AES_KEY enc[1];
  15102. #ifdef HAVE_AES_DECRYPT
  15103. AES_KEY dec[1];
  15104. #endif
  15105. #endif
  15106. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15107. {
  15108. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15109. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15110. };
  15111. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15112. {
  15113. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15114. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15115. };
  15116. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15117. {
  15118. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15119. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15120. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15121. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15122. };
  15123. byte plain[sizeof(msg)];
  15124. byte cipher[sizeof(msg)];
  15125. #ifdef WOLFSSL_SMALL_STACK
  15126. if (enc == NULL)
  15127. return MEMORY_E;
  15128. #ifdef HAVE_AES_DECRYPT
  15129. if (dec == NULL)
  15130. return MEMORY_E;
  15131. #endif
  15132. #endif
  15133. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  15134. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  15135. AES_encrypt(msg, cipher, enc);
  15136. #ifdef HAVE_AES_DECRYPT
  15137. AES_decrypt(cipher, plain, dec);
  15138. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15139. return -8481;
  15140. #endif /* HAVE_AES_DECRYPT */
  15141. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15142. return -8482;
  15143. #ifdef WOLFSSL_SMALL_STACK
  15144. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15145. #ifdef HAVE_AES_DECRYPT
  15146. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15147. #endif
  15148. #endif
  15149. }
  15150. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  15151. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  15152. #ifdef WOLFSSL_AES_COUNTER
  15153. {
  15154. byte plainBuff [64];
  15155. byte cipherBuff[64];
  15156. #ifdef WOLFSSL_AES_128
  15157. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  15158. {
  15159. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15160. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15161. };
  15162. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  15163. {
  15164. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15165. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15166. };
  15167. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  15168. {
  15169. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15170. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15171. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15172. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15173. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15174. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15175. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15176. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15177. };
  15178. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  15179. {
  15180. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  15181. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  15182. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  15183. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  15184. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  15185. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  15186. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  15187. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  15188. };
  15189. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  15190. {
  15191. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  15192. 0xc2
  15193. };
  15194. #endif
  15195. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15196. * NIST Special Publication 800-38A */
  15197. #ifdef WOLFSSL_AES_192
  15198. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  15199. {
  15200. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  15201. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  15202. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  15203. };
  15204. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  15205. {
  15206. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15207. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15208. };
  15209. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  15210. {
  15211. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15212. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15213. };
  15214. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  15215. {
  15216. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  15217. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  15218. };
  15219. #endif /* WOLFSSL_AES_192 */
  15220. #ifdef WOLFSSL_AES_256
  15221. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15222. * NIST Special Publication 800-38A */
  15223. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  15224. {
  15225. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15226. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15227. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15228. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15229. };
  15230. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  15231. {
  15232. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15233. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15234. };
  15235. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  15236. {
  15237. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15238. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15239. };
  15240. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  15241. {
  15242. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  15243. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  15244. };
  15245. #endif /* WOLFSSL_AES_256 */
  15246. #ifdef WOLFSSL_SMALL_STACK
  15247. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15248. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15249. #else
  15250. EVP_CIPHER_CTX en[1];
  15251. EVP_CIPHER_CTX de[1];
  15252. #endif
  15253. #ifdef WOLFSSL_AES_128
  15254. #ifndef WOLFSSL_SMALL_STACK
  15255. EVP_CIPHER_CTX *p_en;
  15256. EVP_CIPHER_CTX *p_de;
  15257. #endif
  15258. #ifdef WOLFSSL_SMALL_STACK
  15259. if ((en == NULL) || (de == NULL))
  15260. return MEMORY_E;
  15261. #endif
  15262. EVP_CIPHER_CTX_init(en);
  15263. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15264. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15265. return -8483;
  15266. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  15267. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15268. return -8484;
  15269. EVP_CIPHER_CTX_init(de);
  15270. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15271. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15272. return -8485;
  15273. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15274. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15275. return -8486;
  15276. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15277. return -8487;
  15278. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15279. return -8488;
  15280. #ifndef WOLFSSL_SMALL_STACK
  15281. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  15282. if (p_en == NULL)
  15283. return -8489;
  15284. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  15285. if (p_de == NULL)
  15286. return -8490;
  15287. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  15288. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15289. return -8491;
  15290. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  15291. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15292. return -8492;
  15293. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  15294. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15295. return -8493;
  15296. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  15297. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15298. return -8494;
  15299. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  15300. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  15301. #endif /* !WOLFSSL_SMALL_STACK */
  15302. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15303. return -8495;
  15304. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15305. return -8496;
  15306. EVP_CIPHER_CTX_init(en);
  15307. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15308. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15309. return -8497;
  15310. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15311. return -8498;
  15312. EVP_CIPHER_CTX_init(de);
  15313. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15314. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15315. return -8499;
  15316. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15317. return -8500;
  15318. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15319. return -8501;
  15320. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  15321. return -8502;
  15322. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15323. return -8503;
  15324. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15325. return -8504;
  15326. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15327. return -8505;
  15328. if (XMEMCMP(cipherBuff, oddCipher, 9))
  15329. return -8506;
  15330. #endif /* WOLFSSL_AES_128 */
  15331. #ifdef WOLFSSL_AES_192
  15332. EVP_CIPHER_CTX_init(en);
  15333. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  15334. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15335. return -8507;
  15336. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  15337. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15338. return -8508;
  15339. EVP_CIPHER_CTX_init(de);
  15340. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  15341. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15342. return -8509;
  15343. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15344. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15345. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15346. return -8510;
  15347. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  15348. return -8511;
  15349. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  15350. return -8512;
  15351. #endif /* WOLFSSL_AES_192 */
  15352. #ifdef WOLFSSL_AES_256
  15353. EVP_CIPHER_CTX_init(en);
  15354. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  15355. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15356. return -8513;
  15357. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  15358. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15359. return -8514;
  15360. EVP_CIPHER_CTX_init(de);
  15361. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  15362. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15363. return -8515;
  15364. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15365. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15366. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15367. return -8516;
  15368. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  15369. return -8517;
  15370. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  15371. return -8518;
  15372. #ifdef WOLFSSL_SMALL_STACK
  15373. wolfSSL_EVP_CIPHER_CTX_free(en);
  15374. wolfSSL_EVP_CIPHER_CTX_free(de);
  15375. #endif
  15376. #endif /* WOLFSSL_AES_256 */
  15377. }
  15378. #endif /* HAVE_AES_COUNTER */
  15379. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  15380. {
  15381. #ifdef WOLFSSL_SMALL_STACK
  15382. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15383. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15384. #else
  15385. AES_KEY enc[1];
  15386. AES_KEY dec[1];
  15387. #endif
  15388. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  15389. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  15390. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  15391. };
  15392. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15393. {
  15394. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15395. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15396. };
  15397. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  15398. {
  15399. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  15400. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  15401. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  15402. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  15403. };
  15404. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15405. {
  15406. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15407. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15408. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15409. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  15410. };
  15411. byte cipher[AES_BLOCK_SIZE * 2];
  15412. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  15413. int num = 0;
  15414. #ifdef WOLFSSL_SMALL_STACK
  15415. if ((enc == NULL) || (dec == NULL))
  15416. return MEMORY_E;
  15417. #endif
  15418. XMEMCPY(iv, setIv, sizeof(setIv));
  15419. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  15420. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  15421. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  15422. &num, AES_ENCRYPT);
  15423. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  15424. return -8519;
  15425. if (num != 15) /* should have used 15 of the 16 bytes */
  15426. return -8520;
  15427. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  15428. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  15429. &num, AES_ENCRYPT);
  15430. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  15431. return -8521;
  15432. if (num != 0)
  15433. return -8522;
  15434. #ifdef WOLFSSL_SMALL_STACK
  15435. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15436. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15437. #endif
  15438. }
  15439. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  15440. return 0;
  15441. }
  15442. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  15443. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  15444. {
  15445. int ret;
  15446. EVP_MD_CTX md_ctx;
  15447. testVector a, b, c, d, e, f;
  15448. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  15449. a.inLen = 0;
  15450. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  15451. (void)a;
  15452. (void)b;
  15453. (void)c;
  15454. (void)d;
  15455. (void)e;
  15456. (void)f;
  15457. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  15458. {
  15459. byte* p;
  15460. p = (byte*)CRYPTO_malloc(10);
  15461. if (p == NULL) {
  15462. return -8600;
  15463. }
  15464. XMEMSET(p, 0, 10);
  15465. CRYPTO_free(p);
  15466. }
  15467. #ifndef NO_MD5
  15468. a.input = "1234567890123456789012345678901234567890123456789012345678"
  15469. "9012345678901234567890";
  15470. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  15471. "\x7a";
  15472. a.inLen = XSTRLEN(a.input);
  15473. a.outLen = WC_MD5_DIGEST_SIZE;
  15474. EVP_MD_CTX_init(&md_ctx);
  15475. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  15476. if (ret == WOLFSSL_SUCCESS) {
  15477. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  15478. if (ret == WOLFSSL_SUCCESS)
  15479. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15480. }
  15481. EVP_MD_CTX_cleanup(&md_ctx);
  15482. if (ret != WOLFSSL_SUCCESS ||
  15483. XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  15484. return -8601;
  15485. }
  15486. #endif /* NO_MD5 */
  15487. #ifndef NO_SHA
  15488. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  15489. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  15490. "aaaaaaaaaa";
  15491. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  15492. "\x53\x99\x5E\x26\xA0";
  15493. b.inLen = XSTRLEN(b.input);
  15494. b.outLen = WC_SHA_DIGEST_SIZE;
  15495. EVP_MD_CTX_init(&md_ctx);
  15496. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  15497. if (ret == WOLFSSL_SUCCESS) {
  15498. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  15499. if (ret == WOLFSSL_SUCCESS)
  15500. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15501. }
  15502. EVP_MD_CTX_cleanup(&md_ctx);
  15503. if (ret != WOLFSSL_SUCCESS ||
  15504. XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0) {
  15505. return -8602;
  15506. }
  15507. #endif /* NO_SHA */
  15508. #ifdef WOLFSSL_SHA224
  15509. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15510. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15511. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  15512. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  15513. e.inLen = XSTRLEN(e.input);
  15514. e.outLen = WC_SHA224_DIGEST_SIZE;
  15515. EVP_MD_CTX_init(&md_ctx);
  15516. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  15517. if (ret == WOLFSSL_SUCCESS) {
  15518. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15519. if (ret == WOLFSSL_SUCCESS)
  15520. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15521. }
  15522. EVP_MD_CTX_cleanup(&md_ctx);
  15523. if (ret != WOLFSSL_SUCCESS ||
  15524. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  15525. return -8603;
  15526. }
  15527. #endif /* WOLFSSL_SHA224 */
  15528. #ifndef NO_SHA256
  15529. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  15530. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  15531. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  15532. "\x06\xC1";
  15533. d.inLen = XSTRLEN(d.input);
  15534. d.outLen = WC_SHA256_DIGEST_SIZE;
  15535. EVP_MD_CTX_init(&md_ctx);
  15536. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  15537. if (ret == WOLFSSL_SUCCESS) {
  15538. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  15539. if (ret == WOLFSSL_SUCCESS)
  15540. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15541. }
  15542. EVP_MD_CTX_cleanup(&md_ctx);
  15543. if (ret != WOLFSSL_SUCCESS ||
  15544. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  15545. return -8604;
  15546. }
  15547. #endif /* !NO_SHA256 */
  15548. #ifdef WOLFSSL_SHA384
  15549. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15550. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15551. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  15552. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  15553. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  15554. "\x74\x60\x39";
  15555. e.inLen = XSTRLEN(e.input);
  15556. e.outLen = WC_SHA384_DIGEST_SIZE;
  15557. EVP_MD_CTX_init(&md_ctx);
  15558. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  15559. if (ret == WOLFSSL_SUCCESS) {
  15560. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15561. if (ret == WOLFSSL_SUCCESS)
  15562. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15563. }
  15564. EVP_MD_CTX_cleanup(&md_ctx);
  15565. if (ret != WOLFSSL_SUCCESS ||
  15566. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  15567. return -8605;
  15568. }
  15569. #endif /* WOLFSSL_SHA384 */
  15570. #ifdef WOLFSSL_SHA512
  15571. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15572. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15573. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  15574. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  15575. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  15576. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  15577. "\x87\x4b\xe9\x09";
  15578. f.inLen = XSTRLEN(f.input);
  15579. f.outLen = WC_SHA512_DIGEST_SIZE;
  15580. EVP_MD_CTX_init(&md_ctx);
  15581. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  15582. if (ret == WOLFSSL_SUCCESS) {
  15583. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  15584. if (ret == WOLFSSL_SUCCESS)
  15585. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15586. }
  15587. EVP_MD_CTX_cleanup(&md_ctx);
  15588. if (ret != WOLFSSL_SUCCESS ||
  15589. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  15590. return -8606;
  15591. }
  15592. #endif /* WOLFSSL_SHA512 */
  15593. #ifdef WOLFSSL_SHA3
  15594. #ifndef WOLFSSL_NOSHA3_224
  15595. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15596. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15597. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  15598. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  15599. e.inLen = XSTRLEN(e.input);
  15600. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  15601. EVP_MD_CTX_init(&md_ctx);
  15602. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  15603. if (ret == WOLFSSL_SUCCESS) {
  15604. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15605. if (ret == WOLFSSL_SUCCESS)
  15606. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15607. }
  15608. EVP_MD_CTX_cleanup(&md_ctx);
  15609. if (ret != WOLFSSL_SUCCESS ||
  15610. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  15611. return -8607;
  15612. }
  15613. #endif /* WOLFSSL_NOSHA3_224 */
  15614. #ifndef WOLFSSL_NOSHA3_256
  15615. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15616. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15617. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  15618. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  15619. "\x1d\x18";
  15620. d.inLen = XSTRLEN(d.input);
  15621. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  15622. EVP_MD_CTX_init(&md_ctx);
  15623. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  15624. if (ret == WOLFSSL_SUCCESS) {
  15625. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  15626. if (ret == WOLFSSL_SUCCESS)
  15627. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15628. }
  15629. EVP_MD_CTX_cleanup(&md_ctx);
  15630. if (ret != WOLFSSL_SUCCESS ||
  15631. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  15632. return -8608;
  15633. }
  15634. #endif /* WOLFSSL_NOSHA3_256 */
  15635. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15636. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15637. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  15638. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  15639. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  15640. "\xaa\x7f\xc7";
  15641. e.inLen = XSTRLEN(e.input);
  15642. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  15643. EVP_MD_CTX_init(&md_ctx);
  15644. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  15645. if (ret == WOLFSSL_SUCCESS) {
  15646. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15647. if (ret == WOLFSSL_SUCCESS)
  15648. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15649. }
  15650. EVP_MD_CTX_cleanup(&md_ctx);
  15651. if (ret != WOLFSSL_SUCCESS ||
  15652. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  15653. return -8609;
  15654. }
  15655. #ifndef WOLFSSL_NOSHA3_512
  15656. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15657. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15658. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  15659. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  15660. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  15661. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  15662. "\xf2\x1d\xd1\x85";
  15663. f.inLen = XSTRLEN(f.input);
  15664. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  15665. EVP_MD_CTX_init(&md_ctx);
  15666. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  15667. if (ret == WOLFSSL_SUCCESS) {
  15668. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  15669. if (ret == WOLFSSL_SUCCESS)
  15670. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15671. }
  15672. EVP_MD_CTX_cleanup(&md_ctx);
  15673. if (ret != WOLFSSL_SUCCESS ||
  15674. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  15675. return -8610;
  15676. }
  15677. #endif /* WOLFSSL_NOSHA3_512 */
  15678. #endif /* WOLFSSL_SHA3 */
  15679. #ifndef WC_NO_RNG
  15680. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  15681. return -8611;
  15682. #endif
  15683. #ifndef NO_MD5
  15684. c.input = "what do ya want for nothing?";
  15685. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  15686. "\x76";
  15687. c.inLen = XSTRLEN(c.input);
  15688. c.outLen = WC_MD5_DIGEST_SIZE;
  15689. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  15690. hash, 0) == NULL ||
  15691. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  15692. {
  15693. return -8612;
  15694. }
  15695. #endif /* NO_MD5 */
  15696. #ifndef NO_DES3
  15697. { /* des test */
  15698. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  15699. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15700. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15701. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15702. };
  15703. byte plain[24];
  15704. byte cipher[24];
  15705. const_DES_cblock key = {
  15706. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  15707. };
  15708. DES_cblock iv = {
  15709. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  15710. };
  15711. DES_key_schedule sched;
  15712. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15713. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  15714. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  15715. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  15716. };
  15717. DES_key_sched(&key, &sched);
  15718. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  15719. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  15720. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  15721. return -8613;
  15722. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  15723. return -8614;
  15724. /* test changing iv */
  15725. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  15726. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  15727. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  15728. return -8615;
  15729. } /* end des test */
  15730. #endif /* NO_DES3 */
  15731. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  15732. if (openssl_aes_test() != 0) {
  15733. return -8616;
  15734. }
  15735. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  15736. { /* evp_cipher test: EVP_aes_128_cbc */
  15737. #ifdef WOLFSSL_SMALL_STACK
  15738. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15739. #else
  15740. EVP_CIPHER_CTX ctx[1];
  15741. #endif
  15742. int idx, cipherSz, plainSz;
  15743. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  15744. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15745. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15746. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15747. };
  15748. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15749. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15750. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  15751. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  15752. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  15753. };
  15754. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  15755. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15756. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  15757. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  15758. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  15759. };
  15760. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15761. "0123456789abcdef "; /* align */
  15762. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15763. "1234567890abcdef "; /* align */
  15764. byte cipher[AES_BLOCK_SIZE * 4];
  15765. byte plain [AES_BLOCK_SIZE * 4];
  15766. #ifdef WOLFSSL_SMALL_STACK
  15767. if (ctx == NULL)
  15768. return MEMORY_E;
  15769. #endif
  15770. cipherSz = 0;
  15771. EVP_CIPHER_CTX_init(ctx);
  15772. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  15773. if (ret == WOLFSSL_SUCCESS) {
  15774. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  15775. if (ret == WOLFSSL_SUCCESS)
  15776. cipherSz += idx;
  15777. }
  15778. if (ret == WOLFSSL_SUCCESS) {
  15779. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  15780. if (ret == WOLFSSL_SUCCESS)
  15781. cipherSz += idx;
  15782. }
  15783. EVP_CIPHER_CTX_cleanup(ctx);
  15784. if (ret != WOLFSSL_SUCCESS)
  15785. return -8617;
  15786. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  15787. return -8618;
  15788. /* check partial decrypt (not enough padding for full block) */
  15789. plainSz = 0;
  15790. EVP_CIPHER_CTX_init(ctx);
  15791. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  15792. if (ret == WOLFSSL_SUCCESS) {
  15793. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  15794. if (ret == WOLFSSL_SUCCESS)
  15795. plainSz += idx;
  15796. }
  15797. if (ret == WOLFSSL_SUCCESS) {
  15798. /* this test should fail... not enough padding for full block */
  15799. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  15800. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  15801. ret = WOLFSSL_SUCCESS;
  15802. else
  15803. ret = -8619;
  15804. }
  15805. else
  15806. ret = -8620;
  15807. EVP_CIPHER_CTX_cleanup(ctx);
  15808. if (ret != WOLFSSL_SUCCESS)
  15809. return ret;
  15810. plainSz = 0;
  15811. EVP_CIPHER_CTX_init(ctx);
  15812. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  15813. if (ret == WOLFSSL_SUCCESS) {
  15814. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  15815. if (ret == WOLFSSL_SUCCESS)
  15816. plainSz += idx;
  15817. }
  15818. if (ret == WOLFSSL_SUCCESS) {
  15819. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  15820. if (ret == WOLFSSL_SUCCESS)
  15821. plainSz += idx;
  15822. }
  15823. EVP_CIPHER_CTX_cleanup(ctx);
  15824. if (ret != WOLFSSL_SUCCESS)
  15825. return -8621;
  15826. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  15827. return -8622;
  15828. cipherSz = 0;
  15829. EVP_CIPHER_CTX_init(ctx);
  15830. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  15831. if (ret == WOLFSSL_SUCCESS) {
  15832. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  15833. if (ret == WOLFSSL_SUCCESS)
  15834. cipherSz += idx;
  15835. }
  15836. if (ret == WOLFSSL_SUCCESS) {
  15837. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  15838. if (ret == WOLFSSL_SUCCESS)
  15839. cipherSz += idx;
  15840. }
  15841. EVP_CIPHER_CTX_cleanup(ctx);
  15842. if (ret != WOLFSSL_SUCCESS)
  15843. return -8623;
  15844. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  15845. return -8624;
  15846. #ifdef WOLFSSL_SMALL_STACK
  15847. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15848. #endif
  15849. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15850. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  15851. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15852. { /* evp_cipher test: EVP_aes_256_ecb*/
  15853. #ifdef WOLFSSL_SMALL_STACK
  15854. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15855. #else
  15856. EVP_CIPHER_CTX ctx[1];
  15857. #endif
  15858. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  15859. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15860. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15861. };
  15862. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15863. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15864. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15865. };
  15866. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  15867. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15868. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15869. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15870. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15871. };
  15872. byte cipher[AES_BLOCK_SIZE * 4];
  15873. byte plain [AES_BLOCK_SIZE * 4];
  15874. #ifdef WOLFSSL_SMALL_STACK
  15875. if (ctx == NULL)
  15876. return MEMORY_E;
  15877. #endif
  15878. EVP_CIPHER_CTX_init(ctx);
  15879. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  15880. if (ret == WOLFSSL_SUCCESS)
  15881. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  15882. EVP_CIPHER_CTX_cleanup(ctx);
  15883. if (ret != 16)
  15884. return -8625;
  15885. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15886. return -8626;
  15887. EVP_CIPHER_CTX_init(ctx);
  15888. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  15889. if (ret == WOLFSSL_SUCCESS)
  15890. ret = EVP_Cipher(ctx, plain, cipher, 16);
  15891. EVP_CIPHER_CTX_cleanup(ctx);
  15892. if (ret != 16)
  15893. return -8627;
  15894. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15895. return -8628;
  15896. #ifdef WOLFSSL_SMALL_STACK
  15897. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15898. #endif
  15899. } /* end evp_cipher test */
  15900. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  15901. #define OPENSSL_TEST_ERROR (-10000)
  15902. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  15903. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  15904. {
  15905. /* Test: AES_encrypt/decrypt/set Key */
  15906. #ifdef WOLFSSL_SMALL_STACK
  15907. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15908. #ifdef HAVE_AES_DECRYPT
  15909. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15910. #endif
  15911. #else
  15912. AES_KEY enc[1];
  15913. #ifdef HAVE_AES_DECRYPT
  15914. AES_KEY dec[1];
  15915. #endif
  15916. #endif
  15917. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15918. {
  15919. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15920. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15921. };
  15922. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15923. {
  15924. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15925. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15926. };
  15927. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15928. {
  15929. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15930. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15931. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15932. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15933. };
  15934. byte plain[sizeof(msg)];
  15935. byte cipher[sizeof(msg)];
  15936. printf("openSSL extra test\n") ;
  15937. #ifdef WOLFSSL_SMALL_STACK
  15938. if (enc == NULL)
  15939. return MEMORY_E;
  15940. #ifdef HAVE_AES_DECRYPT
  15941. if (dec == NULL)
  15942. return MEMORY_E;
  15943. #endif
  15944. #endif
  15945. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  15946. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  15947. AES_encrypt(msg, cipher, enc);
  15948. #ifdef HAVE_AES_DECRYPT
  15949. AES_decrypt(cipher, plain, dec);
  15950. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15951. return OPENSSL_TEST_ERROR-60;
  15952. #endif /* HAVE_AES_DECRYPT */
  15953. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15954. return OPENSSL_TEST_ERROR-61;
  15955. #ifdef WOLFSSL_SMALL_STACK
  15956. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15957. #ifdef HAVE_AES_DECRYPT
  15958. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15959. #endif
  15960. #endif
  15961. }
  15962. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  15963. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  15964. #ifdef WOLFSSL_AES_COUNTER
  15965. {
  15966. byte plainBuff [64];
  15967. byte cipherBuff[64];
  15968. #ifdef WOLFSSL_AES_128
  15969. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  15970. {
  15971. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15972. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15973. };
  15974. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  15975. {
  15976. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15977. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15978. };
  15979. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  15980. {
  15981. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15982. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15983. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15984. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15985. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15986. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15987. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15988. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15989. };
  15990. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  15991. {
  15992. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  15993. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  15994. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  15995. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  15996. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  15997. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  15998. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  15999. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16000. };
  16001. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16002. {
  16003. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16004. 0xc2
  16005. };
  16006. #endif /* WOLFSSL_AES_128 */
  16007. #ifdef WOLFSSL_AES_192
  16008. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16009. * NIST Special Publication 800-38A */
  16010. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16011. {
  16012. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16013. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16014. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16015. };
  16016. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16017. {
  16018. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16019. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16020. };
  16021. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16022. {
  16023. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16024. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16025. };
  16026. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16027. {
  16028. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16029. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16030. };
  16031. #endif /* WOLFSSL_AES_192 */
  16032. #ifdef WOLFSSL_AES_256
  16033. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16034. * NIST Special Publication 800-38A */
  16035. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16036. {
  16037. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16038. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16039. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16040. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16041. };
  16042. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16043. {
  16044. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16045. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16046. };
  16047. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16048. {
  16049. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16050. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16051. };
  16052. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16053. {
  16054. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16055. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16056. };
  16057. #endif /* WOLFSSL_AES_256 */
  16058. #ifdef WOLFSSL_SMALL_STACK
  16059. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16060. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16061. if ((en == NULL) || (de == NULL))
  16062. return MEMORY_E;
  16063. #else
  16064. EVP_CIPHER_CTX en[1];
  16065. EVP_CIPHER_CTX de[1];
  16066. #endif
  16067. #ifdef WOLFSSL_AES_128
  16068. #ifndef WOLFSSL_SMALL_STACK
  16069. EVP_CIPHER_CTX *p_en;
  16070. EVP_CIPHER_CTX *p_de;
  16071. #endif
  16072. EVP_CIPHER_CTX_init(en);
  16073. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16074. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16075. return -8629;
  16076. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16077. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16078. return -8630;
  16079. EVP_CIPHER_CTX_init(de);
  16080. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16081. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16082. return -8631;
  16083. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16084. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16085. return -8632;
  16086. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16087. return -8633;
  16088. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16089. return -8634;
  16090. #ifndef WOLFSSL_SMALL_STACK
  16091. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16092. if(p_en == NULL)return -8635;
  16093. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16094. if(p_de == NULL)return -8636;
  16095. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16096. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16097. return -8637;
  16098. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16099. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16100. return -8638;
  16101. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16102. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16103. return -8639;
  16104. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16105. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16106. return -8640;
  16107. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16108. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16109. #endif /* !WOLFSSL_SMALL_STACK */
  16110. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16111. return -8641;
  16112. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16113. return -8642;
  16114. EVP_CIPHER_CTX_init(en);
  16115. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16116. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16117. return -8643;
  16118. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16119. return -8644;
  16120. EVP_CIPHER_CTX_init(de);
  16121. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16122. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16123. return -8645;
  16124. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16125. return -8646;
  16126. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16127. return -8647;
  16128. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16129. return -8648;
  16130. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16131. return -8649;
  16132. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16133. return -8650;
  16134. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16135. return -8651;
  16136. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16137. return -8652;
  16138. #endif /* WOLFSSL_AES_128 */
  16139. #ifdef WOLFSSL_AES_192
  16140. EVP_CIPHER_CTX_init(en);
  16141. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16142. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16143. return -8653;
  16144. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16145. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16146. return -8654;
  16147. EVP_CIPHER_CTX_init(de);
  16148. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16149. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16150. return -8655;
  16151. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16152. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16153. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16154. return -8656;
  16155. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16156. return -8657;
  16157. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16158. return -8658;
  16159. #endif /* WOLFSSL_AES_192 */
  16160. #ifdef WOLFSSL_AES_256
  16161. EVP_CIPHER_CTX_init(en);
  16162. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16163. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16164. return -8659;
  16165. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16166. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16167. return -8660;
  16168. EVP_CIPHER_CTX_init(de);
  16169. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16170. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16171. return -8661;
  16172. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16173. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16174. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16175. return -8662;
  16176. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16177. return -8663;
  16178. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16179. return -8664;
  16180. #endif /* WOLFSSL_AES_256 */
  16181. #ifdef WOLFSSL_SMALL_STACK
  16182. wolfSSL_EVP_CIPHER_CTX_free(en);
  16183. wolfSSL_EVP_CIPHER_CTX_free(de);
  16184. #endif
  16185. }
  16186. #endif /* HAVE_AES_COUNTER */
  16187. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  16188. {
  16189. /* EVP_CipherUpdate test */
  16190. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16191. {
  16192. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16193. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16194. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16195. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16196. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16197. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16198. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16199. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16200. };
  16201. byte key[] = "0123456789abcdef "; /* align */
  16202. byte iv[] = "1234567890abcdef "; /* align */
  16203. byte cipher[AES_BLOCK_SIZE * 4];
  16204. byte plain [AES_BLOCK_SIZE * 4];
  16205. #ifdef WOLFSSL_SMALL_STACK
  16206. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16207. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16208. #else
  16209. EVP_CIPHER_CTX en[1];
  16210. EVP_CIPHER_CTX de[1];
  16211. #endif
  16212. int outlen ;
  16213. int total = 0;
  16214. #ifdef WOLFSSL_SMALL_STACK
  16215. if ((en == NULL) || (de == NULL))
  16216. return MEMORY_E;
  16217. #endif
  16218. EVP_CIPHER_CTX_init(en);
  16219. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16220. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16221. return -8665;
  16222. /* openSSL compatibility, if(inlen == 0)return 1; */
  16223. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16224. (byte*)cbcPlain, 0) != 1)
  16225. return -8666;
  16226. EVP_CIPHER_CTX_init(en);
  16227. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16228. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16229. return -8667;
  16230. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16231. (byte*)cbcPlain, 9) == 0)
  16232. return -8668;
  16233. if(outlen != 0)
  16234. return -8669;
  16235. total += outlen;
  16236. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16237. (byte*)&cbcPlain[9] , 9) == 0)
  16238. return -8670;
  16239. if(outlen != 16)
  16240. return -8671;
  16241. total += outlen;
  16242. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16243. return -8672;
  16244. if(outlen != 16)
  16245. return -8673;
  16246. total += outlen;
  16247. if(total != 32)
  16248. return -8674;
  16249. total = 0;
  16250. EVP_CIPHER_CTX_init(de);
  16251. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16252. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16253. return -8675;
  16254. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16255. return -8676;
  16256. if(outlen != 0)
  16257. return -8677;
  16258. total += outlen;
  16259. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16260. (byte*)&cipher[6], 12) == 0)
  16261. return -8678;
  16262. if(outlen != 0)
  16263. total += outlen;
  16264. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16265. (byte*)&cipher[6+12], 14) == 0)
  16266. return -8679;
  16267. if(outlen != 16)
  16268. return -8680;
  16269. total += outlen;
  16270. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16271. return -8681;
  16272. if(outlen != 2)
  16273. return -8682;
  16274. total += outlen;
  16275. if(total != 18)
  16276. return -8683;
  16277. if (XMEMCMP(plain, cbcPlain, 18))
  16278. return -8684;
  16279. total = 0;
  16280. EVP_CIPHER_CTX_init(en);
  16281. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  16282. (unsigned char*)key, (unsigned char*)iv) == 0)
  16283. return -8685;
  16284. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  16285. return -8686;
  16286. if(outlen != 0)
  16287. return -8687;
  16288. total += outlen;
  16289. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  16290. return -8688;
  16291. if(outlen != 16)
  16292. return -8689;
  16293. total += outlen;
  16294. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16295. return -8690;
  16296. if(outlen != 16)
  16297. return -8691;
  16298. total += outlen;
  16299. if(total != 32)
  16300. return 3438;
  16301. total = 0;
  16302. EVP_CIPHER_CTX_init(de);
  16303. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  16304. (unsigned char*)key, (unsigned char*)iv) == 0)
  16305. return -8692;
  16306. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16307. return -8693;
  16308. if(outlen != 0)
  16309. return -8694;
  16310. total += outlen;
  16311. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  16312. return -8695;
  16313. if(outlen != 0)
  16314. total += outlen;
  16315. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  16316. return -8696;
  16317. if(outlen != 16)
  16318. return -8697;
  16319. total += outlen;
  16320. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  16321. return -8698;
  16322. if(outlen != 2)
  16323. return -8699;
  16324. total += outlen;
  16325. if(total != 18)
  16326. return 3447;
  16327. if (XMEMCMP(plain, cbcPlain, 18))
  16328. return -8700;
  16329. if (EVP_CIPHER_key_length(NULL) != 0)
  16330. return -8701;
  16331. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  16332. return -8702;
  16333. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  16334. return -8703;
  16335. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  16336. return -8704;
  16337. EVP_CIPHER_CTX_init(en);
  16338. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  16339. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16340. return -8705;
  16341. EVP_CIPHER_CTX_init(en);
  16342. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16343. (unsigned char*)key, (unsigned char*)iv) == 0)
  16344. return -8706;
  16345. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16346. return -8707;
  16347. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16348. return -8708;
  16349. EVP_CIPHER_CTX_init(de);
  16350. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  16351. (unsigned char*)key, (unsigned char*)iv) == 0)
  16352. return -8709;
  16353. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16354. return -8710;
  16355. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16356. return -8711;
  16357. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  16358. return -8712;
  16359. EVP_CIPHER_CTX_init(en);
  16360. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16361. (unsigned char*)key, (unsigned char*)iv);
  16362. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  16363. return -8713;
  16364. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  16365. return -8714;
  16366. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  16367. return -8715;
  16368. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  16369. return -8716;
  16370. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  16371. return -8717;
  16372. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  16373. EVP_CIPHER_CTX_set_flags(en, 42);
  16374. if (en->flags != 42)
  16375. return -8718;
  16376. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  16377. return -8719;
  16378. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  16379. return -8720;
  16380. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  16381. return -8721;
  16382. #ifdef WOLFSSL_SMALL_STACK
  16383. wolfSSL_EVP_CIPHER_CTX_free(en);
  16384. wolfSSL_EVP_CIPHER_CTX_free(de);
  16385. #endif
  16386. }
  16387. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  16388. #endif /* ifndef NO_AES */
  16389. return 0;
  16390. }
  16391. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  16392. {
  16393. int ret = 0;
  16394. #if !defined(NO_SHA256) && !defined(NO_SHA)
  16395. WOLFSSL_EVP_MD_CTX* ctx;
  16396. WOLFSSL_EVP_MD_CTX* ctx2;
  16397. ctx = EVP_MD_CTX_create();
  16398. ctx2 = EVP_MD_CTX_create();
  16399. ret = EVP_DigestInit(ctx, EVP_sha256());
  16400. if (ret != SSL_SUCCESS) {
  16401. ret = -8800;
  16402. goto openSSL_evpMD_test_done;
  16403. }
  16404. ret = EVP_MD_CTX_copy(ctx2, ctx);
  16405. if (ret != SSL_SUCCESS) {
  16406. ret = -8801;
  16407. goto openSSL_evpMD_test_done;
  16408. }
  16409. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  16410. ret = -8802;
  16411. goto openSSL_evpMD_test_done;
  16412. }
  16413. ret = EVP_DigestInit(ctx, EVP_sha1());
  16414. if (ret != SSL_SUCCESS) {
  16415. ret = -8803;
  16416. goto openSSL_evpMD_test_done;
  16417. }
  16418. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  16419. ret = -8804;
  16420. goto openSSL_evpMD_test_done;
  16421. }
  16422. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  16423. if (ret != SSL_SUCCESS) {
  16424. ret = -8805;
  16425. goto openSSL_evpMD_test_done;
  16426. }
  16427. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  16428. ret = -8806;
  16429. goto openSSL_evpMD_test_done;
  16430. }
  16431. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  16432. ret = -8807;
  16433. goto openSSL_evpMD_test_done;
  16434. }
  16435. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  16436. ret = -8808;
  16437. goto openSSL_evpMD_test_done;
  16438. }
  16439. if (EVP_add_digest(NULL) != 0) {
  16440. ret = -8809;
  16441. goto openSSL_evpMD_test_done;
  16442. }
  16443. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  16444. ret = -8810;
  16445. goto openSSL_evpMD_test_done;
  16446. }
  16447. ret = 0; /* got to success state without jumping to end with a fail */
  16448. openSSL_evpMD_test_done:
  16449. EVP_MD_CTX_destroy(ctx);
  16450. EVP_MD_CTX_destroy(ctx2);
  16451. #endif /* NO_SHA256 */
  16452. return ret;
  16453. }
  16454. #ifdef DEBUG_SIGN
  16455. static void show(const char *title, const char *p, unsigned int s) {
  16456. char* i;
  16457. printf("%s: ", title);
  16458. for (i = p;
  16459. i < p + s;
  16460. printf("%c", *i), i++);
  16461. printf("\n");
  16462. }
  16463. #else
  16464. #define show(a,b,c)
  16465. #endif
  16466. #define FOURK_BUFF 4096
  16467. #define ERR_BASE_PKEY -5000
  16468. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  16469. {
  16470. int ret = 0;
  16471. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  16472. byte* prvTmp;
  16473. byte* pubTmp;
  16474. int prvBytes;
  16475. int pubBytes;
  16476. RSA *prvRsa = NULL;
  16477. RSA *pubRsa = NULL;
  16478. EVP_PKEY *prvPkey = NULL;
  16479. EVP_PKEY *pubPkey = NULL;
  16480. EVP_PKEY_CTX *enc = NULL;
  16481. EVP_PKEY_CTX *dec = NULL;
  16482. byte in[] = TEST_STRING;
  16483. byte out[256];
  16484. size_t outlen;
  16485. size_t keySz;
  16486. byte plain[256];
  16487. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16488. XFILE keyFile;
  16489. XFILE keypubFile;
  16490. char cliKey[] = "./certs/client-key.der";
  16491. char cliKeypub[] = "./certs/client-keyPub.der";
  16492. #endif
  16493. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16494. if (prvTmp == NULL)
  16495. return ERR_BASE_PKEY-1;
  16496. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16497. if (pubTmp == NULL) {
  16498. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16499. return ERR_BASE_PKEY-2;
  16500. }
  16501. #ifdef USE_CERT_BUFFERS_1024
  16502. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  16503. prvBytes = sizeof_client_key_der_1024;
  16504. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16505. pubBytes = sizeof_client_keypub_der_1024;
  16506. #elif defined(USE_CERT_BUFFERS_2048)
  16507. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  16508. prvBytes = sizeof_client_key_der_2048;
  16509. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16510. pubBytes = sizeof_client_keypub_der_2048;
  16511. #else
  16512. keyFile = XFOPEN(cliKey, "rb");
  16513. if (!keyFile) {
  16514. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16515. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16516. err_sys("can't open ./certs/client-key.der, "
  16517. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  16518. return ERR_BASE_PKEY-3;
  16519. }
  16520. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  16521. XFCLOSE(keyFile);
  16522. keypubFile = XFOPEN(cliKeypub, "rb");
  16523. if (!keypubFile) {
  16524. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16525. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16526. err_sys("can't open ./certs/client-cert.der, "
  16527. "Please run from wolfSSL home dir", -4);
  16528. return ERR_BASE_PKEY-4;
  16529. }
  16530. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  16531. XFCLOSE(keypubFile);
  16532. #endif /* USE_CERT_BUFFERS */
  16533. prvRsa = wolfSSL_RSA_new();
  16534. pubRsa = wolfSSL_RSA_new();
  16535. if((prvRsa == NULL) || (pubRsa == NULL)){
  16536. printf("error with RSA_new\n");
  16537. ret = ERR_BASE_PKEY-10;
  16538. goto openssl_pkey0_test_done;
  16539. }
  16540. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  16541. if(ret != SSL_SUCCESS){
  16542. printf("error with RSA_LoadDer_ex\n");
  16543. ret = ERR_BASE_PKEY-11;
  16544. goto openssl_pkey0_test_done;
  16545. }
  16546. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  16547. if(ret != SSL_SUCCESS){
  16548. printf("error with RSA_LoadDer_ex\n");
  16549. ret = ERR_BASE_PKEY-12;
  16550. goto openssl_pkey0_test_done;
  16551. }
  16552. keySz = (size_t)RSA_size(pubRsa);
  16553. prvPkey = wolfSSL_EVP_PKEY_new();
  16554. pubPkey = wolfSSL_EVP_PKEY_new();
  16555. if((prvPkey == NULL) || (pubPkey == NULL)){
  16556. printf("error with PKEY_new\n");
  16557. ret = ERR_BASE_PKEY-13;
  16558. goto openssl_pkey0_test_done;
  16559. }
  16560. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  16561. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  16562. if(ret != 2){
  16563. printf("error with PKEY_set1_RSA\n");
  16564. ret = ERR_BASE_PKEY-14;
  16565. goto openssl_pkey0_test_done;
  16566. }
  16567. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  16568. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  16569. if((dec == NULL)||(enc==NULL)){
  16570. printf("error with EVP_PKEY_CTX_new\n");
  16571. ret = ERR_BASE_PKEY-15;
  16572. goto openssl_pkey0_test_done;
  16573. }
  16574. ret = EVP_PKEY_decrypt_init(dec);
  16575. if (ret != 1) {
  16576. printf("error with decrypt init\n");
  16577. ret = ERR_BASE_PKEY-16;
  16578. goto openssl_pkey0_test_done;
  16579. }
  16580. ret = EVP_PKEY_encrypt_init(enc);
  16581. if (ret != 1) {
  16582. printf("error with encrypt init\n");
  16583. ret = ERR_BASE_PKEY-17;
  16584. goto openssl_pkey0_test_done;
  16585. }
  16586. XMEMSET(out, 0, sizeof(out));
  16587. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  16588. if (ret != 1) {
  16589. printf("error encrypting msg\n");
  16590. ret = ERR_BASE_PKEY-18;
  16591. goto openssl_pkey0_test_done;
  16592. }
  16593. show("encrypted msg", out, outlen);
  16594. XMEMSET(plain, 0, sizeof(plain));
  16595. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  16596. if (ret != 1) {
  16597. printf("error decrypting msg\n");
  16598. ret = ERR_BASE_PKEY-19;
  16599. goto openssl_pkey0_test_done;
  16600. }
  16601. show("decrypted msg", plain, outlen);
  16602. /* RSA_PKCS1_OAEP_PADDING test */
  16603. ret = EVP_PKEY_decrypt_init(dec);
  16604. if (ret != 1) {
  16605. printf("error with decrypt init\n");
  16606. ret = ERR_BASE_PKEY-30;
  16607. goto openssl_pkey0_test_done;
  16608. }
  16609. ret = EVP_PKEY_encrypt_init(enc);
  16610. if (ret != 1) {
  16611. printf("error with encrypt init\n");
  16612. ret = ERR_BASE_PKEY-31;
  16613. goto openssl_pkey0_test_done;
  16614. }
  16615. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  16616. printf("first set rsa padding error\n");
  16617. ret = ERR_BASE_PKEY-32;
  16618. goto openssl_pkey0_test_done;
  16619. }
  16620. #ifndef HAVE_FIPS
  16621. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  16622. printf("second set rsa padding error\n");
  16623. ret = ERR_BASE_PKEY-33;
  16624. goto openssl_pkey0_test_done;
  16625. }
  16626. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  16627. printf("third set rsa padding error\n");
  16628. ret = ERR_BASE_PKEY-34;
  16629. goto openssl_pkey0_test_done;
  16630. }
  16631. #endif
  16632. XMEMSET(out, 0, sizeof(out));
  16633. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  16634. if (ret != 1) {
  16635. printf("error encrypting msg\n");
  16636. ret = ERR_BASE_PKEY-35;
  16637. goto openssl_pkey0_test_done;
  16638. }
  16639. show("encrypted msg", out, outlen);
  16640. XMEMSET(plain, 0, sizeof(plain));
  16641. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  16642. if (ret != 1) {
  16643. printf("error decrypting msg\n");
  16644. ret = ERR_BASE_PKEY-36;
  16645. goto openssl_pkey0_test_done;
  16646. }
  16647. show("decrypted msg", plain, outlen);
  16648. ret = 0; /* made it to this point without error then set success */
  16649. openssl_pkey0_test_done:
  16650. wolfSSL_RSA_free(prvRsa);
  16651. wolfSSL_RSA_free(pubRsa);
  16652. EVP_PKEY_free(pubPkey);
  16653. EVP_PKEY_free(prvPkey);
  16654. EVP_PKEY_CTX_free(dec);
  16655. EVP_PKEY_CTX_free(enc);
  16656. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16657. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16658. #endif /* NO_RSA */
  16659. return ret;
  16660. }
  16661. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  16662. {
  16663. int ret = 0;
  16664. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  16665. !defined(NO_SHA)
  16666. EVP_PKEY_CTX* dec = NULL;
  16667. EVP_PKEY_CTX* enc = NULL;
  16668. EVP_PKEY* pubKey = NULL;
  16669. EVP_PKEY* prvKey = NULL;
  16670. X509* x509 = NULL;
  16671. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  16672. const unsigned char* clikey;
  16673. long cliKeySz;
  16674. size_t outlen;
  16675. int keyLenBits = 2048;
  16676. #ifdef WOLFSSL_SMALL_STACK
  16677. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16678. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16679. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16680. if ((tmp == NULL) ||
  16681. (cipher == NULL) ||
  16682. (plain == NULL)) {
  16683. ret = -9015;
  16684. goto openssl_pkey1_test_done;
  16685. }
  16686. #else
  16687. unsigned char tmp[FOURK_BUF];
  16688. unsigned char cipher[RSA_TEST_BYTES];
  16689. unsigned char plain[RSA_TEST_BYTES];
  16690. #endif
  16691. #if defined(USE_CERT_BUFFERS_1024)
  16692. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16693. cliKeySz = (long)sizeof_client_key_der_1024;
  16694. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  16695. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  16696. keyLenBits = 1024;
  16697. #elif defined(USE_CERT_BUFFERS_2048)
  16698. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16699. cliKeySz = (long)sizeof_client_key_der_2048;
  16700. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  16701. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  16702. #elif defined(USE_CERT_BUFFERS_3072)
  16703. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  16704. cliKeySz = (long)sizeof_client_key_der_3072;
  16705. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  16706. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  16707. keyLenBits = 3072;
  16708. #elif defined(USE_CERT_BUFFERS_4096)
  16709. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  16710. cliKeySz = (long)sizeof_client_key_der_4096;
  16711. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  16712. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  16713. keyLenBits = 4096;
  16714. #else
  16715. XFILE f;
  16716. f = XFOPEN(clientKey, "rb");
  16717. if (!f) {
  16718. err_sys("can't open ./certs/client-key.der, "
  16719. "Please run from wolfSSL home dir", -41);
  16720. ret = -9000;
  16721. goto openssl_pkey1_test_done;
  16722. }
  16723. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  16724. XFCLOSE(f);
  16725. /* using existing wolfSSL api to get public and private key */
  16726. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  16727. #endif /* USE_CERT_BUFFERS */
  16728. clikey = tmp;
  16729. if ((prvKey = EVP_PKEY_new()) == NULL) {
  16730. ret = -9001;
  16731. goto openssl_pkey1_test_done;
  16732. }
  16733. EVP_PKEY_free(prvKey);
  16734. prvKey = NULL;
  16735. if (x509 == NULL) {
  16736. ret = -9002;
  16737. goto openssl_pkey1_test_done;
  16738. }
  16739. pubKey = X509_get_pubkey(x509);
  16740. if (pubKey == NULL) {
  16741. ret = -9003;
  16742. goto openssl_pkey1_test_done;
  16743. }
  16744. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  16745. if (prvKey == NULL) {
  16746. ret = -9004;
  16747. goto openssl_pkey1_test_done;
  16748. }
  16749. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  16750. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  16751. ret = -9005;
  16752. goto openssl_pkey1_test_done;
  16753. }
  16754. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  16755. ret = -9006;
  16756. goto openssl_pkey1_test_done;
  16757. }
  16758. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  16759. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  16760. if (dec == NULL || enc == NULL) {
  16761. ret = -9007;
  16762. goto openssl_pkey1_test_done;
  16763. }
  16764. if (EVP_PKEY_decrypt_init(dec) != 1) {
  16765. ret = -9008;
  16766. goto openssl_pkey1_test_done;
  16767. }
  16768. if (EVP_PKEY_encrypt_init(enc) != 1) {
  16769. ret = -9009;
  16770. goto openssl_pkey1_test_done;
  16771. }
  16772. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  16773. ret = -9010;
  16774. goto openssl_pkey1_test_done;
  16775. }
  16776. #ifndef HAVE_FIPS
  16777. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  16778. ret = -9011;
  16779. goto openssl_pkey1_test_done;
  16780. }
  16781. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  16782. ret = -9012;
  16783. goto openssl_pkey1_test_done;
  16784. }
  16785. #endif
  16786. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  16787. outlen = keyLenBits/8;
  16788. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  16789. ret = -9013;
  16790. goto openssl_pkey1_test_done;
  16791. }
  16792. XMEMSET(plain, 0, RSA_TEST_BYTES);
  16793. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  16794. ret = -9014;
  16795. goto openssl_pkey1_test_done;
  16796. }
  16797. openssl_pkey1_test_done:
  16798. if (pubKey != NULL) {
  16799. EVP_PKEY_free(pubKey);
  16800. }
  16801. if (prvKey != NULL) {
  16802. EVP_PKEY_free(prvKey);
  16803. }
  16804. if (dec != NULL) {
  16805. EVP_PKEY_CTX_free(dec);
  16806. }
  16807. if (enc != NULL) {
  16808. EVP_PKEY_CTX_free(enc);
  16809. }
  16810. if (x509 != NULL) {
  16811. X509_free(x509);
  16812. }
  16813. #ifdef WOLFSSL_SMALL_STACK
  16814. if (tmp != NULL)
  16815. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16816. if (cipher != NULL)
  16817. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16818. if (plain != NULL)
  16819. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16820. #endif
  16821. #endif
  16822. return ret;
  16823. }
  16824. #define ERR_BASE_EVPSIG -5100
  16825. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  16826. {
  16827. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  16828. byte* prvTmp;
  16829. byte* pubTmp;
  16830. int prvBytes;
  16831. int pubBytes;
  16832. RSA *prvRsa;
  16833. RSA *pubRsa;
  16834. EVP_PKEY *prvPkey;
  16835. EVP_PKEY *pubPkey;
  16836. EVP_MD_CTX* sign;
  16837. EVP_MD_CTX* verf;
  16838. char msg[] = "see spot run";
  16839. unsigned char sig[256];
  16840. unsigned int sigSz;
  16841. const void* pt;
  16842. unsigned int count;
  16843. int ret, ret1, ret2;
  16844. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16845. XFILE keyFile;
  16846. XFILE keypubFile;
  16847. char cliKey[] = "./certs/client-key.der";
  16848. char cliKeypub[] = "./certs/client-keyPub.der";
  16849. #endif
  16850. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16851. if (prvTmp == NULL)
  16852. return ERR_BASE_EVPSIG-1;
  16853. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16854. if (pubTmp == NULL) {
  16855. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16856. return ERR_BASE_EVPSIG-2;
  16857. }
  16858. #ifdef USE_CERT_BUFFERS_1024
  16859. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  16860. prvBytes = sizeof_client_key_der_1024;
  16861. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16862. pubBytes = sizeof_client_keypub_der_1024;
  16863. #elif defined(USE_CERT_BUFFERS_2048)
  16864. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  16865. prvBytes = sizeof_client_key_der_2048;
  16866. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16867. pubBytes = sizeof_client_keypub_der_2048;
  16868. #else
  16869. keyFile = XFOPEN(cliKey, "rb");
  16870. if (!keyFile) {
  16871. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16872. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16873. err_sys("can't open ./certs/client-key.der, "
  16874. "Please run from wolfSSL home dir", -40);
  16875. return ERR_BASE_EVPSIG-3;
  16876. }
  16877. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  16878. XFCLOSE(keyFile);
  16879. keypubFile = XFOPEN(cliKeypub, "rb");
  16880. if (!keypubFile) {
  16881. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16882. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16883. err_sys("can't open ./certs/client-cert.der, "
  16884. "Please run from wolfSSL home dir", -41);
  16885. return ERR_BASE_EVPSIG-4;
  16886. }
  16887. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  16888. XFCLOSE(keypubFile);
  16889. #endif /* USE_CERT_BUFFERS */
  16890. prvRsa = wolfSSL_RSA_new();
  16891. pubRsa = wolfSSL_RSA_new();
  16892. if((prvRsa == NULL) || (pubRsa == NULL)){
  16893. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16894. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16895. err_sys("ERROR with RSA_new", -9100);
  16896. return ERR_BASE_EVPSIG-5;
  16897. }
  16898. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  16899. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  16900. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  16901. printf("error with RSA_LoadDer_ex\n");
  16902. return ERR_BASE_EVPSIG-6;
  16903. }
  16904. prvPkey = wolfSSL_EVP_PKEY_new();
  16905. pubPkey = wolfSSL_EVP_PKEY_new();
  16906. if((prvPkey == NULL) || (pubPkey == NULL)){
  16907. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16908. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16909. printf("error with KEY_new\n");
  16910. return ERR_BASE_EVPSIG-7;
  16911. }
  16912. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  16913. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  16914. if((ret1 != 1) || (ret2 != 1)){
  16915. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16916. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16917. printf("error with EVP_PKEY_set1_RSA\n");
  16918. return ERR_BASE_EVPSIG-8;
  16919. }
  16920. /****************** sign and verify *******************/
  16921. sign = EVP_MD_CTX_create();
  16922. verf = EVP_MD_CTX_create();
  16923. if((sign == NULL)||(verf == NULL)){
  16924. printf("error with EVP_MD_CTX_create\n");
  16925. EVP_MD_CTX_destroy(sign);
  16926. EVP_MD_CTX_destroy(verf);
  16927. return ERR_BASE_EVPSIG-10;
  16928. }
  16929. ret = EVP_SignInit(sign, EVP_sha1());
  16930. if (ret != SSL_SUCCESS){
  16931. printf("error with EVP_SignInit\n");
  16932. EVP_MD_CTX_destroy(sign);
  16933. EVP_MD_CTX_destroy(verf);
  16934. return ERR_BASE_EVPSIG-11;
  16935. }
  16936. count = sizeof(msg);
  16937. show("message = ", (char *)msg, count);
  16938. /* sign */
  16939. XMEMSET(sig, 0, sizeof(sig));
  16940. pt = (const void*)msg;
  16941. ret1 = EVP_SignUpdate(sign, pt, count);
  16942. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  16943. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  16944. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16945. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16946. EVP_MD_CTX_destroy(sign);
  16947. EVP_MD_CTX_destroy(verf);
  16948. printf("error with EVP_MD_CTX_create\n");
  16949. return ERR_BASE_EVPSIG-12;
  16950. }
  16951. show("signature = ", (char *)sig, sigSz);
  16952. /* verify */
  16953. pt = (const void*)msg;
  16954. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  16955. ret2 = EVP_VerifyUpdate(verf, pt, count);
  16956. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  16957. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16958. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16959. EVP_MD_CTX_destroy(sign);
  16960. EVP_MD_CTX_destroy(verf);
  16961. printf("error with EVP_Verify\n");
  16962. return ERR_BASE_EVPSIG-13;
  16963. }
  16964. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  16965. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16966. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16967. EVP_MD_CTX_destroy(sign);
  16968. EVP_MD_CTX_destroy(verf);
  16969. printf("error with EVP_VerifyFinal\n");
  16970. return ERR_BASE_EVPSIG-14;
  16971. }
  16972. /* expect fail without update */
  16973. EVP_VerifyInit(verf, EVP_sha1());
  16974. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  16975. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16976. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16977. EVP_MD_CTX_destroy(sign);
  16978. EVP_MD_CTX_destroy(verf);
  16979. printf("EVP_VerifyInit without update not detected\n");
  16980. return ERR_BASE_EVPSIG-15;
  16981. }
  16982. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16983. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16984. EVP_MD_CTX_destroy(sign);
  16985. EVP_MD_CTX_destroy(verf);
  16986. wolfSSL_RSA_free(prvRsa);
  16987. wolfSSL_RSA_free(pubRsa);
  16988. EVP_PKEY_free(pubPkey);
  16989. EVP_PKEY_free(prvPkey);
  16990. #endif /* NO_RSA */
  16991. return 0;
  16992. }
  16993. #endif /* OPENSSL_EXTRA */
  16994. #ifndef NO_PWDBASED
  16995. #ifdef HAVE_SCRYPT
  16996. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  16997. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  16998. {
  16999. #ifdef HAVE_FIPS
  17000. /* RFC 7914 test vector keys are too short for FIPS. */
  17001. #else
  17002. int ret;
  17003. byte derived[64];
  17004. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  17005. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  17006. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  17007. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  17008. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  17009. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  17010. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  17011. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  17012. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  17013. };
  17014. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17015. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  17016. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  17017. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  17018. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  17019. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  17020. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  17021. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  17022. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  17023. };
  17024. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17025. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17026. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  17027. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  17028. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  17029. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  17030. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  17031. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  17032. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  17033. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  17034. };
  17035. #endif
  17036. #ifdef SCRYPT_TEST_ALL
  17037. /* Test case is very slow.
  17038. * Use for confirmation after code change or new platform.
  17039. */
  17040. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17041. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  17042. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  17043. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  17044. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  17045. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  17046. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  17047. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  17048. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  17049. };
  17050. #endif
  17051. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  17052. if (ret != 0)
  17053. return -9200;
  17054. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  17055. return -9201;
  17056. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  17057. sizeof(verify2));
  17058. if (ret != 0)
  17059. return -9202;
  17060. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17061. return -9203;
  17062. /* Don't run these test on embedded, since they use large mallocs */
  17063. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17064. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17065. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  17066. if (ret != 0)
  17067. return -9204;
  17068. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  17069. return -9205;
  17070. #ifdef SCRYPT_TEST_ALL
  17071. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17072. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  17073. if (ret != 0)
  17074. return -9206;
  17075. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  17076. return -9207;
  17077. #endif
  17078. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  17079. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  17080. 8, 16, sizeof(verify2));
  17081. if (ret != 0)
  17082. return -9208;
  17083. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17084. return -9209;
  17085. #endif /* !HAVE_FIPS */
  17086. return 0;
  17087. }
  17088. #endif
  17089. #ifdef HAVE_PKCS12
  17090. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  17091. {
  17092. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  17093. 0x00, 0x00 };
  17094. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  17095. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  17096. 0x00, 0x67, 0x00, 0x00 };
  17097. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  17098. byte derived[64];
  17099. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17100. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  17101. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  17102. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  17103. };
  17104. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17105. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  17106. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  17107. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  17108. };
  17109. int id = 1;
  17110. int kLen = 24;
  17111. int iterations = 1;
  17112. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  17113. iterations, kLen, WC_SHA256, id);
  17114. if (ret < 0)
  17115. return -9300;
  17116. if (XMEMCMP(derived, verify, kLen) != 0)
  17117. return -9301;
  17118. iterations = 1000;
  17119. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  17120. iterations, kLen, WC_SHA256, id);
  17121. if (ret < 0)
  17122. return -9302;
  17123. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  17124. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  17125. if (ret < 0)
  17126. return -9303;
  17127. if (XMEMCMP(derived, verify2, 24) != 0)
  17128. return -9304;
  17129. return 0;
  17130. }
  17131. #endif /* HAVE_PKCS12 */
  17132. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17133. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  17134. {
  17135. char passwd[] = "passwordpassword";
  17136. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17137. int iterations = 2048;
  17138. int kLen = 24;
  17139. byte derived[64];
  17140. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17141. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  17142. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  17143. };
  17144. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  17145. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  17146. if (ret != 0)
  17147. return ret;
  17148. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17149. return -9400;
  17150. return 0;
  17151. }
  17152. #endif /* HAVE_PBKDF2 && !NO_SHA256 */
  17153. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17154. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  17155. {
  17156. char passwd[] = "password";
  17157. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17158. int iterations = 1000;
  17159. int kLen = 16;
  17160. byte derived[16];
  17161. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17162. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  17163. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  17164. };
  17165. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  17166. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  17167. HEAP_HINT);
  17168. if (ret != 0)
  17169. return ret;
  17170. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17171. return -9500;
  17172. return 0;
  17173. }
  17174. #endif /* HAVE_PBKDF2 && !NO_SHA */
  17175. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  17176. {
  17177. int ret = 0;
  17178. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17179. ret = pbkdf1_test();
  17180. if (ret != 0)
  17181. return ret;
  17182. #endif
  17183. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17184. ret = pbkdf2_test();
  17185. if (ret != 0)
  17186. return ret;
  17187. #endif
  17188. #ifdef HAVE_PKCS12
  17189. ret = pkcs12_test();
  17190. if (ret != 0)
  17191. return ret;
  17192. #endif
  17193. #ifdef HAVE_SCRYPT
  17194. ret = scrypt_test();
  17195. if (ret != 0)
  17196. return ret;
  17197. #endif
  17198. return ret;
  17199. }
  17200. #endif /* NO_PWDBASED */
  17201. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  17202. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void)
  17203. {
  17204. int ret;
  17205. int L = 42;
  17206. byte okm1[42];
  17207. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17208. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17209. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  17210. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17211. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  17212. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  17213. 0xf8, 0xf9 };
  17214. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  17215. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  17216. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  17217. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  17218. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  17219. 0x49, 0x18 };
  17220. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  17221. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  17222. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  17223. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  17224. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  17225. 0xf8, 0x96 };
  17226. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  17227. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  17228. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  17229. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  17230. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  17231. 0x96, 0xc8 };
  17232. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  17233. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  17234. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  17235. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  17236. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  17237. 0x58, 0x65 };
  17238. (void)res1;
  17239. (void)res2;
  17240. (void)res3;
  17241. (void)res4;
  17242. (void)salt1;
  17243. (void)info1;
  17244. #ifndef NO_SHA
  17245. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17246. if (ret != 0)
  17247. return -9700;
  17248. if (XMEMCMP(okm1, res1, L) != 0)
  17249. return -9701;
  17250. #ifndef HAVE_FIPS
  17251. /* fips can't have key size under 14 bytes, salt is key too */
  17252. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  17253. if (ret != 0)
  17254. return -9702;
  17255. if (XMEMCMP(okm1, res2, L) != 0)
  17256. return -9703;
  17257. #endif /* HAVE_FIPS */
  17258. #endif /* NO_SHA */
  17259. #ifndef NO_SHA256
  17260. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17261. if (ret != 0)
  17262. return -9704;
  17263. if (XMEMCMP(okm1, res3, L) != 0)
  17264. return -9705;
  17265. #ifndef HAVE_FIPS
  17266. /* fips can't have key size under 14 bytes, salt is key too */
  17267. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  17268. if (ret != 0)
  17269. return -9706;
  17270. if (XMEMCMP(okm1, res4, L) != 0)
  17271. return -9707;
  17272. #endif /* HAVE_FIPS */
  17273. #endif /* NO_SHA256 */
  17274. return 0;
  17275. }
  17276. #endif /* HAVE_HKDF */
  17277. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  17278. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  17279. {
  17280. int ret;
  17281. byte kek[128];
  17282. #ifndef NO_SHA
  17283. /* SHA-1, COUNT = 0
  17284. * shared secret length: 192
  17285. * SharedInfo length: 0
  17286. * key data length: 128
  17287. */
  17288. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  17289. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  17290. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  17291. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  17292. };
  17293. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17294. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  17295. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  17296. };
  17297. #endif
  17298. #ifndef NO_SHA256
  17299. /* SHA-256, COUNT = 3
  17300. * shared secret length: 192
  17301. * SharedInfo length: 0
  17302. * key data length: 128
  17303. */
  17304. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  17305. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  17306. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  17307. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  17308. };
  17309. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17310. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  17311. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  17312. };
  17313. #endif
  17314. #ifdef WOLFSSL_SHA512
  17315. /* SHA-512, COUNT = 0
  17316. * shared secret length: 192
  17317. * SharedInfo length: 0
  17318. * key data length: 128
  17319. */
  17320. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  17321. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  17322. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  17323. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  17324. };
  17325. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17326. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  17327. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  17328. };
  17329. /* SHA-512, COUNT = 0
  17330. * shared secret length: 521
  17331. * SharedInfo length: 128
  17332. * key data length: 1024
  17333. */
  17334. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  17335. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  17336. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  17337. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  17338. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  17339. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  17340. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  17341. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  17342. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  17343. 0x5a, 0x2d
  17344. };
  17345. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  17346. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  17347. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  17348. };
  17349. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17350. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  17351. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  17352. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  17353. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  17354. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  17355. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  17356. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  17357. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  17358. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  17359. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  17360. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  17361. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  17362. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  17363. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  17364. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  17365. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  17366. };
  17367. #endif
  17368. #ifndef NO_SHA
  17369. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  17370. kek, sizeof(verify));
  17371. if (ret != 0)
  17372. return -9800;
  17373. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  17374. return -9801;
  17375. #endif
  17376. #ifndef NO_SHA256
  17377. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  17378. kek, sizeof(verify2));
  17379. if (ret != 0)
  17380. return -9802;
  17381. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  17382. return -9803;
  17383. #endif
  17384. #ifdef WOLFSSL_SHA512
  17385. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  17386. kek, sizeof(verify3));
  17387. if (ret != 0)
  17388. return -9804;
  17389. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  17390. return -9805;
  17391. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  17392. sizeof(info4), kek, sizeof(verify4));
  17393. if (ret != 0)
  17394. return -9806;
  17395. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  17396. return -9807;
  17397. #endif
  17398. return 0;
  17399. }
  17400. #endif /* HAVE_X963_KDF */
  17401. #ifdef HAVE_ECC
  17402. /* size to use for ECC key gen tests */
  17403. #ifndef ECC_KEYGEN_SIZE
  17404. #ifndef NO_ECC256
  17405. #define ECC_KEYGEN_SIZE 32
  17406. #elif defined(HAVE_ECC384)
  17407. #define ECC_KEYGEN_SIZE 48
  17408. #elif defined(HAVE_ECC224)
  17409. #define ECC_KEYGEN_SIZE 28
  17410. #elif defined(HAVE_ECC521)
  17411. #define ECC_KEYGEN_SIZE 66
  17412. #else
  17413. #error No ECC keygen size defined for test
  17414. #endif
  17415. #endif
  17416. #ifdef BENCH_EMBEDDED
  17417. #define ECC_SHARED_SIZE 128
  17418. #else
  17419. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  17420. #endif
  17421. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  17422. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  17423. #ifndef NO_ECC_VECTOR_TEST
  17424. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  17425. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  17426. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  17427. #define HAVE_ECC_VECTOR_TEST
  17428. #endif
  17429. #endif
  17430. #ifdef HAVE_ECC_VECTOR_TEST
  17431. typedef struct eccVector {
  17432. const char* msg; /* SHA-1 Encoded Message */
  17433. const char* Qx;
  17434. const char* Qy;
  17435. const char* d; /* Private Key */
  17436. const char* R;
  17437. const char* S;
  17438. const char* curveName;
  17439. word32 msgLen;
  17440. word32 keySize;
  17441. #ifndef NO_ASN
  17442. const byte* r;
  17443. word32 rSz;
  17444. const byte* s;
  17445. word32 sSz;
  17446. #endif
  17447. } eccVector;
  17448. static int ecc_test_vector_item(const eccVector* vector)
  17449. {
  17450. int ret = 0, verify = 0;
  17451. word32 sigSz;
  17452. #ifdef WOLFSSL_SMALL_STACK
  17453. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17454. #else
  17455. ecc_key userA[1];
  17456. #endif
  17457. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  17458. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17459. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  17460. DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  17461. DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  17462. DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  17463. #endif
  17464. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  17465. if (sig == NULL)
  17466. ERROR_OUT(MEMORY_E, done);
  17467. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17468. if (sigRaw == NULL || r == NULL || s == NULL)
  17469. ERROR_OUT(MEMORY_E, done);
  17470. #endif
  17471. #endif
  17472. #ifdef WOLFSSL_SMALL_STACK
  17473. if (userA == NULL)
  17474. ERROR_OUT(MEMORY_E, done);
  17475. #endif
  17476. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  17477. if (ret != 0)
  17478. goto done;
  17479. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  17480. vector->d, vector->curveName);
  17481. if (ret != 0)
  17482. goto done;
  17483. XMEMSET(sig, 0, ECC_SIG_SIZE);
  17484. sigSz = ECC_SIG_SIZE;
  17485. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  17486. if (ret != 0)
  17487. goto done;
  17488. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17489. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  17490. sigRawSz = ECC_SIG_SIZE;
  17491. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  17492. sigRaw, &sigRawSz);
  17493. if (ret != 0)
  17494. goto done;
  17495. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  17496. ret = -9810;
  17497. goto done;
  17498. }
  17499. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  17500. if (ret != 0)
  17501. goto done;
  17502. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  17503. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  17504. ret = -9811;
  17505. goto done;
  17506. }
  17507. #endif
  17508. do {
  17509. #if defined(WOLFSSL_ASYNC_CRYPT)
  17510. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17511. #endif
  17512. if (ret == 0)
  17513. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  17514. vector->msgLen, &verify, userA);
  17515. } while (ret == WC_PENDING_E);
  17516. if (ret != 0)
  17517. goto done;
  17518. TEST_SLEEP();
  17519. if (verify != 1)
  17520. ret = -9812;
  17521. done:
  17522. #ifdef WOLFSSL_SMALL_STACK
  17523. if (userA != NULL) {
  17524. wc_ecc_free(userA);
  17525. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17526. }
  17527. #else
  17528. wc_ecc_free(userA);
  17529. #endif
  17530. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17531. FREE_VAR(sigRaw, HEAP_HINT);
  17532. FREE_VAR(r, HEAP_HINT);
  17533. FREE_VAR(s, HEAP_HINT);
  17534. #endif
  17535. FREE_VAR(sig, HEAP_HINT);
  17536. return ret;
  17537. }
  17538. static int ecc_test_vector(int keySize)
  17539. {
  17540. int ret;
  17541. eccVector vec;
  17542. XMEMSET(&vec, 0, sizeof(vec));
  17543. vec.keySize = (word32)keySize;
  17544. switch(keySize) {
  17545. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  17546. case 14:
  17547. return 0;
  17548. #endif /* HAVE_ECC112 */
  17549. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  17550. case 16:
  17551. return 0;
  17552. #endif /* HAVE_ECC128 */
  17553. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  17554. case 20:
  17555. return 0;
  17556. #endif /* HAVE_ECC160 */
  17557. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  17558. case 24:
  17559. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  17560. #if 1
  17561. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  17562. vec.msgLen = 20;
  17563. #else
  17564. /* This is the raw message prior to SHA-1 */
  17565. vec.msg =
  17566. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  17567. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  17568. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  17569. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  17570. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  17571. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  17572. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  17573. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  17574. vec.msgLen = 128;
  17575. #endif
  17576. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  17577. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  17578. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  17579. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  17580. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  17581. vec.curveName = "SECP192R1";
  17582. #ifndef NO_ASN
  17583. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  17584. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  17585. vec.rSz = 24;
  17586. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  17587. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  17588. vec.sSz = 24;
  17589. #endif
  17590. break;
  17591. #endif /* HAVE_ECC192 */
  17592. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  17593. case 28:
  17594. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  17595. #if 1
  17596. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  17597. vec.msgLen = 20;
  17598. #else
  17599. /* This is the raw message prior to SHA-1 */
  17600. vec.msg =
  17601. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  17602. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  17603. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  17604. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  17605. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  17606. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  17607. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  17608. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  17609. vec.msgLen = 128;
  17610. #endif
  17611. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  17612. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  17613. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  17614. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  17615. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  17616. vec.curveName = "SECP224R1";
  17617. #ifndef NO_ASN
  17618. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  17619. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  17620. "\xbc\x16\x71\xa7";
  17621. vec.rSz = 28;
  17622. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  17623. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  17624. "\x6a\xf3\xad\x5b";
  17625. vec.sSz = 28;
  17626. #endif
  17627. break;
  17628. #endif /* HAVE_ECC224 */
  17629. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  17630. case 30:
  17631. return 0;
  17632. #endif /* HAVE_ECC239 */
  17633. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  17634. case 32:
  17635. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  17636. #if 1
  17637. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  17638. vec.msgLen = 20;
  17639. #else
  17640. /* This is the raw message prior to SHA-1 */
  17641. vec.msg =
  17642. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  17643. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  17644. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  17645. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  17646. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  17647. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  17648. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  17649. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  17650. vec.msgLen = 128;
  17651. #endif
  17652. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  17653. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  17654. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  17655. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  17656. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  17657. #ifndef NO_ASN
  17658. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  17659. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  17660. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  17661. vec.rSz = 32;
  17662. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  17663. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  17664. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  17665. vec.sSz = 32;
  17666. #endif
  17667. vec.curveName = "SECP256R1";
  17668. break;
  17669. #endif /* !NO_ECC256 */
  17670. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  17671. case 40:
  17672. return 0;
  17673. #endif /* HAVE_ECC320 */
  17674. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  17675. case 48:
  17676. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  17677. #if 1
  17678. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  17679. vec.msgLen = 20;
  17680. #else
  17681. /* This is the raw message prior to SHA-1 */
  17682. vec.msg =
  17683. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  17684. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  17685. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  17686. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  17687. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  17688. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  17689. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  17690. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  17691. vec.msgLen = 128;
  17692. #endif
  17693. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  17694. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  17695. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  17696. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  17697. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  17698. vec.curveName = "SECP384R1";
  17699. #ifndef NO_ASN
  17700. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  17701. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  17702. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  17703. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  17704. vec.rSz = 48;
  17705. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  17706. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  17707. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  17708. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  17709. vec.sSz = 48;
  17710. #endif
  17711. break;
  17712. #endif /* HAVE_ECC384 */
  17713. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  17714. case 64:
  17715. return 0;
  17716. #endif /* HAVE_ECC512 */
  17717. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  17718. case 66:
  17719. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  17720. #if 1
  17721. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  17722. vec.msgLen = 20;
  17723. #else
  17724. /* This is the raw message prior to SHA-1 */
  17725. vec.msg =
  17726. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  17727. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  17728. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  17729. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  17730. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  17731. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  17732. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  17733. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  17734. vec.msgLen = 128;
  17735. #endif
  17736. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  17737. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  17738. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  17739. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  17740. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  17741. vec.curveName = "SECP521R1";
  17742. #ifndef NO_ASN
  17743. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  17744. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  17745. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  17746. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  17747. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  17748. "\xa3\xbb\x15\x21\xbe";
  17749. vec.rSz = 65;
  17750. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  17751. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  17752. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  17753. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  17754. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  17755. "\x3d\x22\xf2\x48\x0c";
  17756. vec.sSz = 65;
  17757. #endif
  17758. break;
  17759. #endif /* HAVE_ECC521 */
  17760. default:
  17761. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  17762. }; /* Switch */
  17763. ret = ecc_test_vector_item(&vec);
  17764. if (ret < 0) {
  17765. return ret;
  17766. }
  17767. return 0;
  17768. }
  17769. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  17770. static int ecc_test_sign_vectors(WC_RNG* rng)
  17771. {
  17772. int ret;
  17773. ecc_key key;
  17774. byte sig[72];
  17775. word32 sigSz;
  17776. unsigned char hash[32] = "test wolfSSL deterministic sign";
  17777. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  17778. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  17779. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  17780. const byte k[1] = { 0x02 };
  17781. const byte expSig[71] = {
  17782. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  17783. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  17784. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  17785. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  17786. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  17787. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  17788. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  17789. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  17790. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  17791. };
  17792. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  17793. if (ret != 0) {
  17794. return ret;
  17795. }
  17796. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  17797. if (ret != 0) {
  17798. goto done;
  17799. }
  17800. ret = wc_ecc_sign_set_k(k, sizeof(k), &key);
  17801. if (ret != 0) {
  17802. goto done;
  17803. }
  17804. sigSz = sizeof(sig);
  17805. do {
  17806. #if defined(WOLFSSL_ASYNC_CRYPT)
  17807. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17808. #endif
  17809. if (ret == 0)
  17810. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  17811. } while (ret == WC_PENDING_E);
  17812. if (ret != 0) {
  17813. goto done;
  17814. }
  17815. TEST_SLEEP();
  17816. if (sigSz != sizeof(expSig)) {
  17817. ret = -9830;
  17818. goto done;
  17819. }
  17820. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  17821. ret = -9831;
  17822. goto done;
  17823. }
  17824. sigSz = sizeof(sig);
  17825. do {
  17826. #if defined(WOLFSSL_ASYNC_CRYPT)
  17827. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17828. #endif
  17829. if (ret == 0)
  17830. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  17831. } while (ret == WC_PENDING_E);
  17832. if (ret != 0) {
  17833. goto done;
  17834. }
  17835. TEST_SLEEP();
  17836. done:
  17837. wc_ecc_free(&key);
  17838. return ret;
  17839. }
  17840. #endif
  17841. #ifdef HAVE_ECC_CDH
  17842. static int ecc_test_cdh_vectors(WC_RNG* rng)
  17843. {
  17844. int ret;
  17845. #ifdef WOLFSSL_SMALL_STACK
  17846. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17847. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17848. #else
  17849. ecc_key pub_key[1], priv_key[1];
  17850. #endif
  17851. byte sharedA[32] = {0}, sharedB[32] = {0};
  17852. word32 x, z;
  17853. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  17854. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  17855. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  17856. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  17857. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  17858. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  17859. #ifdef WOLFSSL_SMALL_STACK
  17860. if ((pub_key == NULL) ||
  17861. (priv_key == NULL)) {
  17862. ret = MEMORY_E;
  17863. goto done;
  17864. }
  17865. #endif
  17866. XMEMSET(pub_key, 0, sizeof *pub_key);
  17867. XMEMSET(priv_key, 0, sizeof *priv_key);
  17868. /* setup private and public keys */
  17869. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  17870. if (ret != 0)
  17871. goto done;
  17872. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  17873. if (ret != 0)
  17874. goto done;
  17875. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  17876. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  17877. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  17878. if (ret != 0)
  17879. goto done;
  17880. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  17881. if (ret != 0)
  17882. goto done;
  17883. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  17884. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  17885. !defined(HAVE_SELFTEST)
  17886. ret = wc_ecc_set_rng(priv_key, rng);
  17887. if (ret != 0)
  17888. goto done;
  17889. #else
  17890. (void)rng;
  17891. #endif
  17892. /* compute ECC Cofactor shared secret */
  17893. x = sizeof(sharedA);
  17894. do {
  17895. #if defined(WOLFSSL_ASYNC_CRYPT)
  17896. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17897. #endif
  17898. if (ret == 0)
  17899. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  17900. } while (ret == WC_PENDING_E);
  17901. if (ret != 0) {
  17902. goto done;
  17903. }
  17904. TEST_SLEEP();
  17905. /* read in expected Z */
  17906. z = sizeof(sharedB);
  17907. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  17908. if (ret != 0)
  17909. goto done;
  17910. /* compare results */
  17911. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  17912. ERROR_OUT(-9840, done);
  17913. }
  17914. done:
  17915. #ifdef WOLFSSL_SMALL_STACK
  17916. if (priv_key) {
  17917. wc_ecc_free(priv_key);
  17918. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17919. }
  17920. if (pub_key) {
  17921. wc_ecc_free(pub_key);
  17922. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17923. }
  17924. #else
  17925. wc_ecc_free(priv_key);
  17926. wc_ecc_free(pub_key);
  17927. #endif
  17928. return ret;
  17929. }
  17930. #endif /* HAVE_ECC_CDH */
  17931. #endif /* HAVE_ECC_VECTOR_TEST */
  17932. #ifdef HAVE_ECC_KEY_IMPORT
  17933. /* returns 0 on success */
  17934. static int ecc_test_make_pub(WC_RNG* rng)
  17935. {
  17936. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17937. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17938. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  17939. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17940. #endif
  17941. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17942. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17943. #else
  17944. ecc_key key[1];
  17945. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  17946. ecc_key pub[1];
  17947. #endif
  17948. byte exportBuf[ECC_BUFSIZE];
  17949. byte tmp[ECC_BUFSIZE];
  17950. #endif
  17951. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  17952. word32 x, tmpSz;
  17953. int ret = 0;
  17954. ecc_point* pubPoint = NULL;
  17955. #ifdef HAVE_ECC_VERIFY
  17956. int verify = 0;
  17957. #endif
  17958. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17959. if ((key == NULL) ||
  17960. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  17961. (pub == NULL) ||
  17962. #endif
  17963. (exportBuf == NULL) ||
  17964. (tmp == NULL))
  17965. ERROR_OUT(MEMORY_E, done);
  17966. #endif
  17967. (void)msg;
  17968. (void)verify;
  17969. (void)exportBuf;
  17970. (void)rng;
  17971. wc_ecc_init_ex(key, HEAP_HINT, devId);
  17972. #ifndef NO_ECC256
  17973. #ifdef USE_CERT_BUFFERS_256
  17974. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  17975. tmpSz = (size_t)sizeof_ecc_key_der_256;
  17976. #else
  17977. {
  17978. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  17979. if (!file) {
  17980. ERROR_OUT(-9850, done);
  17981. }
  17982. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  17983. XFCLOSE(file);
  17984. }
  17985. #endif /* USE_CERT_BUFFERS_256 */
  17986. /* import private only then test with */
  17987. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  17988. if (ret == 0) {
  17989. ERROR_OUT(-9851, done);
  17990. }
  17991. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  17992. if (ret == 0) {
  17993. ERROR_OUT(-9852, done);
  17994. }
  17995. x = 0;
  17996. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  17997. if (ret != 0) {
  17998. ERROR_OUT(-9853, done);
  17999. }
  18000. #ifdef HAVE_ECC_KEY_EXPORT
  18001. x = ECC_BUFSIZE;
  18002. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18003. if (ret != 0) {
  18004. ERROR_OUT(-9854, done);
  18005. }
  18006. /* make private only key */
  18007. wc_ecc_free(key);
  18008. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18009. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18010. if (ret != 0) {
  18011. ERROR_OUT(-9855, done);
  18012. }
  18013. x = ECC_BUFSIZE;
  18014. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18015. if (ret == 0) {
  18016. ERROR_OUT(-9856, done);
  18017. }
  18018. #endif /* HAVE_ECC_KEY_EXPORT */
  18019. ret = wc_ecc_make_pub(NULL, NULL);
  18020. if (ret == 0) {
  18021. ERROR_OUT(-9857, done);
  18022. }
  18023. TEST_SLEEP();
  18024. #ifndef WOLFSSL_NO_MALLOC
  18025. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  18026. if (pubPoint == NULL) {
  18027. ERROR_OUT(-9858, done);
  18028. }
  18029. ret = wc_ecc_make_pub(key, pubPoint);
  18030. if (ret != 0) {
  18031. ERROR_OUT(-9859, done);
  18032. }
  18033. TEST_SLEEP();
  18034. #ifdef HAVE_ECC_KEY_EXPORT
  18035. /* export should still fail, is private only key */
  18036. x = ECC_BUFSIZE;
  18037. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18038. if (ret == 0) {
  18039. ERROR_OUT(-9860, done);
  18040. }
  18041. #endif /* HAVE_ECC_KEY_EXPORT */
  18042. #endif /* !WOLFSSL_NO_MALLOC */
  18043. #endif /* !NO_ECC256 */
  18044. /* create a new key since above test for loading key is not supported */
  18045. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256)
  18046. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  18047. if (ret != 0) {
  18048. ERROR_OUT(-9861, done);
  18049. }
  18050. #endif
  18051. #ifdef HAVE_ECC_SIGN
  18052. tmpSz = ECC_BUFSIZE;
  18053. ret = 0;
  18054. do {
  18055. #if defined(WOLFSSL_ASYNC_CRYPT)
  18056. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18057. #endif
  18058. if (ret == 0)
  18059. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp, &tmpSz, rng, key);
  18060. } while (ret == WC_PENDING_E);
  18061. if (ret != 0) {
  18062. ERROR_OUT(-9862, done);
  18063. }
  18064. TEST_SLEEP();
  18065. #ifdef HAVE_ECC_VERIFY
  18066. /* try verify with private only key */
  18067. ret = 0;
  18068. do {
  18069. #if defined(WOLFSSL_ASYNC_CRYPT)
  18070. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18071. #endif
  18072. if (ret == 0)
  18073. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, (word32)XSTRLEN((const char* )msg), &verify, key);
  18074. } while (ret == WC_PENDING_E);
  18075. if (ret != 0) {
  18076. ERROR_OUT(-9863, done);
  18077. }
  18078. if (verify != 1) {
  18079. ERROR_OUT(-9864, done);
  18080. }
  18081. TEST_SLEEP();
  18082. #ifdef HAVE_ECC_KEY_EXPORT
  18083. /* exporting the public part should now work */
  18084. x = ECC_BUFSIZE;
  18085. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18086. if (ret != 0) {
  18087. ERROR_OUT(-9865, done);
  18088. }
  18089. #endif /* HAVE_ECC_KEY_EXPORT */
  18090. #endif /* HAVE_ECC_VERIFY */
  18091. #endif /* HAVE_ECC_SIGN */
  18092. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18093. /* now test private only key with creating a shared secret */
  18094. x = ECC_BUFSIZE;
  18095. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18096. if (ret != 0) {
  18097. ERROR_OUT(-9866, done);
  18098. }
  18099. /* make private only key */
  18100. wc_ecc_free(key);
  18101. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18102. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18103. if (ret != 0) {
  18104. ERROR_OUT(-9867, done);
  18105. }
  18106. /* check that public export fails with private only key */
  18107. x = ECC_BUFSIZE;
  18108. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18109. if (ret == 0) {
  18110. ERROR_OUT(-9868, done);
  18111. }
  18112. /* make public key for shared secret */
  18113. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  18114. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  18115. #if defined(WOLFSSL_ASYNC_CRYPT)
  18116. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  18117. #endif
  18118. if (ret != 0) {
  18119. ERROR_OUT(-9869, done);
  18120. }
  18121. TEST_SLEEP();
  18122. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18123. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18124. !defined(HAVE_SELFTEST)
  18125. ret = wc_ecc_set_rng(key, rng);
  18126. if (ret != 0)
  18127. goto done;
  18128. #endif
  18129. x = ECC_BUFSIZE;
  18130. do {
  18131. #if defined(WOLFSSL_ASYNC_CRYPT)
  18132. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18133. #endif
  18134. if (ret == 0) {
  18135. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  18136. }
  18137. } while (ret == WC_PENDING_E);
  18138. wc_ecc_free(pub);
  18139. if (ret != 0) {
  18140. ERROR_OUT(-9870, done);
  18141. }
  18142. TEST_SLEEP();
  18143. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT */
  18144. ret = 0;
  18145. done:
  18146. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  18147. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18148. if (key != NULL) {
  18149. wc_ecc_free(key);
  18150. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18151. }
  18152. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18153. if (pub != NULL)
  18154. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18155. #endif
  18156. if (exportBuf != NULL)
  18157. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18158. if (tmp != NULL)
  18159. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18160. #else
  18161. wc_ecc_free(key);
  18162. #endif
  18163. return ret;
  18164. }
  18165. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
  18166. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  18167. {
  18168. int ret;
  18169. #ifdef WOLFSSL_SMALL_STACK
  18170. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18171. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18172. #else
  18173. ecc_key eccKey[1];
  18174. byte tmpBuf[ECC_BUFSIZE];
  18175. #endif
  18176. word32 tmpSz;
  18177. word32 idx;
  18178. #ifdef WOLFSSL_SMALL_STACK
  18179. if ((eccKey == NULL) || (tmpBuf == NULL))
  18180. ERROR_OUT(MEMORY_E, done);
  18181. #endif
  18182. ret = wc_ecc_init(eccKey);
  18183. if (ret != 0) {
  18184. goto done;
  18185. }
  18186. ret = wc_ecc_make_key(rng, keySize, eccKey);
  18187. #if defined(WOLFSSL_ASYNC_CRYPT)
  18188. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18189. #endif
  18190. if (ret != 0) {
  18191. goto done;
  18192. }
  18193. tmpSz = ECC_BUFSIZE;
  18194. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  18195. wc_ecc_free(eccKey);
  18196. if (ret < 0) {
  18197. goto done;
  18198. }
  18199. tmpSz = ret;
  18200. ret = wc_ecc_init(eccKey);
  18201. if (ret != 0) {
  18202. goto done;
  18203. }
  18204. idx = 0;
  18205. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18206. if (ret != 0) {
  18207. goto done;
  18208. }
  18209. wc_ecc_free(eccKey);
  18210. ret = wc_ecc_init(eccKey);
  18211. if (ret != 0) {
  18212. goto done;
  18213. }
  18214. idx = 0;
  18215. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18216. if (ret != 0) {
  18217. goto done;
  18218. }
  18219. ret = 0;
  18220. done:
  18221. #ifdef WOLFSSL_SMALL_STACK
  18222. if (eccKey != NULL) {
  18223. wc_ecc_free(eccKey);
  18224. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18225. }
  18226. if (tmpBuf != NULL)
  18227. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18228. #else
  18229. wc_ecc_free(eccKey);
  18230. #endif
  18231. return ret;
  18232. }
  18233. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18234. #endif /* HAVE_ECC_KEY_IMPORT */
  18235. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
  18236. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  18237. {
  18238. int ret = 0;
  18239. int derSz;
  18240. #ifdef HAVE_PKCS8
  18241. word32 pkcs8Sz;
  18242. #endif
  18243. #ifdef WOLFSSL_SMALL_STACK
  18244. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18245. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18246. #else
  18247. byte der[ECC_BUFSIZE];
  18248. ecc_key userA[1];
  18249. #endif
  18250. #ifdef WOLFSSL_SMALL_STACK
  18251. if ((der == NULL) || (userA == NULL))
  18252. ERROR_OUT(MEMORY_E, done);
  18253. #endif
  18254. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18255. if (ret != 0)
  18256. goto done;
  18257. ret = wc_ecc_make_key(rng, keySize, userA);
  18258. #if defined(WOLFSSL_ASYNC_CRYPT)
  18259. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18260. #endif
  18261. if (ret != 0)
  18262. goto done;
  18263. TEST_SLEEP();
  18264. ret = wc_ecc_check_key(userA);
  18265. if (ret != 0)
  18266. goto done;
  18267. TEST_SLEEP();
  18268. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  18269. if (derSz < 0) {
  18270. ERROR_OUT(derSz, done);
  18271. }
  18272. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  18273. ECC_PRIVATEKEY_TYPE, -8347);
  18274. if (ret != 0) {
  18275. goto done;
  18276. }
  18277. /* test export of public key */
  18278. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  18279. if (derSz < 0) {
  18280. ERROR_OUT(derSz, done);
  18281. }
  18282. if (derSz == 0) {
  18283. ERROR_OUT(-9890, done);
  18284. }
  18285. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  18286. if (ret != 0) {
  18287. goto done;
  18288. }
  18289. #ifdef HAVE_PKCS8
  18290. /* test export of PKCS#8 unencrypted private key */
  18291. pkcs8Sz = FOURK_BUF;
  18292. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  18293. if (derSz < 0) {
  18294. ERROR_OUT(derSz, done);
  18295. }
  18296. if (derSz == 0) {
  18297. ERROR_OUT(-9891, done);
  18298. }
  18299. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  18300. if (ret != 0) {
  18301. goto done;
  18302. }
  18303. #endif /* HAVE_PKCS8 */
  18304. done:
  18305. #ifdef WOLFSSL_SMALL_STACK
  18306. if (der != NULL)
  18307. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18308. if (userA != NULL) {
  18309. wc_ecc_free(userA);
  18310. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18311. }
  18312. #else
  18313. wc_ecc_free(userA);
  18314. #endif
  18315. return ret;
  18316. }
  18317. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18318. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  18319. int curve_id, const ecc_set_type* dp)
  18320. {
  18321. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  18322. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18323. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  18324. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  18325. #endif
  18326. #ifdef HAVE_ECC_KEY_EXPORT
  18327. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  18328. DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  18329. #endif
  18330. word32 x = 0;
  18331. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  18332. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18333. word32 y;
  18334. #endif
  18335. #ifdef HAVE_ECC_SIGN
  18336. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  18337. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  18338. int i;
  18339. #ifdef HAVE_ECC_VERIFY
  18340. int verify;
  18341. #endif /* HAVE_ECC_VERIFY */
  18342. #endif /* HAVE_ECC_SIGN */
  18343. int ret;
  18344. #ifdef WOLFSSL_SMALL_STACK
  18345. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  18346. *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  18347. *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18348. #else
  18349. ecc_key userA[1],
  18350. userB[1],
  18351. pubKey[1];
  18352. #endif
  18353. int curveSize;
  18354. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  18355. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \
  18356. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18357. if (sharedA == NULL || sharedB == NULL)
  18358. ERROR_OUT(-9900, done);
  18359. #endif
  18360. #ifdef HAVE_ECC_KEY_EXPORT
  18361. if (exportBuf == NULL)
  18362. ERROR_OUT(-9901, done);
  18363. #endif
  18364. #ifdef HAVE_ECC_SIGN
  18365. if (sig == NULL || digest == NULL)
  18366. ERROR_OUT(-9902, done);
  18367. #endif
  18368. #endif /* WOLFSSL_SMALL_STACK */
  18369. (void)testVerifyCount;
  18370. (void)dp;
  18371. (void)x;
  18372. #ifdef WOLFSSL_SMALL_STACK
  18373. if ((userA == NULL) ||
  18374. (userB == NULL) ||
  18375. (pubKey == NULL))
  18376. ERROR_OUT(-9903, done);
  18377. #endif
  18378. XMEMSET(userA, 0, sizeof *userA);
  18379. XMEMSET(userB, 0, sizeof *userB);
  18380. XMEMSET(pubKey, 0, sizeof *pubKey);
  18381. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18382. if (ret != 0)
  18383. ERROR_OUT(-9904, done);
  18384. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  18385. if (ret != 0)
  18386. ERROR_OUT(-9905, done);
  18387. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  18388. if (ret != 0)
  18389. ERROR_OUT(-9906, done);
  18390. #ifdef WOLFSSL_CUSTOM_CURVES
  18391. if (dp != NULL) {
  18392. ret = wc_ecc_set_custom_curve(userA, dp);
  18393. if (ret != 0)
  18394. ERROR_OUT(-9907, done);
  18395. ret = wc_ecc_set_custom_curve(userB, dp);
  18396. if (ret != 0)
  18397. ERROR_OUT(-9908, done);
  18398. }
  18399. #endif
  18400. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  18401. if (ret != 0)
  18402. ERROR_OUT(-9909, done);
  18403. #if defined(WOLFSSL_ASYNC_CRYPT)
  18404. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18405. #endif
  18406. if (ret != 0)
  18407. ERROR_OUT(-9910, done);
  18408. TEST_SLEEP();
  18409. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  18410. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  18411. if (curveSize != userA->dp->size)
  18412. ERROR_OUT(-9911, done);
  18413. }
  18414. ret = wc_ecc_check_key(userA);
  18415. if (ret != 0)
  18416. ERROR_OUT(-9912, done);
  18417. TEST_SLEEP();
  18418. /* ATECC508/608 configuration may not support more than one ECDH key */
  18419. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18420. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  18421. if (ret != 0)
  18422. ERROR_OUT(-9913, done);
  18423. #if defined(WOLFSSL_ASYNC_CRYPT)
  18424. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  18425. #endif
  18426. if (ret != 0)
  18427. ERROR_OUT(-9914, done);
  18428. TEST_SLEEP();
  18429. /* only perform the below tests if the key size matches */
  18430. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  18431. ERROR_OUT(ECC_CURVE_OID_E, done);
  18432. #ifdef HAVE_ECC_DHE
  18433. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18434. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18435. !defined(HAVE_SELFTEST)
  18436. ret = wc_ecc_set_rng(userA, rng);
  18437. if (ret != 0)
  18438. ERROR_OUT(-9915, done);
  18439. ret = wc_ecc_set_rng(userB, rng);
  18440. if (ret != 0)
  18441. ERROR_OUT(-9916, done);
  18442. #endif
  18443. x = ECC_SHARED_SIZE;
  18444. do {
  18445. #if defined(WOLFSSL_ASYNC_CRYPT)
  18446. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18447. #endif
  18448. if (ret == 0)
  18449. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  18450. } while (ret == WC_PENDING_E);
  18451. if (ret != 0) {
  18452. ERROR_OUT(-9917, done);
  18453. }
  18454. TEST_SLEEP();
  18455. y = ECC_SHARED_SIZE;
  18456. do {
  18457. #if defined(WOLFSSL_ASYNC_CRYPT)
  18458. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18459. #endif
  18460. if (ret == 0)
  18461. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  18462. } while (ret == WC_PENDING_E);
  18463. if (ret != 0)
  18464. ERROR_OUT(-9918, done);
  18465. if (y != x)
  18466. ERROR_OUT(-9919, done);
  18467. if (XMEMCMP(sharedA, sharedB, x))
  18468. ERROR_OUT(-9920, done);
  18469. TEST_SLEEP();
  18470. #endif /* HAVE_ECC_DHE */
  18471. #ifdef HAVE_ECC_CDH
  18472. /* add cofactor flag */
  18473. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  18474. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  18475. x = ECC_SHARED_SIZE;
  18476. do {
  18477. #if defined(WOLFSSL_ASYNC_CRYPT)
  18478. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18479. #endif
  18480. if (ret == 0)
  18481. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  18482. } while (ret == WC_PENDING_E);
  18483. if (ret != 0)
  18484. ERROR_OUT(-9921, done);
  18485. TEST_SLEEP();
  18486. y = ECC_SHARED_SIZE;
  18487. do {
  18488. #if defined(WOLFSSL_ASYNC_CRYPT)
  18489. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18490. #endif
  18491. if (ret == 0)
  18492. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  18493. } while (ret == WC_PENDING_E);
  18494. if (ret != 0)
  18495. ERROR_OUT(-9922, done);
  18496. if (y != x)
  18497. ERROR_OUT(-9923, done);
  18498. if (XMEMCMP(sharedA, sharedB, x))
  18499. ERROR_OUT(-9924, done);
  18500. TEST_SLEEP();
  18501. /* remove cofactor flag */
  18502. wc_ecc_set_flags(userA, 0);
  18503. wc_ecc_set_flags(userB, 0);
  18504. #endif /* HAVE_ECC_CDH */
  18505. #endif /* WOLFSSL_ATECC508A */
  18506. #ifdef HAVE_ECC_KEY_EXPORT
  18507. x = ECC_KEY_EXPORT_BUF_SIZE;
  18508. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  18509. if (ret != 0)
  18510. ERROR_OUT(-9925, done);
  18511. #ifdef HAVE_ECC_KEY_IMPORT
  18512. #ifdef WOLFSSL_CUSTOM_CURVES
  18513. if (dp != NULL) {
  18514. ret = wc_ecc_set_custom_curve(pubKey, dp);
  18515. if (ret != 0)
  18516. ERROR_OUT(-9926, done);
  18517. }
  18518. #endif
  18519. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  18520. if (ret != 0)
  18521. ERROR_OUT(-9927, done);
  18522. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18523. #ifdef HAVE_ECC_DHE
  18524. y = ECC_SHARED_SIZE;
  18525. do {
  18526. #if defined(WOLFSSL_ASYNC_CRYPT)
  18527. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18528. #endif
  18529. if (ret == 0)
  18530. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  18531. } while (ret == WC_PENDING_E);
  18532. if (ret != 0)
  18533. ERROR_OUT(-9928, done);
  18534. if (XMEMCMP(sharedA, sharedB, y))
  18535. ERROR_OUT(-9929, done);
  18536. TEST_SLEEP();
  18537. #endif /* HAVE_ECC_DHE */
  18538. #ifdef HAVE_COMP_KEY
  18539. /* try compressed export / import too */
  18540. x = ECC_KEY_EXPORT_BUF_SIZE;
  18541. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  18542. if (ret != 0)
  18543. ERROR_OUT(-9930, done);
  18544. wc_ecc_free(pubKey);
  18545. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  18546. if (ret != 0)
  18547. ERROR_OUT(-9931, done);
  18548. #ifdef WOLFSSL_CUSTOM_CURVES
  18549. if (dp != NULL) {
  18550. ret = wc_ecc_set_custom_curve(pubKey, dp);
  18551. if (ret != 0)
  18552. ERROR_OUT(-9932, done);
  18553. }
  18554. #endif
  18555. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  18556. if (ret != 0)
  18557. ERROR_OUT(-9933, done);
  18558. #ifdef HAVE_ECC_DHE
  18559. y = ECC_SHARED_SIZE;
  18560. do {
  18561. #if defined(WOLFSSL_ASYNC_CRYPT)
  18562. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18563. #endif
  18564. if (ret == 0)
  18565. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  18566. } while (ret == WC_PENDING_E);
  18567. if (ret != 0)
  18568. ERROR_OUT(-9934, done);
  18569. if (XMEMCMP(sharedA, sharedB, y))
  18570. ERROR_OUT(-9935, done);
  18571. TEST_SLEEP();
  18572. #endif /* HAVE_ECC_DHE */
  18573. #endif /* HAVE_COMP_KEY */
  18574. #endif /* WOLFSSL_ATECC508A */
  18575. #endif /* HAVE_ECC_KEY_IMPORT */
  18576. #endif /* HAVE_ECC_KEY_EXPORT */
  18577. #ifdef HAVE_ECC_SIGN
  18578. /* ECC w/out Shamir has issue with all 0 digest */
  18579. /* WC_BIGINT doesn't have 0 len well on hardware */
  18580. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT)
  18581. /* test DSA sign hash with zeros */
  18582. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  18583. digest[i] = 0;
  18584. }
  18585. x = ECC_SIG_SIZE;
  18586. do {
  18587. #if defined(WOLFSSL_ASYNC_CRYPT)
  18588. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18589. #endif
  18590. if (ret == 0)
  18591. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  18592. userA);
  18593. } while (ret == WC_PENDING_E);
  18594. if (ret != 0)
  18595. ERROR_OUT(-9936, done);
  18596. TEST_SLEEP();
  18597. #ifdef HAVE_ECC_VERIFY
  18598. for (i=0; i<testVerifyCount; i++) {
  18599. verify = 0;
  18600. do {
  18601. #if defined(WOLFSSL_ASYNC_CRYPT)
  18602. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18603. #endif
  18604. if (ret == 0)
  18605. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  18606. &verify, userA);
  18607. } while (ret == WC_PENDING_E);
  18608. if (ret != 0)
  18609. ERROR_OUT(-9937, done);
  18610. if (verify != 1)
  18611. ERROR_OUT(-9938, done);
  18612. TEST_SLEEP();
  18613. }
  18614. #endif /* HAVE_ECC_VERIFY */
  18615. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT */
  18616. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  18617. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  18618. digest[i] = (byte)i;
  18619. }
  18620. x = ECC_SIG_SIZE;
  18621. do {
  18622. #if defined(WOLFSSL_ASYNC_CRYPT)
  18623. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18624. #endif
  18625. if (ret == 0)
  18626. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  18627. userA);
  18628. } while (ret == WC_PENDING_E);
  18629. if (ret != 0)
  18630. ERROR_OUT(-9939, done);
  18631. TEST_SLEEP();
  18632. #ifdef HAVE_ECC_VERIFY
  18633. for (i=0; i<testVerifyCount; i++) {
  18634. verify = 0;
  18635. do {
  18636. #if defined(WOLFSSL_ASYNC_CRYPT)
  18637. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18638. #endif
  18639. if (ret == 0)
  18640. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  18641. &verify, userA);
  18642. } while (ret == WC_PENDING_E);
  18643. if (ret != 0)
  18644. ERROR_OUT(-9940, done);
  18645. if (verify != 1)
  18646. ERROR_OUT(-9941, done);
  18647. TEST_SLEEP();
  18648. }
  18649. #endif /* HAVE_ECC_VERIFY */
  18650. #endif /* HAVE_ECC_SIGN */
  18651. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WOLFSSL_ATECC508) && \
  18652. !defined(WOLFSSL_ATECC608A)
  18653. x = ECC_KEY_EXPORT_BUF_SIZE;
  18654. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  18655. if (ret != 0)
  18656. ERROR_OUT(-9942, done);
  18657. #endif /* HAVE_ECC_KEY_EXPORT */
  18658. done:
  18659. #ifdef WOLFSSL_SMALL_STACK
  18660. if (userA != NULL) {
  18661. wc_ecc_free(userA);
  18662. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18663. }
  18664. if (userB != NULL) {
  18665. wc_ecc_free(userB);
  18666. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18667. }
  18668. if (pubKey != NULL) {
  18669. wc_ecc_free(pubKey);
  18670. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18671. }
  18672. #else
  18673. wc_ecc_free(pubKey);
  18674. wc_ecc_free(userB);
  18675. wc_ecc_free(userA);
  18676. #endif
  18677. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  18678. FREE_VAR(sharedA, HEAP_HINT);
  18679. FREE_VAR(sharedB, HEAP_HINT);
  18680. #endif
  18681. #ifdef HAVE_ECC_KEY_EXPORT
  18682. FREE_VAR(exportBuf, HEAP_HINT);
  18683. #endif
  18684. #ifdef HAVE_ECC_SIGN
  18685. FREE_VAR(sig, HEAP_HINT);
  18686. FREE_VAR(digest, HEAP_HINT);
  18687. #endif
  18688. return ret;
  18689. }
  18690. #undef ECC_TEST_VERIFY_COUNT
  18691. #define ECC_TEST_VERIFY_COUNT 2
  18692. static int ecc_test_curve(WC_RNG* rng, int keySize)
  18693. {
  18694. int ret;
  18695. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  18696. ECC_CURVE_DEF, NULL);
  18697. if (ret < 0) {
  18698. if (ret == ECC_CURVE_OID_E) {
  18699. /* ignore error for curves not found */
  18700. /* some curve sizes are only available with:
  18701. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  18702. and HAVE_ECC_KOBLITZ */
  18703. }
  18704. else {
  18705. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  18706. return ret;
  18707. }
  18708. }
  18709. #ifdef HAVE_ECC_VECTOR_TEST
  18710. ret = ecc_test_vector(keySize);
  18711. if (ret < 0) {
  18712. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  18713. return ret;
  18714. }
  18715. #endif
  18716. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  18717. !defined(NO_ASN_CRYPT)
  18718. ret = ecc_test_key_decode(rng, keySize);
  18719. if (ret < 0) {
  18720. if (ret == ECC_CURVE_OID_E) {
  18721. /* ignore error for curves not found */
  18722. }
  18723. else {
  18724. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  18725. return ret;
  18726. }
  18727. }
  18728. #endif
  18729. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
  18730. ret = ecc_test_key_gen(rng, keySize);
  18731. if (ret < 0) {
  18732. if (ret == ECC_CURVE_OID_E) {
  18733. /* ignore error for curves not found */
  18734. }
  18735. else {
  18736. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  18737. return ret;
  18738. }
  18739. }
  18740. #endif
  18741. return 0;
  18742. }
  18743. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  18744. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  18745. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  18746. !defined(WOLFSSL_NO_MALLOC)
  18747. static int ecc_point_test(void)
  18748. {
  18749. int ret;
  18750. ecc_point* point;
  18751. ecc_point* point2;
  18752. #ifdef HAVE_COMP_KEY
  18753. ecc_point* point3;
  18754. ecc_point* point4;
  18755. #endif
  18756. word32 outLen;
  18757. byte out[65];
  18758. byte der[] = { 0x04, /* = Uncompressed */
  18759. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18760. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18761. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18762. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18763. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18764. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18765. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18766. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18767. #ifdef HAVE_COMP_KEY
  18768. byte derComp0[] = { 0x02, /* = Compressed, y even */
  18769. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18770. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18771. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18772. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18773. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  18774. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18775. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18776. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18777. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18778. #endif
  18779. byte altDer[] = { 0x04, /* = Uncompressed */
  18780. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18781. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18782. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18783. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18784. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18785. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18786. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18787. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  18788. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  18789. /* if curve P256 is not enabled then test should not fail */
  18790. if (curve_idx == ECC_CURVE_INVALID)
  18791. return 0;
  18792. outLen = sizeof(out);
  18793. point = wc_ecc_new_point();
  18794. if (point == NULL)
  18795. return -10000;
  18796. point2 = wc_ecc_new_point();
  18797. if (point2 == NULL) {
  18798. wc_ecc_del_point(point);
  18799. return -10001;
  18800. }
  18801. #ifdef HAVE_COMP_KEY
  18802. point3 = wc_ecc_new_point();
  18803. if (point3 == NULL) {
  18804. wc_ecc_del_point(point2);
  18805. wc_ecc_del_point(point);
  18806. return -10002;
  18807. }
  18808. point4 = wc_ecc_new_point();
  18809. if (point4 == NULL) {
  18810. wc_ecc_del_point(point3);
  18811. wc_ecc_del_point(point2);
  18812. wc_ecc_del_point(point);
  18813. return -10003;
  18814. }
  18815. #endif
  18816. /* Parameter Validation testing. */
  18817. wc_ecc_del_point(NULL);
  18818. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  18819. if (ret != ECC_BAD_ARG_E) {
  18820. ret = -10004;
  18821. goto done;
  18822. }
  18823. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  18824. if (ret != ECC_BAD_ARG_E) {
  18825. ret = -10005;
  18826. goto done;
  18827. }
  18828. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  18829. if (ret != ECC_BAD_ARG_E) {
  18830. ret = -10006;
  18831. goto done;
  18832. }
  18833. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  18834. if (ret != ECC_BAD_ARG_E) {
  18835. ret = -10007;
  18836. goto done;
  18837. }
  18838. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  18839. if (ret != ECC_BAD_ARG_E) {
  18840. ret = -10008;
  18841. goto done;
  18842. }
  18843. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  18844. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  18845. ret = -10009;
  18846. goto done;
  18847. }
  18848. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  18849. if (ret != ECC_BAD_ARG_E) {
  18850. ret = -10010;
  18851. goto done;
  18852. }
  18853. outLen = 0;
  18854. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  18855. if (ret != BUFFER_E) {
  18856. ret = -10011;
  18857. goto done;
  18858. }
  18859. ret = wc_ecc_copy_point(NULL, NULL);
  18860. if (ret != ECC_BAD_ARG_E) {
  18861. ret = -10012;
  18862. goto done;
  18863. }
  18864. ret = wc_ecc_copy_point(NULL, point2);
  18865. if (ret != ECC_BAD_ARG_E) {
  18866. ret = -10013;
  18867. goto done;
  18868. }
  18869. ret = wc_ecc_copy_point(point, NULL);
  18870. if (ret != ECC_BAD_ARG_E) {
  18871. ret = -10014;
  18872. goto done;
  18873. }
  18874. ret = wc_ecc_cmp_point(NULL, NULL);
  18875. if (ret != BAD_FUNC_ARG) {
  18876. ret = -10015;
  18877. goto done;
  18878. }
  18879. ret = wc_ecc_cmp_point(NULL, point2);
  18880. if (ret != BAD_FUNC_ARG) {
  18881. ret = -10016;
  18882. goto done;
  18883. }
  18884. ret = wc_ecc_cmp_point(point, NULL);
  18885. if (ret != BAD_FUNC_ARG) {
  18886. ret = -10017;
  18887. goto done;
  18888. }
  18889. /* Use API. */
  18890. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  18891. if (ret != 0) {
  18892. ret = -10018;
  18893. goto done;
  18894. }
  18895. outLen = sizeof(out);
  18896. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  18897. if (ret != 0) {
  18898. ret = -10019;
  18899. goto done;
  18900. }
  18901. if (outLen != sizeof(der)) {
  18902. ret = -10020;
  18903. goto done;
  18904. }
  18905. if (XMEMCMP(out, der, outLen) != 0) {
  18906. ret = -10021;
  18907. goto done;
  18908. }
  18909. ret = wc_ecc_copy_point(point2, point);
  18910. if (ret != MP_OKAY) {
  18911. ret = -10022;
  18912. goto done;
  18913. }
  18914. ret = wc_ecc_cmp_point(point2, point);
  18915. if (ret != MP_EQ) {
  18916. ret = -10023;
  18917. goto done;
  18918. }
  18919. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  18920. if (ret != 0) {
  18921. ret = -10024;
  18922. goto done;
  18923. }
  18924. ret = wc_ecc_cmp_point(point2, point);
  18925. if (ret != MP_GT) {
  18926. ret = -10025;
  18927. goto done;
  18928. }
  18929. #ifdef HAVE_COMP_KEY
  18930. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  18931. if (ret != 0) {
  18932. ret = -10026;
  18933. goto done;
  18934. }
  18935. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  18936. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  18937. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  18938. if (ret != 0) {
  18939. ret = -10027;
  18940. goto done;
  18941. }
  18942. #endif
  18943. ret = wc_ecc_cmp_point(point3, point4);
  18944. if (ret != MP_EQ) {
  18945. ret = -10028;
  18946. goto done;
  18947. }
  18948. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  18949. if (ret != 0) {
  18950. ret = -10029;
  18951. goto done;
  18952. }
  18953. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  18954. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  18955. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  18956. if (ret != 0) {
  18957. ret = -10030;
  18958. goto done;
  18959. }
  18960. #endif
  18961. ret = wc_ecc_cmp_point(point3, point4);
  18962. if (ret != MP_EQ) {
  18963. ret = -10031;
  18964. goto done;
  18965. }
  18966. #endif
  18967. done:
  18968. #ifdef HAVE_COMP_KEY
  18969. wc_ecc_del_point(point4);
  18970. wc_ecc_del_point(point3);
  18971. #endif
  18972. wc_ecc_del_point(point2);
  18973. wc_ecc_del_point(point);
  18974. return ret;
  18975. }
  18976. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  18977. #ifndef NO_SIG_WRAPPER
  18978. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  18979. {
  18980. int ret;
  18981. word32 sigSz;
  18982. int size;
  18983. byte out[ECC_MAX_SIG_SIZE];
  18984. byte in[] = TEST_STRING;
  18985. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  18986. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  18987. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  18988. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  18989. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  18990. };
  18991. word32 inLen = (word32)XSTRLEN((char*)in);
  18992. size = wc_ecc_sig_size(key);
  18993. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  18994. if (ret != size)
  18995. return -10040;
  18996. sigSz = (word32)ret;
  18997. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  18998. inLen, out, &sigSz, key, sizeof(*key), rng);
  18999. if (ret != 0)
  19000. return -10041;
  19001. TEST_SLEEP();
  19002. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  19003. inLen, out, sigSz, key, sizeof(*key));
  19004. if (ret != 0)
  19005. return -10042;
  19006. TEST_SLEEP();
  19007. sigSz = (word32)sizeof(out);
  19008. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19009. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  19010. if (ret != 0)
  19011. return -10043;
  19012. TEST_SLEEP();
  19013. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19014. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  19015. if (ret != 0)
  19016. return -10044;
  19017. TEST_SLEEP();
  19018. return 0;
  19019. }
  19020. #endif
  19021. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19022. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19023. static int ecc_exp_imp_test(ecc_key* key)
  19024. {
  19025. int ret;
  19026. int curve_id;
  19027. #ifdef WOLFSSL_SMALL_STACK
  19028. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  19029. #else
  19030. ecc_key keyImp[1];
  19031. #endif
  19032. byte priv[32];
  19033. word32 privLen;
  19034. byte pub[65];
  19035. word32 pubLen, pubLenX, pubLenY;
  19036. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  19037. "ce46cbc947616d0cbaa82323818a793d";
  19038. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  19039. "922f8b30ea6e8811742ac7238fe87308";
  19040. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  19041. "2e7a493ec1153b3a95feb8a4873f8d08";
  19042. #ifdef WOLFSSL_SMALL_STACK
  19043. if (keyImp == NULL)
  19044. ERROR_OUT(-10050, done);
  19045. #endif
  19046. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19047. privLen = sizeof(priv);
  19048. ret = wc_ecc_export_private_only(key, priv, &privLen);
  19049. if (ret != 0) {
  19050. ret = -10051;
  19051. goto done;
  19052. }
  19053. pubLen = sizeof(pub);
  19054. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  19055. if (ret != 0) {
  19056. ret = -10052;
  19057. goto done;
  19058. }
  19059. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  19060. if (ret != 0) {
  19061. ret = -10053;
  19062. goto done;
  19063. }
  19064. wc_ecc_free(keyImp);
  19065. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19066. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  19067. if (ret != 0) {
  19068. ret = -10054;
  19069. goto done;
  19070. }
  19071. wc_ecc_free(keyImp);
  19072. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19073. curve_id = wc_ecc_get_curve_id(key->idx);
  19074. if (curve_id < 0) {
  19075. ret = -10055;
  19076. goto done;
  19077. }
  19078. /* test import private only */
  19079. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  19080. curve_id);
  19081. if (ret != 0) {
  19082. ret = -10056;
  19083. goto done;
  19084. }
  19085. wc_ecc_free(keyImp);
  19086. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19087. /* test export public raw */
  19088. pubLenX = pubLenY = 32;
  19089. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  19090. if (ret != 0) {
  19091. ret = -10057;
  19092. goto done;
  19093. }
  19094. #ifndef HAVE_SELFTEST
  19095. /* test import of public */
  19096. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  19097. if (ret != 0) {
  19098. ret = -10058;
  19099. goto done;
  19100. }
  19101. #endif
  19102. wc_ecc_free(keyImp);
  19103. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19104. /* test export private and public raw */
  19105. pubLenX = pubLenY = privLen = 32;
  19106. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  19107. priv, &privLen);
  19108. if (ret != 0) {
  19109. ret = -10059;
  19110. goto done;
  19111. }
  19112. #ifndef HAVE_SELFTEST
  19113. /* test import of private and public */
  19114. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  19115. if (ret != 0) {
  19116. ret = -10060;
  19117. goto done;
  19118. }
  19119. #endif
  19120. done:
  19121. #ifdef WOLFSSL_SMALL_STACK
  19122. if (keyImp != NULL) {
  19123. wc_ecc_free(keyImp);
  19124. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19125. }
  19126. #else
  19127. wc_ecc_free(keyImp);
  19128. #endif
  19129. return ret;
  19130. }
  19131. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  19132. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19133. !defined(WOLFSSL_CRYPTOCELL)
  19134. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19135. static int ecc_mulmod_test(ecc_key* key1)
  19136. {
  19137. int ret;
  19138. #ifdef WOLFSSL_SMALL_STACK
  19139. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19140. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19141. #else
  19142. ecc_key key2[1];
  19143. ecc_key key3[1];
  19144. #endif
  19145. #ifdef WOLFSSL_SMALL_STACK
  19146. if ((key2 == NULL) || (key3 == NULL))
  19147. ERROR_OUT(MEMORY_E, done);
  19148. #endif
  19149. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  19150. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  19151. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  19152. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  19153. * private key in key2.
  19154. */
  19155. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  19156. ECC_SECP256R1);
  19157. if (ret != 0)
  19158. goto done;
  19159. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  19160. * in key3.
  19161. */
  19162. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  19163. key1->dp->prime, ECC_SECP256R1);
  19164. if (ret != 0)
  19165. goto done;
  19166. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  19167. 1);
  19168. if (ret != 0) {
  19169. ret = -10070;
  19170. goto done;
  19171. }
  19172. done:
  19173. #ifdef WOLFSSL_SMALL_STACK
  19174. if (key2 != NULL) {
  19175. wc_ecc_free(key2);
  19176. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19177. }
  19178. if (key3 != NULL) {
  19179. wc_ecc_free(key3);
  19180. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19181. }
  19182. #else
  19183. wc_ecc_free(key3);
  19184. wc_ecc_free(key2);
  19185. #endif
  19186. return ret;
  19187. }
  19188. #endif
  19189. #ifdef HAVE_ECC_DHE
  19190. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  19191. {
  19192. int ret;
  19193. byte out[128];
  19194. word32 outLen = sizeof(out);
  19195. /* Parameter Validation testing. */
  19196. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  19197. if (ret != BAD_FUNC_ARG)
  19198. return -10080;
  19199. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  19200. if (ret != BAD_FUNC_ARG)
  19201. return -10081;
  19202. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  19203. if (ret != BAD_FUNC_ARG)
  19204. return -10082;
  19205. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  19206. if (ret != BAD_FUNC_ARG)
  19207. return -10083;
  19208. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19209. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19210. !defined(HAVE_SELFTEST)
  19211. ret = wc_ecc_set_rng(key, rng);
  19212. if (ret != 0)
  19213. return -10084;
  19214. #else
  19215. (void)rng;
  19216. #endif
  19217. /* Use API. */
  19218. ret = 0;
  19219. do {
  19220. #if defined(WOLFSSL_ASYNC_CRYPT)
  19221. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19222. #endif
  19223. if (ret == 0)
  19224. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  19225. } while (ret == WC_PENDING_E);
  19226. if (ret != 0)
  19227. return -10085;
  19228. TEST_SLEEP();
  19229. return 0;
  19230. }
  19231. #endif /* HAVE_ECC_DHE */
  19232. #endif
  19233. static int ecc_def_curve_test(WC_RNG *rng)
  19234. {
  19235. int ret;
  19236. #ifdef WOLFSSL_SMALL_STACK
  19237. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19238. #else
  19239. ecc_key key[1];
  19240. #endif
  19241. #ifdef WOLFSSL_SMALL_STACK
  19242. if (key == NULL)
  19243. ERROR_OUT(MEMORY_E, done);
  19244. #endif
  19245. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19246. /* Use API */
  19247. ret = wc_ecc_set_flags(NULL, 0);
  19248. if (ret != BAD_FUNC_ARG) {
  19249. ret = -10090;
  19250. goto done;
  19251. }
  19252. ret = wc_ecc_set_flags(key, 0);
  19253. if (ret != 0) {
  19254. ret = -10091;
  19255. goto done;
  19256. }
  19257. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19258. #if defined(WOLFSSL_ASYNC_CRYPT)
  19259. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19260. #endif
  19261. if (ret != 0) {
  19262. ret = -10092;
  19263. goto done;
  19264. }
  19265. TEST_SLEEP();
  19266. #ifndef NO_SIG_WRAPPER
  19267. ret = ecc_sig_test(rng, key);
  19268. if (ret < 0)
  19269. goto done;
  19270. #endif
  19271. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19272. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  19273. ret = ecc_exp_imp_test(key);
  19274. if (ret < 0)
  19275. goto done;
  19276. #endif
  19277. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19278. !defined(WOLFSSL_CRYPTOCELL)
  19279. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19280. ret = ecc_mulmod_test(key);
  19281. if (ret < 0)
  19282. goto done;
  19283. #endif
  19284. #ifdef HAVE_ECC_DHE
  19285. ret = ecc_ssh_test(key, rng);
  19286. if (ret < 0)
  19287. goto done;
  19288. #endif
  19289. #endif /* WOLFSSL_ATECC508A */
  19290. done:
  19291. #ifdef WOLFSSL_SMALL_STACK
  19292. if (key != NULL) {
  19293. wc_ecc_free(key);
  19294. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19295. }
  19296. #else
  19297. wc_ecc_free(key);
  19298. #endif
  19299. return ret;
  19300. }
  19301. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  19302. #ifdef WOLFSSL_CERT_EXT
  19303. static int ecc_decode_test(void)
  19304. {
  19305. int ret;
  19306. word32 inSz;
  19307. word32 inOutIdx;
  19308. #ifdef WOLFSSL_SMALL_STACK
  19309. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19310. #else
  19311. ecc_key key[1];
  19312. #endif
  19313. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  19314. /* This is ecc_clikeypub_der_256. */
  19315. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  19316. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  19317. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  19318. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  19319. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  19320. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  19321. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  19322. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  19323. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  19324. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  19325. 0xb4 };
  19326. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  19327. 0x00, 0x04, 0x01, 0x01 };
  19328. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  19329. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19330. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  19331. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19332. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  19333. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19334. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  19335. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19336. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19337. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  19338. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19339. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  19340. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  19341. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19342. 0x03, 0x03, 0x04, 0x01, 0x01 };
  19343. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  19344. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19345. 0x03, 0x03, 0x00, 0x04, 0x01 };
  19346. #ifdef WOLFSSL_SMALL_STACK
  19347. if (key == NULL)
  19348. ERROR_OUT(MEMORY_E, done);
  19349. #endif
  19350. XMEMSET(key, 0, sizeof *key);
  19351. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19352. inSz = sizeof(good);
  19353. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  19354. if (ret != BAD_FUNC_ARG) {
  19355. ret = -10100;
  19356. goto done;
  19357. }
  19358. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  19359. if (ret != BAD_FUNC_ARG) {
  19360. ret = -10101;
  19361. goto done;
  19362. }
  19363. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  19364. if (ret != BAD_FUNC_ARG) {
  19365. ret = -10102;
  19366. goto done;
  19367. }
  19368. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  19369. if (ret != BAD_FUNC_ARG) {
  19370. ret = -10103;
  19371. goto done;
  19372. }
  19373. /* Change offset to produce bad input data. */
  19374. inOutIdx = 2;
  19375. inSz = sizeof(good) - inOutIdx;
  19376. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19377. if (ret != ASN_PARSE_E) {
  19378. ret = -10104;
  19379. goto done;
  19380. }
  19381. inOutIdx = 4;
  19382. inSz = sizeof(good) - inOutIdx;
  19383. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19384. if (ret != ASN_PARSE_E) {
  19385. ret = -10105;
  19386. goto done;
  19387. }
  19388. /* Bad data. */
  19389. inSz = sizeof(badNoObjId);
  19390. inOutIdx = 0;
  19391. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  19392. if (ret != ASN_OBJECT_ID_E) {
  19393. ret = -10106;
  19394. goto done;
  19395. }
  19396. inSz = sizeof(badOneObjId);
  19397. inOutIdx = 0;
  19398. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  19399. if (ret != ASN_OBJECT_ID_E) {
  19400. ret = -10107;
  19401. goto done;
  19402. }
  19403. inSz = sizeof(badObjId1Len);
  19404. inOutIdx = 0;
  19405. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  19406. if (ret != ASN_PARSE_E) {
  19407. ret = -10108;
  19408. goto done;
  19409. }
  19410. inSz = sizeof(badObj2d1Len);
  19411. inOutIdx = 0;
  19412. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  19413. if (ret != ASN_PARSE_E) {
  19414. ret = -10109;
  19415. goto done;
  19416. }
  19417. inSz = sizeof(badNotBitStr);
  19418. inOutIdx = 0;
  19419. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  19420. if (ret != ASN_BITSTR_E) {
  19421. ret = -10110;
  19422. goto done;
  19423. }
  19424. inSz = sizeof(badBitStrLen);
  19425. inOutIdx = 0;
  19426. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  19427. if (ret != ASN_PARSE_E) {
  19428. ret = -10111;
  19429. goto done;
  19430. }
  19431. inSz = sizeof(badNoBitStrZero);
  19432. inOutIdx = 0;
  19433. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  19434. if (ret != ASN_EXPECT_0_E) {
  19435. ret = -10112;
  19436. goto done;
  19437. }
  19438. inSz = sizeof(badPoint);
  19439. inOutIdx = 0;
  19440. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  19441. if (ret != ASN_ECC_KEY_E) {
  19442. ret = -10113;
  19443. goto done;
  19444. }
  19445. inSz = sizeof(good);
  19446. inOutIdx = 0;
  19447. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19448. if (ret != 0) {
  19449. ret = -10114;
  19450. goto done;
  19451. }
  19452. done:
  19453. #ifdef WOLFSSL_SMALL_STACK
  19454. if (key != NULL) {
  19455. wc_ecc_free(key);
  19456. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19457. }
  19458. #else
  19459. wc_ecc_free(key);
  19460. #endif
  19461. return ret;
  19462. }
  19463. #endif /* WOLFSSL_CERT_EXT */
  19464. #ifdef WOLFSSL_CUSTOM_CURVES
  19465. static const byte eccKeyExplicitCurve[] = {
  19466. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  19467. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  19468. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  19469. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  19470. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  19471. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19472. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19473. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  19474. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  19475. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  19476. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  19477. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  19478. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  19479. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  19480. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  19481. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  19482. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  19483. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  19484. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19485. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19486. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  19487. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  19488. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  19489. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  19490. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  19491. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  19492. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  19493. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  19494. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  19495. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  19496. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  19497. };
  19498. static int ecc_test_custom_curves(WC_RNG* rng)
  19499. {
  19500. int ret;
  19501. word32 inOutIdx;
  19502. #ifdef WOLFSSL_SMALL_STACK
  19503. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19504. #else
  19505. ecc_key key[1];
  19506. #endif
  19507. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  19508. #ifndef WOLFSSL_ECC_CURVE_STATIC
  19509. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  19510. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  19511. };
  19512. #define ecc_oid_brainpoolp256r1_sz \
  19513. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  19514. #else
  19515. #define ecc_oid_brainpoolp256r1 { \
  19516. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  19517. }
  19518. #define ecc_oid_brainpoolp256r1_sz 9
  19519. #endif
  19520. #define ecc_oid_brainpoolp256r1_sum 104
  19521. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  19522. 32, /* size/bytes */
  19523. ECC_CURVE_CUSTOM, /* ID */
  19524. "BRAINPOOLP256R1", /* curve name */
  19525. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  19526. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  19527. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  19528. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  19529. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  19530. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  19531. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  19532. ecc_oid_brainpoolp256r1_sz,
  19533. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  19534. 1, /* cofactor */
  19535. };
  19536. #ifdef WOLFSSL_SMALL_STACK
  19537. if (! key) {
  19538. ret = MEMORY_E;
  19539. goto done;
  19540. }
  19541. #endif
  19542. XMEMSET(key, 0, sizeof *key);
  19543. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  19544. &ecc_dp_brainpool256r1);
  19545. if (ret != 0) {
  19546. printf("ECC test for custom curve failed! %d\n", ret);
  19547. goto done;
  19548. }
  19549. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  19550. {
  19551. int curve_id;
  19552. #ifdef HAVE_ECC_BRAINPOOL
  19553. curve_id = ECC_BRAINPOOLP256R1;
  19554. #else
  19555. curve_id = ECC_SECP256K1;
  19556. #endif
  19557. /* Test and demonstrate use of non-SECP curve */
  19558. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  19559. if (ret < 0) {
  19560. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  19561. goto done;
  19562. }
  19563. }
  19564. #endif
  19565. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  19566. if (ret != 0) {
  19567. ret = -10120;
  19568. goto done;
  19569. }
  19570. inOutIdx = 0;
  19571. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  19572. sizeof(eccKeyExplicitCurve));
  19573. if (ret != 0)
  19574. ret = -10121;
  19575. done:
  19576. #ifdef WOLFSSL_SMALL_STACK
  19577. if (key) {
  19578. wc_ecc_free(key);
  19579. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19580. }
  19581. #else
  19582. wc_ecc_free(key);
  19583. #endif
  19584. return ret;
  19585. }
  19586. #endif /* WOLFSSL_CUSTOM_CURVES */
  19587. #ifdef WOLFSSL_CERT_GEN
  19588. /* Make Cert / Sign example for ECC cert and ECC CA */
  19589. static int ecc_test_cert_gen(WC_RNG* rng)
  19590. {
  19591. int ret;
  19592. #ifdef WOLFSSL_SMALL_STACK
  19593. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19594. #ifdef WOLFSSL_TEST_CERT
  19595. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19596. #endif
  19597. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19598. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19599. #else
  19600. Cert myCert[1];
  19601. #ifdef WOLFSSL_TEST_CERT
  19602. DecodedCert decode[1];
  19603. #endif
  19604. ecc_key caEccKey[1];
  19605. ecc_key certPubKey[1];
  19606. #endif
  19607. int certSz;
  19608. size_t bytes;
  19609. word32 idx = 0;
  19610. #ifndef USE_CERT_BUFFERS_256
  19611. XFILE file;
  19612. #endif
  19613. #ifdef WOLFSSL_SMALL_STACK
  19614. byte* der = NULL;
  19615. #else
  19616. byte der[FOURK_BUF];
  19617. #endif
  19618. #ifdef WOLFSSL_SMALL_STACK
  19619. if ((myCert == NULL)
  19620. #ifdef WOLFSSL_TEST_CERT
  19621. || (decode == NULL)
  19622. #endif
  19623. || (caEccKey == NULL) || (certPubKey == NULL))
  19624. ERROR_OUT(MEMORY_E, exit);
  19625. #endif
  19626. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  19627. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  19628. #ifdef WOLFSSL_SMALL_STACK
  19629. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19630. if (der == NULL) {
  19631. ERROR_OUT(-10130, exit);
  19632. }
  19633. #endif
  19634. /* Get cert private key */
  19635. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19636. /* Get Cert Key 384 */
  19637. #ifdef USE_CERT_BUFFERS_256
  19638. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  19639. bytes = sizeof_ca_ecc_key_der_384;
  19640. #else
  19641. file = XFOPEN(eccCaKey384File, "rb");
  19642. if (!file) {
  19643. ERROR_OUT(-10131, exit);
  19644. }
  19645. bytes = XFREAD(der, 1, FOURK_BUF, file);
  19646. XFCLOSE(file);
  19647. (void)eccCaKeyFile;
  19648. #endif /* USE_CERT_BUFFERS_256 */
  19649. #else
  19650. #ifdef USE_CERT_BUFFERS_256
  19651. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  19652. bytes = sizeof_ca_ecc_key_der_256;
  19653. #else
  19654. file = XFOPEN(eccCaKeyFile, "rb");
  19655. if (!file) {
  19656. ERROR_OUT(-10132, exit);
  19657. }
  19658. bytes = XFREAD(der, 1, FOURK_BUF, file);
  19659. XFCLOSE(file);
  19660. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19661. (void)eccCaKey384File;
  19662. #endif
  19663. #endif /* USE_CERT_BUFFERS_256 */
  19664. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  19665. /* Get CA Key */
  19666. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  19667. if (ret != 0) {
  19668. ERROR_OUT(-10133, exit);
  19669. }
  19670. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  19671. if (ret != 0) {
  19672. ERROR_OUT(-10134, exit);
  19673. }
  19674. /* Make a public key */
  19675. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  19676. if (ret != 0) {
  19677. ERROR_OUT(-10135, exit);
  19678. }
  19679. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  19680. #if defined(WOLFSSL_ASYNC_CRYPT)
  19681. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19682. #endif
  19683. if (ret != 0) {
  19684. ERROR_OUT(-10136, exit);
  19685. }
  19686. TEST_SLEEP();
  19687. /* Setup Certificate */
  19688. if (wc_InitCert(myCert)) {
  19689. ERROR_OUT(-10137, exit);
  19690. }
  19691. #ifndef NO_SHA256
  19692. myCert->sigType = CTC_SHA256wECDSA;
  19693. #else
  19694. myCert->sigType = CTC_SHAwECDSA;
  19695. #endif
  19696. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  19697. #ifdef WOLFSSL_CERT_EXT
  19698. /* add Policies */
  19699. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  19700. CTC_MAX_CERTPOL_SZ);
  19701. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  19702. CTC_MAX_CERTPOL_SZ);
  19703. myCert->certPoliciesNb = 2;
  19704. /* add SKID from the Public Key */
  19705. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  19706. ERROR_OUT(-10138, exit);
  19707. }
  19708. /* add AKID from the Public Key */
  19709. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  19710. ERROR_OUT(-10139, exit);
  19711. }
  19712. /* add Key Usage */
  19713. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  19714. ERROR_OUT(-10140, exit);
  19715. }
  19716. #endif /* WOLFSSL_CERT_EXT */
  19717. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19718. #if defined(USE_CERT_BUFFERS_256)
  19719. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  19720. sizeof_ca_ecc_cert_der_384);
  19721. #else
  19722. ret = wc_SetIssuer(myCert, eccCaCert384File);
  19723. (void)eccCaCertFile;
  19724. #endif
  19725. #else
  19726. #if defined(USE_CERT_BUFFERS_256)
  19727. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  19728. sizeof_ca_ecc_cert_der_256);
  19729. #else
  19730. ret = wc_SetIssuer(myCert, eccCaCertFile);
  19731. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19732. (void)eccCaCert384File;
  19733. #endif
  19734. #endif
  19735. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  19736. if (ret < 0) {
  19737. ERROR_OUT(-10141, exit);
  19738. }
  19739. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  19740. if (certSz < 0) {
  19741. ERROR_OUT(-10142, exit);
  19742. }
  19743. ret = 0;
  19744. do {
  19745. #if defined(WOLFSSL_ASYNC_CRYPT)
  19746. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19747. #endif
  19748. if (ret >= 0) {
  19749. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  19750. FOURK_BUF, NULL, caEccKey, rng);
  19751. }
  19752. } while (ret == WC_PENDING_E);
  19753. if (ret < 0) {
  19754. ERROR_OUT(-10143, exit);
  19755. }
  19756. certSz = ret;
  19757. TEST_SLEEP();
  19758. #ifdef WOLFSSL_TEST_CERT
  19759. InitDecodedCert(decode, der, certSz, 0);
  19760. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  19761. if (ret != 0) {
  19762. FreeDecodedCert(decode);
  19763. ERROR_OUT(-10144, exit);
  19764. }
  19765. FreeDecodedCert(decode);
  19766. #endif
  19767. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  19768. CERT_TYPE, -6735);
  19769. if (ret != 0) {
  19770. goto exit;
  19771. }
  19772. exit:
  19773. #ifdef WOLFSSL_SMALL_STACK
  19774. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19775. #endif
  19776. #ifdef WOLFSSL_SMALL_STACK
  19777. if (myCert != NULL)
  19778. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19779. #ifdef WOLFSSL_TEST_CERT
  19780. if (decode != NULL)
  19781. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19782. #endif
  19783. if (caEccKey != NULL) {
  19784. wc_ecc_free(caEccKey);
  19785. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19786. }
  19787. if (certPubKey != NULL) {
  19788. wc_ecc_free(certPubKey);
  19789. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19790. }
  19791. #else
  19792. wc_ecc_free(certPubKey);
  19793. wc_ecc_free(caEccKey);
  19794. #endif
  19795. return ret;
  19796. }
  19797. #endif /* WOLFSSL_CERT_GEN */
  19798. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  19799. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  19800. static int ecc_test_allocator(WC_RNG* rng)
  19801. {
  19802. int ret = 0;
  19803. ecc_key* key;
  19804. key = wc_ecc_key_new(HEAP_HINT);
  19805. if (key == NULL) {
  19806. ERROR_OUT(-10150, exit);
  19807. }
  19808. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19809. if (ret != 0) {
  19810. ERROR_OUT(-10151, exit);
  19811. }
  19812. exit:
  19813. wc_ecc_key_free(key);
  19814. return ret;
  19815. }
  19816. #endif
  19817. /* ECC Non-blocking tests for Sign and Verify */
  19818. /* Requires SP math and supports P384 or P256 */
  19819. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  19820. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  19821. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  19822. /* Test Data - Random */
  19823. static const uint8_t kMsg[] = {
  19824. 0x69, 0xbc, 0x9f, 0xce, 0x68, 0x17, 0xc2, 0x10, 0xea, 0xfc, 0x10, 0x65, 0x67, 0x52, 0xed, 0x78,
  19825. 0x6e, 0xb8, 0x83, 0x9c, 0x9a, 0xb4, 0x56, 0x0d, 0xc1, 0x0d, 0x1f, 0x78, 0x6e, 0x75, 0xd7, 0xbe,
  19826. 0x92, 0x6b, 0x12, 0xf6, 0x76, 0x60, 0x8e, 0xb1, 0xf4, 0x19, 0x0c, 0x81, 0xe7, 0x54, 0x5e, 0xbc,
  19827. 0xe0, 0xae, 0xc2, 0x7d, 0x1b, 0xc4, 0x6e, 0xec, 0xb1, 0x99, 0x6c, 0xbf, 0x0e, 0x38, 0xa8, 0x01,
  19828. 0xa6, 0x9a, 0x48, 0x12, 0xe4, 0xc9, 0x3b, 0xf0, 0x63, 0x46, 0x15, 0xb4, 0x61, 0xa8, 0x1a, 0x60,
  19829. 0x71, 0x87, 0x98, 0xd7, 0x6f, 0x98, 0x7b, 0x2d, 0xb9, 0x19, 0x1b, 0x21, 0x9c, 0x70, 0x58, 0xe8,
  19830. 0x0d, 0x0f, 0xe9, 0x2d, 0x9a, 0x9a, 0xf1, 0x55, 0xa0, 0x4c, 0xd3, 0x07, 0xbd, 0x97, 0x48, 0xec,
  19831. 0x88, 0x0a, 0xaf, 0xb3, 0x80, 0x78, 0xa4, 0x59, 0x43, 0x57, 0xd3, 0xa7, 0x01, 0x66, 0x0e, 0xfc
  19832. };
  19833. /* ECC Private Key "d" */
  19834. static const uint8_t kPrivKey[] = {
  19835. #ifdef HAVE_ECC384
  19836. /* SECP384R1 */
  19837. /* d */
  19838. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  19839. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  19840. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  19841. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  19842. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  19843. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  19844. #else
  19845. /* SECP256R1 */
  19846. /* d */
  19847. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  19848. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  19849. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  19850. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  19851. #endif
  19852. };
  19853. /* ECC public key Qx/Qy */
  19854. static const uint8_t kPubKey[] = {
  19855. #ifdef HAVE_ECC384
  19856. /* SECP384R1 */
  19857. /* Qx */
  19858. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  19859. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  19860. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  19861. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  19862. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  19863. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  19864. /* Qy */
  19865. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  19866. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  19867. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  19868. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  19869. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  19870. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  19871. #else
  19872. /* SECP256R1 */
  19873. /* Qx */
  19874. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  19875. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  19876. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  19877. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  19878. /* Qy */
  19879. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  19880. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  19881. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  19882. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  19883. #endif
  19884. };
  19885. /* ECC Curve */
  19886. #ifdef HAVE_ECC384
  19887. /* SECP384R1 */
  19888. #define ECC_CURVE_SZ 48
  19889. #define ECC_CURVE_ID ECC_SECP384R1
  19890. #else
  19891. /* SECP256R1 */
  19892. #define ECC_CURVE_SZ 32
  19893. #define ECC_CURVE_ID ECC_SECP256R1
  19894. #endif
  19895. /* Hash Algorithm */
  19896. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  19897. #define HASH_DIGEST_SZ WC_SHA3_384_DIGEST_SIZE
  19898. #define HASH_SHA_VER 3
  19899. #define CRYPTO_HASH_FN crypto_sha3_384
  19900. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  19901. #define HASH_DIGEST_SZ WC_SHA384_DIGEST_SIZE
  19902. #define HASH_SHA_VER 2
  19903. #define CRYPTO_HASH_FN crypto_sha2_384
  19904. #elif !defined(NO_SHA256)
  19905. #define HASH_DIGEST_SZ WC_SHA256_DIGEST_SIZE
  19906. #define HASH_SHA_VER 2
  19907. #define CRYPTO_HASH_FN crypto_sha2_256
  19908. #else
  19909. #error test configuration not supported
  19910. #endif
  19911. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  19912. /* helper to perform hashing block by block */
  19913. static int crypto_sha3_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  19914. uint32_t hashSz, uint32_t blkSz)
  19915. {
  19916. int ret;
  19917. uint32_t i = 0, chunk;
  19918. wc_Sha3 sha3;
  19919. /* validate arguments */
  19920. if ((buf == NULL && len > 0) || hash == NULL ||
  19921. hashSz < WC_SHA3_384_DIGEST_SIZE || blkSz == 0)
  19922. {
  19923. return BAD_FUNC_ARG;
  19924. }
  19925. /* Init Sha3_384 structure */
  19926. ret = wc_InitSha3_384(&sha3, NULL, INVALID_DEVID);
  19927. if (ret != 0) {
  19928. return ret;
  19929. }
  19930. while (i < len) {
  19931. chunk = blkSz;
  19932. if ((chunk + i) > len)
  19933. chunk = len - i;
  19934. /* Perform chunked update */
  19935. ret = wc_Sha3_384_Update(&sha3, (buf + i), chunk);
  19936. if (ret != 0) {
  19937. break;
  19938. }
  19939. i += chunk;
  19940. }
  19941. if (ret == 0) {
  19942. /* Get final digest result */
  19943. ret = wc_Sha3_384_Final(&sha3, hash);
  19944. }
  19945. return ret;
  19946. }
  19947. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  19948. /* helper to perform hashing block by block */
  19949. static int crypto_sha2_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  19950. uint32_t hashSz, uint32_t blkSz)
  19951. {
  19952. int ret;
  19953. uint32_t i = 0, chunk;
  19954. wc_Sha384 sha384;
  19955. /* validate arguments */
  19956. if ((buf == NULL && len > 0) || hash == NULL ||
  19957. hashSz < WC_SHA384_DIGEST_SIZE || blkSz == 0)
  19958. {
  19959. return BAD_FUNC_ARG;
  19960. }
  19961. /* Init Sha384 structure */
  19962. ret = wc_InitSha384(&sha384);
  19963. if (ret != 0) {
  19964. return ret;
  19965. }
  19966. while (i < len) {
  19967. chunk = blkSz;
  19968. if ((chunk + i) > len)
  19969. chunk = len - i;
  19970. /* Perform chunked update */
  19971. ret = wc_Sha384Update(&sha384, (buf + i), chunk);
  19972. if (ret != 0) {
  19973. break;
  19974. }
  19975. i += chunk;
  19976. }
  19977. if (ret == 0) {
  19978. /* Get final digest result */
  19979. ret = wc_Sha384Final(&sha384, hash);
  19980. }
  19981. return ret;
  19982. }
  19983. #elif !defined(NO_SHA256)
  19984. /* helper to perform hashing block by block */
  19985. static int crypto_sha2_256(const uint8_t *buf, uint32_t len, uint8_t *hash,
  19986. uint32_t hashSz, uint32_t blkSz)
  19987. {
  19988. int ret;
  19989. uint32_t i = 0, chunk;
  19990. wc_Sha256 sha256;
  19991. /* validate arguments */
  19992. if ((buf == NULL && len > 0) || hash == NULL ||
  19993. hashSz < WC_SHA256_DIGEST_SIZE || blkSz == 0)
  19994. {
  19995. return BAD_FUNC_ARG;
  19996. }
  19997. /* Init Sha256 structure */
  19998. ret = wc_InitSha256(&sha256);
  19999. if (ret != 0) {
  20000. return ret;
  20001. }
  20002. while (i < len) {
  20003. chunk = blkSz;
  20004. if ((chunk + i) > len)
  20005. chunk = len - i;
  20006. /* Perform chunked update */
  20007. ret = wc_Sha256Update(&sha256, (buf + i), chunk);
  20008. if (ret != 0) {
  20009. break;
  20010. }
  20011. i += chunk;
  20012. }
  20013. if (ret == 0) {
  20014. /* Get final digest result */
  20015. ret = wc_Sha256Final(&sha256, hash);
  20016. }
  20017. return ret;
  20018. }
  20019. #endif
  20020. /* perform verify of signature and hash using public key */
  20021. /* key is public Qx + public Qy */
  20022. /* sig is r + s */
  20023. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  20024. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  20025. uint32_t curveSz, int curveId)
  20026. {
  20027. int ret, verify_res = 0, count = 0;
  20028. mp_int r, s;
  20029. ecc_key ecc;
  20030. ecc_nb_ctx_t nb_ctx;
  20031. /* validate arguments */
  20032. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  20033. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  20034. {
  20035. return BAD_FUNC_ARG;
  20036. }
  20037. /* Setup the ECC key */
  20038. ret = wc_ecc_init(&ecc);
  20039. if (ret < 0) {
  20040. return ret;
  20041. }
  20042. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20043. if (ret != MP_OKAY) {
  20044. wc_ecc_free(&ecc);
  20045. return ret;
  20046. }
  20047. /* Setup the signature r/s variables */
  20048. ret = mp_init(&r);
  20049. if (ret != MP_OKAY) {
  20050. wc_ecc_free(&ecc);
  20051. return ret;
  20052. }
  20053. ret = mp_init(&s);
  20054. if (ret != MP_OKAY) {
  20055. mp_clear(&r);
  20056. wc_ecc_free(&ecc);
  20057. return ret;
  20058. }
  20059. /* Import public key x/y */
  20060. ret = wc_ecc_import_unsigned(
  20061. &ecc,
  20062. (byte*)key, /* Public "x" Coordinate */
  20063. (byte*)(key + curveSz), /* Public "y" Coordinate */
  20064. NULL, /* Private "d" (optional) */
  20065. curveId /* ECC Curve Id */
  20066. );
  20067. /* Make sure it was a public key imported */
  20068. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  20069. ret = ECC_BAD_ARG_E;
  20070. }
  20071. /* Import signature r/s */
  20072. if (ret == 0) {
  20073. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  20074. }
  20075. if (ret == 0) {
  20076. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  20077. }
  20078. /* Verify ECC Signature */
  20079. if (ret == 0) {
  20080. do {
  20081. ret = wc_ecc_verify_hash_ex(
  20082. &r, &s, /* r/s as mp_int */
  20083. hash, hashSz, /* computed hash digest */
  20084. &verify_res, /* verification result 1=success */
  20085. &ecc
  20086. );
  20087. count++;
  20088. /* TODO: Real-time work can be called here */
  20089. } while (ret == FP_WOULDBLOCK);
  20090. #ifdef DEBUG_WOLFSSL
  20091. printf("ECC non-block verify: %d times\n", count);
  20092. #endif
  20093. }
  20094. /* check verify result */
  20095. if (ret == 0 && verify_res == 0) {
  20096. ret = SIG_VERIFY_E;
  20097. }
  20098. mp_clear(&r);
  20099. mp_clear(&s);
  20100. wc_ecc_free(&ecc);
  20101. (void)count;
  20102. return ret;
  20103. }
  20104. /* perform signature operation against hash using private key */
  20105. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  20106. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  20107. uint32_t curveSz, int curveId, WC_RNG* rng)
  20108. {
  20109. int ret, count = 0;
  20110. mp_int r, s;
  20111. ecc_key ecc;
  20112. ecc_nb_ctx_t nb_ctx;
  20113. /* validate arguments */
  20114. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  20115. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  20116. {
  20117. return BAD_FUNC_ARG;
  20118. }
  20119. /* Initialize signature result */
  20120. memset(sig, 0, curveSz*2);
  20121. /* Setup the ECC key */
  20122. ret = wc_ecc_init(&ecc);
  20123. if (ret < 0) {
  20124. return ret;
  20125. }
  20126. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20127. if (ret != MP_OKAY) {
  20128. wc_ecc_free(&ecc);
  20129. return ret;
  20130. }
  20131. /* Setup the signature r/s variables */
  20132. ret = mp_init(&r);
  20133. if (ret != MP_OKAY) {
  20134. wc_ecc_free(&ecc);
  20135. return ret;
  20136. }
  20137. ret = mp_init(&s);
  20138. if (ret != MP_OKAY) {
  20139. mp_clear(&r);
  20140. wc_ecc_free(&ecc);
  20141. return ret;
  20142. }
  20143. /* Import private key "k" */
  20144. ret = wc_ecc_import_private_key_ex(
  20145. key, keySz, /* private key "d" */
  20146. NULL, 0, /* public (optional) */
  20147. &ecc,
  20148. curveId /* ECC Curve Id */
  20149. );
  20150. if (ret == 0) {
  20151. do {
  20152. /* Verify ECC Signature */
  20153. ret = wc_ecc_sign_hash_ex(
  20154. hash, hashSz, /* computed hash digest */
  20155. rng, &ecc, /* random and key context */
  20156. &r, &s /* r/s as mp_int */
  20157. );
  20158. count++;
  20159. /* TODO: Real-time work can be called here */
  20160. } while (ret == FP_WOULDBLOCK);
  20161. #ifdef DEBUG_WOLFSSL
  20162. printf("ECC non-block sign: %d times\n", count);
  20163. #endif
  20164. }
  20165. if (ret == 0) {
  20166. /* export r/s */
  20167. mp_to_unsigned_bin(&r, sig);
  20168. mp_to_unsigned_bin(&s, sig + curveSz);
  20169. }
  20170. mp_clear(&r);
  20171. mp_clear(&s);
  20172. wc_ecc_free(&ecc);
  20173. (void)count;
  20174. return ret;
  20175. }
  20176. static int ecc_test_nonblock(WC_RNG* rng)
  20177. {
  20178. int ret;
  20179. uint8_t hash[HASH_DIGEST_SZ];
  20180. uint8_t sig[ECC_CURVE_SZ*2];
  20181. uint32_t sigSz = sizeof(sig);
  20182. ret = CRYPTO_HASH_FN(
  20183. kMsg, sizeof(kMsg), /* input message */
  20184. hash, sizeof(hash), /* hash digest result */
  20185. 32 /* configurable block / chunk size */
  20186. );
  20187. if (ret == 0) {
  20188. /* Sign hash using private key */
  20189. /* Note: result of an ECC sign varies for each call even with same
  20190. private key and hash. This is because a new random public key is
  20191. used for each operation. */
  20192. ret = crypto_ecc_sign(
  20193. kPrivKey, sizeof(kPrivKey), /* private key */
  20194. hash, sizeof(hash), /* computed hash digest */
  20195. sig, &sigSz, /* signature r/s */
  20196. ECC_CURVE_SZ, /* curve size in bytes */
  20197. ECC_CURVE_ID, /* curve id */
  20198. rng
  20199. );
  20200. }
  20201. if (ret == 0) {
  20202. /* Verify generated signature is valid */
  20203. ret = crypto_ecc_verify(
  20204. kPubKey, sizeof(kPubKey), /* public key point x/y */
  20205. hash, sizeof(hash), /* computed hash digest */
  20206. sig, sigSz, /* signature r/s */
  20207. ECC_CURVE_SZ, /* curve size in bytes */
  20208. ECC_CURVE_ID /* curve id */
  20209. );
  20210. }
  20211. return ret;
  20212. }
  20213. #endif /* WC_ECC_NONBLOCK && WOLFSSL_PUBLIC_MP && HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  20214. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  20215. {
  20216. int ret;
  20217. WC_RNG rng;
  20218. #ifdef WOLFSSL_CERT_EXT
  20219. ret = ecc_decode_test();
  20220. if (ret < 0)
  20221. return ret;
  20222. #endif
  20223. #ifndef HAVE_FIPS
  20224. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20225. #else
  20226. ret = wc_InitRng(&rng);
  20227. #endif
  20228. #ifndef WC_NO_RNG
  20229. if (ret != 0)
  20230. return -10300;
  20231. #endif
  20232. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  20233. ret = ecc_test_curve(&rng, 14);
  20234. if (ret < 0) {
  20235. goto done;
  20236. }
  20237. #endif /* HAVE_ECC112 */
  20238. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  20239. ret = ecc_test_curve(&rng, 16);
  20240. if (ret < 0) {
  20241. goto done;
  20242. }
  20243. #endif /* HAVE_ECC128 */
  20244. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  20245. ret = ecc_test_curve(&rng, 20);
  20246. if (ret < 0) {
  20247. goto done;
  20248. }
  20249. #endif /* HAVE_ECC160 */
  20250. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  20251. ret = ecc_test_curve(&rng, 24);
  20252. if (ret < 0) {
  20253. goto done;
  20254. }
  20255. #endif /* HAVE_ECC192 */
  20256. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  20257. ret = ecc_test_curve(&rng, 28);
  20258. if (ret < 0) {
  20259. goto done;
  20260. }
  20261. #endif /* HAVE_ECC224 */
  20262. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  20263. ret = ecc_test_curve(&rng, 30);
  20264. if (ret < 0) {
  20265. goto done;
  20266. }
  20267. #endif /* HAVE_ECC239 */
  20268. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  20269. ret = ecc_test_curve(&rng, 32);
  20270. if (ret < 0) {
  20271. goto done;
  20272. }
  20273. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20274. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  20275. !defined(WOLFSSL_NO_MALLOC)
  20276. ret = ecc_point_test();
  20277. if (ret < 0) {
  20278. goto done;
  20279. }
  20280. #endif
  20281. ret = ecc_def_curve_test(&rng);
  20282. if (ret < 0) {
  20283. goto done;
  20284. }
  20285. #endif /* !NO_ECC256 */
  20286. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  20287. ret = ecc_test_curve(&rng, 40);
  20288. if (ret < 0) {
  20289. goto done;
  20290. }
  20291. #endif /* HAVE_ECC320 */
  20292. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  20293. ret = ecc_test_curve(&rng, 48);
  20294. if (ret < 0) {
  20295. goto done;
  20296. }
  20297. #endif /* HAVE_ECC384 */
  20298. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  20299. ret = ecc_test_curve(&rng, 64);
  20300. if (ret < 0) {
  20301. goto done;
  20302. }
  20303. #endif /* HAVE_ECC512 */
  20304. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  20305. ret = ecc_test_curve(&rng, 66);
  20306. if (ret < 0) {
  20307. goto done;
  20308. }
  20309. #endif /* HAVE_ECC521 */
  20310. #if defined(WOLFSSL_CUSTOM_CURVES)
  20311. ret = ecc_test_custom_curves(&rng);
  20312. if (ret != 0) {
  20313. goto done;
  20314. }
  20315. #endif
  20316. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  20317. ret = ecc_test_sign_vectors(&rng);
  20318. if (ret != 0) {
  20319. printf("ecc_test_sign_vectors failed! %d\n", ret);
  20320. goto done;
  20321. }
  20322. #endif
  20323. #ifdef HAVE_ECC_CDH
  20324. ret = ecc_test_cdh_vectors(&rng);
  20325. if (ret != 0) {
  20326. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  20327. goto done;
  20328. }
  20329. #endif
  20330. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20331. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL)
  20332. ret = ecc_test_make_pub(&rng);
  20333. if (ret != 0) {
  20334. printf("ecc_test_make_pub failed!: %d\n", ret);
  20335. goto done;
  20336. }
  20337. #elif defined(HAVE_ECC_KEY_IMPORT)
  20338. (void) ecc_test_make_pub;/* for compiler warning */
  20339. #endif
  20340. #ifdef WOLFSSL_CERT_GEN
  20341. ret = ecc_test_cert_gen(&rng);
  20342. if (ret != 0) {
  20343. printf("ecc_test_cert_gen failed!: %d\n", ret);
  20344. goto done;
  20345. }
  20346. #endif
  20347. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  20348. ret = ecc_test_allocator(&rng);
  20349. if (ret != 0) {
  20350. printf("ecc_test_allocator failed!: %d\n", ret);
  20351. goto done;
  20352. }
  20353. #endif
  20354. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  20355. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  20356. ret = ecc_test_nonblock(&rng);
  20357. if (ret != 0) {
  20358. printf("ecc_test_nonblock failed!: %d\n", ret);
  20359. goto done;
  20360. }
  20361. #endif
  20362. done:
  20363. wc_FreeRng(&rng);
  20364. return ret;
  20365. }
  20366. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  20367. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  20368. {
  20369. WC_RNG rng;
  20370. int ret = 0;
  20371. #ifdef WOLFSSL_SMALL_STACK
  20372. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  20373. *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20374. #else
  20375. ecc_key userA[1], userB[1];
  20376. #endif
  20377. byte msg[48];
  20378. byte plain[48];
  20379. byte out[80];
  20380. word32 outSz = sizeof(out);
  20381. word32 plainSz = sizeof(plain);
  20382. int i;
  20383. ecEncCtx* cliCtx = NULL;
  20384. ecEncCtx* srvCtx = NULL;
  20385. byte cliSalt[EXCHANGE_SALT_SZ];
  20386. byte srvSalt[EXCHANGE_SALT_SZ];
  20387. const byte* tmpSalt;
  20388. byte msg2[48];
  20389. byte plain2[48];
  20390. byte out2[80];
  20391. word32 outSz2 = sizeof(out2);
  20392. word32 plainSz2 = sizeof(plain2);
  20393. #ifndef HAVE_FIPS
  20394. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20395. #else
  20396. ret = wc_InitRng(&rng);
  20397. #endif
  20398. if (ret != 0)
  20399. return -10400;
  20400. #ifdef WOLFSSL_SMALL_STACK
  20401. if ((userA == NULL) ||
  20402. (userB == NULL))
  20403. ERROR_OUT(MEMORY_E, done);
  20404. #endif
  20405. XMEMSET(userA, 0, sizeof *userA);
  20406. XMEMSET(userB, 0, sizeof *userB);
  20407. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20408. if (ret != 0)
  20409. goto done;
  20410. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  20411. if (ret != 0)
  20412. goto done;
  20413. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  20414. #if defined(WOLFSSL_ASYNC_CRYPT)
  20415. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  20416. #endif
  20417. if (ret != 0){
  20418. ret = -10401; goto done;
  20419. }
  20420. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  20421. #if defined(WOLFSSL_ASYNC_CRYPT)
  20422. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  20423. #endif
  20424. if (ret != 0){
  20425. ret = -10402; goto done;
  20426. }
  20427. /* set message to incrementing 0,1,2,etc... */
  20428. for (i = 0; i < (int)sizeof(msg); i++)
  20429. msg[i] = i;
  20430. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20431. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20432. !defined(HAVE_SELFTEST)
  20433. ret = wc_ecc_set_rng(userA, &rng);
  20434. if (ret != 0) {
  20435. ret = -10403; goto done;
  20436. }
  20437. ret = wc_ecc_set_rng(userB, &rng);
  20438. if (ret != 0) {
  20439. ret = -10404; goto done;
  20440. }
  20441. #endif
  20442. /* encrypt msg to B */
  20443. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  20444. if (ret != 0) {
  20445. ret = -10405; goto done;
  20446. }
  20447. /* decrypt msg from A */
  20448. ret = wc_ecc_decrypt(userB, userA, out, outSz, plain, &plainSz, NULL);
  20449. if (ret != 0) {
  20450. ret = -10406; goto done;
  20451. }
  20452. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  20453. ret = -10407; goto done;
  20454. }
  20455. /* let's verify message exchange works, A is client, B is server */
  20456. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  20457. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  20458. if (cliCtx == NULL || srvCtx == NULL) {
  20459. ret = -10408; goto done;
  20460. }
  20461. /* get salt to send to peer */
  20462. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  20463. if (tmpSalt == NULL) {
  20464. ret = -10409; goto done;
  20465. }
  20466. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  20467. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  20468. if (tmpSalt == NULL) {
  20469. ret = -10410; goto done;
  20470. }
  20471. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  20472. /* in actual use, we'd get the peer's salt over the transport */
  20473. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  20474. if (ret != 0)
  20475. goto done;
  20476. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  20477. if (ret != 0)
  20478. goto done;
  20479. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  20480. if (ret != 0)
  20481. goto done;
  20482. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  20483. if (ret != 0)
  20484. goto done;
  20485. /* get encrypted msg (request) to send to B */
  20486. outSz = sizeof(out);
  20487. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  20488. if (ret != 0)
  20489. goto done;
  20490. /* B decrypts msg (request) from A */
  20491. plainSz = sizeof(plain);
  20492. ret = wc_ecc_decrypt(userB, userA, out, outSz, plain, &plainSz, srvCtx);
  20493. if (ret != 0)
  20494. goto done;
  20495. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  20496. ret = -10411; goto done;
  20497. }
  20498. /* msg2 (response) from B to A */
  20499. for (i = 0; i < (int)sizeof(msg2); i++)
  20500. msg2[i] = i + sizeof(msg2);
  20501. /* get encrypted msg (response) to send to B */
  20502. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  20503. &outSz2, srvCtx);
  20504. if (ret != 0)
  20505. goto done;
  20506. /* A decrypts msg (response) from B */
  20507. ret = wc_ecc_decrypt(userA, userB, out2, outSz2, plain2, &plainSz2,
  20508. cliCtx);
  20509. if (ret != 0)
  20510. goto done;
  20511. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  20512. ret = -10412; goto done;
  20513. }
  20514. done:
  20515. /* cleanup */
  20516. wc_ecc_ctx_free(srvCtx);
  20517. wc_ecc_ctx_free(cliCtx);
  20518. #ifdef WOLFSSL_SMALL_STACK
  20519. if (userA != NULL) {
  20520. wc_ecc_free(userA);
  20521. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20522. }
  20523. if (userB != NULL) {
  20524. wc_ecc_free(userB);
  20525. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20526. }
  20527. #else
  20528. wc_ecc_free(userB);
  20529. wc_ecc_free(userA);
  20530. #endif
  20531. wc_FreeRng(&rng);
  20532. return ret;
  20533. }
  20534. #endif /* HAVE_ECC_ENCRYPT */
  20535. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  20536. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  20537. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  20538. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  20539. {
  20540. size_t bytes;
  20541. #ifdef WOLFSSL_SMALL_STACK
  20542. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20543. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20544. #else
  20545. ecc_key cliKey[1];
  20546. ecc_key servKey[1];
  20547. #endif
  20548. WC_RNG rng;
  20549. word32 idx = 0;
  20550. int ret;
  20551. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  20552. byte in[] = "Everyone gets Friday off. ecc p";
  20553. word32 inLen = (word32)XSTRLEN((char*)in);
  20554. byte out[256];
  20555. byte plain[256];
  20556. int verify = 0;
  20557. word32 x;
  20558. #ifdef WOLFSSL_SMALL_STACK
  20559. if ((cliKey == NULL) || (servKey == NULL))
  20560. ERROR_OUT(MEMORY_E, done);
  20561. #endif
  20562. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  20563. if (ret != 0)
  20564. ERROR_OUT(-10420, done);
  20565. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  20566. if (ret != 0)
  20567. ERROR_OUT(-10421, done);
  20568. bytes = (size_t)sizeof_ecc_clikey_der_256;
  20569. /* place client key into ecc_key struct cliKey */
  20570. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  20571. (word32)bytes);
  20572. if (ret != 0)
  20573. ERROR_OUT(-10422, done);
  20574. idx = 0;
  20575. bytes = (size_t)sizeof_ecc_key_der_256;
  20576. /* place server key into ecc_key struct servKey */
  20577. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  20578. (word32)bytes);
  20579. if (ret != 0)
  20580. ERROR_OUT(-10423, done);
  20581. #ifndef HAVE_FIPS
  20582. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20583. #else
  20584. ret = wc_InitRng(&rng);
  20585. #endif
  20586. if (ret != 0)
  20587. ERROR_OUT(-10424, done);
  20588. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20589. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20590. !defined(HAVE_SELFTEST)
  20591. ret = wc_ecc_set_rng(cliKey, &rng);
  20592. if (ret != 0) {
  20593. ERROR_OUT(-10425, done);
  20594. }
  20595. #endif
  20596. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF)
  20597. {
  20598. word32 y;
  20599. /* test encrypt and decrypt if they're available */
  20600. x = sizeof(out);
  20601. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  20602. if (ret < 0)
  20603. ERROR_OUT(-10426, done);
  20604. y = sizeof(plain);
  20605. ret = wc_ecc_decrypt(cliKey, servKey, out, x, plain, &y, NULL);
  20606. if (ret < 0)
  20607. ERROR_OUT(-10427, done);
  20608. if (XMEMCMP(plain, in, inLen))
  20609. ERROR_OUT(-10428, done);
  20610. }
  20611. #endif
  20612. x = sizeof(out);
  20613. do {
  20614. #if defined(WOLFSSL_ASYNC_CRYPT)
  20615. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20616. #endif
  20617. if (ret == 0)
  20618. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  20619. } while (ret == WC_PENDING_E);
  20620. if (ret < 0)
  20621. ERROR_OUT(-10429, done);
  20622. TEST_SLEEP();
  20623. XMEMSET(plain, 0, sizeof(plain));
  20624. do {
  20625. #if defined(WOLFSSL_ASYNC_CRYPT)
  20626. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20627. #endif
  20628. if (ret == 0)
  20629. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  20630. cliKey);
  20631. } while (ret == WC_PENDING_E);
  20632. if (ret < 0)
  20633. ERROR_OUT(-10430, done);
  20634. if (verify != 1)
  20635. ERROR_OUT(-10431, done);
  20636. TEST_SLEEP();
  20637. #ifdef WOLFSSL_CERT_EXT
  20638. idx = 0;
  20639. bytes = sizeof_ecc_clikeypub_der_256;
  20640. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  20641. (word32) bytes);
  20642. if (ret != 0)
  20643. ERROR_OUT(-10432, done);
  20644. #endif
  20645. ret = 0;
  20646. done:
  20647. #ifdef WOLFSSL_SMALL_STACK
  20648. if (cliKey != NULL) {
  20649. wc_ecc_free(cliKey);
  20650. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20651. }
  20652. if (servKey != NULL) {
  20653. wc_ecc_free(servKey);
  20654. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20655. }
  20656. #else
  20657. wc_ecc_free(cliKey);
  20658. wc_ecc_free(servKey);
  20659. #endif
  20660. wc_FreeRng(&rng);
  20661. return ret;
  20662. }
  20663. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  20664. #endif /* HAVE_ECC */
  20665. #ifdef HAVE_CURVE25519
  20666. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  20667. defined(HAVE_CURVE25519_KEY_IMPORT)
  20668. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20669. #define X25519_TEST_CNT 5
  20670. #else
  20671. #define X25519_TEST_CNT 1
  20672. #endif
  20673. static int curve25519_overflow_test(void)
  20674. {
  20675. /* secret key for party a */
  20676. byte sa[X25519_TEST_CNT][32] = {
  20677. {
  20678. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  20679. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  20680. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  20681. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  20682. },
  20683. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20684. {
  20685. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  20686. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  20687. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  20688. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  20689. },
  20690. {
  20691. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20692. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20693. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20694. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20695. },
  20696. {
  20697. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20698. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20699. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20700. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20701. },
  20702. {
  20703. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20704. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20705. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20706. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20707. }
  20708. #endif
  20709. };
  20710. /* public key for party b */
  20711. byte pb[X25519_TEST_CNT][32] = {
  20712. {
  20713. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20714. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20715. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20716. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  20717. },
  20718. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20719. {
  20720. /* 0xff first byte in original - invalid! */
  20721. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20722. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20723. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20724. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  20725. },
  20726. {
  20727. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  20728. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  20729. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  20730. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  20731. },
  20732. {
  20733. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  20734. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  20735. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  20736. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  20737. },
  20738. {
  20739. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  20740. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  20741. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  20742. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  20743. }
  20744. #endif
  20745. };
  20746. /* expected shared key */
  20747. byte ss[X25519_TEST_CNT][32] = {
  20748. {
  20749. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  20750. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  20751. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  20752. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  20753. },
  20754. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20755. {
  20756. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  20757. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  20758. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  20759. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  20760. },
  20761. {
  20762. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20763. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20764. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20765. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  20766. },
  20767. {
  20768. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20769. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20770. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20771. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  20772. },
  20773. {
  20774. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20775. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20776. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20777. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  20778. }
  20779. #endif
  20780. };
  20781. int i;
  20782. word32 y;
  20783. byte shared[32];
  20784. curve25519_key userA;
  20785. wc_curve25519_init(&userA);
  20786. for (i = 0; i < X25519_TEST_CNT; i++) {
  20787. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  20788. sizeof(pb[i]), &userA) != 0)
  20789. return -10500 - i;
  20790. /* test against known test vector */
  20791. XMEMSET(shared, 0, sizeof(shared));
  20792. y = sizeof(shared);
  20793. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0)
  20794. return -10510 - i;
  20795. if (XMEMCMP(ss[i], shared, y))
  20796. return -10520 - i;
  20797. }
  20798. return 0;
  20799. }
  20800. /* Test the wc_curve25519_check_public API.
  20801. *
  20802. * returns 0 on success and -ve on failure.
  20803. */
  20804. static int curve25519_check_public_test(void)
  20805. {
  20806. /* Little-endian values that will fail */
  20807. byte fail_le[][CURVE25519_KEYSIZE] = {
  20808. {
  20809. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20810. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20811. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20812. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20813. },
  20814. {
  20815. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20816. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20817. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20818. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20819. },
  20820. {
  20821. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20822. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20823. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20824. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  20825. },
  20826. };
  20827. /* Big-endian values that will fail */
  20828. byte fail_be[][CURVE25519_KEYSIZE] = {
  20829. {
  20830. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20831. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20832. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20833. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20834. },
  20835. {
  20836. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20837. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  20840. },
  20841. {
  20842. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20843. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20844. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20845. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  20846. },
  20847. };
  20848. /* Good or valid public value */
  20849. byte good[CURVE25519_KEYSIZE] = {
  20850. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20851. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20852. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20853. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  20854. };
  20855. int i;
  20856. /* Parameter checks */
  20857. /* NULL pointer */
  20858. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  20859. BAD_FUNC_ARG) {
  20860. return -10600;
  20861. }
  20862. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  20863. BAD_FUNC_ARG) {
  20864. return -10601;
  20865. }
  20866. /* Length of 0 treated differently to other invalid lengths for TLS */
  20867. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  20868. return -10602;
  20869. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  20870. return -10603;
  20871. /* Length not CURVE25519_KEYSIZE */
  20872. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  20873. if (i == CURVE25519_KEYSIZE)
  20874. continue;
  20875. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  20876. ECC_BAD_ARG_E) {
  20877. return -10604 - i;
  20878. }
  20879. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  20880. ECC_BAD_ARG_E) {
  20881. return -10614 - i;
  20882. }
  20883. }
  20884. /* Little-endian fail cases */
  20885. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  20886. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  20887. EC25519_LITTLE_ENDIAN) == 0) {
  20888. return -10624 - i;
  20889. }
  20890. }
  20891. /* Big-endian fail cases */
  20892. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  20893. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  20894. EC25519_BIG_ENDIAN) == 0) {
  20895. return -10634 - i;
  20896. }
  20897. }
  20898. /* Check a valid public value works! */
  20899. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  20900. EC25519_LITTLE_ENDIAN) != 0) {
  20901. return -10644;
  20902. }
  20903. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  20904. EC25519_BIG_ENDIAN) != 0) {
  20905. return -10645;
  20906. }
  20907. return 0;
  20908. }
  20909. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  20910. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  20911. {
  20912. WC_RNG rng;
  20913. int ret;
  20914. #ifdef HAVE_CURVE25519_SHARED_SECRET
  20915. byte sharedA[32];
  20916. byte sharedB[32];
  20917. word32 y;
  20918. #endif
  20919. #ifdef HAVE_CURVE25519_KEY_EXPORT
  20920. byte exportBuf[32];
  20921. #endif
  20922. word32 x = 0;
  20923. curve25519_key userA, userB, pubKey;
  20924. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  20925. defined(HAVE_CURVE25519_KEY_IMPORT)
  20926. /* test vectors from
  20927. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  20928. */
  20929. /* secret key for party a */
  20930. byte sa[] = {
  20931. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  20932. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  20933. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  20934. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  20935. };
  20936. /* public key for party a */
  20937. byte pa[] = {
  20938. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  20939. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  20940. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  20941. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  20942. };
  20943. /* secret key for party b */
  20944. byte sb[] = {
  20945. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  20946. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  20947. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  20948. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  20949. };
  20950. /* public key for party b */
  20951. byte pb[] = {
  20952. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  20953. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  20954. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  20955. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  20956. };
  20957. /* expected shared key */
  20958. byte ss[] = {
  20959. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  20960. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  20961. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  20962. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  20963. };
  20964. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  20965. (void)x;
  20966. #ifndef HAVE_FIPS
  20967. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20968. #else
  20969. ret = wc_InitRng(&rng);
  20970. #endif
  20971. if (ret != 0)
  20972. return -10700;
  20973. wc_curve25519_init(&userA);
  20974. wc_curve25519_init(&userB);
  20975. wc_curve25519_init(&pubKey);
  20976. /* make curve25519 keys */
  20977. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  20978. return -10701;
  20979. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  20980. return -10702;
  20981. #ifdef HAVE_CURVE25519_SHARED_SECRET
  20982. /* find shared secret key */
  20983. x = sizeof(sharedA);
  20984. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  20985. return -10703;
  20986. y = sizeof(sharedB);
  20987. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  20988. return -10704;
  20989. /* compare shared secret keys to test they are the same */
  20990. if (y != x)
  20991. return -10705;
  20992. if (XMEMCMP(sharedA, sharedB, x))
  20993. return -10706;
  20994. #endif
  20995. #ifdef HAVE_CURVE25519_KEY_EXPORT
  20996. /* export a public key and import it for another user */
  20997. x = sizeof(exportBuf);
  20998. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  20999. return -10707;
  21000. #ifdef HAVE_CURVE25519_KEY_IMPORT
  21001. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  21002. return -10708;
  21003. #endif
  21004. #endif
  21005. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  21006. defined(HAVE_CURVE25519_KEY_IMPORT)
  21007. /* test shared key after importing a public key */
  21008. XMEMSET(sharedB, 0, sizeof(sharedB));
  21009. y = sizeof(sharedB);
  21010. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  21011. return -10709;
  21012. if (XMEMCMP(sharedA, sharedB, y))
  21013. return -10710;
  21014. /* import RFC test vectors and compare shared key */
  21015. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  21016. != 0)
  21017. return -10711;
  21018. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  21019. != 0)
  21020. return -10712;
  21021. /* test against known test vector */
  21022. XMEMSET(sharedB, 0, sizeof(sharedB));
  21023. y = sizeof(sharedB);
  21024. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  21025. return -10713;
  21026. if (XMEMCMP(ss, sharedB, y))
  21027. return -10714;
  21028. /* test swapping roles of keys and generating same shared key */
  21029. XMEMSET(sharedB, 0, sizeof(sharedB));
  21030. y = sizeof(sharedB);
  21031. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21032. return -10715;
  21033. if (XMEMCMP(ss, sharedB, y))
  21034. return -10716;
  21035. /* test with 1 generated key and 1 from known test vector */
  21036. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  21037. != 0)
  21038. return -10717;
  21039. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  21040. return -10718;
  21041. x = sizeof(sharedA);
  21042. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  21043. return -10719;
  21044. y = sizeof(sharedB);
  21045. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21046. return -10720;
  21047. /* compare shared secret keys to test they are the same */
  21048. if (y != x)
  21049. return -10721;
  21050. if (XMEMCMP(sharedA, sharedB, x))
  21051. return -10722;
  21052. ret = curve25519_overflow_test();
  21053. if (ret != 0)
  21054. return ret;
  21055. ret = curve25519_check_public_test();
  21056. if (ret != 0)
  21057. return ret;
  21058. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  21059. /* clean up keys when done */
  21060. wc_curve25519_free(&pubKey);
  21061. wc_curve25519_free(&userB);
  21062. wc_curve25519_free(&userA);
  21063. wc_FreeRng(&rng);
  21064. return 0;
  21065. }
  21066. #endif /* HAVE_CURVE25519 */
  21067. #ifdef HAVE_ED25519
  21068. #ifdef WOLFSSL_TEST_CERT
  21069. static int ed25519_test_cert(void)
  21070. {
  21071. DecodedCert cert[2];
  21072. DecodedCert* serverCert = NULL;
  21073. DecodedCert* caCert = NULL;
  21074. #ifdef HAVE_ED25519_VERIFY
  21075. ed25519_key key;
  21076. ed25519_key* pubKey = NULL;
  21077. int verify;
  21078. #endif /* HAVE_ED25519_VERIFY */
  21079. int ret;
  21080. byte* tmp;
  21081. size_t bytes;
  21082. XFILE file;
  21083. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21084. if (tmp == NULL) {
  21085. ERROR_OUT(-10730, done);
  21086. }
  21087. #ifdef USE_CERT_BUFFERS_256
  21088. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  21089. bytes = sizeof_ca_ed25519_cert;
  21090. #elif !defined(NO_FILESYSTEM)
  21091. file = XFOPEN(caEd25519Cert, "rb");
  21092. if (file == NULL) {
  21093. ERROR_OUT(-10731, done);
  21094. }
  21095. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  21096. XFCLOSE(file);
  21097. #else
  21098. /* No certificate to use. */
  21099. ERROR_OUT(-10732, done);
  21100. #endif
  21101. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  21102. caCert = &cert[0];
  21103. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  21104. if (ret != 0) {
  21105. ERROR_OUT(-10733, done);
  21106. }
  21107. #ifdef USE_CERT_BUFFERS_256
  21108. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  21109. bytes = sizeof_server_ed25519_cert;
  21110. #elif !defined(NO_FILESYSTEM)
  21111. file = XFOPEN(serverEd25519Cert, "rb");
  21112. if (file == NULL) {
  21113. ERROR_OUT(-10734, done);
  21114. }
  21115. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  21116. XFCLOSE(file);
  21117. #else
  21118. /* No certificate to use. */
  21119. ERROR_OUT(-10735, done);
  21120. #endif
  21121. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  21122. serverCert = &cert[1];
  21123. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  21124. if (ret != 0) {
  21125. ERROR_OUT(-10736, done);
  21126. }
  21127. #ifdef HAVE_ED25519_VERIFY
  21128. ret = wc_ed25519_init(&key);
  21129. if (ret < 0) {
  21130. ERROR_OUT(-10737, done);
  21131. }
  21132. pubKey = &key;
  21133. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  21134. pubKey);
  21135. if (ret < 0) {
  21136. ERROR_OUT(-10738, done);
  21137. }
  21138. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  21139. serverCert->source + serverCert->certBegin,
  21140. serverCert->sigIndex - serverCert->certBegin,
  21141. &verify, pubKey) < 0 || verify != 1) {
  21142. ERROR_OUT(-10739, done);
  21143. }
  21144. #endif /* HAVE_ED25519_VERIFY */
  21145. done:
  21146. if (tmp != NULL)
  21147. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21148. #ifdef HAVE_ED25519_VERIFY
  21149. wc_ed25519_free(pubKey);
  21150. #endif /* HAVE_ED25519_VERIFY */
  21151. if (caCert != NULL)
  21152. FreeDecodedCert(caCert);
  21153. if (serverCert != NULL)
  21154. FreeDecodedCert(serverCert);
  21155. return ret;
  21156. }
  21157. static int ed25519_test_make_cert(void)
  21158. {
  21159. WC_RNG rng;
  21160. Cert cert;
  21161. DecodedCert decode;
  21162. ed25519_key key;
  21163. ed25519_key* privKey = NULL;
  21164. int ret = 0;
  21165. byte* tmp = NULL;
  21166. wc_InitCert(&cert);
  21167. #ifndef HAVE_FIPS
  21168. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21169. #else
  21170. ret = wc_InitRng(&rng);
  21171. #endif
  21172. if (ret != 0)
  21173. return -10750;
  21174. wc_ed25519_init(&key);
  21175. privKey = &key;
  21176. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  21177. cert.daysValid = 365 * 2;
  21178. cert.selfSigned = 1;
  21179. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  21180. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  21181. cert.isCA = 0;
  21182. #ifdef WOLFSSL_CERT_EXT
  21183. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  21184. if (ret < 0) {
  21185. ERROR_OUT(-10751, done);
  21186. }
  21187. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  21188. if (ret < 0) {
  21189. ERROR_OUT(-10752, done);
  21190. }
  21191. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  21192. if (ret < 0) {
  21193. ERROR_OUT(-10753, done);
  21194. }
  21195. #endif
  21196. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21197. if (tmp == NULL) {
  21198. ERROR_OUT(-10754, done);
  21199. }
  21200. cert.sigType = CTC_ED25519;
  21201. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  21202. if (ret < 0) {
  21203. ERROR_OUT(-10755, done);
  21204. }
  21205. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  21206. ED25519_TYPE, privKey, &rng);
  21207. if (ret < 0) {
  21208. ERROR_OUT(-10756, done);
  21209. }
  21210. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  21211. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  21212. FreeDecodedCert(&decode);
  21213. if (ret != 0) {
  21214. ERROR_OUT(-10757, done);
  21215. }
  21216. done:
  21217. if (tmp != NULL)
  21218. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21219. wc_ed25519_free(privKey);
  21220. wc_FreeRng(&rng);
  21221. return ret;
  21222. }
  21223. #endif /* WOLFSSL_TEST_CERT */
  21224. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21225. defined(HAVE_ED25519_KEY_IMPORT)
  21226. static int ed25519ctx_test(void)
  21227. {
  21228. byte out[ED25519_SIG_SIZE];
  21229. word32 outlen;
  21230. #ifdef HAVE_ED25519_VERIFY
  21231. int verify;
  21232. #endif /* HAVE_ED25519_VERIFY */
  21233. ed25519_key key;
  21234. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  21235. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  21236. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  21237. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  21238. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  21239. };
  21240. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  21241. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  21242. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  21243. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  21244. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  21245. };
  21246. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  21247. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  21248. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  21249. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  21250. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  21251. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  21252. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  21253. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  21254. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  21255. };
  21256. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  21257. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  21258. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  21259. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  21260. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  21261. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  21262. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  21263. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  21264. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  21265. };
  21266. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  21267. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  21268. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  21269. };
  21270. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  21271. 0x66,0x6f,0x6f
  21272. };
  21273. outlen = sizeof(out);
  21274. XMEMSET(out, 0, sizeof(out));
  21275. if (wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  21276. sizeof(pKeyCtx), &key) != 0)
  21277. return -10800;
  21278. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  21279. contextCtx, sizeof(contextCtx)) != 0)
  21280. return -10801;
  21281. if (XMEMCMP(out, sigCtx1, 64))
  21282. return -10802;
  21283. #if defined(HAVE_ED25519_VERIFY)
  21284. /* test verify on good msg */
  21285. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  21286. &key, contextCtx, sizeof(contextCtx)) != 0 ||
  21287. verify != 1)
  21288. return -10803;
  21289. #endif
  21290. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key, NULL,
  21291. 0) != 0)
  21292. return -10804;
  21293. if (XMEMCMP(out, sigCtx2, 64))
  21294. return -10805;
  21295. #if defined(HAVE_ED25519_VERIFY)
  21296. /* test verify on good msg */
  21297. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  21298. &key, NULL, 0) != 0 || verify != 1)
  21299. return -10806;
  21300. #endif
  21301. wc_ed25519_free(&key);
  21302. return 0;
  21303. }
  21304. static int ed25519ph_test(void)
  21305. {
  21306. byte out[ED25519_SIG_SIZE];
  21307. word32 outlen;
  21308. #ifdef HAVE_ED25519_VERIFY
  21309. int verify;
  21310. #endif /* HAVE_ED25519_VERIFY */
  21311. ed25519_key key;
  21312. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  21313. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  21314. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  21315. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  21316. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  21317. };
  21318. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  21319. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  21320. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  21321. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  21322. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  21323. };
  21324. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  21325. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  21326. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  21327. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  21328. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  21329. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  21330. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  21331. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  21332. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  21333. };
  21334. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  21335. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  21336. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  21337. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  21338. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  21339. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  21340. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  21341. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  21342. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  21343. };
  21344. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  21345. 0x61,0x62,0x63
  21346. };
  21347. /* SHA-512 hash of msgPh */
  21348. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  21349. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  21350. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  21351. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  21352. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  21353. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  21354. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  21355. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  21356. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  21357. };
  21358. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  21359. 0x66,0x6f,0x6f
  21360. };
  21361. outlen = sizeof(out);
  21362. XMEMSET(out, 0, sizeof(out));
  21363. if (wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  21364. sizeof(pKeyPh), &key) != 0) {
  21365. return -10900;
  21366. }
  21367. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  21368. 0) != 0) {
  21369. return -10901;
  21370. }
  21371. if (XMEMCMP(out, sigPh1, 64))
  21372. return -10902;
  21373. #if defined(HAVE_ED25519_VERIFY)
  21374. /* test verify on good msg */
  21375. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  21376. &key, NULL, 0) != 0 ||
  21377. verify != 1) {
  21378. return -10903;
  21379. }
  21380. #endif
  21381. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  21382. contextPh2, sizeof(contextPh2)) != 0) {
  21383. return -10904;
  21384. }
  21385. if (XMEMCMP(out, sigPh2, 64))
  21386. return -10905;
  21387. #if defined(HAVE_ED25519_VERIFY)
  21388. /* test verify on good msg */
  21389. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  21390. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  21391. verify != 1) {
  21392. return -10906;
  21393. }
  21394. #endif
  21395. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  21396. 0) != 0) {
  21397. return -10907;
  21398. }
  21399. if (XMEMCMP(out, sigPh1, 64))
  21400. return -10908;
  21401. #if defined(HAVE_ED25519_VERIFY)
  21402. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  21403. &key, NULL, 0) != 0 ||
  21404. verify != 1) {
  21405. return -10909;
  21406. }
  21407. #endif
  21408. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  21409. contextPh2, sizeof(contextPh2)) != 0) {
  21410. return -10910;
  21411. }
  21412. if (XMEMCMP(out, sigPh2, 64))
  21413. return -10911;
  21414. #if defined(HAVE_ED25519_VERIFY)
  21415. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  21416. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  21417. verify != 1) {
  21418. return -10912;
  21419. }
  21420. #endif
  21421. wc_ed25519_free(&key);
  21422. return 0;
  21423. }
  21424. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21425. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  21426. {
  21427. int ret;
  21428. WC_RNG rng;
  21429. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  21430. defined(HAVE_ED25519_KEY_IMPORT)
  21431. byte out[ED25519_SIG_SIZE];
  21432. byte exportPKey[ED25519_KEY_SIZE];
  21433. byte exportSKey[ED25519_KEY_SIZE];
  21434. word32 exportPSz;
  21435. word32 exportSSz;
  21436. int i;
  21437. word32 outlen;
  21438. #ifdef HAVE_ED25519_VERIFY
  21439. int verify;
  21440. #endif /* HAVE_ED25519_VERIFY */
  21441. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21442. word32 keySz, sigSz;
  21443. ed25519_key key;
  21444. ed25519_key key2;
  21445. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21446. defined(HAVE_ED25519_KEY_IMPORT)
  21447. /* test vectors from
  21448. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  21449. */
  21450. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  21451. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21452. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21453. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21454. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21455. };
  21456. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  21457. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  21458. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  21459. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  21460. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  21461. };
  21462. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  21463. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  21464. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  21465. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  21466. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  21467. };
  21468. /* uncompressed test */
  21469. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  21470. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21471. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21472. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21473. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21474. };
  21475. /* compressed prefix test */
  21476. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  21477. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21478. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21479. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21480. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21481. };
  21482. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  21483. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  21484. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  21485. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  21486. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  21487. };
  21488. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  21489. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  21490. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  21491. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  21492. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  21493. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  21494. };
  21495. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  21496. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  21497. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  21498. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  21499. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  21500. };
  21501. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  21502. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  21503. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  21504. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  21505. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  21506. };
  21507. /* uncompressed test */
  21508. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  21509. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  21510. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  21511. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  21512. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  21513. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  21514. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  21515. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  21516. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  21517. 0xd7
  21518. };
  21519. /* compressed prefix */
  21520. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  21521. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  21522. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  21523. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  21524. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  21525. };
  21526. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  21527. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  21528. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  21529. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  21530. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  21531. };
  21532. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  21533. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  21534. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  21535. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  21536. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21537. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21538. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21539. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21540. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21541. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21542. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21543. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21544. };
  21545. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  21546. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  21547. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  21548. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  21549. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  21550. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  21551. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  21552. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  21553. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  21554. };
  21555. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  21556. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  21557. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  21558. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  21559. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  21560. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  21561. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  21562. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  21563. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  21564. };
  21565. /* uncompressed test */
  21566. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  21567. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21568. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21569. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21570. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21571. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21572. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21573. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21574. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21575. };
  21576. /* compressed prefix */
  21577. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  21578. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21579. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21580. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21581. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21582. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21583. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21584. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21585. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21586. };
  21587. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  21588. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  21589. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  21590. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  21591. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  21592. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  21593. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  21594. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  21595. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  21596. };
  21597. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  21598. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  21599. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  21600. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  21601. /* test of a 1024 byte long message */
  21602. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  21603. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  21604. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  21605. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  21606. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  21607. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  21608. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  21609. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  21610. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  21611. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  21612. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  21613. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  21614. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  21615. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  21616. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  21617. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  21618. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  21619. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  21620. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  21621. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  21622. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  21623. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  21624. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  21625. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  21626. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  21627. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  21628. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  21629. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  21630. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  21631. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  21632. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  21633. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  21634. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  21635. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  21636. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  21637. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  21638. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  21639. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  21640. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  21641. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  21642. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  21643. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  21644. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  21645. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  21646. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  21647. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  21648. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  21649. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  21650. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  21651. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  21652. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  21653. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  21654. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  21655. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  21656. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  21657. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  21658. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  21659. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  21660. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  21661. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  21662. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  21663. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  21664. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  21665. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  21666. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  21667. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  21668. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  21669. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  21670. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  21671. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  21672. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  21673. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  21674. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  21675. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  21676. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  21677. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  21678. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  21679. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  21680. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  21681. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  21682. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  21683. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  21684. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  21685. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  21686. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  21687. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  21688. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  21689. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  21690. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  21691. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  21692. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  21693. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  21694. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  21695. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  21696. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  21697. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  21698. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  21699. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  21700. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  21701. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  21702. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  21703. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  21704. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  21705. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  21706. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  21707. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  21708. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  21709. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  21710. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  21711. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  21712. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  21713. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  21714. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  21715. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  21716. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  21717. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  21718. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  21719. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  21720. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  21721. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  21722. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  21723. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  21724. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  21725. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  21726. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  21727. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  21728. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  21729. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  21730. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  21731. };
  21732. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  21733. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  21734. sizeof(msg2),
  21735. sizeof(msg3),
  21736. 0 /*sizeof(msg1)*/,
  21737. 0 /*sizeof(msg1)*/,
  21738. sizeof(msg4)
  21739. };
  21740. #ifndef NO_ASN
  21741. static byte privateEd25519[] = {
  21742. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  21743. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  21744. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21745. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21746. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21747. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21748. };
  21749. static byte publicEd25519[] = {
  21750. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  21751. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  21752. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  21753. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  21754. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  21755. 0xf7,0x07,0x51,0x1a
  21756. };
  21757. static byte privPubEd25519[] = {
  21758. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  21759. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  21760. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21761. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21762. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21763. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  21764. 0xa1,0x22,0x04,0x20,0xd7,0x5a,0x98,0x01,
  21765. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  21766. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  21767. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  21768. 0xf7,0x07,0x51,0x1a
  21769. };
  21770. word32 idx;
  21771. ed25519_key key3;
  21772. #endif /* NO_ASN */
  21773. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21774. /* create ed25519 keys */
  21775. #ifndef HAVE_FIPS
  21776. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21777. #else
  21778. ret = wc_InitRng(&rng);
  21779. #endif
  21780. if (ret != 0)
  21781. return -11000;
  21782. wc_ed25519_init(&key);
  21783. wc_ed25519_init(&key2);
  21784. #ifndef NO_ASN
  21785. wc_ed25519_init(&key3);
  21786. #endif
  21787. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  21788. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  21789. /* helper functions for signature and key size */
  21790. keySz = wc_ed25519_size(&key);
  21791. sigSz = wc_ed25519_sig_size(&key);
  21792. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  21793. defined(HAVE_ED25519_KEY_IMPORT)
  21794. for (i = 0; i < 6; i++) {
  21795. outlen = sizeof(out);
  21796. XMEMSET(out, 0, sizeof(out));
  21797. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  21798. pKeySz[i], &key) != 0)
  21799. return -11001 - i;
  21800. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  21801. return -11011 - i;
  21802. if (XMEMCMP(out, sigs[i], 64))
  21803. return -11021 - i;
  21804. #if defined(HAVE_ED25519_VERIFY)
  21805. /* test verify on good msg */
  21806. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21807. &key) != 0 || verify != 1)
  21808. return -11031 - i;
  21809. /* test verify on bad msg */
  21810. out[outlen-1] = out[outlen-1] + 1;
  21811. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21812. &key) == 0 || verify == 1)
  21813. return -11041 - i;
  21814. #endif /* HAVE_ED25519_VERIFY */
  21815. /* test api for import/exporting keys */
  21816. exportPSz = sizeof(exportPKey);
  21817. exportSSz = sizeof(exportSKey);
  21818. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  21819. return -11051 - i;
  21820. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  21821. return -11061 - i;
  21822. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  21823. return -11071 - i;
  21824. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  21825. exportPKey, exportPSz, &key2) != 0)
  21826. return -11081 - i;
  21827. /* clear "out" buffer and test sign with imported keys */
  21828. outlen = sizeof(out);
  21829. XMEMSET(out, 0, sizeof(out));
  21830. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  21831. return -11091 - i;
  21832. #if defined(HAVE_ED25519_VERIFY)
  21833. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21834. &key2) != 0 || verify != 1)
  21835. return -11101 - i;
  21836. if (XMEMCMP(out, sigs[i], 64))
  21837. return -11111 - i;
  21838. #endif /* HAVE_ED25519_VERIFY */
  21839. }
  21840. ret = ed25519ctx_test();
  21841. if (ret != 0)
  21842. return ret;
  21843. ret = ed25519ph_test();
  21844. if (ret != 0)
  21845. return ret;
  21846. #ifndef NO_ASN
  21847. /* Try ASN.1 encoded private-only key and public key. */
  21848. idx = 0;
  21849. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  21850. sizeof(privateEd25519)) != 0)
  21851. return -11121 - i;
  21852. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  21853. != BAD_FUNC_ARG)
  21854. return -11131 - i;
  21855. idx = 0;
  21856. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  21857. sizeof(publicEd25519)) != 0)
  21858. return -11141 - i;
  21859. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  21860. return -11151 - i;
  21861. if (XMEMCMP(out, sigs[0], 64))
  21862. return -11161 - i;
  21863. #if defined(HAVE_ED25519_VERIFY)
  21864. /* test verify on good msg */
  21865. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  21866. != 0 || verify != 1)
  21867. return -11171 - i;
  21868. #endif /* HAVE_ED25519_VERIFY */
  21869. wc_ed25519_free(&key3);
  21870. wc_ed25519_init(&key3);
  21871. idx = 0;
  21872. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  21873. sizeof(privPubEd25519)) != 0)
  21874. return -11181 - i;
  21875. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  21876. return -11191 - i;
  21877. if (XMEMCMP(out, sigs[0], 64))
  21878. return -11201 - i;
  21879. wc_ed25519_free(&key3);
  21880. #endif /* NO_ASN */
  21881. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21882. /* clean up keys when done */
  21883. wc_ed25519_free(&key);
  21884. wc_ed25519_free(&key2);
  21885. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  21886. wc_FreeRng(&rng);
  21887. #endif
  21888. /* hush warnings of unused keySz and sigSz */
  21889. (void)keySz;
  21890. (void)sigSz;
  21891. #ifdef WOLFSSL_TEST_CERT
  21892. ret = ed25519_test_cert();
  21893. if (ret < 0)
  21894. return ret;
  21895. #ifdef WOLFSSL_CERT_GEN
  21896. ret = ed25519_test_make_cert();
  21897. if (ret < 0)
  21898. return ret;
  21899. #endif /* WOLFSSL_CERT_GEN */
  21900. #endif /* WOLFSSL_TEST_CERT */
  21901. return 0;
  21902. }
  21903. #endif /* HAVE_ED25519 */
  21904. #ifdef HAVE_CURVE448
  21905. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  21906. defined(HAVE_CURVE448_KEY_IMPORT)
  21907. /* Test the wc_curve448_check_public API.
  21908. *
  21909. * returns 0 on success and -ve on failure.
  21910. */
  21911. static int curve448_check_public_test(void)
  21912. {
  21913. /* Little-endian values that will fail */
  21914. byte fail_le[][CURVE448_KEY_SIZE] = {
  21915. {
  21916. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21917. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21918. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21919. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21920. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21921. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21922. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21923. },
  21924. {
  21925. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21926. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21927. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21928. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21929. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21930. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21931. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21932. },
  21933. };
  21934. /* Big-endian values that will fail */
  21935. byte fail_be[][CURVE448_KEY_SIZE] = {
  21936. {
  21937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21938. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21939. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21940. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21943. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  21944. },
  21945. {
  21946. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21947. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21948. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21949. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21950. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21951. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21952. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21953. },
  21954. };
  21955. /* Good or valid public value */
  21956. byte good[CURVE448_KEY_SIZE] = {
  21957. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21958. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21959. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21960. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21961. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21962. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21963. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21964. };
  21965. int i;
  21966. /* Parameter checks */
  21967. /* NULL pointer */
  21968. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  21969. BAD_FUNC_ARG) {
  21970. return -11300;
  21971. }
  21972. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  21973. return -11301;
  21974. }
  21975. /* Length of 0 treated differently to other invalid lengths for TLS */
  21976. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  21977. return -11302;
  21978. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  21979. return -11303;
  21980. /* Length not CURVE448_KEY_SIZE */
  21981. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  21982. if (i == CURVE448_KEY_SIZE)
  21983. continue;
  21984. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  21985. ECC_BAD_ARG_E) {
  21986. return -11304 - i;
  21987. }
  21988. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  21989. ECC_BAD_ARG_E) {
  21990. return -11314 - i;
  21991. }
  21992. }
  21993. /* Little-endian fail cases */
  21994. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  21995. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  21996. EC448_LITTLE_ENDIAN) == 0) {
  21997. return -11324 - i;
  21998. }
  21999. }
  22000. /* Big-endian fail cases */
  22001. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  22002. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  22003. EC448_BIG_ENDIAN) == 0) {
  22004. return -11334 - i;
  22005. }
  22006. }
  22007. /* Check a valid public value works! */
  22008. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  22009. EC448_LITTLE_ENDIAN) != 0) {
  22010. return -11344;
  22011. }
  22012. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  22013. EC448_BIG_ENDIAN) != 0) {
  22014. return -11345;
  22015. }
  22016. return 0;
  22017. }
  22018. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  22019. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  22020. {
  22021. WC_RNG rng;
  22022. int ret;
  22023. #ifdef HAVE_CURVE448_SHARED_SECRET
  22024. byte sharedA[CURVE448_KEY_SIZE];
  22025. byte sharedB[CURVE448_KEY_SIZE];
  22026. word32 y;
  22027. #endif
  22028. #ifdef HAVE_CURVE448_KEY_EXPORT
  22029. byte exportBuf[CURVE448_KEY_SIZE];
  22030. #endif
  22031. word32 x;
  22032. curve448_key userA, userB, pubKey;
  22033. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22034. defined(HAVE_CURVE448_KEY_IMPORT)
  22035. /* test vectors from
  22036. https://www.rfc-editor.org/rfc/rfc7748.html
  22037. */
  22038. /* secret key for party a */
  22039. byte sa[] = {
  22040. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  22041. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  22042. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  22043. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  22044. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  22045. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  22046. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  22047. };
  22048. /* public key for party a */
  22049. byte pa[] = {
  22050. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  22051. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  22052. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  22053. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  22054. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  22055. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  22056. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  22057. };
  22058. /* secret key for party b */
  22059. byte sb[] = {
  22060. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  22061. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  22062. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  22063. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  22064. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  22065. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  22066. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  22067. };
  22068. /* public key for party b */
  22069. byte pb[] = {
  22070. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  22071. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  22072. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  22073. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  22074. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  22075. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  22076. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  22077. };
  22078. /* expected shared key */
  22079. byte ss[] = {
  22080. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  22081. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  22082. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  22083. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  22084. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  22085. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  22086. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  22087. };
  22088. #endif /* HAVE_CURVE448_SHARED_SECRET */
  22089. (void)x;
  22090. #ifndef HAVE_FIPS
  22091. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22092. #else
  22093. ret = wc_InitRng(&rng);
  22094. #endif
  22095. if (ret != 0)
  22096. return -11400;
  22097. wc_curve448_init(&userA);
  22098. wc_curve448_init(&userB);
  22099. wc_curve448_init(&pubKey);
  22100. /* make curve448 keys */
  22101. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  22102. return -11401;
  22103. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  22104. return -11402;
  22105. #ifdef HAVE_CURVE448_SHARED_SECRET
  22106. /* find shared secret key */
  22107. x = sizeof(sharedA);
  22108. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22109. return -11403;
  22110. y = sizeof(sharedB);
  22111. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22112. return -11404;
  22113. /* compare shared secret keys to test they are the same */
  22114. if (y != x)
  22115. return -11405;
  22116. if (XMEMCMP(sharedA, sharedB, x))
  22117. return -11406;
  22118. #endif
  22119. #ifdef HAVE_CURVE448_KEY_EXPORT
  22120. /* export a public key and import it for another user */
  22121. x = sizeof(exportBuf);
  22122. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  22123. return -11407;
  22124. #ifdef HAVE_CURVE448_KEY_IMPORT
  22125. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  22126. return -11408;
  22127. #endif
  22128. #endif
  22129. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22130. defined(HAVE_CURVE448_KEY_IMPORT)
  22131. /* test shared key after importing a public key */
  22132. XMEMSET(sharedB, 0, sizeof(sharedB));
  22133. y = sizeof(sharedB);
  22134. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  22135. return -11409;
  22136. if (XMEMCMP(sharedA, sharedB, y))
  22137. return -11410;
  22138. /* import RFC test vectors and compare shared key */
  22139. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22140. != 0)
  22141. return -11411;
  22142. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  22143. != 0)
  22144. return -11412;
  22145. /* test against known test vector */
  22146. XMEMSET(sharedB, 0, sizeof(sharedB));
  22147. y = sizeof(sharedB);
  22148. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  22149. return -11413;
  22150. if (XMEMCMP(ss, sharedB, y))
  22151. return -11414;
  22152. /* test swapping roles of keys and generating same shared key */
  22153. XMEMSET(sharedB, 0, sizeof(sharedB));
  22154. y = sizeof(sharedB);
  22155. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22156. return -11415;
  22157. if (XMEMCMP(ss, sharedB, y))
  22158. return -11416;
  22159. /* test with 1 generated key and 1 from known test vector */
  22160. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22161. != 0)
  22162. return -11417;
  22163. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  22164. return -11418;
  22165. x = sizeof(sharedA);
  22166. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22167. return -11419;
  22168. y = sizeof(sharedB);
  22169. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22170. return -11420;
  22171. /* compare shared secret keys to test they are the same */
  22172. if (y != x)
  22173. return -11421;
  22174. if (XMEMCMP(sharedA, sharedB, x))
  22175. return -11422;
  22176. ret = curve448_check_public_test();
  22177. if (ret != 0)
  22178. return ret;
  22179. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  22180. /* clean up keys when done */
  22181. wc_curve448_free(&pubKey);
  22182. wc_curve448_free(&userB);
  22183. wc_curve448_free(&userA);
  22184. wc_FreeRng(&rng);
  22185. return 0;
  22186. }
  22187. #endif /* HAVE_CURVE448 */
  22188. #ifdef HAVE_ED448
  22189. #ifdef WOLFSSL_TEST_CERT
  22190. static int ed448_test_cert(void)
  22191. {
  22192. DecodedCert cert[2];
  22193. DecodedCert* serverCert = NULL;
  22194. DecodedCert* caCert = NULL;
  22195. #ifdef HAVE_ED448_VERIFY
  22196. ed448_key key;
  22197. ed448_key* pubKey = NULL;
  22198. int verify;
  22199. #endif /* HAVE_ED448_VERIFY */
  22200. int ret;
  22201. byte* tmp;
  22202. size_t bytes;
  22203. XFILE file;
  22204. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22205. if (tmp == NULL) {
  22206. ERROR_OUT(-11430, done);
  22207. }
  22208. #ifdef USE_CERT_BUFFERS_256
  22209. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  22210. bytes = sizeof_ca_ed448_cert;
  22211. #elif !defined(NO_FILESYSTEM)
  22212. file = XFOPEN(caEd448Cert, "rb");
  22213. if (file == NULL) {
  22214. ERROR_OUT(-11431, done);
  22215. }
  22216. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22217. XFCLOSE(file);
  22218. #else
  22219. /* No certificate to use. */
  22220. ERROR_OUT(-11432, done);
  22221. #endif
  22222. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  22223. caCert = &cert[0];
  22224. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  22225. if (ret != 0) {
  22226. ERROR_OUT(-11433, done);
  22227. }
  22228. #ifdef USE_CERT_BUFFERS_256
  22229. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  22230. bytes = sizeof_server_ed448_cert;
  22231. #elif !defined(NO_FILESYSTEM)
  22232. file = XFOPEN(serverEd448Cert, "rb");
  22233. if (file == NULL) {
  22234. ERROR_OUT(-11434, done);
  22235. }
  22236. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22237. XFCLOSE(file);
  22238. #else
  22239. /* No certificate to use. */
  22240. ERROR_OUT(-11435, done);
  22241. #endif
  22242. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  22243. serverCert = &cert[1];
  22244. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  22245. if (ret != 0) {
  22246. ERROR_OUT(-11436, done);
  22247. }
  22248. #ifdef HAVE_ED448_VERIFY
  22249. ret = wc_ed448_init(&key);
  22250. if (ret < 0) {
  22251. ERROR_OUT(-11437, done);
  22252. }
  22253. pubKey = &key;
  22254. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  22255. if (ret < 0) {
  22256. ERROR_OUT(-11438, done);
  22257. }
  22258. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  22259. serverCert->source + serverCert->certBegin,
  22260. serverCert->sigIndex - serverCert->certBegin,
  22261. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  22262. ERROR_OUT(-11439, done);
  22263. }
  22264. #endif /* HAVE_ED448_VERIFY */
  22265. done:
  22266. if (tmp != NULL)
  22267. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22268. #ifdef HAVE_ED448_VERIFY
  22269. wc_ed448_free(pubKey);
  22270. #endif /* HAVE_ED448_VERIFY */
  22271. if (caCert != NULL)
  22272. FreeDecodedCert(caCert);
  22273. if (serverCert != NULL)
  22274. FreeDecodedCert(serverCert);
  22275. return ret;
  22276. }
  22277. static int ed448_test_make_cert(void)
  22278. {
  22279. WC_RNG rng;
  22280. Cert cert;
  22281. DecodedCert decode;
  22282. ed448_key key;
  22283. ed448_key* privKey = NULL;
  22284. int ret = 0;
  22285. byte* tmp = NULL;
  22286. wc_InitCert(&cert);
  22287. #ifndef HAVE_FIPS
  22288. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22289. #else
  22290. ret = wc_InitRng(&rng);
  22291. #endif
  22292. if (ret != 0)
  22293. return -11450;
  22294. wc_ed448_init(&key);
  22295. privKey = &key;
  22296. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  22297. cert.daysValid = 365 * 2;
  22298. cert.selfSigned = 1;
  22299. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  22300. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  22301. cert.isCA = 0;
  22302. #ifdef WOLFSSL_CERT_EXT
  22303. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  22304. if (ret < 0) {
  22305. ERROR_OUT(-11451, done);
  22306. }
  22307. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  22308. if (ret < 0) {
  22309. ERROR_OUT(-11452, done);
  22310. }
  22311. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  22312. if (ret < 0) {
  22313. ERROR_OUT(-11453, done);
  22314. }
  22315. #endif
  22316. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22317. if (tmp == NULL) {
  22318. ERROR_OUT(-11454, done);
  22319. }
  22320. cert.sigType = CTC_ED448;
  22321. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  22322. if (ret < 0) {
  22323. ERROR_OUT(-11455, done);
  22324. }
  22325. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  22326. privKey, &rng);
  22327. if (ret < 0) {
  22328. ERROR_OUT(-11456, done);
  22329. }
  22330. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  22331. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  22332. FreeDecodedCert(&decode);
  22333. if (ret != 0) {
  22334. ERROR_OUT(-11457, done);
  22335. }
  22336. done:
  22337. if (tmp != NULL)
  22338. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22339. wc_ed448_free(privKey);
  22340. wc_FreeRng(&rng);
  22341. return ret;
  22342. }
  22343. #endif /* WOLFSSL_TEST_CERT */
  22344. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  22345. defined(HAVE_ED448_KEY_IMPORT)
  22346. static int ed448_ctx_test(void)
  22347. {
  22348. byte out[ED448_SIG_SIZE];
  22349. word32 outlen;
  22350. #ifdef HAVE_ED448_VERIFY
  22351. int verify;
  22352. #endif /* HAVE_ED448_VERIFY */
  22353. ed448_key key;
  22354. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  22355. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  22356. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  22357. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  22358. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  22359. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  22360. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  22361. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  22362. 0x4e
  22363. };
  22364. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  22365. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  22366. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  22367. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  22368. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  22369. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  22370. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  22371. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  22372. 0x80
  22373. };
  22374. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  22375. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  22376. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  22377. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  22378. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  22379. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  22380. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  22381. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  22382. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  22383. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  22384. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  22385. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  22386. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  22387. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  22388. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  22389. 0x3c, 0x00
  22390. };
  22391. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  22392. 0x03
  22393. };
  22394. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  22395. 0x66,0x6f,0x6f
  22396. };
  22397. outlen = sizeof(out);
  22398. XMEMSET(out, 0, sizeof(out));
  22399. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  22400. sizeof(pKeyCtx), &key) != 0)
  22401. return -11500;
  22402. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  22403. contextCtx, sizeof(contextCtx)) != 0)
  22404. return -11501;
  22405. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  22406. return -11502;
  22407. #if defined(HAVE_ED448_VERIFY)
  22408. /* test verify on good msg */
  22409. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  22410. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  22411. return -11503;
  22412. #endif
  22413. wc_ed448_free(&key);
  22414. return 0;
  22415. }
  22416. static int ed448ph_test(void)
  22417. {
  22418. byte out[ED448_SIG_SIZE];
  22419. word32 outlen;
  22420. #ifdef HAVE_ED448_VERIFY
  22421. int verify;
  22422. #endif /* HAVE_ED448_VERIFY */
  22423. ed448_key key;
  22424. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  22425. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  22426. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  22427. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  22428. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  22429. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  22430. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  22431. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  22432. 0x49
  22433. };
  22434. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  22435. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  22436. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  22437. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  22438. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  22439. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  22440. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  22441. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  22442. 0x80
  22443. };
  22444. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  22445. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  22446. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  22447. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  22448. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  22449. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  22450. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  22451. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  22452. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  22453. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  22454. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  22455. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  22456. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  22457. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  22458. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  22459. 0x0f, 0x00
  22460. };
  22461. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  22462. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  22463. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  22464. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  22465. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  22466. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  22467. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  22468. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  22469. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  22470. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  22471. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  22472. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  22473. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  22474. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  22475. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  22476. 0x21, 0x00
  22477. };
  22478. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  22479. 0x61,0x62,0x63
  22480. };
  22481. /* SHA-512 hash of msgPh */
  22482. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  22483. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  22484. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  22485. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  22486. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  22487. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  22488. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  22489. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  22490. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  22491. };
  22492. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  22493. 0x66,0x6f,0x6f
  22494. };
  22495. outlen = sizeof(out);
  22496. XMEMSET(out, 0, sizeof(out));
  22497. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  22498. sizeof(pKeyPh), &key) != 0) {
  22499. return -11600;
  22500. }
  22501. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  22502. 0) != 0) {
  22503. return -11601;
  22504. }
  22505. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  22506. return -11602;
  22507. #if defined(HAVE_ED448_VERIFY)
  22508. /* test verify on good msg */
  22509. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  22510. NULL, 0) != 0 || verify != 1) {
  22511. return -11603;
  22512. }
  22513. #endif
  22514. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  22515. contextPh2, sizeof(contextPh2)) != 0) {
  22516. return -11604;
  22517. }
  22518. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  22519. return -11605;
  22520. #if defined(HAVE_ED448_VERIFY)
  22521. /* test verify on good msg */
  22522. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  22523. contextPh2, sizeof(contextPh2)) != 0 ||
  22524. verify != 1) {
  22525. return -11606;
  22526. }
  22527. #endif
  22528. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  22529. 0) != 0) {
  22530. return -11607;
  22531. }
  22532. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  22533. return -11608;
  22534. #if defined(HAVE_ED448_VERIFY)
  22535. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22536. &key, NULL, 0) != 0 || verify != 1) {
  22537. return -11609;
  22538. }
  22539. #endif
  22540. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  22541. contextPh2, sizeof(contextPh2)) != 0) {
  22542. return -11610;
  22543. }
  22544. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  22545. return -11611;
  22546. #if defined(HAVE_ED448_VERIFY)
  22547. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22548. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  22549. verify != 1) {
  22550. return -11612;
  22551. }
  22552. #endif
  22553. wc_ed448_free(&key);
  22554. return 0;
  22555. }
  22556. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  22557. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  22558. {
  22559. int ret;
  22560. WC_RNG rng;
  22561. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  22562. defined(HAVE_ED448_KEY_IMPORT)
  22563. byte out[ED448_SIG_SIZE];
  22564. byte exportPKey[ED448_KEY_SIZE];
  22565. byte exportSKey[ED448_KEY_SIZE];
  22566. word32 exportPSz;
  22567. word32 exportSSz;
  22568. int i;
  22569. word32 outlen;
  22570. #ifdef HAVE_ED448_VERIFY
  22571. int verify;
  22572. #endif /* HAVE_ED448_VERIFY */
  22573. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  22574. word32 keySz, sigSz;
  22575. ed448_key key;
  22576. ed448_key key2;
  22577. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  22578. defined(HAVE_ED448_KEY_IMPORT)
  22579. /* test vectors from
  22580. https://tools.ietf.org/html/rfc8032
  22581. */
  22582. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  22583. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22584. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22585. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22586. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22587. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22588. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22589. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22590. 0x5b
  22591. };
  22592. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  22593. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  22594. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  22595. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  22596. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  22597. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  22598. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  22599. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  22600. 0x4e
  22601. };
  22602. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  22603. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  22604. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  22605. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  22606. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  22607. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  22608. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  22609. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  22610. 0x1b
  22611. };
  22612. /* uncompressed test */
  22613. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  22614. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22615. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22616. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22617. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22618. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22619. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22620. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22621. 0x5b
  22622. };
  22623. /* compressed prefix test */
  22624. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  22625. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22626. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22627. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22628. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22629. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22630. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22631. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22632. 0x5b
  22633. };
  22634. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  22635. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  22636. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  22637. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  22638. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  22639. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  22640. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  22641. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  22642. 0xe8
  22643. };
  22644. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  22645. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  22646. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22647. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22648. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22649. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22650. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22651. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22652. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22653. 0x80
  22654. };
  22655. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  22656. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  22657. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  22658. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  22659. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  22660. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  22661. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  22662. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  22663. 0x80
  22664. };
  22665. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  22666. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  22667. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  22668. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  22669. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  22670. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  22671. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  22672. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  22673. 0x80
  22674. };
  22675. /* uncompressed test */
  22676. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  22677. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22678. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22679. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22680. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22681. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22682. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22683. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22684. 0x80
  22685. };
  22686. /* compressed prefix */
  22687. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  22688. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22689. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22690. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22691. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22692. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22693. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22694. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22695. 0x80
  22696. };
  22697. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  22698. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  22699. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  22700. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  22701. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  22702. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  22703. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  22704. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  22705. 0x00
  22706. };
  22707. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  22708. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  22709. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  22710. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  22711. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22712. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22713. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22714. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22715. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22716. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22717. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22718. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22719. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22720. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22721. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22722. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22723. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22724. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22725. 0x26, 0x00
  22726. };
  22727. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  22728. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  22729. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  22730. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  22731. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  22732. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  22733. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  22734. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  22735. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  22736. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  22737. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  22738. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  22739. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  22740. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  22741. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  22742. 0x3a, 0x00
  22743. };
  22744. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  22745. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  22746. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  22747. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  22748. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  22749. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  22750. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  22751. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  22752. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  22753. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  22754. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  22755. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  22756. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  22757. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  22758. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  22759. 0x3c, 0x00
  22760. };
  22761. /* uncompressed test */
  22762. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  22763. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22764. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22765. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22766. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22767. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22768. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22769. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22770. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22771. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22772. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22773. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22774. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22775. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22776. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22777. 0x26, 0x00
  22778. };
  22779. /* compressed prefix */
  22780. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  22781. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22782. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22783. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22784. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22785. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22786. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22787. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22788. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22789. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22790. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22791. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22792. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22793. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22794. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22795. 0x26, 0x00
  22796. };
  22797. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  22798. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  22799. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  22800. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  22801. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  22802. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  22803. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  22804. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  22805. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  22806. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  22807. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  22808. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  22809. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  22810. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  22811. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  22812. 0x1a, 0x00
  22813. };
  22814. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  22815. #define SIGSZ sizeof(sig1)
  22816. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  22817. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  22818. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  22819. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  22820. /* test of a 1023 byte long message */
  22821. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  22822. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  22823. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  22824. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  22825. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  22826. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  22827. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  22828. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  22829. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  22830. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  22831. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  22832. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  22833. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  22834. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  22835. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  22836. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  22837. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  22838. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  22839. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  22840. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  22841. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  22842. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  22843. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  22844. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  22845. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  22846. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  22847. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  22848. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  22849. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  22850. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  22851. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  22852. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  22853. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  22854. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  22855. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  22856. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  22857. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  22858. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  22859. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  22860. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  22861. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  22862. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  22863. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  22864. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  22865. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  22866. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  22867. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  22868. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  22869. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  22870. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  22871. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  22872. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  22873. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  22874. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  22875. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  22876. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  22877. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  22878. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  22879. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  22880. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  22881. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  22882. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  22883. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  22884. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  22885. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  22886. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  22887. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  22888. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  22889. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  22890. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  22891. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  22892. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  22893. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  22894. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  22895. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  22896. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  22897. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  22898. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  22899. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  22900. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  22901. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  22902. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  22903. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  22904. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  22905. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  22906. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  22907. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  22908. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  22909. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  22910. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  22911. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  22912. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  22913. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  22914. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  22915. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  22916. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  22917. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  22918. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  22919. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  22920. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  22921. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  22922. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  22923. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  22924. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  22925. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  22926. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  22927. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  22928. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  22929. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  22930. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  22931. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  22932. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  22933. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  22934. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  22935. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  22936. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  22937. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  22938. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  22939. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  22940. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  22941. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  22942. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  22943. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  22944. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  22945. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  22946. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  22947. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  22948. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  22949. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  22950. };
  22951. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  22952. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  22953. sizeof(msg2),
  22954. sizeof(msg3),
  22955. 0 /*sizeof(msg1)*/,
  22956. 0 /*sizeof(msg1)*/,
  22957. sizeof(msg4)
  22958. };
  22959. #ifndef NO_ASN
  22960. static byte privateEd448[] = {
  22961. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  22962. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  22963. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22964. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22965. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22966. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22967. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22968. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22969. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22970. 0x5b
  22971. };
  22972. static byte publicEd448[] = {
  22973. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  22974. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  22975. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  22976. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  22977. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  22978. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  22979. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  22980. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  22981. 0xaf, 0xe8, 0x25, 0x61, 0x80
  22982. };
  22983. static byte privPubEd448[] = {
  22984. 0x30, 0x81, 0x84, 0x02, 0x01, 0x00, 0x30, 0x05,
  22985. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  22986. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  22987. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  22988. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  22989. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  22990. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  22991. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  22992. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  22993. 0xf9, 0x5b, 0xa1, 0x3b, 0x04, 0x39, 0x5f, 0xd7,
  22994. 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  22995. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1,
  22996. 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e,
  22997. 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1,
  22998. 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6,
  22999. 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  23000. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  23001. };
  23002. word32 idx;
  23003. ed448_key key3;
  23004. #endif /* NO_ASN */
  23005. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23006. /* create ed448 keys */
  23007. #ifndef HAVE_FIPS
  23008. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23009. #else
  23010. ret = wc_InitRng(&rng);
  23011. #endif
  23012. if (ret != 0)
  23013. return -11700;
  23014. wc_ed448_init(&key);
  23015. wc_ed448_init(&key2);
  23016. #ifndef NO_ASN
  23017. wc_ed448_init(&key3);
  23018. #endif
  23019. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  23020. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key2);
  23021. /* helper functions for signature and key size */
  23022. keySz = wc_ed448_size(&key);
  23023. sigSz = wc_ed448_sig_size(&key);
  23024. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  23025. defined(HAVE_ED448_KEY_IMPORT)
  23026. for (i = 0; i < 6; i++) {
  23027. outlen = sizeof(out);
  23028. XMEMSET(out, 0, sizeof(out));
  23029. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  23030. pKeySz[i], &key) != 0)
  23031. return -11701 - i;
  23032. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key, NULL,
  23033. 0) != 0) {
  23034. return -11711 - i;
  23035. }
  23036. if (XMEMCMP(out, sigs[i], 114))
  23037. return -11721 - i;
  23038. #if defined(HAVE_ED448_VERIFY)
  23039. /* test verify on good msg */
  23040. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  23041. NULL, 0) != 0 || verify != 1) {
  23042. return -11731 - i;
  23043. }
  23044. /* test verify on bad msg */
  23045. out[outlen-2] = out[outlen-2] + 1;
  23046. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  23047. NULL, 0) == 0 || verify == 1) {
  23048. return -11741 - i;
  23049. }
  23050. #endif /* HAVE_ED448_VERIFY */
  23051. /* test api for import/exporting keys */
  23052. exportPSz = sizeof(exportPKey);
  23053. exportSSz = sizeof(exportSKey);
  23054. if (wc_ed448_export_public(&key, exportPKey, &exportPSz) != 0)
  23055. return -11751 - i;
  23056. if (wc_ed448_import_public(exportPKey, exportPSz, &key2) != 0)
  23057. return -11761 - i;
  23058. if (wc_ed448_export_private_only(&key, exportSKey, &exportSSz) != 0)
  23059. return -11771 - i;
  23060. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  23061. exportPKey, exportPSz, &key2) != 0)
  23062. return -11781 - i;
  23063. /* clear "out" buffer and test sign with imported keys */
  23064. outlen = sizeof(out);
  23065. XMEMSET(out, 0, sizeof(out));
  23066. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2, NULL,
  23067. 0) != 0) {
  23068. return -11791 - i;
  23069. }
  23070. #if defined(HAVE_ED448_VERIFY)
  23071. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key2,
  23072. NULL, 0) != 0 || verify != 1)
  23073. return -11801 - i;
  23074. if (XMEMCMP(out, sigs[i], SIGSZ))
  23075. return -11811 - i;
  23076. #endif /* HAVE_ED448_VERIFY */
  23077. }
  23078. ret = ed448_ctx_test();
  23079. if (ret != 0)
  23080. return ret;
  23081. ret = ed448ph_test();
  23082. if (ret != 0)
  23083. return ret;
  23084. #ifndef NO_ASN
  23085. /* Try ASN.1 encoded private-only key and public key. */
  23086. idx = 0;
  23087. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, &key3,
  23088. sizeof(privateEd448)) != 0)
  23089. return -11821 - i;
  23090. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0)
  23091. != BAD_FUNC_ARG)
  23092. return -11831 - i;
  23093. idx = 0;
  23094. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, &key3,
  23095. sizeof(publicEd448)) != 0)
  23096. return -11841 - i;
  23097. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  23098. return -11851 - i;
  23099. if (XMEMCMP(out, sigs[0], SIGSZ))
  23100. return -11861 - i;
  23101. #if defined(HAVE_ED448_VERIFY)
  23102. /* test verify on good msg */
  23103. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3,
  23104. NULL, 0) != 0 || verify != 1)
  23105. return -11871 - i;
  23106. #endif /* HAVE_ED448_VERIFY */
  23107. wc_ed448_free(&key3);
  23108. wc_ed448_init(&key3);
  23109. idx = 0;
  23110. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, &key3,
  23111. sizeof(privPubEd448)) != 0)
  23112. return -11881 - i;
  23113. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  23114. return -11891 - i;
  23115. if (XMEMCMP(out, sigs[0], SIGSZ))
  23116. return -11901 - i;
  23117. wc_ed448_free(&key3);
  23118. #endif /* NO_ASN */
  23119. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23120. /* clean up keys when done */
  23121. wc_ed448_free(&key);
  23122. wc_ed448_free(&key2);
  23123. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  23124. wc_FreeRng(&rng);
  23125. #endif
  23126. /* hush warnings of unused keySz and sigSz */
  23127. (void)keySz;
  23128. (void)sigSz;
  23129. #ifdef WOLFSSL_TEST_CERT
  23130. ret = ed448_test_cert();
  23131. if (ret < 0)
  23132. return ret;
  23133. #ifdef WOLFSSL_CERT_GEN
  23134. ret = ed448_test_make_cert();
  23135. if (ret < 0)
  23136. return ret;
  23137. #endif /* WOLFSSL_CERT_GEN */
  23138. #endif /* WOLFSSL_TEST_CERT */
  23139. return 0;
  23140. }
  23141. #endif /* HAVE_ED448 */
  23142. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  23143. typedef struct CMAC_Test_Case {
  23144. int type;
  23145. int partial;
  23146. const byte* m;
  23147. word32 mSz;
  23148. const byte* k;
  23149. word32 kSz;
  23150. const byte* t;
  23151. word32 tSz;
  23152. } CMAC_Test_Case;
  23153. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  23154. {
  23155. #ifdef WOLFSSL_AES_128
  23156. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  23157. {
  23158. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  23159. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  23160. };
  23161. #define KLEN_128 (sizeof(k128))
  23162. #endif
  23163. #ifdef WOLFSSL_AES_192
  23164. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  23165. {
  23166. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  23167. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  23168. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  23169. };
  23170. #define KLEN_192 (sizeof(k192))
  23171. #endif
  23172. #ifdef WOLFSSL_AES_256
  23173. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  23174. {
  23175. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  23176. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  23177. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  23178. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  23179. };
  23180. #define KLEN_256 (sizeof(k256))
  23181. #endif
  23182. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  23183. {
  23184. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  23185. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  23186. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  23187. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  23188. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  23189. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  23190. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  23191. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  23192. };
  23193. #define MLEN_0 (0)
  23194. #define MLEN_128 (128/8)
  23195. #define MLEN_320 (320/8)
  23196. #define MLEN_319 (MLEN_320 - 1)
  23197. #define MLEN_512 (512/8)
  23198. #ifdef WOLFSSL_AES_128
  23199. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  23200. {
  23201. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  23202. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  23203. };
  23204. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  23205. {
  23206. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  23207. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  23208. };
  23209. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  23210. {
  23211. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  23212. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  23213. };
  23214. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  23215. {
  23216. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  23217. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  23218. };
  23219. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  23220. {
  23221. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  23222. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  23223. };
  23224. #endif
  23225. #ifdef WOLFSSL_AES_192
  23226. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  23227. {
  23228. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  23229. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  23230. };
  23231. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  23232. {
  23233. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  23234. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  23235. };
  23236. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  23237. {
  23238. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  23239. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  23240. };
  23241. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  23242. {
  23243. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  23244. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  23245. };
  23246. #endif
  23247. #ifdef WOLFSSL_AES_256
  23248. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  23249. {
  23250. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  23251. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  23252. };
  23253. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  23254. {
  23255. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  23256. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  23257. };
  23258. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  23259. {
  23260. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  23261. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  23262. };
  23263. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  23264. {
  23265. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  23266. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  23267. };
  23268. #endif
  23269. const CMAC_Test_Case testCases[] =
  23270. {
  23271. #ifdef WOLFSSL_AES_128
  23272. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  23273. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  23274. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  23275. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  23276. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  23277. #endif
  23278. #ifdef WOLFSSL_AES_192
  23279. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  23280. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  23281. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  23282. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  23283. #endif
  23284. #ifdef WOLFSSL_AES_256
  23285. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  23286. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  23287. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  23288. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  23289. #endif
  23290. #ifdef WOLFSSL_AES_128
  23291. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  23292. #endif
  23293. };
  23294. #ifdef WOLFSSL_SMALL_STACK
  23295. Cmac *cmac;
  23296. #else
  23297. Cmac cmac[1];
  23298. #endif
  23299. byte tag[AES_BLOCK_SIZE];
  23300. const CMAC_Test_Case* tc;
  23301. word32 i, tagSz;
  23302. int ret;
  23303. #ifdef WOLFSSL_SMALL_STACK
  23304. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  23305. ERROR_OUT(-12009, out);
  23306. #endif
  23307. for (i = 0, tc = testCases;
  23308. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  23309. i++, tc++) {
  23310. XMEMSET(tag, 0, sizeof(tag));
  23311. tagSz = AES_BLOCK_SIZE;
  23312. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  23313. ERROR_OUT(-12000, out);
  23314. if (tc->partial) {
  23315. if (wc_CmacUpdate(cmac, tc->m,
  23316. tc->mSz/2 - tc->partial) != 0)
  23317. ERROR_OUT(-12001, out);
  23318. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  23319. tc->mSz/2 + tc->partial) != 0)
  23320. ERROR_OUT(-12002, out);
  23321. }
  23322. else {
  23323. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  23324. ERROR_OUT(-12003, out);
  23325. }
  23326. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  23327. ERROR_OUT(-12004, out);
  23328. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  23329. ERROR_OUT(-12005, out);
  23330. XMEMSET(tag, 0, sizeof(tag));
  23331. tagSz = sizeof(tag);
  23332. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  23333. tc->k, tc->kSz) != 0)
  23334. ERROR_OUT(-12006, out);
  23335. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  23336. ERROR_OUT(-12007, out);
  23337. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  23338. tc->k, tc->kSz) != 0)
  23339. ERROR_OUT(-12008, out);
  23340. }
  23341. ret = 0;
  23342. out:
  23343. #ifdef WOLFSSL_SMALL_STACK
  23344. if (cmac)
  23345. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  23346. #endif
  23347. return ret;
  23348. }
  23349. #endif /* NO_AES && WOLFSSL_CMAC */
  23350. #ifdef HAVE_LIBZ
  23351. static const byte sample_text[] =
  23352. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  23353. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  23354. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  23355. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  23356. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  23357. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  23358. "\n"
  23359. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  23360. "four loko you probably haven't heard of them high life. Messenger bag\n"
  23361. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  23362. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  23363. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  23364. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  23365. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  23366. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  23367. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  23368. "four loko whatever street art yr farm-to-table.\n"
  23369. "\n"
  23370. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  23371. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  23372. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  23373. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  23374. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  23375. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  23376. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  23377. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  23378. "locavore.\n"
  23379. "\n"
  23380. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  23381. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  23382. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  23383. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  23384. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  23385. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  23386. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  23387. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  23388. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  23389. "neutra PBR selvage.\n"
  23390. "\n"
  23391. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  23392. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  23393. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  23394. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  23395. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  23396. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  23397. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  23398. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  23399. "mollit magna, sriracha sartorial helvetica.\n"
  23400. "\n"
  23401. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  23402. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  23403. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  23404. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  23405. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  23406. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  23407. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  23408. "\n"
  23409. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  23410. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  23411. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  23412. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  23413. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  23414. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  23415. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  23416. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  23417. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  23418. "\n"
  23419. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  23420. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  23421. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  23422. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  23423. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  23424. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  23425. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  23426. "bag dolor terry richardson sapiente.\n";
  23427. static const byte sample_text_gz[] = {
  23428. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  23429. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  23430. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  23431. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  23432. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  23433. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  23434. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  23435. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  23436. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  23437. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  23438. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  23439. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  23440. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  23441. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  23442. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  23443. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  23444. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  23445. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  23446. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  23447. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  23448. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  23449. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  23450. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  23451. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  23452. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  23453. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  23454. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  23455. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  23456. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  23457. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  23458. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  23459. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  23460. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  23461. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  23462. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  23463. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  23464. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  23465. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  23466. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  23467. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  23468. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  23469. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  23470. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  23471. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  23472. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  23473. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  23474. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  23475. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  23476. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  23477. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  23478. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  23479. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  23480. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  23481. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  23482. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  23483. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  23484. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  23485. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  23486. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  23487. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  23488. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  23489. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  23490. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  23491. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  23492. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  23493. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  23494. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  23495. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  23496. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  23497. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  23498. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  23499. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  23500. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  23501. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  23502. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  23503. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  23504. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  23505. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  23506. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  23507. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  23508. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  23509. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  23510. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  23511. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  23512. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  23513. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  23514. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  23515. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  23516. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  23517. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  23518. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  23519. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  23520. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  23521. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  23522. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  23523. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  23524. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  23525. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  23526. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  23527. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  23528. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  23529. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  23530. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  23531. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  23532. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  23533. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  23534. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  23535. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  23536. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  23537. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  23538. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  23539. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  23540. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  23541. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  23542. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  23543. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  23544. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  23545. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  23546. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  23547. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  23548. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  23549. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  23550. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  23551. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  23552. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  23553. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  23554. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  23555. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  23556. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  23557. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  23558. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  23559. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  23560. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  23561. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  23562. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  23563. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  23564. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  23565. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  23566. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  23567. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  23568. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  23569. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  23570. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  23571. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  23572. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  23573. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  23574. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  23575. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  23576. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  23577. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  23578. };
  23579. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  23580. {
  23581. int ret = 0;
  23582. word32 dSz = sizeof(sample_text);
  23583. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  23584. byte *c;
  23585. byte *d;
  23586. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23587. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23588. if (c == NULL || d == NULL) {
  23589. ERROR_OUT(-12100, exit);
  23590. }
  23591. /* follow calloc and initialize to 0 */
  23592. XMEMSET(c, 0, cSz);
  23593. XMEMSET(d, 0, dSz);
  23594. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  23595. ERROR_OUT(-12101, exit);
  23596. }
  23597. cSz = (word32)ret;
  23598. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  23599. ERROR_OUT(-12102, exit);
  23600. }
  23601. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23602. ERROR_OUT(-12103, exit);
  23603. }
  23604. /* GZIP tests */
  23605. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  23606. XMEMSET(c, 0, cSz);
  23607. XMEMSET(d, 0, dSz);
  23608. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  23609. if (ret < 0) {
  23610. ERROR_OUT(-12104, exit);
  23611. }
  23612. cSz = (word32)ret;
  23613. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  23614. if (ret < 0) {
  23615. ERROR_OUT(-12105, exit);
  23616. }
  23617. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23618. ERROR_OUT(-12106, exit);
  23619. }
  23620. /* Try with gzip generated output */
  23621. XMEMSET(d, 0, dSz);
  23622. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  23623. LIBZ_WINBITS_GZIP);
  23624. if (ret < 0) {
  23625. ERROR_OUT(-12107, exit);
  23626. }
  23627. dSz = (word32)ret;
  23628. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23629. ERROR_OUT(-12108, exit);
  23630. }
  23631. ret = 0; /* success */
  23632. exit:
  23633. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23634. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23635. return ret;
  23636. }
  23637. #endif /* HAVE_LIBZ */
  23638. #ifdef HAVE_PKCS7
  23639. /* External Debugging/Testing Note:
  23640. *
  23641. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  23642. * additional testing. To dump bundles to files DER encoded files, please
  23643. * define:
  23644. *
  23645. * #define PKCS7_OUTPUT_TEST_BUNDLES
  23646. */
  23647. /* Loads certs and keys for use with PKCS7 tests, from either files
  23648. * or buffers.
  23649. *
  23650. * rsaClientCertBuf - output buffer for RSA client cert
  23651. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  23652. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  23653. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  23654. *
  23655. * rsaServerCertBuf - output buffer for RSA server cert
  23656. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  23657. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  23658. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  23659. *
  23660. * rsaCaCertBuf - output buffer for RSA CA cert
  23661. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  23662. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  23663. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  23664. *
  23665. * eccClientCertBuf - output buffer for ECC cert
  23666. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  23667. * eccClientPrivKeyBuf - output buffer for ECC private key
  23668. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  23669. *
  23670. * Returns 0 on success, negative on error
  23671. */
  23672. static int pkcs7_load_certs_keys(
  23673. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  23674. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  23675. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  23676. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  23677. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  23678. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  23679. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  23680. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  23681. {
  23682. #ifndef NO_FILESYSTEM
  23683. XFILE certFile;
  23684. XFILE keyFile;
  23685. (void)certFile;
  23686. (void)keyFile;
  23687. #endif
  23688. #ifndef NO_RSA
  23689. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  23690. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  23691. return BAD_FUNC_ARG;
  23692. #endif
  23693. #ifdef HAVE_ECC
  23694. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  23695. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  23696. return BAD_FUNC_ARG;
  23697. #endif
  23698. /* RSA */
  23699. #ifndef NO_RSA
  23700. #ifdef USE_CERT_BUFFERS_1024
  23701. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  23702. return -12110;
  23703. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  23704. sizeof_client_cert_der_1024);
  23705. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  23706. if (rsaServerCertBuf != NULL) {
  23707. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  23708. return -12111;
  23709. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  23710. sizeof_server_cert_der_1024);
  23711. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  23712. }
  23713. if (rsaCaCertBuf != NULL) {
  23714. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  23715. return -12112;
  23716. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  23717. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  23718. }
  23719. #elif defined(USE_CERT_BUFFERS_2048)
  23720. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  23721. return -12113;
  23722. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  23723. sizeof_client_cert_der_2048);
  23724. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  23725. if (rsaServerCertBuf != NULL) {
  23726. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  23727. return -12114;
  23728. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  23729. sizeof_server_cert_der_2048);
  23730. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  23731. }
  23732. if (rsaCaCertBuf != NULL) {
  23733. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  23734. return -12115;
  23735. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  23736. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  23737. }
  23738. #else
  23739. certFile = XFOPEN(clientCert, "rb");
  23740. if (!certFile)
  23741. return -12116;
  23742. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  23743. *rsaClientCertBufSz, certFile);
  23744. XFCLOSE(certFile);
  23745. if (rsaServerCertBuf != NULL) {
  23746. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  23747. if (!certFile)
  23748. return -12117;
  23749. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  23750. *rsaServerCertBufSz, certFile);
  23751. XFCLOSE(certFile);
  23752. }
  23753. if (rsaCaCertBuf != NULL) {
  23754. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  23755. if (!certFile)
  23756. return -12118;
  23757. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  23758. certFile);
  23759. XFCLOSE(certFile);
  23760. }
  23761. #endif
  23762. #ifdef USE_CERT_BUFFERS_1024
  23763. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  23764. return -12119;
  23765. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  23766. sizeof_client_key_der_1024);
  23767. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  23768. if (rsaServerPrivKeyBuf != NULL) {
  23769. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  23770. return -12120;
  23771. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  23772. sizeof_server_key_der_1024);
  23773. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  23774. }
  23775. if (rsaCaPrivKeyBuf != NULL) {
  23776. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  23777. return -12121;
  23778. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  23779. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  23780. }
  23781. #elif defined(USE_CERT_BUFFERS_2048)
  23782. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  23783. return -12122;
  23784. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  23785. sizeof_client_key_der_2048);
  23786. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  23787. if (rsaServerPrivKeyBuf != NULL) {
  23788. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  23789. return -12123;
  23790. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  23791. sizeof_server_key_der_2048);
  23792. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  23793. }
  23794. if (rsaCaPrivKeyBuf != NULL) {
  23795. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  23796. return -12124;
  23797. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  23798. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  23799. }
  23800. #else
  23801. keyFile = XFOPEN(clientKey, "rb");
  23802. if (!keyFile)
  23803. return -12125;
  23804. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  23805. *rsaClientPrivKeyBufSz, keyFile);
  23806. XFCLOSE(keyFile);
  23807. if (rsaServerPrivKeyBuf != NULL) {
  23808. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  23809. if (!keyFile)
  23810. return -12126;
  23811. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  23812. *rsaServerPrivKeyBufSz, keyFile);
  23813. XFCLOSE(keyFile);
  23814. }
  23815. if (rsaCaPrivKeyBuf != NULL) {
  23816. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  23817. if (!keyFile)
  23818. return -12127;
  23819. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  23820. *rsaCaPrivKeyBufSz, keyFile);
  23821. XFCLOSE(keyFile);
  23822. }
  23823. #endif /* USE_CERT_BUFFERS */
  23824. #endif /* NO_RSA */
  23825. /* ECC */
  23826. #ifdef HAVE_ECC
  23827. #ifdef USE_CERT_BUFFERS_256
  23828. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  23829. return -12128;
  23830. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23831. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  23832. #else
  23833. certFile = XFOPEN(eccClientCert, "rb");
  23834. if (!certFile)
  23835. return -12129;
  23836. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  23837. *eccClientCertBufSz, certFile);
  23838. XFCLOSE(certFile);
  23839. #endif /* USE_CERT_BUFFERS_256 */
  23840. #ifdef USE_CERT_BUFFERS_256
  23841. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  23842. return -12130;
  23843. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23844. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  23845. #else
  23846. keyFile = XFOPEN(eccClientKey, "rb");
  23847. if (!keyFile)
  23848. return -12131;
  23849. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  23850. *eccClientPrivKeyBufSz, keyFile);
  23851. XFCLOSE(keyFile);
  23852. #endif /* USE_CERT_BUFFERS_256 */
  23853. #endif /* HAVE_ECC */
  23854. #ifdef NO_RSA
  23855. (void)rsaClientCertBuf;
  23856. (void)rsaClientCertBufSz;
  23857. (void)rsaClientPrivKeyBuf;
  23858. (void)rsaClientPrivKeyBufSz;
  23859. (void)rsaServerCertBuf;
  23860. (void)rsaServerCertBufSz;
  23861. (void)rsaServerPrivKeyBuf;
  23862. (void)rsaServerPrivKeyBufSz;
  23863. (void)rsaCaCertBuf;
  23864. (void)rsaCaCertBufSz;
  23865. (void)rsaCaPrivKeyBuf;
  23866. (void)rsaCaPrivKeyBufSz;
  23867. #endif
  23868. #ifndef HAVE_ECC
  23869. (void)eccClientCertBuf;
  23870. (void)eccClientCertBufSz;
  23871. (void)eccClientPrivKeyBuf;
  23872. (void)eccClientPrivKeyBufSz;
  23873. #endif
  23874. #ifndef NO_FILESYSTEM
  23875. (void)certFile;
  23876. (void)keyFile;
  23877. #endif
  23878. return 0;
  23879. }
  23880. typedef struct {
  23881. const byte* content;
  23882. word32 contentSz;
  23883. int contentOID;
  23884. int encryptOID;
  23885. int keyWrapOID;
  23886. int keyAgreeOID;
  23887. byte* cert;
  23888. size_t certSz;
  23889. byte* privateKey;
  23890. word32 privateKeySz;
  23891. byte* optionalUkm;
  23892. word32 optionalUkmSz;
  23893. int ktriOptions; /* KTRI options flags */
  23894. int kariOptions; /* KARI options flags */
  23895. /* KEKRI specific */
  23896. byte* secretKey; /* key, only for kekri RecipientInfo types */
  23897. word32 secretKeySz; /* size of secretKey, bytes */
  23898. byte* secretKeyId; /* key identifier */
  23899. word32 secretKeyIdSz; /* size of key identifier, bytes */
  23900. void* timePtr; /* time_t pointer */
  23901. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  23902. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  23903. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  23904. word32 otherAttrSz; /* size of otherAttr, bytes */
  23905. int kekriOptions; /* KEKRI options flags */
  23906. /* PWRI specific */
  23907. char* password;
  23908. word32 passwordSz;
  23909. byte* salt;
  23910. word32 saltSz;
  23911. int kdfOID;
  23912. int hashOID;
  23913. int kdfIterations;
  23914. int pwriOptions; /* PWRI options flags */
  23915. /* ORI specific */
  23916. int isOri;
  23917. int oriOptions; /* ORI options flags */
  23918. const char* outFileName;
  23919. } pkcs7EnvelopedVector;
  23920. static const byte asnDataOid[] = {
  23921. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  23922. };
  23923. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  23924. * and giving wolfCrypt the value for oriOID and oriValue to place in
  23925. * OtherRecipientInfo.
  23926. *
  23927. * Returns 0 on success, negative upon error. */
  23928. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  23929. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  23930. void* ctx)
  23931. {
  23932. int i;
  23933. /* make sure buffers are large enough */
  23934. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  23935. return -12140;
  23936. /* our simple encryption algorithm will be take the bitwise complement */
  23937. oriValue[0] = 0x04; /*ASN OCTET STRING */
  23938. oriValue[1] = (byte)cekSz; /* length */
  23939. for (i = 0; i < (int)cekSz; i++) {
  23940. oriValue[2 + i] = ~cek[i];
  23941. }
  23942. *oriValueSz = 2 + cekSz;
  23943. /* set oriType to ASN.1 encoded data OID */
  23944. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  23945. *oriTypeSz = sizeof(asnDataOid);
  23946. (void)pkcs7;
  23947. (void)ctx;
  23948. return 0;
  23949. }
  23950. /* ORI decrypt callback, responsible for providing a decrypted content
  23951. * encryption key (CEK) placed into decryptedKey and size placed into
  23952. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  23953. * in decrypting the encrypted CEK.
  23954. *
  23955. * Returns 0 on success, negative upon error. */
  23956. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  23957. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  23958. word32* decryptedKeySz, void* ctx)
  23959. {
  23960. int i;
  23961. /* make sure oriType matches what we expect */
  23962. if (oriTypeSz != sizeof(asnDataOid))
  23963. return -12150;
  23964. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  23965. return -12151;
  23966. /* make sure decrypted buffer is large enough */
  23967. if (*decryptedKeySz < oriValueSz)
  23968. return -12152;
  23969. /* decrypt encrypted CEK using simple bitwise complement,
  23970. only for example */
  23971. for (i = 0; i < (int)oriValueSz - 2; i++) {
  23972. decryptedKey[i] = ~oriValue[2 + i];
  23973. }
  23974. *decryptedKeySz = oriValueSz - 2;
  23975. (void)pkcs7;
  23976. (void)ctx;
  23977. return 0;
  23978. }
  23979. #ifndef NO_AES
  23980. /* returns 0 on success */
  23981. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  23982. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  23983. byte* in, int inSz, byte* out, void* usrCtx)
  23984. {
  23985. int keyId = -1, ret, keySz;
  23986. word32 keyIdSz = 8;
  23987. const byte* key;
  23988. byte keyIdRaw[8];
  23989. #ifdef WOLFSSL_SMALL_STACK
  23990. Aes *aes;
  23991. #else
  23992. Aes aes[1];
  23993. #endif
  23994. /* looking for KEY ID
  23995. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  23996. */
  23997. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  23998. /* 0x06, 0x0B do not pass in tag and length */
  23999. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24000. 0x01, 0x09, 0x10, 0x02, 0x25
  24001. };
  24002. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  24003. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24004. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24005. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24006. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24007. };
  24008. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  24009. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24010. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24011. };
  24012. /* test user context passed in */
  24013. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  24014. return -12160;
  24015. }
  24016. #ifdef WOLFSSL_SMALL_STACK
  24017. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  24018. return -12164;
  24019. #endif
  24020. /* if needing to find keyIdSz can call with NULL */
  24021. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  24022. &keyIdSz);
  24023. if (ret != LENGTH_ONLY_E) {
  24024. printf("Unexpected error %d when getting keyIdSz\n", ret);
  24025. printf("Possibly no KEY ID attribute set\n");
  24026. ERROR_OUT(-12161, out);
  24027. }
  24028. else {
  24029. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  24030. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  24031. &keyIdSz);
  24032. if (ret < 0) {
  24033. goto out;
  24034. }
  24035. if (keyIdSz < 3) {
  24036. printf("keyIdSz is smaller than expected\n");
  24037. ERROR_OUT(-12162, out);
  24038. }
  24039. if (keyIdSz > 2 + sizeof(int)) {
  24040. printf("example case was only expecting a keyId of int size\n");
  24041. ERROR_OUT(-12163, out);
  24042. }
  24043. /* keyIdRaw[0] OCTET TAG */
  24044. /* keyIdRaw[1] Length */
  24045. #ifdef BIG_ENDIAN_ORDER
  24046. if (keyIdRaw[1] == 0x01) {
  24047. keyId = 1;
  24048. }
  24049. #else
  24050. keyId = *(int*)(keyIdRaw + 2);
  24051. #endif
  24052. }
  24053. /* Use keyID here if found to select key and decrypt in HSM or in this
  24054. * example just select key and do software decryption */
  24055. if (keyId == 1) {
  24056. key = altKey;
  24057. keySz = sizeof(altKey);
  24058. }
  24059. else {
  24060. key = defKey;
  24061. keySz = sizeof(defKey);
  24062. }
  24063. switch (encryptOID) {
  24064. case AES256CBCb:
  24065. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  24066. ERROR_OUT(BAD_FUNC_ARG, out);
  24067. break;
  24068. case AES128CBCb:
  24069. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  24070. ERROR_OUT(BAD_FUNC_ARG, out);
  24071. break;
  24072. default:
  24073. printf("Unsupported content cipher type for example");
  24074. ERROR_OUT(ALGO_ID_E, out);
  24075. };
  24076. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  24077. if (ret == 0) {
  24078. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  24079. if (ret == 0)
  24080. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  24081. wc_AesFree(aes);
  24082. }
  24083. out:
  24084. #ifdef WOLFSSL_SMALL_STACK
  24085. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24086. #endif
  24087. (void)aad;
  24088. (void)aadSz;
  24089. (void)authTag;
  24090. (void)authTagSz;
  24091. return ret;
  24092. }
  24093. #endif /* NO_AES */
  24094. #define PKCS7_BUF_SIZE 2048
  24095. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  24096. byte* rsaPrivKey, word32 rsaPrivKeySz,
  24097. byte* eccCert, word32 eccCertSz,
  24098. byte* eccPrivKey, word32 eccPrivKeySz)
  24099. {
  24100. int ret = 0, testSz, i;
  24101. int envelopedSz, decodedSz;
  24102. byte *enveloped;
  24103. byte *decoded;
  24104. PKCS7* pkcs7;
  24105. #ifdef ECC_TIMING_RESISTANT
  24106. WC_RNG rng;
  24107. #endif
  24108. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24109. XFILE pkcs7File;
  24110. #endif
  24111. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  24112. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24113. 0x72,0x6c,0x64
  24114. };
  24115. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  24116. defined(WOLFSSL_SHA512)
  24117. byte optionalUkm[] = {
  24118. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24119. };
  24120. #endif /* NO_AES */
  24121. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24122. /* encryption key for kekri recipient types */
  24123. byte secretKey[] = {
  24124. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  24125. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24126. };
  24127. /* encryption key identifier */
  24128. byte secretKeyId[] = {
  24129. 0x02,0x02,0x03,0x04
  24130. };
  24131. #endif
  24132. #if !defined(NO_PWDBASED) && !defined(NO_AES) && \
  24133. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24134. #ifndef HAVE_FIPS
  24135. char password[] = "password"; /* NOTE: Password is too short for FIPS */
  24136. #else
  24137. char password[] = "passwordFIPS_MODE";
  24138. #endif
  24139. byte salt[] = {
  24140. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  24141. };
  24142. #endif
  24143. const pkcs7EnvelopedVector testVectors[] =
  24144. {
  24145. /* key transport key encryption technique */
  24146. #ifndef NO_RSA
  24147. #ifndef NO_DES3
  24148. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  24149. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24150. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24151. "pkcs7envelopedDataDES3.der"},
  24152. #endif
  24153. #ifndef NO_AES
  24154. #ifdef WOLFSSL_AES_128
  24155. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  24156. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24157. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24158. "pkcs7envelopedDataAES128CBC.der"},
  24159. #endif
  24160. #ifdef WOLFSSL_AES_192
  24161. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  24162. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24163. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24164. "pkcs7envelopedDataAES192CBC.der"},
  24165. #endif
  24166. #ifdef WOLFSSL_AES_256
  24167. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24168. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24169. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24170. "pkcs7envelopedDataAES256CBC.der"},
  24171. /* explicitly using SKID for SubjectKeyIdentifier */
  24172. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24173. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  24174. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24175. "pkcs7envelopedDataAES256CBC_SKID.der"},
  24176. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  24177. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24178. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  24179. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  24180. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der"},
  24181. #endif
  24182. #endif /* NO_AES */
  24183. #endif
  24184. /* key agreement key encryption technique*/
  24185. #ifdef HAVE_ECC
  24186. #ifndef NO_AES
  24187. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24188. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  24189. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24190. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24191. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24192. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  24193. #endif
  24194. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  24195. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24196. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24197. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24198. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24199. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  24200. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  24201. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  24202. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24203. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24204. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24205. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24206. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  24207. /* with optional user keying material (ukm) */
  24208. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24209. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24210. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  24211. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24212. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  24213. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  24214. #endif /* NO_AES */
  24215. #endif
  24216. /* kekri (KEKRecipientInfo) recipient types */
  24217. #ifndef NO_AES
  24218. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24219. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  24220. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  24221. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  24222. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24223. "pkcs7envelopedDataAES128CBC_KEKRI.der"},
  24224. #endif
  24225. #endif
  24226. /* pwri (PasswordRecipientInfo) recipient types */
  24227. #if !defined(NO_PWDBASED) && !defined(NO_AES)
  24228. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24229. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  24230. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24231. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  24232. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  24233. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der"},
  24234. #endif
  24235. #endif
  24236. #if !defined(NO_AES) && !defined(NO_AES_128)
  24237. /* ori (OtherRecipientInfo) recipient types */
  24238. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  24239. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  24240. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der"},
  24241. #endif
  24242. };
  24243. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24244. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24245. if ((! enveloped) || (! decoded)) {
  24246. ERROR_OUT(-12170, out);
  24247. }
  24248. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  24249. #ifdef ECC_TIMING_RESISTANT
  24250. #ifndef HAVE_FIPS
  24251. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24252. #else
  24253. ret = wc_InitRng(&rng);
  24254. #endif
  24255. if (ret != 0) {
  24256. ERROR_OUT(-12171, out);
  24257. }
  24258. #endif
  24259. for (i = 0; i < testSz; i++) {
  24260. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  24261. #ifdef WOLFSSL_ASYNC_CRYPT
  24262. INVALID_DEVID /* async PKCS7 is not supported */
  24263. #else
  24264. devId
  24265. #endif
  24266. );
  24267. if (pkcs7 == NULL) {
  24268. ERROR_OUT(-12172, out);
  24269. }
  24270. if (testVectors[i].secretKey != NULL) {
  24271. /* KEKRI recipient type */
  24272. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24273. if (ret != 0) {
  24274. ERROR_OUT(-12173, out);
  24275. }
  24276. pkcs7->content = (byte*)testVectors[i].content;
  24277. pkcs7->contentSz = testVectors[i].contentSz;
  24278. pkcs7->contentOID = testVectors[i].contentOID;
  24279. pkcs7->encryptOID = testVectors[i].encryptOID;
  24280. pkcs7->ukm = testVectors[i].optionalUkm;
  24281. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24282. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  24283. testVectors[i].secretKey, testVectors[i].secretKeySz,
  24284. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  24285. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  24286. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  24287. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  24288. if (ret < 0) {
  24289. wc_PKCS7_Free(pkcs7);
  24290. ERROR_OUT(-12174, out);
  24291. }
  24292. /* set key, for decryption */
  24293. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  24294. testVectors[i].secretKeySz);
  24295. if (ret != 0) {
  24296. wc_PKCS7_Free(pkcs7);
  24297. ERROR_OUT(-12175, out);
  24298. }
  24299. } else if (testVectors[i].password != NULL) {
  24300. #ifndef NO_PWDBASED
  24301. /* PWRI recipient type */
  24302. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24303. if (ret != 0) {
  24304. ERROR_OUT(-12176, out);
  24305. }
  24306. pkcs7->content = (byte*)testVectors[i].content;
  24307. pkcs7->contentSz = testVectors[i].contentSz;
  24308. pkcs7->contentOID = testVectors[i].contentOID;
  24309. pkcs7->encryptOID = testVectors[i].encryptOID;
  24310. pkcs7->ukm = testVectors[i].optionalUkm;
  24311. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24312. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  24313. (byte*)testVectors[i].password,
  24314. testVectors[i].passwordSz, testVectors[i].salt,
  24315. testVectors[i].saltSz, testVectors[i].kdfOID,
  24316. testVectors[i].hashOID, testVectors[i].kdfIterations,
  24317. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  24318. if (ret < 0) {
  24319. wc_PKCS7_Free(pkcs7);
  24320. ERROR_OUT(-12177, out);
  24321. }
  24322. /* set password, for decryption */
  24323. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  24324. testVectors[i].passwordSz);
  24325. if (ret < 0) {
  24326. wc_PKCS7_Free(pkcs7);
  24327. ERROR_OUT(-12178, out);
  24328. }
  24329. #endif /* NO_PWDBASED */
  24330. } else if (testVectors[i].isOri == 1) {
  24331. /* ORI recipient type */
  24332. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24333. if (ret != 0) {
  24334. ERROR_OUT(-12179, out);
  24335. }
  24336. pkcs7->content = (byte*)testVectors[i].content;
  24337. pkcs7->contentSz = testVectors[i].contentSz;
  24338. pkcs7->contentOID = testVectors[i].contentOID;
  24339. pkcs7->encryptOID = testVectors[i].encryptOID;
  24340. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  24341. testVectors[i].oriOptions);
  24342. if (ret < 0) {
  24343. wc_PKCS7_Free(pkcs7);
  24344. ERROR_OUT(-12180, out);
  24345. }
  24346. /* set decrypt callback for decryption */
  24347. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  24348. if (ret < 0) {
  24349. wc_PKCS7_Free(pkcs7);
  24350. ERROR_OUT(-12181, out);
  24351. }
  24352. } else {
  24353. /* KTRI or KARI recipient types */
  24354. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24355. if (ret != 0) {
  24356. ERROR_OUT(-12182, out);
  24357. }
  24358. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  24359. (word32)testVectors[i].certSz);
  24360. if (ret != 0) {
  24361. wc_PKCS7_Free(pkcs7);
  24362. ERROR_OUT(-12183, out);
  24363. }
  24364. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  24365. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  24366. pkcs7->privateKey = testVectors[i].privateKey;
  24367. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  24368. pkcs7->content = (byte*)testVectors[i].content;
  24369. pkcs7->contentSz = testVectors[i].contentSz;
  24370. pkcs7->contentOID = testVectors[i].contentOID;
  24371. pkcs7->encryptOID = testVectors[i].encryptOID;
  24372. pkcs7->ukm = testVectors[i].optionalUkm;
  24373. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24374. /* set SubjectIdentifier type for KTRI types */
  24375. if (testVectors[i].ktriOptions & CMS_SKID) {
  24376. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  24377. if (ret != 0) {
  24378. wc_PKCS7_Free(pkcs7);
  24379. ERROR_OUT(-12184, out);
  24380. }
  24381. } else if (testVectors[i].ktriOptions &
  24382. CMS_ISSUER_AND_SERIAL_NUMBER) {
  24383. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  24384. CMS_ISSUER_AND_SERIAL_NUMBER);
  24385. if (ret != 0) {
  24386. wc_PKCS7_Free(pkcs7);
  24387. ERROR_OUT(-12185, out);
  24388. }
  24389. }
  24390. }
  24391. #ifdef ECC_TIMING_RESISTANT
  24392. pkcs7->rng = &rng;
  24393. #endif
  24394. /* encode envelopedData */
  24395. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  24396. PKCS7_BUF_SIZE);
  24397. if (envelopedSz <= 0) {
  24398. wc_PKCS7_Free(pkcs7);
  24399. ERROR_OUT(-12186, out);
  24400. }
  24401. /* decode envelopedData */
  24402. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  24403. decoded, PKCS7_BUF_SIZE);
  24404. if (decodedSz <= 0) {
  24405. wc_PKCS7_Free(pkcs7);
  24406. ERROR_OUT(-12187, out);
  24407. }
  24408. /* test decode result */
  24409. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  24410. wc_PKCS7_Free(pkcs7);
  24411. ERROR_OUT(-12188, out);
  24412. }
  24413. #ifndef NO_PKCS7_STREAM
  24414. { /* test reading byte by byte */
  24415. int z;
  24416. for (z = 0; z < envelopedSz; z++) {
  24417. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  24418. decoded, PKCS7_BUF_SIZE);
  24419. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  24420. printf("unexpected error %d\n", decodedSz);
  24421. ERROR_OUT(-12189, out);
  24422. }
  24423. }
  24424. /* test decode result */
  24425. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  24426. printf("stream read compare failed\n");
  24427. wc_PKCS7_Free(pkcs7);
  24428. ERROR_OUT(-12190, out);
  24429. }
  24430. }
  24431. #endif
  24432. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24433. /* output pkcs7 envelopedData for external testing */
  24434. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  24435. if (!pkcs7File) {
  24436. wc_PKCS7_Free(pkcs7);
  24437. ERROR_OUT(-12191, out);
  24438. }
  24439. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  24440. XFCLOSE(pkcs7File);
  24441. if (ret != envelopedSz) {
  24442. wc_PKCS7_Free(pkcs7);
  24443. ERROR_OUT(-12192, out);
  24444. }
  24445. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  24446. wc_PKCS7_Free(pkcs7);
  24447. pkcs7 = NULL;
  24448. }
  24449. #ifdef ECC_TIMING_RESISTANT
  24450. wc_FreeRng(&rng);
  24451. #endif
  24452. (void)eccCert;
  24453. (void)eccCertSz;
  24454. (void)eccPrivKey;
  24455. (void)eccPrivKeySz;
  24456. (void)rsaCert;
  24457. (void)rsaCertSz;
  24458. (void)rsaPrivKey;
  24459. (void)rsaPrivKeySz;
  24460. out:
  24461. if (enveloped)
  24462. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24463. if (decoded)
  24464. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24465. return ret;
  24466. }
  24467. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  24468. {
  24469. int ret = 0;
  24470. byte* rsaCert = NULL;
  24471. byte* rsaPrivKey = NULL;
  24472. word32 rsaCertSz = 0;
  24473. word32 rsaPrivKeySz = 0;
  24474. byte* eccCert = NULL;
  24475. byte* eccPrivKey = NULL;
  24476. word32 eccCertSz = 0;
  24477. word32 eccPrivKeySz = 0;
  24478. #ifndef NO_RSA
  24479. /* read client RSA cert and key in DER format */
  24480. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24481. if (rsaCert == NULL)
  24482. return -12200;
  24483. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24484. if (rsaPrivKey == NULL) {
  24485. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24486. return -12201;
  24487. }
  24488. rsaCertSz = FOURK_BUF;
  24489. rsaPrivKeySz = FOURK_BUF;
  24490. #endif /* NO_RSA */
  24491. #ifdef HAVE_ECC
  24492. /* read client ECC cert and key in DER format */
  24493. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24494. if (eccCert == NULL) {
  24495. #ifndef NO_RSA
  24496. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24497. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24498. #endif
  24499. return -12202;
  24500. }
  24501. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24502. if (eccPrivKey == NULL) {
  24503. #ifndef NO_RSA
  24504. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24505. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24506. #endif
  24507. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24508. return -12203;
  24509. }
  24510. eccCertSz = FOURK_BUF;
  24511. eccPrivKeySz = FOURK_BUF;
  24512. #endif /* HAVE_ECC */
  24513. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  24514. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  24515. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  24516. eccPrivKey, &eccPrivKeySz);
  24517. if (ret < 0) {
  24518. #ifndef NO_RSA
  24519. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24520. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24521. #endif
  24522. #ifdef HAVE_ECC
  24523. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24524. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24525. #endif
  24526. return -12204;
  24527. }
  24528. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  24529. rsaPrivKey, (word32)rsaPrivKeySz,
  24530. eccCert, (word32)eccCertSz,
  24531. eccPrivKey, (word32)eccPrivKeySz);
  24532. #ifndef NO_RSA
  24533. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24534. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24535. #endif
  24536. #ifdef HAVE_ECC
  24537. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24538. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24539. #endif
  24540. return ret;
  24541. }
  24542. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  24543. typedef struct {
  24544. const byte* content;
  24545. word32 contentSz;
  24546. int contentOID;
  24547. int encryptOID;
  24548. int keyWrapOID;
  24549. int keyAgreeOID;
  24550. byte* cert;
  24551. size_t certSz;
  24552. byte* privateKey;
  24553. word32 privateKeySz;
  24554. PKCS7Attrib* authAttribs;
  24555. word32 authAttribsSz;
  24556. PKCS7Attrib* unauthAttribs;
  24557. word32 unauthAttribsSz;
  24558. /* KARI / KTRI specific */
  24559. byte* optionalUkm;
  24560. word32 optionalUkmSz;
  24561. int ktriOptions; /* KTRI options flags */
  24562. int kariOptions; /* KARI options flags */
  24563. /* KEKRI specific */
  24564. byte* secretKey; /* key, only for kekri RecipientInfo types */
  24565. word32 secretKeySz; /* size of secretKey, bytes */
  24566. byte* secretKeyId; /* key identifier */
  24567. word32 secretKeyIdSz; /* size of key identifier, bytes */
  24568. void* timePtr; /* time_t pointer */
  24569. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  24570. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  24571. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  24572. word32 otherAttrSz; /* size of otherAttr, bytes */
  24573. int kekriOptions; /* KEKRI options flags */
  24574. /* PWRI specific */
  24575. char* password; /* password */
  24576. word32 passwordSz; /* password size, bytes */
  24577. byte* salt; /* KDF salt */
  24578. word32 saltSz; /* KDF salt size, bytes */
  24579. int kdfOID; /* KDF OID */
  24580. int hashOID; /* KDF hash algorithm OID */
  24581. int kdfIterations; /* KDF iterations */
  24582. int kekEncryptOID; /* KEK encryption algorithm OID */
  24583. int pwriOptions; /* PWRI options flags */
  24584. /* ORI specific */
  24585. int isOri;
  24586. int oriOptions; /* ORI options flags */
  24587. const char* outFileName;
  24588. } pkcs7AuthEnvelopedVector;
  24589. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  24590. byte* rsaPrivKey, word32 rsaPrivKeySz,
  24591. byte* eccCert, word32 eccCertSz,
  24592. byte* eccPrivKey, word32 eccPrivKeySz)
  24593. {
  24594. int ret = 0, testSz = 0, i;
  24595. int envelopedSz, decodedSz;
  24596. byte *enveloped = NULL;
  24597. byte *decoded = NULL;
  24598. WC_RNG rng;
  24599. PKCS7* pkcs7;
  24600. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24601. XFILE pkcs7File;
  24602. #endif
  24603. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  24604. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24605. 0x72,0x6c,0x64
  24606. };
  24607. byte senderNonce[PKCS7_NONCE_SZ + 2];
  24608. #ifdef HAVE_ECC
  24609. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  24610. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24611. 0x09, 0x05 };
  24612. PKCS7Attrib attribs[] =
  24613. {
  24614. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  24615. sizeof(senderNonce) }
  24616. };
  24617. #endif
  24618. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  24619. defined(WOLFSSL_SHA512)
  24620. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  24621. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24622. };
  24623. #endif /* NO_AES */
  24624. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24625. /* encryption key for kekri recipient types */
  24626. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  24627. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  24628. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24629. };
  24630. /* encryption key identifier */
  24631. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  24632. 0x02,0x02,0x03,0x04
  24633. };
  24634. #endif
  24635. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  24636. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24637. #ifndef HAVE_FIPS
  24638. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  24639. #else
  24640. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  24641. #endif
  24642. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  24643. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  24644. };
  24645. #endif
  24646. pkcs7AuthEnvelopedVector *testVectors = NULL;
  24647. {
  24648. #define ADD_PKCS7_TEST_VEC(...) { \
  24649. const pkcs7AuthEnvelopedVector vec = __VA_ARGS__; \
  24650. testVectors = (pkcs7AuthEnvelopedVector *) \
  24651. XREALLOC(testVectors, \
  24652. sizeof *testVectors * (testSz + 1), \
  24653. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); \
  24654. if (testVectors == NULL) \
  24655. ERROR_OUT(-12233, out); \
  24656. XMEMCPY(&testVectors[testSz++], &vec, sizeof *testVectors); \
  24657. }
  24658. /* key transport key encryption technique */
  24659. #ifndef NO_RSA
  24660. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24661. #ifdef WOLFSSL_AES_128
  24662. ADD_PKCS7_TEST_VEC(
  24663. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  24664. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24665. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24666. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der"});
  24667. #endif
  24668. #ifdef WOLFSSL_AES_192
  24669. ADD_PKCS7_TEST_VEC(
  24670. {data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  24671. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24672. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24673. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der"});
  24674. #endif
  24675. #ifdef WOLFSSL_AES_256
  24676. ADD_PKCS7_TEST_VEC(
  24677. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24678. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24679. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24680. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der"});
  24681. /* test with contentType set to FirmwarePkgData */
  24682. ADD_PKCS7_TEST_VEC(
  24683. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  24684. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  24685. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  24686. 0, 0, 0, 0, 0, 0, 0, 0,
  24687. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der"});
  24688. /* explicitly using SKID for SubjectKeyIdentifier */
  24689. ADD_PKCS7_TEST_VEC(
  24690. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24691. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  24692. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  24693. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der"});
  24694. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  24695. ADD_PKCS7_TEST_VEC(
  24696. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24697. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  24698. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  24699. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24700. "pkcs7authEnvelopedDataAES256GCM_IANDS.der"});
  24701. #endif
  24702. #endif /* NO_AES */
  24703. #endif
  24704. /* key agreement key encryption technique*/
  24705. #ifdef HAVE_ECC
  24706. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24707. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24708. ADD_PKCS7_TEST_VEC(
  24709. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  24710. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24711. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  24712. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24713. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der"});
  24714. #endif
  24715. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  24716. ADD_PKCS7_TEST_VEC(
  24717. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24718. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24719. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  24720. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24721. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der"});
  24722. /* with authenticated attributes */
  24723. ADD_PKCS7_TEST_VEC(
  24724. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24725. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24726. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24727. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24728. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  24729. 0, 0, 0,
  24730. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der"});
  24731. /* with unauthenticated attributes */
  24732. ADD_PKCS7_TEST_VEC(
  24733. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24734. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24735. eccPrivKeySz, NULL, 0, attribs,
  24736. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  24737. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  24738. 0, 0, 0,
  24739. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der"});
  24740. /* with authenticated AND unauthenticated attributes */
  24741. ADD_PKCS7_TEST_VEC(
  24742. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24743. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24744. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24745. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  24746. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24747. 0, 0, 0, 0, 0, 0,
  24748. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der"});
  24749. /* with authenticated AND unauthenticated attributes AND
  24750. * contentType of FirmwarePkgData */
  24751. ADD_PKCS7_TEST_VEC(
  24752. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  24753. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24754. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24755. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  24756. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24757. 0, 0, 0, 0, 0, 0,
  24758. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der"});
  24759. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  24760. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  24761. ADD_PKCS7_TEST_VEC(
  24762. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24763. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24764. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  24765. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24766. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der"});
  24767. /* with optional user keying material (ukm) */
  24768. ADD_PKCS7_TEST_VEC(
  24769. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24770. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24771. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  24772. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24773. 0, 0, 0, 0, 0, 0,
  24774. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der"});
  24775. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  24776. #endif /* NO_AES */
  24777. #endif
  24778. /* kekri (KEKRecipientInfo) recipient types */
  24779. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24780. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24781. ADD_PKCS7_TEST_VEC(
  24782. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  24783. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  24784. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  24785. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24786. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der"});
  24787. #endif
  24788. #endif
  24789. /* pwri (PasswordRecipientInfo) recipient types */
  24790. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  24791. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24792. ADD_PKCS7_TEST_VEC(
  24793. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  24794. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24795. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  24796. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  24797. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der"});
  24798. #endif
  24799. #endif
  24800. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24801. #ifdef WOLFSSL_AES_128
  24802. /* ori (OtherRecipientInfo) recipient types */
  24803. ADD_PKCS7_TEST_VEC(
  24804. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  24805. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  24806. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  24807. "pkcs7authEnvelopedDataAES128GCM_ORI.der"});
  24808. #endif
  24809. #endif
  24810. }
  24811. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24812. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24813. if ((! enveloped) || (! decoded)) {
  24814. ERROR_OUT(-12210, out);
  24815. }
  24816. /* generate senderNonce */
  24817. {
  24818. #ifndef HAVE_FIPS
  24819. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24820. #else
  24821. ret = wc_InitRng(&rng);
  24822. #endif
  24823. if (ret != 0) {
  24824. ERROR_OUT(-12211, out);
  24825. }
  24826. senderNonce[0] = 0x04;
  24827. senderNonce[1] = PKCS7_NONCE_SZ;
  24828. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  24829. if (ret != 0) {
  24830. wc_FreeRng(&rng);
  24831. ERROR_OUT(-12212, out);
  24832. }
  24833. }
  24834. for (i = 0; i < testSz; i++) {
  24835. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  24836. #ifdef WOLFSSL_ASYNC_CRYPT
  24837. INVALID_DEVID /* async PKCS7 is not supported */
  24838. #else
  24839. devId
  24840. #endif
  24841. );
  24842. if (pkcs7 == NULL) {
  24843. ERROR_OUT(-12213, out);
  24844. }
  24845. if (testVectors[i].secretKey != NULL) {
  24846. /* KEKRI recipient type */
  24847. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24848. if (ret != 0) {
  24849. ERROR_OUT(-12214, out);
  24850. }
  24851. pkcs7->content = (byte*)testVectors[i].content;
  24852. pkcs7->contentSz = testVectors[i].contentSz;
  24853. pkcs7->contentOID = testVectors[i].contentOID;
  24854. pkcs7->encryptOID = testVectors[i].encryptOID;
  24855. pkcs7->ukm = testVectors[i].optionalUkm;
  24856. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24857. pkcs7->authAttribs = testVectors[i].authAttribs;
  24858. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24859. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24860. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24861. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  24862. testVectors[i].secretKey, testVectors[i].secretKeySz,
  24863. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  24864. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  24865. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  24866. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  24867. if (ret < 0) {
  24868. wc_PKCS7_Free(pkcs7);
  24869. ERROR_OUT(-12215, out);
  24870. }
  24871. /* set key, for decryption */
  24872. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  24873. testVectors[i].secretKeySz);
  24874. if (ret != 0) {
  24875. wc_PKCS7_Free(pkcs7);
  24876. ERROR_OUT(-12216, out);
  24877. }
  24878. } else if (testVectors[i].password != NULL) {
  24879. #ifndef NO_PWDBASED
  24880. /* PWRI recipient type */
  24881. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24882. if (ret != 0) {
  24883. ERROR_OUT(-12217, out);
  24884. }
  24885. pkcs7->content = (byte*)testVectors[i].content;
  24886. pkcs7->contentSz = testVectors[i].contentSz;
  24887. pkcs7->contentOID = testVectors[i].contentOID;
  24888. pkcs7->encryptOID = testVectors[i].encryptOID;
  24889. pkcs7->ukm = testVectors[i].optionalUkm;
  24890. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24891. pkcs7->authAttribs = testVectors[i].authAttribs;
  24892. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24893. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24894. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24895. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  24896. (byte*)testVectors[i].password,
  24897. testVectors[i].passwordSz, testVectors[i].salt,
  24898. testVectors[i].saltSz, testVectors[i].kdfOID,
  24899. testVectors[i].hashOID, testVectors[i].kdfIterations,
  24900. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  24901. if (ret < 0) {
  24902. wc_PKCS7_Free(pkcs7);
  24903. ERROR_OUT(-12218, out);
  24904. }
  24905. /* set password, for decryption */
  24906. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  24907. testVectors[i].passwordSz);
  24908. if (ret < 0) {
  24909. wc_PKCS7_Free(pkcs7);
  24910. ERROR_OUT(-12219, out);
  24911. }
  24912. #endif /* NO_PWDBASED */
  24913. } else if (testVectors[i].isOri == 1) {
  24914. /* ORI recipient type */
  24915. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24916. if (ret != 0) {
  24917. ERROR_OUT(-12220, out);
  24918. }
  24919. pkcs7->content = (byte*)testVectors[i].content;
  24920. pkcs7->contentSz = testVectors[i].contentSz;
  24921. pkcs7->contentOID = testVectors[i].contentOID;
  24922. pkcs7->encryptOID = testVectors[i].encryptOID;
  24923. pkcs7->authAttribs = testVectors[i].authAttribs;
  24924. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24925. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24926. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24927. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  24928. testVectors[i].oriOptions);
  24929. if (ret < 0) {
  24930. wc_PKCS7_Free(pkcs7);
  24931. ERROR_OUT(-12221, out);
  24932. }
  24933. /* set decrypt callback for decryption */
  24934. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  24935. if (ret < 0) {
  24936. wc_PKCS7_Free(pkcs7);
  24937. ERROR_OUT(-12222, out);
  24938. }
  24939. } else {
  24940. /* KTRI or KARI recipient types */
  24941. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  24942. (word32)testVectors[i].certSz);
  24943. if (ret != 0) {
  24944. wc_PKCS7_Free(pkcs7);
  24945. ERROR_OUT(-12223, out);
  24946. }
  24947. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  24948. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  24949. pkcs7->privateKey = testVectors[i].privateKey;
  24950. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  24951. pkcs7->content = (byte*)testVectors[i].content;
  24952. pkcs7->contentSz = testVectors[i].contentSz;
  24953. pkcs7->contentOID = testVectors[i].contentOID;
  24954. pkcs7->encryptOID = testVectors[i].encryptOID;
  24955. pkcs7->ukm = testVectors[i].optionalUkm;
  24956. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24957. pkcs7->authAttribs = testVectors[i].authAttribs;
  24958. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  24959. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  24960. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  24961. /* set SubjectIdentifier type for KTRI types */
  24962. if (testVectors[i].ktriOptions & CMS_SKID) {
  24963. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  24964. if (ret != 0) {
  24965. wc_PKCS7_Free(pkcs7);
  24966. ERROR_OUT(-12224, out);
  24967. }
  24968. } else if (testVectors[i].ktriOptions &
  24969. CMS_ISSUER_AND_SERIAL_NUMBER) {
  24970. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  24971. CMS_ISSUER_AND_SERIAL_NUMBER);
  24972. if (ret != 0) {
  24973. wc_PKCS7_Free(pkcs7);
  24974. ERROR_OUT(-12225, out);
  24975. }
  24976. }
  24977. }
  24978. #ifdef ECC_TIMING_RESISTANT
  24979. pkcs7->rng = &rng;
  24980. #endif
  24981. /* encode envelopedData */
  24982. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  24983. PKCS7_BUF_SIZE);
  24984. if (envelopedSz <= 0) {
  24985. wc_PKCS7_Free(pkcs7);
  24986. ERROR_OUT(-12226, out);
  24987. }
  24988. #ifndef NO_PKCS7_STREAM
  24989. { /* test reading byte by byte */
  24990. int z;
  24991. for (z = 0; z < envelopedSz; z++) {
  24992. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  24993. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  24994. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  24995. printf("unexpected error %d\n", decodedSz);
  24996. ERROR_OUT(-12227, out);
  24997. }
  24998. }
  24999. /* test decode result */
  25000. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25001. printf("stream read compare failed\n");
  25002. wc_PKCS7_Free(pkcs7);
  25003. ERROR_OUT(-12228, out);
  25004. }
  25005. }
  25006. #endif
  25007. /* decode envelopedData */
  25008. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  25009. envelopedSz, decoded,
  25010. PKCS7_BUF_SIZE);
  25011. if (decodedSz <= 0) {
  25012. wc_PKCS7_Free(pkcs7);
  25013. ERROR_OUT(-12229, out);
  25014. }
  25015. /* test decode result */
  25016. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  25017. wc_PKCS7_Free(pkcs7);
  25018. ERROR_OUT(-12230, out);
  25019. }
  25020. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25021. /* output pkcs7 envelopedData for external testing */
  25022. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25023. if (!pkcs7File) {
  25024. wc_PKCS7_Free(pkcs7);
  25025. ERROR_OUT(-12231, out);
  25026. }
  25027. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  25028. XFCLOSE(pkcs7File);
  25029. if (ret != envelopedSz) {
  25030. wc_PKCS7_Free(pkcs7);
  25031. ERROR_OUT(-12232, out);
  25032. }
  25033. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  25034. wc_PKCS7_Free(pkcs7);
  25035. pkcs7 = NULL;
  25036. }
  25037. wc_FreeRng(&rng);
  25038. #if !defined(HAVE_ECC) || defined(NO_AES)
  25039. (void)eccCert;
  25040. (void)eccCertSz;
  25041. (void)eccPrivKey;
  25042. (void)eccPrivKeySz;
  25043. (void)secretKey;
  25044. (void)secretKeyId;
  25045. #endif
  25046. #ifdef NO_RSA
  25047. (void)rsaCert;
  25048. (void)rsaCertSz;
  25049. (void)rsaPrivKey;
  25050. (void)rsaPrivKeySz;
  25051. #endif
  25052. out:
  25053. if (testVectors)
  25054. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25055. if (enveloped)
  25056. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25057. if (decoded)
  25058. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25059. return ret;
  25060. }
  25061. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  25062. {
  25063. int ret = 0;
  25064. byte* rsaCert = NULL;
  25065. byte* rsaPrivKey = NULL;
  25066. word32 rsaCertSz = 0;
  25067. word32 rsaPrivKeySz = 0;
  25068. byte* eccCert = NULL;
  25069. byte* eccPrivKey = NULL;
  25070. word32 eccCertSz = 0;
  25071. word32 eccPrivKeySz = 0;
  25072. #ifndef NO_RSA
  25073. /* read client RSA cert and key in DER format */
  25074. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25075. if (rsaCert == NULL)
  25076. return -12300;
  25077. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25078. if (rsaPrivKey == NULL) {
  25079. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25080. return -12301;
  25081. }
  25082. rsaCertSz = FOURK_BUF;
  25083. rsaPrivKeySz = FOURK_BUF;
  25084. #endif /* NO_RSA */
  25085. #ifdef HAVE_ECC
  25086. /* read client ECC cert and key in DER format */
  25087. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25088. if (eccCert == NULL) {
  25089. #ifndef NO_RSA
  25090. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25091. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25092. #endif
  25093. return -12302;
  25094. }
  25095. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25096. if (eccPrivKey == NULL) {
  25097. #ifndef NO_RSA
  25098. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25099. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25100. #endif
  25101. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25102. return -12303;
  25103. }
  25104. eccCertSz = FOURK_BUF;
  25105. eccPrivKeySz = FOURK_BUF;
  25106. #endif /* HAVE_ECC */
  25107. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  25108. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  25109. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  25110. eccPrivKey, &eccPrivKeySz);
  25111. if (ret < 0) {
  25112. #ifndef NO_RSA
  25113. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25114. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25115. #endif
  25116. #ifdef HAVE_ECC
  25117. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25118. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25119. #endif
  25120. return -12304;
  25121. }
  25122. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  25123. rsaPrivKey, (word32)rsaPrivKeySz,
  25124. eccCert, (word32)eccCertSz,
  25125. eccPrivKey, (word32)eccPrivKeySz);
  25126. #ifndef NO_RSA
  25127. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25128. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25129. #endif
  25130. #ifdef HAVE_ECC
  25131. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25132. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25133. #endif
  25134. return ret;
  25135. }
  25136. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  25137. #ifndef NO_AES
  25138. static const byte p7DefKey[] = {
  25139. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25140. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25141. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25142. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25143. };
  25144. static const byte p7AltKey[] = {
  25145. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25146. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25147. };
  25148. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25149. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25150. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25151. {
  25152. int ret;
  25153. if (cek == NULL || out == NULL)
  25154. return BAD_FUNC_ARG;
  25155. /* test case sanity checks */
  25156. if (keyIdSz != 1) {
  25157. return -12310;
  25158. }
  25159. if (keyId[0] != 0x00) {
  25160. return -12311;
  25161. }
  25162. if (type != (int)PKCS7_KEKRI) {
  25163. return -12312;
  25164. }
  25165. switch (keyWrapAlgo) {
  25166. case AES256_WRAP:
  25167. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  25168. out, outSz, NULL);
  25169. if (ret <= 0)
  25170. return ret;
  25171. break;
  25172. default:
  25173. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25174. return BAD_KEYWRAP_ALG_E;
  25175. };
  25176. (void)pkcs7;
  25177. (void)direction;
  25178. (void)orginKey; /* used with KAKRI */
  25179. (void)orginKeySz;
  25180. return ret;
  25181. }
  25182. /* returns key size on success */
  25183. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  25184. {
  25185. int ret;
  25186. word32 atrSz;
  25187. byte atr[256];
  25188. /* Additionally can look for fwWrappedFirmwareKey
  25189. * 1.2.840.113529.1.9.16.1.16 */
  25190. const unsigned char fwWrappedFirmwareKey[] = {
  25191. /* 0x06, 0x0B */
  25192. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25193. 0x01, 0x09, 0x10, 0x02, 0x27
  25194. };
  25195. /* find keyID in fwWrappedFirmwareKey */
  25196. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  25197. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  25198. if (ret == LENGTH_ONLY_E) {
  25199. XMEMSET(atr, 0, sizeof(atr));
  25200. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  25201. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  25202. /* keyIdRaw[0] OCTET TAG */
  25203. /* keyIdRaw[1] Length */
  25204. if (ret > 0) {
  25205. PKCS7* envPkcs7;
  25206. envPkcs7 = wc_PKCS7_New(NULL, 0);
  25207. if (envPkcs7 == NULL) {
  25208. return MEMORY_E;
  25209. }
  25210. wc_PKCS7_Init(envPkcs7, NULL, 0);
  25211. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  25212. if (ret == 0) {
  25213. /* expecting FIRMWARE_PKG_DATA content */
  25214. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  25215. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  25216. key, keySz);
  25217. }
  25218. wc_PKCS7_Free(envPkcs7);
  25219. }
  25220. }
  25221. return ret;
  25222. }
  25223. /* create a KEKRI enveloped data
  25224. * return size on success */
  25225. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  25226. word32 outSz)
  25227. {
  25228. int ret;
  25229. PKCS7* pkcs7;
  25230. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  25231. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  25232. if (pkcs7 == NULL)
  25233. return -12330;
  25234. pkcs7->content = in;
  25235. pkcs7->contentSz = inSz;
  25236. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  25237. pkcs7->encryptOID = AES256CBCb;
  25238. pkcs7->ukm = NULL;
  25239. pkcs7->ukmSz = 0;
  25240. /* add recipient (KEKRI type) */
  25241. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  25242. sizeof(p7DefKey), (byte*)keyId,
  25243. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  25244. if (ret < 0) {
  25245. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  25246. wc_PKCS7_Free(pkcs7);
  25247. return -12331;
  25248. }
  25249. /* encode envelopedData, returns size */
  25250. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  25251. if (ret <= 0) {
  25252. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  25253. wc_PKCS7_Free(pkcs7);
  25254. return -12332;
  25255. }
  25256. wc_PKCS7_Free(pkcs7);
  25257. return ret;
  25258. }
  25259. /*
  25260. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  25261. * returns size of buffer output on success
  25262. */
  25263. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  25264. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  25265. byte* key, word32 keySz)
  25266. {
  25267. int ret, attribNum = 1;
  25268. PKCS7* pkcs7;
  25269. /* KEY ID
  25270. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  25271. */
  25272. const unsigned char fwDecryptKeyID[] = {
  25273. 0x06, 0x0B,
  25274. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25275. 0x01, 0x09, 0x10, 0x02, 0x25
  25276. };
  25277. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  25278. const unsigned char fwWrappedFirmwareKey[] = {
  25279. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25280. 0x01, 0x09, 0x10, 0x02, 0x27
  25281. };
  25282. byte keyID[] = { 0x04, 0x01, 0x00 };
  25283. byte env[256];
  25284. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  25285. PKCS7Attrib attribs[] =
  25286. {
  25287. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  25288. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  25289. };
  25290. keyID[2] = keyHint;
  25291. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  25292. if (keyHint == 0) {
  25293. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  25294. sizeof(env));
  25295. if (ret <= 0) {
  25296. return ret;
  25297. }
  25298. attribs[1].valueSz = ret;
  25299. attribNum++;
  25300. }
  25301. /* init PKCS7 */
  25302. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  25303. if (pkcs7 == NULL)
  25304. return -12340;
  25305. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  25306. if (ret != 0) {
  25307. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  25308. wc_PKCS7_Free(pkcs7);
  25309. return -12341;
  25310. }
  25311. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  25312. if (ret != 0) {
  25313. wc_PKCS7_Free(pkcs7);
  25314. return -12342;
  25315. }
  25316. /* encode Signed Encrypted FirmwarePkgData */
  25317. if (encryptKeySz == 16) {
  25318. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  25319. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  25320. (byte*)data, sizeof(data), NULL, 0,
  25321. attribs, attribNum, out, *outSz);
  25322. }
  25323. else {
  25324. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  25325. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  25326. (byte*)data, sizeof(data), NULL, 0,
  25327. attribs, attribNum, out, *outSz);
  25328. }
  25329. if (ret <= 0) {
  25330. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  25331. "ret = %d\n", ret);
  25332. wc_PKCS7_Free(pkcs7);
  25333. return -12343;
  25334. } else {
  25335. *outSz = ret;
  25336. }
  25337. wc_PKCS7_Free(pkcs7);
  25338. return ret;
  25339. }
  25340. /* test verification and decryption of PKCS7 bundle
  25341. * return 0 on success
  25342. */
  25343. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  25344. {
  25345. int ret = 0;
  25346. int usrCtx = 1; /* test value to pass as user context to callback */
  25347. PKCS7* pkcs7;
  25348. byte* sid;
  25349. word32 sidSz;
  25350. byte key[256];
  25351. word32 keySz = sizeof(key);
  25352. byte decoded[FOURK_BUF/2];
  25353. int decodedSz = FOURK_BUF/2;
  25354. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  25355. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  25356. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  25357. 0xD7, 0x85, 0x65, 0xC0
  25358. };
  25359. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  25360. if (pkcs7 == NULL) {
  25361. return MEMORY_E;
  25362. }
  25363. /* Test verify */
  25364. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  25365. if (ret != 0) {
  25366. wc_PKCS7_Free(pkcs7);
  25367. return ret;
  25368. }
  25369. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  25370. if (ret != 0) {
  25371. wc_PKCS7_Free(pkcs7);
  25372. return ret;
  25373. }
  25374. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  25375. if (ret != 0) {
  25376. wc_PKCS7_Free(pkcs7);
  25377. return ret;
  25378. }
  25379. /* Get size of SID and print it out */
  25380. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  25381. if (ret != LENGTH_ONLY_E) {
  25382. wc_PKCS7_Free(pkcs7);
  25383. return ret;
  25384. }
  25385. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25386. if (sid == NULL) {
  25387. wc_PKCS7_Free(pkcs7);
  25388. return ret;
  25389. }
  25390. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  25391. if (ret != 0) {
  25392. wc_PKCS7_Free(pkcs7);
  25393. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25394. return ret;
  25395. }
  25396. ret = XMEMCMP(sid, expectedSid, sidSz);
  25397. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25398. if (ret != 0) {
  25399. wc_PKCS7_Free(pkcs7);
  25400. return ret;
  25401. }
  25402. /* get expected fwWrappedFirmwareKey */
  25403. if (keyHint == 0) {
  25404. ret = getFirmwareKey(pkcs7, key, keySz);
  25405. if (ret < 0) {
  25406. wc_PKCS7_Free(pkcs7);
  25407. return ret;
  25408. }
  25409. pkcs7->encryptionKey = key;
  25410. pkcs7->encryptionKeySz = ret;
  25411. }
  25412. else {
  25413. decodedSz = PKCS7_BUF_SIZE;
  25414. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  25415. if (ret != 0) {
  25416. wc_PKCS7_Free(pkcs7);
  25417. return ret;
  25418. }
  25419. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  25420. if (ret != 0) {
  25421. wc_PKCS7_Free(pkcs7);
  25422. return ret;
  25423. }
  25424. }
  25425. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  25426. pkcs7->contentSz, decoded, decodedSz);
  25427. if (decodedSz < 0) {
  25428. ret = decodedSz;
  25429. wc_PKCS7_Free(pkcs7);
  25430. return ret;
  25431. }
  25432. wc_PKCS7_Free(pkcs7);
  25433. return 0;
  25434. }
  25435. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  25436. {
  25437. int ret = 0;
  25438. word32 derSz;
  25439. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25440. if (! derBuf)
  25441. ERROR_OUT(-12360, out);
  25442. /* Doing default generation and verify */
  25443. derSz = FOURK_BUF;
  25444. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  25445. certSz, key, keySz);
  25446. if (ret <= 0) {
  25447. ERROR_OUT(-12361, out);
  25448. }
  25449. ret = verifyBundle(derBuf, derSz, 0);
  25450. if (ret != 0) {
  25451. ERROR_OUT(-12362, out);
  25452. }
  25453. /* test choosing other key with keyID */
  25454. derSz = FOURK_BUF;
  25455. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  25456. cert, certSz, key, keySz);
  25457. if (ret <= 0) {
  25458. ERROR_OUT(-12363, out);
  25459. }
  25460. ret = verifyBundle(derBuf, derSz, 1);
  25461. if (ret != 0) {
  25462. ERROR_OUT(-12364, out);
  25463. }
  25464. /* test fail case with wrong keyID */
  25465. derSz = FOURK_BUF;
  25466. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  25467. cert, certSz, key, keySz);
  25468. if (ret <= 0) {
  25469. ERROR_OUT(-12365, out);
  25470. }
  25471. ret = verifyBundle(derBuf, derSz, 1);
  25472. if (ret == 0) {
  25473. ERROR_OUT(-12366, out);
  25474. }
  25475. ret = 0;
  25476. out:
  25477. if (derBuf)
  25478. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25479. return ret;
  25480. }
  25481. #endif /* NO_AES */
  25482. #ifndef NO_PKCS7_ENCRYPTED_DATA
  25483. typedef struct {
  25484. const byte* content;
  25485. word32 contentSz;
  25486. int contentOID;
  25487. int encryptOID;
  25488. byte* encryptionKey;
  25489. word32 encryptionKeySz;
  25490. PKCS7Attrib* attribs;
  25491. word32 attribsSz;
  25492. const char* outFileName;
  25493. } pkcs7EncryptedVector;
  25494. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  25495. {
  25496. int ret = 0;
  25497. int i, testSz;
  25498. int encryptedSz, decodedSz, attribIdx;
  25499. PKCS7* pkcs7;
  25500. byte *encrypted;
  25501. byte *decoded;
  25502. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25503. XFILE pkcs7File;
  25504. #endif
  25505. PKCS7Attrib* expectedAttrib;
  25506. PKCS7DecodedAttrib* decodedAttrib;
  25507. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25508. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25509. 0x72,0x6c,0x64
  25510. };
  25511. #ifndef NO_DES3
  25512. byte desKey[] = {
  25513. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25514. };
  25515. byte des3Key[] = {
  25516. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25517. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25518. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25519. };
  25520. #endif
  25521. #ifndef NO_AES
  25522. #ifdef WOLFSSL_AES_128
  25523. byte aes128Key[] = {
  25524. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25525. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25526. };
  25527. #endif
  25528. #ifdef WOLFSSL_AES_192
  25529. byte aes192Key[] = {
  25530. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25531. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25532. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25533. };
  25534. #endif
  25535. #ifdef WOLFSSL_AES_256
  25536. byte aes256Key[] = {
  25537. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25538. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25539. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25540. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25541. };
  25542. #endif
  25543. #ifdef WOLFSSL_AES_256
  25544. /* Attribute example from RFC 4134, Section 7.2
  25545. * OID = 1.2.5555
  25546. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  25547. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  25548. static byte genAttr[] = { 0x04, 47,
  25549. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  25550. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  25551. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  25552. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  25553. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  25554. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  25555. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  25556. static byte genAttr2[] = { 0x04, 47,
  25557. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  25558. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  25559. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  25560. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  25561. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  25562. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  25563. PKCS7Attrib attribs[] =
  25564. {
  25565. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  25566. };
  25567. PKCS7Attrib multiAttribs[] =
  25568. {
  25569. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  25570. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  25571. };
  25572. #endif
  25573. #endif /* NO_AES */
  25574. const pkcs7EncryptedVector testVectors[] =
  25575. {
  25576. #ifndef NO_DES3
  25577. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  25578. NULL, 0, "pkcs7encryptedDataDES3.der"},
  25579. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  25580. NULL, 0, "pkcs7encryptedDataDES.der"},
  25581. #endif /* NO_DES3 */
  25582. #ifndef NO_AES
  25583. #ifdef WOLFSSL_AES_128
  25584. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25585. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  25586. #endif
  25587. #ifdef WOLFSSL_AES_192
  25588. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25589. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  25590. #endif
  25591. #ifdef WOLFSSL_AES_256
  25592. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25593. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  25594. /* test with optional unprotected attributes */
  25595. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25596. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25597. "pkcs7encryptedDataAES256CBC_attribs.der"},
  25598. /* test with multiple optional unprotected attributes */
  25599. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25600. sizeof(aes256Key), multiAttribs,
  25601. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  25602. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  25603. /* test with contentType set to FirmwarePkgData */
  25604. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  25605. sizeof(aes256Key), NULL, 0,
  25606. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  25607. #endif
  25608. #endif /* NO_AES */
  25609. };
  25610. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25611. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25612. if ((! encrypted) || (! decoded)) {
  25613. ERROR_OUT(MEMORY_E, out);
  25614. }
  25615. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25616. for (i = 0; i < testSz; i++) {
  25617. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  25618. if (pkcs7 == NULL) {
  25619. ERROR_OUT(-12400, out);
  25620. }
  25621. pkcs7->content = (byte*)testVectors[i].content;
  25622. pkcs7->contentSz = testVectors[i].contentSz;
  25623. pkcs7->contentOID = testVectors[i].contentOID;
  25624. pkcs7->encryptOID = testVectors[i].encryptOID;
  25625. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25626. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25627. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  25628. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  25629. /* encode encryptedData */
  25630. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25631. PKCS7_BUF_SIZE);
  25632. if (encryptedSz <= 0) {
  25633. wc_PKCS7_Free(pkcs7);
  25634. ERROR_OUT(-12401, out);
  25635. }
  25636. /* decode encryptedData */
  25637. #ifndef NO_PKCS7_STREAM
  25638. { /* test reading byte by byte */
  25639. int z;
  25640. for (z = 0; z < encryptedSz; z++) {
  25641. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  25642. decoded, PKCS7_BUF_SIZE);
  25643. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  25644. printf("unexpected error %d\n", decodedSz);
  25645. ERROR_OUT(-12402, out);
  25646. }
  25647. }
  25648. /* test decode result */
  25649. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25650. printf("stream read failed\n");
  25651. wc_PKCS7_Free(pkcs7);
  25652. ERROR_OUT(-12403, out);
  25653. }
  25654. }
  25655. #endif
  25656. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25657. decoded, PKCS7_BUF_SIZE);
  25658. if (decodedSz <= 0){
  25659. wc_PKCS7_Free(pkcs7);
  25660. ERROR_OUT(-12404, out);
  25661. }
  25662. /* test decode result */
  25663. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25664. wc_PKCS7_Free(pkcs7);
  25665. ERROR_OUT(-12405, out);
  25666. }
  25667. /* verify decoded unprotected attributes */
  25668. if (pkcs7->decodedAttrib != NULL) {
  25669. decodedAttrib = pkcs7->decodedAttrib;
  25670. attribIdx = 1;
  25671. while (decodedAttrib != NULL) {
  25672. /* expected attribute, stored list is reversed */
  25673. expectedAttrib = &(pkcs7->unprotectedAttribs
  25674. [pkcs7->unprotectedAttribsSz - attribIdx]);
  25675. /* verify oid */
  25676. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  25677. decodedAttrib->oidSz) != 0) {
  25678. wc_PKCS7_Free(pkcs7);
  25679. ERROR_OUT(-12406, out);
  25680. }
  25681. /* verify value */
  25682. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  25683. decodedAttrib->valueSz) != 0) {
  25684. wc_PKCS7_Free(pkcs7);
  25685. ERROR_OUT(-12407, out);
  25686. }
  25687. decodedAttrib = decodedAttrib->next;
  25688. attribIdx++;
  25689. }
  25690. }
  25691. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25692. /* output pkcs7 envelopedData for external testing */
  25693. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25694. if (!pkcs7File) {
  25695. wc_PKCS7_Free(pkcs7);
  25696. ERROR_OUT(-12408, out);
  25697. }
  25698. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  25699. XFCLOSE(pkcs7File);
  25700. if (ret > 0)
  25701. ret = 0;
  25702. #endif
  25703. wc_PKCS7_Free(pkcs7);
  25704. }
  25705. out:
  25706. if (encrypted)
  25707. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25708. if (decoded)
  25709. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25710. return ret;
  25711. }
  25712. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  25713. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  25714. typedef struct {
  25715. const byte* content;
  25716. word32 contentSz;
  25717. int contentOID;
  25718. const char* outFileName;
  25719. } pkcs7CompressedVector;
  25720. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  25721. {
  25722. int ret = 0;
  25723. int i, testSz;
  25724. int compressedSz, decodedSz;
  25725. PKCS7* pkcs7;
  25726. #ifdef WOLFSSL_SMALL_STACK
  25727. byte *compressed;
  25728. byte *decoded;
  25729. #else
  25730. byte compressed[PKCS7_BUF_SIZE];
  25731. byte decoded[PKCS7_BUF_SIZE];
  25732. #endif
  25733. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25734. XFILE pkcs7File;
  25735. #endif
  25736. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25737. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25738. 0x72,0x6c,0x64
  25739. };
  25740. const pkcs7CompressedVector testVectors[] =
  25741. {
  25742. {data, (word32)sizeof(data), DATA,
  25743. "pkcs7compressedData_data_zlib.der"},
  25744. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  25745. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  25746. };
  25747. #ifdef WOLFSSL_SMALL_STACK
  25748. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25749. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25750. if ((! compressed) || (! decoded)) {
  25751. ERROR_OUT(MEMORY_E, out);
  25752. }
  25753. #endif
  25754. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  25755. for (i = 0; i < testSz; i++) {
  25756. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  25757. if (pkcs7 == NULL) {
  25758. ERROR_OUT(-12500, out);
  25759. }
  25760. pkcs7->content = (byte*)testVectors[i].content;
  25761. pkcs7->contentSz = testVectors[i].contentSz;
  25762. pkcs7->contentOID = testVectors[i].contentOID;
  25763. /* encode compressedData */
  25764. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  25765. PKCS7_BUF_SIZE);
  25766. if (compressedSz <= 0) {
  25767. wc_PKCS7_Free(pkcs7);
  25768. ERROR_OUT(-12501, out);
  25769. }
  25770. /* decode compressedData */
  25771. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  25772. compressedSz, decoded,
  25773. PKCS7_BUF_SIZE);
  25774. if (decodedSz <= 0){
  25775. wc_PKCS7_Free(pkcs7);
  25776. ERROR_OUT(-12502, out);
  25777. }
  25778. /* test decode result */
  25779. if (XMEMCMP(decoded, testVectors[i].content,
  25780. testVectors[i].contentSz) != 0) {
  25781. wc_PKCS7_Free(pkcs7);
  25782. ERROR_OUT(-12503, out);
  25783. }
  25784. /* make sure content type is the same */
  25785. if (testVectors[i].contentOID != pkcs7->contentOID) {
  25786. ERROR_OUT(-12504, out);
  25787. }
  25788. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25789. /* output pkcs7 compressedData for external testing */
  25790. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25791. if (!pkcs7File) {
  25792. wc_PKCS7_Free(pkcs7);
  25793. ERROR_OUT(-12505, out);
  25794. }
  25795. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  25796. XFCLOSE(pkcs7File);
  25797. if (ret > 0)
  25798. ret = 0;
  25799. #endif
  25800. wc_PKCS7_Free(pkcs7);
  25801. }
  25802. out:
  25803. #ifdef WOLFSSL_SMALL_STACK
  25804. if (compressed)
  25805. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25806. if (decoded)
  25807. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25808. #endif
  25809. return ret;
  25810. } /* pkcs7compressed_test() */
  25811. #undef PKCS7_BUF_SIZE
  25812. #endif /* HAVE_LIBZ */
  25813. typedef struct {
  25814. const byte* content;
  25815. word32 contentSz;
  25816. int hashOID;
  25817. int signOID;
  25818. byte* privateKey;
  25819. word32 privateKeySz;
  25820. byte* cert;
  25821. size_t certSz;
  25822. byte* caCert;
  25823. size_t caCertSz;
  25824. PKCS7Attrib* signedAttribs;
  25825. word32 signedAttribsSz;
  25826. const char* outFileName;
  25827. int contentOID;
  25828. byte* contentType;
  25829. word32 contentTypeSz;
  25830. int sidType;
  25831. int encryptOID; /* for single-shot encrypt alg OID */
  25832. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  25833. byte* encryptKey; /* for single-shot, encryptedData */
  25834. word32 encryptKeySz; /* for single-shot, encryptedData */
  25835. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  25836. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  25837. word16 detachedSignature; /* generate detached signature (0:1) */
  25838. } pkcs7SignedVector;
  25839. static int pkcs7signed_run_vectors(
  25840. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  25841. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  25842. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  25843. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  25844. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  25845. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  25846. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  25847. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  25848. {
  25849. int ret, testSz, i;
  25850. int encodedSz;
  25851. byte* out;
  25852. word32 outSz;
  25853. WC_RNG rng;
  25854. PKCS7* pkcs7;
  25855. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25856. XFILE file;
  25857. #endif
  25858. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25859. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25860. 0x72,0x6c,0x64
  25861. };
  25862. static byte transIdOid[] =
  25863. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25864. 0x09, 0x07 };
  25865. static byte messageTypeOid[] =
  25866. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25867. 0x09, 0x02 };
  25868. static byte senderNonceOid[] =
  25869. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25870. 0x09, 0x05 };
  25871. #ifndef NO_SHA
  25872. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  25873. #else
  25874. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  25875. #endif
  25876. static byte messageType[] = { 0x13, 2, '1', '9' };
  25877. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  25878. static PKCS7Attrib attribs[] =
  25879. {
  25880. { transIdOid, sizeof(transIdOid), transId,
  25881. sizeof(transId) - 1 }, /* take off the null */
  25882. { messageTypeOid, sizeof(messageTypeOid), messageType,
  25883. sizeof(messageType) },
  25884. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  25885. sizeof(senderNonce) }
  25886. };
  25887. /* for testing custom contentType, FirmwarePkgData */
  25888. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  25889. 0x48, 0x86, 0xF7, 0x0D,
  25890. 0x01, 0x09, 0x10, 0x01, 0x10 };
  25891. const pkcs7SignedVector testVectors[] =
  25892. {
  25893. #ifndef NO_RSA
  25894. #ifndef NO_SHA
  25895. /* RSA with SHA */
  25896. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  25897. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25898. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25899. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  25900. 0, 0},
  25901. /* RSA with SHA, no signed attributes */
  25902. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  25903. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  25904. NULL, 0, NULL, 0,
  25905. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25906. NULL, 0, 0},
  25907. #endif
  25908. #ifdef WOLFSSL_SHA224
  25909. /* RSA with SHA224 */
  25910. {data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  25911. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25912. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25913. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25914. NULL, 0, 0},
  25915. #endif
  25916. #ifndef NO_SHA256
  25917. /* RSA with SHA256 */
  25918. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25919. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25920. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25921. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25922. NULL, 0, 0},
  25923. /* RSA with SHA256, detached signature */
  25924. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25925. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25926. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25927. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  25928. NULL, 0, NULL, 0, 1},
  25929. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  25930. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25931. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25932. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25933. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  25934. NULL, 0, NULL, 0, 0},
  25935. /* RSA with SHA256 and custom contentType */
  25936. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25937. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25938. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25939. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  25940. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  25941. NULL, 0, 0},
  25942. /* RSA with SHA256 and FirmwarePkgData contentType */
  25943. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  25944. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25945. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25946. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  25947. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  25948. /* RSA with SHA256 using server cert and ca cert */
  25949. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  25950. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  25951. rsaCaCertBuf, rsaCaCertBufSz,
  25952. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25953. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  25954. NULL, 0, NULL, 0, 0},
  25955. #endif
  25956. #if defined(WOLFSSL_SHA384)
  25957. /* RSA with SHA384 */
  25958. {data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  25959. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25960. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25961. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25962. NULL, 0, 0},
  25963. #endif
  25964. #if defined(WOLFSSL_SHA512)
  25965. /* RSA with SHA512 */
  25966. {data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  25967. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  25968. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25969. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25970. NULL, 0, 0},
  25971. #endif
  25972. #endif /* NO_RSA */
  25973. #ifdef HAVE_ECC
  25974. #ifndef NO_SHA
  25975. /* ECDSA with SHA */
  25976. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  25977. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  25978. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25979. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25980. NULL, 0, 0},
  25981. /* ECDSA with SHA, no signed attributes */
  25982. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  25983. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  25984. NULL, 0, NULL, 0,
  25985. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25986. NULL, 0, 0},
  25987. #endif
  25988. #ifdef WOLFSSL_SHA224
  25989. /* ECDSA with SHA224 */
  25990. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  25991. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  25992. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25993. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  25994. NULL, 0, 0},
  25995. #endif
  25996. #ifndef NO_SHA256
  25997. /* ECDSA with SHA256 */
  25998. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  25999. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26000. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26001. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26002. NULL, 0, 0},
  26003. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  26004. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26005. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26006. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26007. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  26008. NULL, 0, NULL, 0, 0},
  26009. /* ECDSA with SHA256 and custom contentType */
  26010. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26011. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26012. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26013. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  26014. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  26015. NULL, 0, 0},
  26016. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  26017. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26018. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26019. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26020. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  26021. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26022. #endif
  26023. #ifdef WOLFSSL_SHA384
  26024. /* ECDSA with SHA384 */
  26025. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  26026. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26027. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26028. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26029. NULL, 0, 0},
  26030. #endif
  26031. #ifdef WOLFSSL_SHA512
  26032. /* ECDSA with SHA512 */
  26033. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  26034. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26035. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26036. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26037. NULL, 0, 0},
  26038. #endif
  26039. #endif /* HAVE_ECC */
  26040. };
  26041. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  26042. outSz = FOURK_BUF;
  26043. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26044. if (out == NULL)
  26045. return -12510;
  26046. XMEMSET(out, 0, outSz);
  26047. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  26048. if (ret < 0) {
  26049. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26050. return -12511;
  26051. }
  26052. #ifndef HAVE_FIPS
  26053. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26054. #else
  26055. ret = wc_InitRng(&rng);
  26056. #endif
  26057. if (ret != 0) {
  26058. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26059. return -12512;
  26060. }
  26061. for (i = 0; i < testSz; i++) {
  26062. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26063. if (pkcs7 == NULL)
  26064. return -12513;
  26065. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  26066. (word32)testVectors[i].certSz);
  26067. if (ret != 0) {
  26068. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26069. wc_PKCS7_Free(pkcs7);
  26070. return -12514;
  26071. }
  26072. /* load CA certificate, if present */
  26073. if (testVectors[i].caCert != NULL) {
  26074. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  26075. (word32)testVectors[i].caCertSz);
  26076. if (ret != 0) {
  26077. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26078. wc_PKCS7_Free(pkcs7);
  26079. return -12515;
  26080. }
  26081. }
  26082. pkcs7->rng = &rng;
  26083. pkcs7->content = (byte*)testVectors[i].content;
  26084. pkcs7->contentSz = testVectors[i].contentSz;
  26085. pkcs7->contentOID = testVectors[i].contentOID;
  26086. pkcs7->hashOID = testVectors[i].hashOID;
  26087. pkcs7->encryptOID = testVectors[i].signOID;
  26088. pkcs7->privateKey = testVectors[i].privateKey;
  26089. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  26090. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  26091. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  26092. /* optional custom contentType, default is DATA,
  26093. overrides contentOID if set */
  26094. if (testVectors[i].contentType != NULL) {
  26095. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  26096. testVectors[i].contentTypeSz);
  26097. if (ret != 0) {
  26098. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26099. wc_PKCS7_Free(pkcs7);
  26100. return -12516;
  26101. }
  26102. }
  26103. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  26104. default is IssuerAndSerialNumber */
  26105. if (testVectors[i].sidType == CMS_SKID) {
  26106. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  26107. if (ret != 0) {
  26108. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26109. wc_PKCS7_Free(pkcs7);
  26110. return -12517;
  26111. }
  26112. }
  26113. /* generate senderNonce */
  26114. {
  26115. senderNonce[0] = 0x04;
  26116. senderNonce[1] = PKCS7_NONCE_SZ;
  26117. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  26118. if (ret != 0) {
  26119. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26120. wc_PKCS7_Free(pkcs7);
  26121. return -12518;
  26122. }
  26123. }
  26124. /* generate transactionID (used with SCEP) */
  26125. {
  26126. #ifndef NO_SHA
  26127. wc_Sha sha;
  26128. byte digest[WC_SHA_DIGEST_SIZE];
  26129. #else
  26130. wc_Sha256 sha;
  26131. byte digest[WC_SHA256_DIGEST_SIZE];
  26132. #endif
  26133. int j,k;
  26134. transId[0] = 0x13;
  26135. transId[1] = sizeof(digest) * 2;
  26136. #ifndef NO_SHA
  26137. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  26138. if (ret != 0) {
  26139. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26140. wc_PKCS7_Free(pkcs7);
  26141. return -12519;
  26142. }
  26143. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  26144. wc_ShaFinal(&sha, digest);
  26145. wc_ShaFree(&sha);
  26146. #else
  26147. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  26148. if (ret != 0) {
  26149. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26150. wc_PKCS7_Free(pkcs7);
  26151. return -12520;
  26152. }
  26153. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  26154. wc_Sha256Final(&sha, digest);
  26155. wc_Sha256Free(&sha);
  26156. #endif
  26157. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  26158. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  26159. }
  26160. }
  26161. /* enable detached signature generation, if set */
  26162. if (testVectors[i].detachedSignature == 1) {
  26163. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  26164. if (ret != 0) {
  26165. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26166. wc_PKCS7_Free(pkcs7);
  26167. return -12521;
  26168. }
  26169. }
  26170. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  26171. if (encodedSz < 0) {
  26172. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26173. wc_PKCS7_Free(pkcs7);
  26174. return -12522;
  26175. }
  26176. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26177. /* write PKCS#7 to output file for more testing */
  26178. file = XFOPEN(testVectors[i].outFileName, "wb");
  26179. if (!file) {
  26180. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26181. wc_PKCS7_Free(pkcs7);
  26182. return -12523;
  26183. }
  26184. ret = (int)XFWRITE(out, 1, encodedSz, file);
  26185. XFCLOSE(file);
  26186. if (ret != (int)encodedSz) {
  26187. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26188. wc_PKCS7_Free(pkcs7);
  26189. return -12524;
  26190. }
  26191. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26192. wc_PKCS7_Free(pkcs7);
  26193. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26194. if (pkcs7 == NULL)
  26195. return -12525;
  26196. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  26197. if (testVectors[i].detachedSignature == 1) {
  26198. /* set content for verifying detached signatures */
  26199. pkcs7->content = (byte*)testVectors[i].content;
  26200. pkcs7->contentSz = testVectors[i].contentSz;
  26201. }
  26202. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  26203. if (ret < 0) {
  26204. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26205. wc_PKCS7_Free(pkcs7);
  26206. return -12526;
  26207. }
  26208. /* verify contentType extracted successfully for custom content types */
  26209. if (testVectors[i].contentTypeSz > 0) {
  26210. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  26211. return -12527;
  26212. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  26213. pkcs7->contentTypeSz) != 0) {
  26214. return -12528;
  26215. }
  26216. }
  26217. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  26218. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26219. wc_PKCS7_Free(pkcs7);
  26220. return -12529;
  26221. }
  26222. {
  26223. /* check getting signed attributes */
  26224. #ifndef NO_SHA
  26225. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  26226. #else
  26227. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  26228. #endif
  26229. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  26230. int oidSz = (int)sizeof(transIdOid) - 2;
  26231. int bufSz = 0;
  26232. if (testVectors[i].signedAttribs != NULL &&
  26233. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  26234. NULL, (word32*)&bufSz) != LENGTH_ONLY_E) {
  26235. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26236. wc_PKCS7_Free(pkcs7);
  26237. return -12530;
  26238. }
  26239. if (bufSz > (int)sizeof(buf)) {
  26240. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26241. wc_PKCS7_Free(pkcs7);
  26242. return -12531;
  26243. }
  26244. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  26245. buf, (word32*)&bufSz);
  26246. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  26247. (testVectors[i].signedAttribs == NULL && bufSz > 0)) {
  26248. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26249. wc_PKCS7_Free(pkcs7);
  26250. return -12532;
  26251. }
  26252. }
  26253. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26254. file = XFOPEN("./pkcs7cert.der", "wb");
  26255. if (!file) {
  26256. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26257. wc_PKCS7_Free(pkcs7);
  26258. return -12533;
  26259. }
  26260. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  26261. XFCLOSE(file);
  26262. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26263. wc_PKCS7_Free(pkcs7);
  26264. }
  26265. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26266. wc_FreeRng(&rng);
  26267. if (ret > 0)
  26268. return 0;
  26269. (void)rsaClientCertBuf;
  26270. (void)rsaClientCertBufSz;
  26271. (void)rsaClientPrivKeyBuf;
  26272. (void)rsaClientPrivKeyBufSz;
  26273. (void)rsaServerCertBuf;
  26274. (void)rsaServerCertBufSz;
  26275. (void)rsaServerPrivKeyBuf;
  26276. (void)rsaServerPrivKeyBufSz;
  26277. (void)rsaCaCertBuf;
  26278. (void)rsaCaCertBufSz;
  26279. (void)rsaCaPrivKeyBuf;
  26280. (void)rsaCaPrivKeyBufSz;
  26281. (void)eccClientCertBuf;
  26282. (void)eccClientCertBufSz;
  26283. (void)eccClientPrivKeyBuf;
  26284. (void)eccClientPrivKeyBufSz;
  26285. return ret;
  26286. }
  26287. static int pkcs7signed_run_SingleShotVectors(
  26288. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  26289. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  26290. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  26291. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  26292. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  26293. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  26294. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  26295. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  26296. {
  26297. int ret, testSz, i;
  26298. int encodedSz;
  26299. byte* out;
  26300. word32 outSz;
  26301. WC_RNG rng;
  26302. PKCS7* pkcs7;
  26303. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26304. XFILE file;
  26305. #endif
  26306. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  26307. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26308. 0x72,0x6c,0x64
  26309. };
  26310. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26311. static byte aes256Key[] = {
  26312. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26313. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26314. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26315. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26316. };
  26317. #endif
  26318. static byte messageTypeOid[] =
  26319. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26320. 0x09, 0x02 };
  26321. static byte messageType[] = { 0x13, 2, '1', '9' };
  26322. PKCS7Attrib attribs[] =
  26323. {
  26324. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26325. sizeof(messageType) },
  26326. };
  26327. const pkcs7SignedVector testVectors[] =
  26328. {
  26329. #ifndef NO_RSA
  26330. #ifndef NO_SHA256
  26331. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  26332. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26333. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26334. NULL, 0,
  26335. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  26336. 0, 0, NULL, 0, NULL, 0, 0},
  26337. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  26338. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26339. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26340. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26341. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  26342. NULL, 0, NULL, 0, 0},
  26343. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  26344. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26345. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26346. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26347. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  26348. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  26349. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  26350. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  26351. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  26352. rsaCaCertBuf, rsaCaCertBufSz,
  26353. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26354. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  26355. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26356. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26357. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  26358. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26359. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26360. NULL, 0,
  26361. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  26362. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  26363. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  26364. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26365. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26366. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26367. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  26368. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  26369. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26370. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  26371. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26372. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  26373. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26374. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26375. NULL, 0,
  26376. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  26377. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26378. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  26379. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26380. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26381. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26382. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  26383. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26384. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26385. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  26386. no attribs */
  26387. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26388. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26389. NULL, 0,
  26390. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  26391. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  26392. 0, 0},
  26393. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  26394. attribs */
  26395. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26396. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26397. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26398. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  26399. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  26400. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26401. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  26402. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26403. #endif /* NO_SHA256 */
  26404. #endif /* NO_RSA */
  26405. #ifdef HAVE_ECC
  26406. #ifndef NO_SHA256
  26407. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  26408. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26409. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26410. NULL, 0,
  26411. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26412. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26413. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  26414. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26415. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26416. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26417. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26418. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26419. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  26420. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26421. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26422. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26423. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  26424. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  26425. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26426. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  26427. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26428. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26429. NULL, 0,
  26430. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26431. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  26432. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  26433. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26434. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26435. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26436. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26437. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  26438. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26439. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  26440. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26441. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  26442. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26443. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26444. NULL, 0,
  26445. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26446. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26447. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  26448. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26449. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26450. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26451. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26452. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26453. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26454. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  26455. no attribs */
  26456. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26457. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26458. NULL, 0,
  26459. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  26460. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  26461. 0, 0},
  26462. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  26463. attribs */
  26464. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26465. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26466. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26467. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  26468. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  26469. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26470. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  26471. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26472. #endif /* NO_SHA256 */
  26473. #endif /* HAVE_ECC */
  26474. };
  26475. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  26476. outSz = FOURK_BUF;
  26477. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26478. if (out == NULL)
  26479. return -12540;
  26480. XMEMSET(out, 0, outSz);
  26481. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  26482. if (ret < 0) {
  26483. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26484. return -12541;
  26485. }
  26486. #ifndef HAVE_FIPS
  26487. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26488. #else
  26489. ret = wc_InitRng(&rng);
  26490. #endif
  26491. if (ret != 0) {
  26492. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26493. return -12542;
  26494. }
  26495. for (i = 0; i < testSz; i++) {
  26496. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26497. if (pkcs7 == NULL)
  26498. return -12543;
  26499. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  26500. (word32)testVectors[i].certSz);
  26501. if (ret != 0) {
  26502. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26503. wc_PKCS7_Free(pkcs7);
  26504. return -12544;
  26505. }
  26506. /* load CA certificate, if present */
  26507. if (testVectors[i].caCert != NULL) {
  26508. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  26509. (word32)testVectors[i].caCertSz);
  26510. if (ret != 0) {
  26511. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26512. wc_PKCS7_Free(pkcs7);
  26513. return -12545;
  26514. }
  26515. }
  26516. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  26517. default is IssuerAndSerialNumber */
  26518. if (testVectors[i].sidType == CMS_SKID) {
  26519. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  26520. if (ret != 0) {
  26521. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26522. wc_PKCS7_Free(pkcs7);
  26523. return -12546;
  26524. }
  26525. }
  26526. if (testVectors[i].encCompFlag == 0) {
  26527. /* encode Signed FirmwarePkgData */
  26528. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  26529. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26530. testVectors[i].signOID, testVectors[i].hashOID,
  26531. (byte*)testVectors[i].content, testVectors[i].contentSz,
  26532. testVectors[i].signedAttribs,
  26533. testVectors[i].signedAttribsSz, out, outSz);
  26534. if (encodedSz < 0) {
  26535. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26536. wc_PKCS7_Free(pkcs7);
  26537. return -12547;
  26538. }
  26539. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26540. } else if (testVectors[i].encCompFlag == 1) {
  26541. /* encode Signed Encrypted FirmwarePkgData */
  26542. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  26543. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  26544. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26545. testVectors[i].encryptOID, testVectors[i].signOID,
  26546. testVectors[i].hashOID, (byte*)testVectors[i].content,
  26547. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  26548. testVectors[i].unprotectedAttribsSz,
  26549. testVectors[i].signedAttribs,
  26550. testVectors[i].signedAttribsSz, out, outSz);
  26551. if (encodedSz <= 0) {
  26552. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26553. wc_PKCS7_Free(pkcs7);
  26554. return -12548;
  26555. }
  26556. #endif
  26557. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26558. } else if (testVectors[i].encCompFlag == 2) {
  26559. /* encode Signed Compressed FirmwarePkgData */
  26560. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  26561. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26562. testVectors[i].signOID, testVectors[i].hashOID,
  26563. (byte*)testVectors[i].content, testVectors[i].contentSz,
  26564. testVectors[i].signedAttribs,
  26565. testVectors[i].signedAttribsSz, out, outSz);
  26566. if (encodedSz <= 0) {
  26567. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26568. wc_PKCS7_Free(pkcs7);
  26569. return -12549;
  26570. }
  26571. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26572. } else if (testVectors[i].encCompFlag == 3) {
  26573. /* encode Signed Encrypted Compressed FirmwarePkgData */
  26574. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  26575. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  26576. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26577. testVectors[i].encryptOID, testVectors[i].signOID,
  26578. testVectors[i].hashOID, (byte*)testVectors[i].content,
  26579. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  26580. testVectors[i].unprotectedAttribsSz,
  26581. testVectors[i].signedAttribs,
  26582. testVectors[i].signedAttribsSz, out, outSz);
  26583. if (encodedSz <= 0) {
  26584. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26585. wc_PKCS7_Free(pkcs7);
  26586. return -12550;
  26587. }
  26588. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  26589. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26590. } else {
  26591. /* unsupported SignedData single-shot combination */
  26592. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26593. wc_PKCS7_Free(pkcs7);
  26594. return -12551;
  26595. }
  26596. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26597. /* write PKCS#7 to output file for more testing */
  26598. file = XFOPEN(testVectors[i].outFileName, "wb");
  26599. if (!file) {
  26600. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26601. wc_PKCS7_Free(pkcs7);
  26602. return -12552;
  26603. }
  26604. ret = (int)XFWRITE(out, 1, encodedSz, file);
  26605. XFCLOSE(file);
  26606. if (ret != (int)encodedSz) {
  26607. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26608. wc_PKCS7_Free(pkcs7);
  26609. return -12553;
  26610. }
  26611. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26612. wc_PKCS7_Free(pkcs7);
  26613. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26614. if (pkcs7 == NULL)
  26615. return -12554;
  26616. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  26617. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  26618. if (ret < 0) {
  26619. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26620. wc_PKCS7_Free(pkcs7);
  26621. return -12555;
  26622. }
  26623. #ifndef NO_PKCS7_STREAM
  26624. {
  26625. word32 z;
  26626. for (z = 0; z < outSz && ret != 0; z++) {
  26627. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  26628. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  26629. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26630. wc_PKCS7_Free(pkcs7);
  26631. printf("unexpected error %d\n", ret);
  26632. return -12556;
  26633. }
  26634. }
  26635. }
  26636. #endif
  26637. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  26638. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26639. wc_PKCS7_Free(pkcs7);
  26640. return -12557;
  26641. }
  26642. if (testVectors[i].encCompFlag == 0) {
  26643. /* verify decoded content matches expected */
  26644. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  26645. XMEMCMP(pkcs7->content, testVectors[i].content,
  26646. pkcs7->contentSz)) {
  26647. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26648. wc_PKCS7_Free(pkcs7);
  26649. return -12558;
  26650. }
  26651. }
  26652. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26653. else if (testVectors[i].encCompFlag == 1) {
  26654. /* decrypt inner encryptedData */
  26655. pkcs7->encryptionKey = testVectors[i].encryptKey;
  26656. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  26657. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  26658. pkcs7->contentSz, out, outSz);
  26659. if (ret < 0) {
  26660. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26661. wc_PKCS7_Free(pkcs7);
  26662. return -12559;
  26663. }
  26664. /* compare decrypted to expected */
  26665. if (((word32)ret != testVectors[i].contentSz) ||
  26666. XMEMCMP(out, testVectors[i].content, ret)) {
  26667. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26668. wc_PKCS7_Free(pkcs7);
  26669. return -12560;
  26670. }
  26671. }
  26672. #endif
  26673. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26674. else if (testVectors[i].encCompFlag == 2) {
  26675. /* decompress inner compressedData */
  26676. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  26677. pkcs7->contentSz, out, outSz);
  26678. if (ret < 0) {
  26679. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26680. wc_PKCS7_Free(pkcs7);
  26681. return -12561;
  26682. }
  26683. /* compare decompressed to expected */
  26684. if (((word32)ret != testVectors[i].contentSz) ||
  26685. XMEMCMP(out, testVectors[i].content, ret)) {
  26686. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26687. wc_PKCS7_Free(pkcs7);
  26688. return -12562;
  26689. }
  26690. }
  26691. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26692. else if (testVectors[i].encCompFlag == 3) {
  26693. byte* encryptedTmp;
  26694. int encryptedTmpSz;
  26695. encryptedTmpSz = FOURK_BUF;
  26696. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  26697. DYNAMIC_TYPE_TMP_BUFFER);
  26698. if (encryptedTmp == NULL) {
  26699. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26700. wc_PKCS7_Free(pkcs7);
  26701. return -12563;
  26702. }
  26703. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  26704. /* decrypt inner encryptedData */
  26705. pkcs7->encryptionKey = testVectors[i].encryptKey;
  26706. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  26707. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  26708. pkcs7->contentSz, encryptedTmp,
  26709. encryptedTmpSz);
  26710. if (encryptedTmpSz < 0) {
  26711. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26712. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26713. wc_PKCS7_Free(pkcs7);
  26714. return -12564;
  26715. }
  26716. /* decompress inner compressedData */
  26717. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  26718. encryptedTmpSz, out, outSz);
  26719. if (ret < 0) {
  26720. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26721. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26722. wc_PKCS7_Free(pkcs7);
  26723. return -12565;
  26724. }
  26725. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26726. /* compare decompressed to expected */
  26727. if (((word32)ret != testVectors[i].contentSz) ||
  26728. XMEMCMP(out, testVectors[i].content, ret)) {
  26729. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26730. wc_PKCS7_Free(pkcs7);
  26731. return -12566;
  26732. }
  26733. }
  26734. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  26735. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26736. wc_PKCS7_Free(pkcs7);
  26737. }
  26738. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26739. wc_FreeRng(&rng);
  26740. if (ret > 0)
  26741. return 0;
  26742. (void)eccClientCertBuf;
  26743. (void)eccClientCertBufSz;
  26744. (void)eccClientPrivKeyBuf;
  26745. (void)eccClientPrivKeyBufSz;
  26746. (void)rsaClientCertBuf;
  26747. (void)rsaClientCertBufSz;
  26748. (void)rsaClientPrivKeyBuf;
  26749. (void)rsaClientPrivKeyBufSz;
  26750. (void)rsaServerCertBuf;
  26751. (void)rsaServerCertBufSz;
  26752. (void)rsaServerPrivKeyBuf;
  26753. (void)rsaServerPrivKeyBufSz;
  26754. (void)rsaCaCertBuf;
  26755. (void)rsaCaCertBufSz;
  26756. (void)rsaCaPrivKeyBuf;
  26757. (void)rsaCaPrivKeyBufSz;
  26758. return ret;
  26759. }
  26760. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  26761. {
  26762. int ret = 0;
  26763. byte* rsaClientCertBuf = NULL;
  26764. byte* rsaServerCertBuf = NULL;
  26765. byte* rsaCaCertBuf = NULL;
  26766. byte* eccClientCertBuf = NULL;
  26767. byte* rsaClientPrivKeyBuf = NULL;
  26768. byte* rsaServerPrivKeyBuf = NULL;
  26769. byte* rsaCaPrivKeyBuf = NULL;
  26770. byte* eccClientPrivKeyBuf = NULL;
  26771. word32 rsaClientCertBufSz = 0;
  26772. word32 rsaServerCertBufSz = 0;
  26773. word32 rsaCaCertBufSz = 0;
  26774. word32 eccClientCertBufSz = 0;
  26775. word32 rsaClientPrivKeyBufSz = 0;
  26776. word32 rsaServerPrivKeyBufSz = 0;
  26777. word32 rsaCaPrivKeyBufSz = 0;
  26778. word32 eccClientPrivKeyBufSz = 0;
  26779. #ifndef NO_RSA
  26780. /* read client RSA cert and key in DER format */
  26781. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26782. DYNAMIC_TYPE_TMP_BUFFER);
  26783. if (rsaClientCertBuf == NULL)
  26784. ret = -12600;
  26785. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26786. DYNAMIC_TYPE_TMP_BUFFER);
  26787. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  26788. ret = -12601;
  26789. }
  26790. rsaClientCertBufSz = FOURK_BUF;
  26791. rsaClientPrivKeyBufSz = FOURK_BUF;
  26792. /* read server RSA cert and key in DER format */
  26793. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26794. DYNAMIC_TYPE_TMP_BUFFER);
  26795. if (ret == 0 && rsaServerCertBuf == NULL)
  26796. ret = -12602;
  26797. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26798. DYNAMIC_TYPE_TMP_BUFFER);
  26799. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  26800. ret = -12603;
  26801. }
  26802. rsaServerCertBufSz = FOURK_BUF;
  26803. rsaServerPrivKeyBufSz = FOURK_BUF;
  26804. /* read CA RSA cert and key in DER format, for use with server cert */
  26805. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26806. DYNAMIC_TYPE_TMP_BUFFER);
  26807. if (ret == 0 && rsaCaCertBuf == NULL)
  26808. ret = -12604;
  26809. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26810. DYNAMIC_TYPE_TMP_BUFFER);
  26811. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  26812. ret = -12605;
  26813. }
  26814. rsaCaCertBufSz = FOURK_BUF;
  26815. rsaCaPrivKeyBufSz = FOURK_BUF;
  26816. #endif /* NO_RSA */
  26817. #ifdef HAVE_ECC
  26818. /* read client ECC cert and key in DER format */
  26819. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26820. DYNAMIC_TYPE_TMP_BUFFER);
  26821. if (ret == 0 && eccClientCertBuf == NULL) {
  26822. ret = -12606;
  26823. }
  26824. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26825. DYNAMIC_TYPE_TMP_BUFFER);
  26826. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  26827. ret = -12607;
  26828. }
  26829. eccClientCertBufSz = FOURK_BUF;
  26830. eccClientPrivKeyBufSz = FOURK_BUF;
  26831. #endif /* HAVE_ECC */
  26832. if (ret >= 0)
  26833. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  26834. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  26835. rsaServerCertBuf, &rsaServerCertBufSz,
  26836. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  26837. rsaCaCertBuf, &rsaCaCertBufSz,
  26838. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  26839. eccClientCertBuf, &eccClientCertBufSz,
  26840. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  26841. if (ret < 0) {
  26842. ret = -12608;
  26843. }
  26844. if (ret >= 0)
  26845. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  26846. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  26847. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  26848. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  26849. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  26850. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  26851. eccClientCertBuf, (word32)eccClientCertBufSz,
  26852. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  26853. if (ret >= 0)
  26854. ret = pkcs7signed_run_SingleShotVectors(
  26855. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  26856. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  26857. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  26858. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  26859. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  26860. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  26861. eccClientCertBuf, (word32)eccClientCertBufSz,
  26862. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  26863. #ifndef NO_AES
  26864. if (ret >= 0)
  26865. ret = pkcs7callback_test(
  26866. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  26867. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  26868. #endif
  26869. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26870. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26871. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26872. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26873. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26874. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26875. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26876. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26877. return ret;
  26878. }
  26879. #endif /* HAVE_PKCS7 */
  26880. #ifdef HAVE_VALGRIND
  26881. /* Need a static build to have access to symbols. */
  26882. /* Maximum number of bytes in a number to test. */
  26883. #define MP_MAX_TEST_BYTE_LEN 32
  26884. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  26885. {
  26886. byte d[MP_MAX_TEST_BYTE_LEN];
  26887. int ret;
  26888. (void)heap;
  26889. do {
  26890. ret = wc_RNG_GenerateBlock(rng, d, len);
  26891. if (ret != 0)
  26892. return ret;
  26893. ret = mp_read_unsigned_bin(n, d, len);
  26894. if (ret != 0)
  26895. return ret;
  26896. } while (mp_iszero(n));
  26897. return 0;
  26898. }
  26899. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  26900. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  26901. {
  26902. int i, j;
  26903. mp_digit rem;
  26904. mp_digit rem2;
  26905. for (i = 0; i < 10; i++) {
  26906. for (j = 1; j < 10; j++) {
  26907. if (randNum(a, j, rng, NULL) != 0)
  26908. return -12620;
  26909. if (mp_div_3(a, r, &rem) != 0)
  26910. return -12621;
  26911. if (mp_mul_d(r, 3, r) != 0)
  26912. return -12622;
  26913. if (mp_add_d(r, rem, r) != 0)
  26914. return -12623;
  26915. if (mp_cmp(r, a) != MP_EQ)
  26916. return -12624;
  26917. }
  26918. }
  26919. if (mp_div_3(a, r, &rem) != 0)
  26920. return -12625;
  26921. if (mp_div_3(a, a, NULL) != 0)
  26922. return -12626;
  26923. if (mp_cmp(r, a) != MP_EQ)
  26924. return -12627;
  26925. #if defined(WOLFSSL_SP_MATH_ALL)
  26926. if (mp_div_d(a, 10, r, &rem) != 0)
  26927. return -12628;
  26928. if (mp_div_d(a, 10, a, NULL) != 0)
  26929. return -12629;
  26930. if (mp_cmp(r, a) != MP_EQ)
  26931. return -12630;
  26932. if (mp_div_d(a, 12, r, &rem) != 0)
  26933. return -12631;
  26934. if (mp_div_d(a, 12, a, NULL) != 0)
  26935. return -12632;
  26936. if (mp_cmp(r, a) != MP_EQ)
  26937. return -12633;
  26938. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  26939. return -12634;
  26940. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  26941. return -12635;
  26942. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  26943. return -12636;
  26944. if (mp_cmp(r, a) != MP_EQ)
  26945. return -12637;
  26946. if (rem != rem2)
  26947. return -12638;
  26948. #else
  26949. (void)rem2;
  26950. #endif
  26951. return 0;
  26952. }
  26953. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  26954. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  26955. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  26956. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  26957. {
  26958. int ret;
  26959. int i, j;
  26960. int size;
  26961. char str[30];
  26962. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  26963. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  26964. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  26965. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  26966. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  26967. for (i = 0; i < 10; i++) {
  26968. for (j = 2; j < 12; j++) {
  26969. if (randNum(a, j, rng, NULL) != 0)
  26970. return -12640;
  26971. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  26972. return -12641;
  26973. mp_toradix(a, str, MP_RADIX_DEC);
  26974. if ((int)XSTRLEN(str) != size - 1)
  26975. return -12642;
  26976. mp_read_radix(r, str, MP_RADIX_DEC);
  26977. if (mp_cmp(a, r) != MP_EQ)
  26978. return -12643;
  26979. }
  26980. }
  26981. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  26982. return -12644;
  26983. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  26984. return -12645;
  26985. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  26986. return -12646;
  26987. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  26988. return -12647;
  26989. if (!mp_iszero(r))
  26990. return -12648;
  26991. mp_set(r, 1);
  26992. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  26993. return -12649;
  26994. if (!mp_iszero(r))
  26995. return -12650;
  26996. mp_zero(a);
  26997. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  26998. if (ret != 0)
  26999. return -12651;
  27000. if (size != 2)
  27001. return -12652;
  27002. ret = mp_toradix(a, str, MP_RADIX_DEC);
  27003. if (ret != 0)
  27004. return -12653;
  27005. if ((int)XSTRLEN(str) != size - 1)
  27006. return -12654;
  27007. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  27008. if (ret != 0)
  27009. return -12655;
  27010. if (!mp_iszero(r))
  27011. return -12656;
  27012. return 0;
  27013. }
  27014. #endif
  27015. #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC)
  27016. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  27017. {
  27018. int ret;
  27019. int i, j;
  27020. int size;
  27021. char str[30];
  27022. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  27023. static char longStr[2 * sizeof(a->dp) + 2];
  27024. #endif
  27025. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  27026. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  27027. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  27028. for (i = 0; i < 10; i++) {
  27029. for (j = 2; j < 12; j++) {
  27030. if (randNum(a, j, rng, NULL) != 0)
  27031. return -12660;
  27032. mp_radix_size(a, MP_RADIX_HEX, &size);
  27033. mp_toradix(a, str, MP_RADIX_HEX);
  27034. if ((int)XSTRLEN(str) != size - 1)
  27035. return -12661;
  27036. mp_read_radix(r, str, MP_RADIX_HEX);
  27037. if (mp_cmp(a, r) != MP_EQ)
  27038. return -12662;
  27039. }
  27040. }
  27041. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  27042. return -12663;
  27043. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  27044. return -12664;
  27045. mp_set(r, 1);
  27046. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  27047. return -12665;
  27048. if (!mp_iszero(r))
  27049. return -12666;
  27050. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  27051. /* Fixed MP data size - string can be too long. */
  27052. longStr[0] = '8';
  27053. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  27054. longStr[sizeof(longStr)-1] = '\0';
  27055. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  27056. return -12667;
  27057. #endif
  27058. mp_zero(a);
  27059. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  27060. if (ret != 0)
  27061. return -12668;
  27062. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  27063. if (size != 3)
  27064. #else
  27065. if (size != 2)
  27066. #endif
  27067. return -12669;
  27068. ret = mp_toradix(a, str, MP_RADIX_HEX);
  27069. if (ret != 0)
  27070. return -12670;
  27071. if ((int)XSTRLEN(str) != size - 1)
  27072. return -12671;
  27073. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  27074. if (ret != 0)
  27075. return -12672;
  27076. if (!mp_iszero(r))
  27077. return -12673;
  27078. #ifdef WOLFSSL_SP_MATH
  27079. ret = mp_toradix(a, str, 8);
  27080. if (ret != MP_VAL)
  27081. return -12674;
  27082. ret = mp_radix_size(a, 8, &size);
  27083. if (ret != MP_VAL)
  27084. return -12675;
  27085. #endif
  27086. return 0;
  27087. }
  27088. #endif
  27089. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  27090. {
  27091. int i;
  27092. if (randNum(a, 4, rng, NULL) != 0)
  27093. return -12680;
  27094. for (i = 0; i < 4; i++) {
  27095. mp_copy(r1, a);
  27096. if (mp_lshd(r1, i) != MP_OKAY)
  27097. return -12681;
  27098. mp_rshd(r1, i);
  27099. if (mp_cmp(a, r1) != MP_EQ)
  27100. return -12682;
  27101. }
  27102. for (i = 0; i < DIGIT_BIT+1; i++) {
  27103. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  27104. return -12683;
  27105. mp_rshb(r1, i);
  27106. if (mp_cmp(a, r1) != MP_EQ)
  27107. return -12684;
  27108. }
  27109. return 0;
  27110. }
  27111. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  27112. {
  27113. int i, j;
  27114. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  27115. mp_zero(a);
  27116. mp_set_bit(a, i);
  27117. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  27118. return -12690;
  27119. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  27120. mp_zero(r1);
  27121. mp_set_bit(r1, i);
  27122. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  27123. return -12691;
  27124. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  27125. return -12692;
  27126. if (mp_cmp(a, r1) != MP_EQ)
  27127. return -12693;
  27128. }
  27129. }
  27130. mp_zero(r1);
  27131. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  27132. return -12694;
  27133. if (r1->used != 1)
  27134. return -12695;
  27135. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  27136. return -12696;
  27137. if (r1->used != 0)
  27138. return -12697;
  27139. #ifdef WOLFSSL_SP_MATH
  27140. if (mp_set(r1, 1) != MP_OKAY)
  27141. return -12698;
  27142. if (mp_mul_2d(r1, SP_INT_MAX_BITS - 1, r1) != MP_OKAY)
  27143. return -12699;
  27144. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  27145. return -12700;
  27146. if (mp_mul_2d(r1, 1, r1) != MP_OKAY)
  27147. return -12701;
  27148. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  27149. return -12702;
  27150. if (mp_add_d(r1, 1, r1) == MP_OKAY)
  27151. return -12703;
  27152. #endif
  27153. return 0;
  27154. }
  27155. static int mp_test_read_to_bin(mp_int* a)
  27156. {
  27157. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  27158. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  27159. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  27160. };
  27161. byte out[24];
  27162. int i, j, k;
  27163. const byte* p;
  27164. int ret;
  27165. for (i = 0; i < (int)sizeof(in); i++) {
  27166. p = in + sizeof(in) - i;
  27167. ret = mp_read_unsigned_bin(a, p, i);
  27168. if (ret != 0)
  27169. return -12710;
  27170. for (j = i; j < (int)sizeof(out); j++) {
  27171. XMEMSET(out, 0xff, sizeof(out));
  27172. ret = mp_to_unsigned_bin_len(a, out, j);
  27173. if (ret != 0)
  27174. return -12711;
  27175. for (k = 0; k < j - i; k++) {
  27176. if (out[k] != 0)
  27177. return -12712;
  27178. }
  27179. for (; k < j; k++) {
  27180. if (out[k] != p[k - (j - i)])
  27181. return -12713;
  27182. }
  27183. }
  27184. }
  27185. ret = mp_read_unsigned_bin(a, NULL, 0);
  27186. if (ret != 0)
  27187. return -12714;
  27188. if (!mp_iszero(a))
  27189. return -12715;
  27190. return 0;
  27191. }
  27192. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  27193. static int mp_test_set_int(mp_int* a)
  27194. {
  27195. #if SP_ULONG_BITS == 64
  27196. unsigned long n = 0xfedcba9876543210UL;
  27197. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  27198. byte out[8] = { 0 };
  27199. #elif SP_ULONG_BITS == 32
  27200. unsigned long n = 0xfedcba98UL;
  27201. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  27202. byte out[4] = { 0 };
  27203. #elif SP_ULONG_BITS == 16
  27204. unsigned long n = 0xfedc;
  27205. byte exp[2] = { 0xfe, 0xdc };
  27206. byte out[2] = { 0 };
  27207. #elif SP_ULONG_BITS == 8
  27208. unsigned long n = 0xfe;
  27209. byte exp[1] = { 0xfe };
  27210. byte out[1] = { 0 };
  27211. #endif
  27212. int ret;
  27213. ret = mp_set_int(a, n);
  27214. if (ret != 0)
  27215. return -12720;
  27216. ret = mp_unsigned_bin_size(a);
  27217. if (ret != sizeof(exp))
  27218. return -12721;
  27219. ret = mp_to_unsigned_bin(a, out);
  27220. if (ret != 0)
  27221. return -12722;
  27222. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  27223. return -12723;
  27224. return 0;
  27225. }
  27226. #endif
  27227. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  27228. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  27229. {
  27230. byte buffer[16];
  27231. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  27232. char hexStr[] = "abcdef0123456789";
  27233. #ifndef WOLFSSL_SP_INT_NEGATIVE
  27234. char negStr[] = "-1234";
  27235. #endif
  27236. #endif
  27237. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  27238. defined(HAVE_COMP_KEY)
  27239. char decStr[] = "0987654321";
  27240. #endif
  27241. int ret;
  27242. #ifdef WOLFSSL_SP_MATH_ALL
  27243. mp_digit rho;
  27244. int size;
  27245. #endif
  27246. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27247. int result;
  27248. #endif
  27249. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  27250. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  27251. mp_digit rd;
  27252. #endif
  27253. (void)rng;
  27254. (void)r;
  27255. ret = mp_init(NULL);
  27256. if (ret != MP_VAL)
  27257. return -12730;
  27258. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  27259. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  27260. if (ret != MP_OKAY)
  27261. return -12731;
  27262. #endif
  27263. mp_free(NULL);
  27264. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  27265. ret = mp_grow(NULL, 1);
  27266. if (ret != MP_VAL)
  27267. return -12732;
  27268. #ifdef WOLFSSL_SP_MATH
  27269. ret = mp_grow(a, SP_INT_DIGITS + 1);
  27270. if (ret != MP_MEM)
  27271. return -12733;
  27272. #endif
  27273. #endif
  27274. mp_clear(NULL);
  27275. ret = mp_abs(NULL, NULL);
  27276. if (ret != MP_VAL)
  27277. return -12734;
  27278. ret = mp_abs(a, NULL);
  27279. if (ret != MP_VAL)
  27280. return -12735;
  27281. ret = mp_abs(NULL, b);
  27282. if (ret != MP_VAL)
  27283. return -12736;
  27284. ret = mp_unsigned_bin_size(NULL);
  27285. if (ret != 0)
  27286. return -12737;
  27287. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  27288. if (ret != MP_VAL)
  27289. return -12738;
  27290. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  27291. if (ret != MP_VAL)
  27292. return -12739;
  27293. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  27294. if (ret != MP_VAL)
  27295. return -12740;
  27296. ret = mp_read_unsigned_bin(a, buffer,
  27297. (SP_INT_DIGITS - 1) * SP_WORD_SIZEOF + 1);
  27298. if (ret != MP_VAL)
  27299. return -12741;
  27300. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  27301. ret = mp_read_radix(NULL, NULL, 16);
  27302. if (ret != MP_VAL)
  27303. return -12742;
  27304. ret = mp_read_radix(a, NULL, 16);
  27305. if (ret != MP_VAL)
  27306. return -12743;
  27307. ret = mp_read_radix(NULL, hexStr, 16);
  27308. if (ret != MP_VAL)
  27309. return -12744;
  27310. #ifndef WOLFSSL_SP_INT_NEGATIVE
  27311. ret = mp_read_radix(a, negStr, 16);
  27312. if (ret != MP_VAL)
  27313. return -12745;
  27314. #ifdef WOLFSSL_SP_MATH_ALL
  27315. ret = mp_read_radix(a, negStr, 10);
  27316. if (ret != MP_VAL)
  27317. return -12746;
  27318. #endif /* WOLFSSL_SP_MATH_ALL */
  27319. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  27320. #endif
  27321. #ifndef WOLFSSL_SP_MATH_ALL
  27322. /* Radix 10 only supported with ALL. */
  27323. ret = mp_read_radix(a, decStr, 10);
  27324. if (ret != MP_VAL)
  27325. return -12747;
  27326. #endif
  27327. /* Radix 8 not supported SP_INT. */
  27328. ret = mp_read_radix(a, "0123", 8);
  27329. if (ret != MP_VAL)
  27330. return -12748;
  27331. ret = mp_count_bits(NULL);
  27332. if (ret != 0)
  27333. return -12749;
  27334. ret = mp_is_bit_set(NULL, 0);
  27335. if (ret != 0)
  27336. return -12750;
  27337. ret = mp_leading_bit(NULL);
  27338. if (ret != 0)
  27339. return -12751;
  27340. mp_zero(a);
  27341. ret = mp_leading_bit(a);
  27342. if (ret != 0)
  27343. return -12752;
  27344. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27345. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  27346. !defined(NO_RSA)
  27347. ret = mp_set_bit(NULL, 1);
  27348. if (ret != MP_VAL)
  27349. return -12753;
  27350. #endif
  27351. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  27352. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27353. ret = mp_to_unsigned_bin(NULL, NULL);
  27354. if (ret != MP_VAL)
  27355. return -12754;
  27356. ret = mp_to_unsigned_bin(a, NULL);
  27357. if (ret != MP_VAL)
  27358. return -12755;
  27359. ret = mp_to_unsigned_bin(NULL, buffer);
  27360. if (ret != MP_VAL)
  27361. return -12756;
  27362. #endif
  27363. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  27364. if (ret != MP_VAL)
  27365. return -12757;
  27366. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  27367. if (ret != MP_VAL)
  27368. return -12758;
  27369. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  27370. if (ret != MP_VAL)
  27371. return -12759;
  27372. #ifdef WOLFSSL_SP_MATH_ALL
  27373. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  27374. if (ret != MP_VAL)
  27375. return -12760;
  27376. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  27377. if (ret != MP_VAL)
  27378. return -12761;
  27379. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  27380. if (ret != MP_VAL)
  27381. return -12762;
  27382. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  27383. if (ret != MP_OKAY)
  27384. return -12763;
  27385. #endif
  27386. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  27387. ret = mp_copy(NULL, NULL);
  27388. if (ret != MP_VAL)
  27389. return -12764;
  27390. ret = mp_copy(a, NULL);
  27391. if (ret != MP_VAL)
  27392. return -12765;
  27393. ret = mp_copy(NULL, b);
  27394. if (ret != MP_VAL)
  27395. return -12766;
  27396. #endif
  27397. #if defined(WOLFSSL_KEY_GEN)
  27398. ret = sp_2expt(NULL, 1);
  27399. if (ret != MP_VAL)
  27400. return -12767;
  27401. #endif
  27402. ret = mp_set(NULL, 0);
  27403. if (ret != MP_VAL)
  27404. return -12768;
  27405. ret = mp_cmp_d(NULL, 0);
  27406. if (ret != MP_LT)
  27407. return -12769;
  27408. ret = mp_cmp(NULL, NULL);
  27409. if (ret != MP_EQ)
  27410. return -12770;
  27411. ret = mp_cmp(a, NULL);
  27412. if (ret != MP_GT)
  27413. return -12771;
  27414. ret = mp_cmp(NULL, b);
  27415. if (ret != MP_LT)
  27416. return -12772;
  27417. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27418. mp_rshd(NULL, 1);
  27419. #endif
  27420. mp_zero(NULL);
  27421. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  27422. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27423. ret = mp_lshd(NULL, 0);
  27424. if (ret != MP_VAL)
  27425. return -12773;
  27426. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  27427. if (ret != MP_VAL)
  27428. return -12774;
  27429. #endif
  27430. #if defined(WOLFSSL_SP_MATH_ALL)
  27431. ret = mp_div(NULL, NULL, a, b);
  27432. if (ret != MP_VAL)
  27433. return -12775;
  27434. ret = mp_div(a, NULL, a, b);
  27435. if (ret != MP_VAL)
  27436. return -12776;
  27437. ret = mp_div(NULL, b, a, b);
  27438. if (ret != MP_VAL)
  27439. return -12777;
  27440. ret = mp_div(a, b, NULL, NULL);
  27441. if (ret != MP_VAL)
  27442. return -12778;
  27443. #endif
  27444. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  27445. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27446. ret = mp_mod(NULL, NULL, NULL);
  27447. if (ret != MP_VAL)
  27448. return -12779;
  27449. ret = mp_mod(a, NULL, NULL);
  27450. if (ret != MP_VAL)
  27451. return -12780;
  27452. ret = mp_mod(NULL, b, NULL);
  27453. if (ret != MP_VAL)
  27454. return -12781;
  27455. ret = mp_mod(NULL, NULL, r);
  27456. if (ret != MP_VAL)
  27457. return -12782;
  27458. ret = mp_mod(a, b, NULL);
  27459. if (ret != MP_VAL)
  27460. return -12783;
  27461. ret = mp_mod(a, NULL, r);
  27462. if (ret != MP_VAL)
  27463. return -12784;
  27464. ret = mp_mod(NULL, b, r);
  27465. if (ret != MP_VAL)
  27466. return -12785;
  27467. #endif
  27468. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  27469. ret = mp_set_int(NULL, 0);
  27470. if (ret != MP_VAL)
  27471. return -12786;
  27472. #endif
  27473. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  27474. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  27475. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  27476. if (ret != MP_VAL)
  27477. return 9950;
  27478. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  27479. if (ret != MP_VAL)
  27480. return 9951;
  27481. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  27482. if (ret != MP_VAL)
  27483. return 9952;
  27484. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  27485. if (ret != MP_VAL)
  27486. return 9953;
  27487. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  27488. if (ret != MP_VAL)
  27489. return 9954;
  27490. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  27491. if (ret != MP_VAL)
  27492. return 9955;
  27493. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  27494. if (ret != MP_VAL)
  27495. return 9956;
  27496. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  27497. if (ret != MP_VAL)
  27498. return 9957;
  27499. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  27500. if (ret != MP_VAL)
  27501. return 9958;
  27502. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  27503. if (ret != MP_VAL)
  27504. return 9960;
  27505. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  27506. if (ret != MP_VAL)
  27507. return 9961;
  27508. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  27509. if (ret != MP_VAL)
  27510. return 9962;
  27511. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  27512. if (ret != MP_VAL)
  27513. return 9963;
  27514. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  27515. if (ret != MP_VAL)
  27516. return 9964;
  27517. ret = mp_exptmod_nct(a, a, a, NULL);
  27518. if (ret != MP_VAL)
  27519. return 9965;
  27520. ret = mp_exptmod_nct(a, a, NULL, a);
  27521. if (ret != MP_VAL)
  27522. return 9966;
  27523. ret = mp_exptmod_nct(a, NULL, a, a);
  27524. if (ret != MP_VAL)
  27525. return 9967;
  27526. ret = mp_exptmod_nct(NULL, a, a, a);
  27527. if (ret != MP_VAL)
  27528. return 9968;
  27529. #endif
  27530. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  27531. !defined(WC_NO_RNG)
  27532. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  27533. if (ret != MP_VAL)
  27534. return -12787;
  27535. ret = mp_rand_prime(a, 32, NULL, NULL);
  27536. if (ret != MP_VAL)
  27537. return -12788;
  27538. ret = mp_rand_prime(NULL, 32, rng, NULL);
  27539. if (ret != MP_VAL)
  27540. return -12789;
  27541. ret = mp_rand_prime(a, 0, rng, NULL);
  27542. if (ret != MP_VAL)
  27543. return -9969;
  27544. #endif
  27545. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27546. ret = mp_mul(NULL, NULL, NULL);
  27547. if (ret != MP_VAL)
  27548. return -12790;
  27549. ret = mp_mul(a, NULL, NULL);
  27550. if (ret != MP_VAL)
  27551. return -12791;
  27552. ret = mp_mul(NULL, b, NULL);
  27553. if (ret != MP_VAL)
  27554. return -12792;
  27555. ret = mp_mul(NULL, NULL, r);
  27556. if (ret != MP_VAL)
  27557. return -12793;
  27558. ret = mp_mul(a, b, NULL);
  27559. if (ret != MP_VAL)
  27560. return -12794;
  27561. ret = mp_mul(a, NULL, r);
  27562. if (ret != MP_VAL)
  27563. return -12795;
  27564. ret = mp_mul(NULL, b, r);
  27565. if (ret != MP_VAL)
  27566. return -12796;
  27567. #endif
  27568. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27569. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27570. ret = mp_sqr(NULL, NULL);
  27571. if (ret != MP_VAL)
  27572. return -12797;
  27573. ret = mp_sqr(a, NULL);
  27574. if (ret != MP_VAL)
  27575. return -12798;
  27576. ret = mp_sqr(NULL, r);
  27577. if (ret != MP_VAL)
  27578. return -12799;
  27579. #endif
  27580. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27581. ret = mp_sqrmod(NULL, NULL, NULL);
  27582. if (ret != MP_VAL)
  27583. return -12800;
  27584. ret = mp_sqrmod(a, NULL, NULL);
  27585. if (ret != MP_VAL)
  27586. return -12801;
  27587. ret = mp_sqrmod(NULL, a, NULL);
  27588. if (ret != MP_VAL)
  27589. return -12802;
  27590. ret = mp_sqrmod(NULL, NULL, a);
  27591. if (ret != MP_VAL)
  27592. return -12803;
  27593. ret = mp_sqrmod(a, b, NULL);
  27594. if (ret != MP_VAL)
  27595. return -12804;
  27596. ret = mp_sqrmod(a, NULL, b);
  27597. if (ret != MP_VAL)
  27598. return -12805;
  27599. ret = mp_sqrmod(NULL, a, b);
  27600. if (ret != MP_VAL)
  27601. return -12806;
  27602. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  27603. if (ret != MP_VAL)
  27604. return -12807;
  27605. ret = mp_mulmod(a, NULL, NULL, NULL);
  27606. if (ret != MP_VAL)
  27607. return -12808;
  27608. ret = mp_mulmod(NULL, a, NULL, NULL);
  27609. if (ret != MP_VAL)
  27610. return -12809;
  27611. ret = mp_mulmod(NULL, NULL, a, NULL);
  27612. if (ret != MP_VAL)
  27613. return -12810;
  27614. ret = mp_mulmod(NULL, NULL, NULL, a);
  27615. if (ret != MP_VAL)
  27616. return -12811;
  27617. ret = mp_mulmod(a, b, b, NULL);
  27618. if (ret != MP_VAL)
  27619. return -12812;
  27620. ret = mp_mulmod(a, b, NULL, a);
  27621. if (ret != MP_VAL)
  27622. return -12813;
  27623. ret = mp_mulmod(a, NULL, b, a);
  27624. if (ret != MP_VAL)
  27625. return -12814;
  27626. ret = mp_mulmod(NULL, b, b, a);
  27627. if (ret != MP_VAL)
  27628. return -12815;
  27629. #endif
  27630. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  27631. !defined(NO_RSA) || !defined(NO_DSA)
  27632. ret = mp_add_d(NULL, 1, NULL);
  27633. if (ret != MP_VAL)
  27634. return -12816;
  27635. ret = mp_add_d(a, 1, NULL);
  27636. if (ret != MP_VAL)
  27637. return -12817;
  27638. ret = mp_add_d(NULL, 1, b);
  27639. if (ret != MP_VAL)
  27640. return -12818;
  27641. #endif
  27642. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  27643. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  27644. ret = mp_sub_d(NULL, 1, NULL);
  27645. if (ret != MP_VAL)
  27646. return -12819;
  27647. ret = mp_sub_d(a, 1, NULL);
  27648. if (ret != MP_VAL)
  27649. return -12820;
  27650. ret = mp_sub_d(NULL, 1, b);
  27651. if (ret != MP_VAL)
  27652. return -12821;
  27653. #endif
  27654. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27655. (defined(HAVE_ECC) && defined(FP_ECC))
  27656. ret = mp_div_d(NULL, 0, NULL, NULL);
  27657. if (ret != MP_VAL)
  27658. return -12822;
  27659. ret = mp_div_d(a, 0, NULL, NULL);
  27660. if (ret != MP_VAL)
  27661. return -12823;
  27662. ret = mp_div_d(NULL, 1, NULL, NULL);
  27663. if (ret != MP_VAL)
  27664. return -12824;
  27665. #endif
  27666. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  27667. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  27668. ret = mp_mod_d(NULL, 0, NULL);
  27669. if (ret != MP_VAL)
  27670. return -12825;
  27671. ret = mp_mod_d(a, 0, NULL);
  27672. if (ret != MP_VAL)
  27673. return -12826;
  27674. ret = mp_mod_d(NULL, 0, &rd);
  27675. if (ret != MP_VAL)
  27676. return -12827;
  27677. #endif
  27678. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27679. (defined(HAVE_ECC) && defined(FP_ECC))
  27680. ret = mp_gcd(NULL, NULL, NULL);
  27681. if (ret != MP_VAL)
  27682. return -12828;
  27683. ret = mp_gcd(a, NULL, NULL);
  27684. if (ret != MP_VAL)
  27685. return -12829;
  27686. ret = mp_gcd(NULL, a, NULL);
  27687. if (ret != MP_VAL)
  27688. return -12830;
  27689. ret = mp_gcd(NULL, NULL, a);
  27690. if (ret != MP_VAL)
  27691. return -12831;
  27692. ret = mp_gcd(a, b, NULL);
  27693. if (ret != MP_VAL)
  27694. return -12832;
  27695. ret = mp_gcd(a, NULL, b);
  27696. if (ret != MP_VAL)
  27697. return -12833;
  27698. ret = mp_gcd(NULL, a, b);
  27699. if (ret != MP_VAL)
  27700. return -12834;
  27701. #endif
  27702. #ifdef HAVE_ECC
  27703. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  27704. if (ret != MP_VAL)
  27705. return -12835;
  27706. ret = mp_div_2_mod_ct(a, NULL, NULL);
  27707. if (ret != MP_VAL)
  27708. return -12836;
  27709. ret = mp_div_2_mod_ct(NULL, b, NULL);
  27710. if (ret != MP_VAL)
  27711. return -12837;
  27712. ret = mp_div_2_mod_ct(NULL, NULL, a);
  27713. if (ret != MP_VAL)
  27714. return -12838;
  27715. ret = mp_div_2_mod_ct(a, b, NULL);
  27716. if (ret != MP_VAL)
  27717. return -12839;
  27718. ret = mp_div_2_mod_ct(a, b, NULL);
  27719. if (ret != MP_VAL)
  27720. return -12840;
  27721. ret = mp_div_2_mod_ct(NULL, b, a);
  27722. if (ret != MP_VAL)
  27723. return -12841;
  27724. ret = mp_div_2(NULL, NULL);
  27725. if (ret != MP_VAL)
  27726. return -12842;
  27727. ret = mp_div_2(a, NULL);
  27728. if (ret != MP_VAL)
  27729. return -12843;
  27730. ret = mp_div_2(NULL, a);
  27731. if (ret != MP_VAL)
  27732. return -12844;
  27733. #endif
  27734. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  27735. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  27736. ret = mp_invmod(NULL, NULL, NULL);
  27737. if (ret != MP_VAL)
  27738. return -12845;
  27739. ret = mp_invmod(a, NULL, NULL);
  27740. if (ret != MP_VAL)
  27741. return -12846;
  27742. ret = mp_invmod(NULL, b, NULL);
  27743. if (ret != MP_VAL)
  27744. return -12847;
  27745. ret = mp_invmod(NULL, NULL, a);
  27746. if (ret != MP_VAL)
  27747. return -12848;
  27748. ret = mp_invmod(a, b, NULL);
  27749. if (ret != MP_VAL)
  27750. return -12849;
  27751. ret = mp_invmod(a, NULL, a);
  27752. if (ret != MP_VAL)
  27753. return -12850;
  27754. ret = mp_invmod(NULL, b, a);
  27755. if (ret != MP_VAL)
  27756. return -12851;
  27757. #endif
  27758. #ifdef HAVE_ECC
  27759. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  27760. if (ret != MP_VAL)
  27761. return -12852;
  27762. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  27763. if (ret != MP_VAL)
  27764. return -12853;
  27765. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  27766. if (ret != MP_VAL)
  27767. return -12854;
  27768. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  27769. if (ret != MP_VAL)
  27770. return -12855;
  27771. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  27772. if (ret != MP_VAL)
  27773. return -12856;
  27774. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  27775. if (ret != MP_VAL)
  27776. return -12857;
  27777. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  27778. if (ret != MP_VAL)
  27779. return -12858;
  27780. #endif
  27781. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  27782. ret = mp_lcm(NULL, NULL, NULL);
  27783. if (ret != MP_VAL)
  27784. return -12859;
  27785. ret = mp_lcm(a, NULL, NULL);
  27786. if (ret != MP_VAL)
  27787. return -12860;
  27788. ret = mp_lcm(NULL, b, NULL);
  27789. if (ret != MP_VAL)
  27790. return -12861;
  27791. ret = mp_lcm(NULL, NULL, a);
  27792. if (ret != MP_VAL)
  27793. return -12862;
  27794. ret = mp_lcm(a, b, NULL);
  27795. if (ret != MP_VAL)
  27796. return -12863;
  27797. ret = mp_lcm(a, NULL, a);
  27798. if (ret != MP_VAL)
  27799. return -12864;
  27800. ret = mp_lcm(NULL, b, a);
  27801. if (ret != MP_VAL)
  27802. return -12865;
  27803. #endif
  27804. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27805. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  27806. if (ret != MP_VAL)
  27807. return -12866;
  27808. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  27809. if (ret != MP_VAL)
  27810. return -12867;
  27811. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  27812. if (ret != MP_VAL)
  27813. return -12868;
  27814. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  27815. if (ret != MP_VAL)
  27816. return -12869;
  27817. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  27818. if (ret != MP_VAL)
  27819. return -12870;
  27820. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  27821. if (ret != MP_VAL)
  27822. return -12871;
  27823. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  27824. if (ret != MP_VAL)
  27825. return -12872;
  27826. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  27827. if (ret != MP_VAL)
  27828. return -12873;
  27829. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  27830. if (ret != MP_VAL)
  27831. return -12874;
  27832. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  27833. if (ret != MP_VAL)
  27834. return -12875;
  27835. ret = mp_exptmod(a, NULL, NULL, NULL);
  27836. if (ret != MP_VAL)
  27837. return -12876;
  27838. ret = mp_exptmod(NULL, b, NULL, NULL);
  27839. if (ret != MP_VAL)
  27840. return -12877;
  27841. ret = mp_exptmod(NULL, NULL, b, NULL);
  27842. if (ret != MP_VAL)
  27843. return -12878;
  27844. ret = mp_exptmod(NULL, NULL, NULL, a);
  27845. if (ret != MP_VAL)
  27846. return -12879;
  27847. ret = mp_exptmod(a, b, b, NULL);
  27848. if (ret != MP_VAL)
  27849. return -12880;
  27850. ret = mp_exptmod(a, b, NULL, a);
  27851. if (ret != MP_VAL)
  27852. return -12881;
  27853. ret = mp_exptmod(a, NULL, b, a);
  27854. if (ret != MP_VAL)
  27855. return -12882;
  27856. ret = mp_exptmod(NULL, b, b, a);
  27857. if (ret != MP_VAL)
  27858. return -12883;
  27859. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  27860. if (ret != MP_VAL)
  27861. return -12884;
  27862. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  27863. if (ret != MP_VAL)
  27864. return -12885;
  27865. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  27866. if (ret != MP_VAL)
  27867. return -12886;
  27868. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  27869. if (ret != MP_VAL)
  27870. return -12887;
  27871. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  27872. if (ret != MP_VAL)
  27873. return -12888;
  27874. ret = mp_exptmod_nct(a, b, b, NULL);
  27875. if (ret != MP_VAL)
  27876. return -12889;
  27877. ret = mp_exptmod_nct(a, b, NULL, a);
  27878. if (ret != MP_VAL)
  27879. return -12890;
  27880. ret = mp_exptmod_nct(a, NULL, b, a);
  27881. if (ret != MP_VAL)
  27882. return -12891;
  27883. ret = mp_exptmod_nct(NULL, b, b, a);
  27884. if (ret != MP_VAL)
  27885. return -12892;
  27886. #endif
  27887. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  27888. ret = mp_cnt_lsb(NULL);
  27889. if (ret != 0)
  27890. return -12893;
  27891. #endif
  27892. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27893. ret = mp_prime_is_prime(NULL, 1, NULL);
  27894. if (ret != MP_VAL)
  27895. return -12894;
  27896. ret = mp_prime_is_prime(a, 1, NULL);
  27897. if (ret != MP_VAL)
  27898. return -12895;
  27899. ret = mp_prime_is_prime(NULL, 1, &result);
  27900. if (ret != MP_VAL)
  27901. return -12896;
  27902. ret = mp_prime_is_prime(a, 0, &result);
  27903. if (ret != MP_VAL)
  27904. return -12897;
  27905. ret = mp_prime_is_prime(a, 1024, &result);
  27906. if (ret != MP_VAL)
  27907. return -12898;
  27908. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  27909. if (ret != MP_VAL)
  27910. return -12899;
  27911. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  27912. if (ret != MP_VAL)
  27913. return -12900;
  27914. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  27915. if (ret != MP_VAL)
  27916. return -12901;
  27917. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  27918. if (ret != MP_VAL)
  27919. return -12902;
  27920. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  27921. if (ret != MP_VAL)
  27922. return -12903;
  27923. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  27924. if (ret != MP_VAL)
  27925. return -12904;
  27926. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  27927. if (ret != MP_VAL)
  27928. return -12905;
  27929. #endif
  27930. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  27931. ret = mp_exch(NULL, NULL);
  27932. if (ret != MP_VAL)
  27933. return -12906;
  27934. ret = mp_exch(a, NULL);
  27935. if (ret != MP_VAL)
  27936. return -12907;
  27937. ret = mp_exch(NULL, b);
  27938. if (ret != MP_VAL)
  27939. return -12908;
  27940. #endif
  27941. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  27942. defined(WOLFSSL_SP_MATH_ALL)
  27943. ret = mp_mul_d(NULL, 1, NULL);
  27944. if (ret != MP_VAL)
  27945. return -12909;
  27946. ret = mp_mul_d(a, 1, NULL);
  27947. if (ret != MP_VAL)
  27948. return -12910;
  27949. ret = mp_mul_d(NULL, 1, b);
  27950. if (ret != MP_VAL)
  27951. return -12911;
  27952. #endif
  27953. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27954. ret = mp_add(NULL, NULL, NULL);
  27955. if (ret != MP_VAL)
  27956. return -12912;
  27957. ret = mp_add(a, NULL, NULL);
  27958. if (ret != MP_VAL)
  27959. return -12913;
  27960. ret = mp_add(NULL, b, NULL);
  27961. if (ret != MP_VAL)
  27962. return -12914;
  27963. ret = mp_add(NULL, NULL, r);
  27964. if (ret != MP_VAL)
  27965. return -12915;
  27966. ret = mp_add(a, b, NULL);
  27967. if (ret != MP_VAL)
  27968. return -12916;
  27969. ret = mp_add(a, NULL, r);
  27970. if (ret != MP_VAL)
  27971. return -12917;
  27972. ret = mp_add(NULL, b, r);
  27973. if (ret != MP_VAL)
  27974. return -12918;
  27975. #endif
  27976. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  27977. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27978. ret = mp_sub(NULL, NULL, NULL);
  27979. if (ret != MP_VAL)
  27980. return -12919;
  27981. ret = mp_sub(a, NULL, NULL);
  27982. if (ret != MP_VAL)
  27983. return -12920;
  27984. ret = mp_sub(NULL, b, NULL);
  27985. if (ret != MP_VAL)
  27986. return -12921;
  27987. ret = mp_sub(NULL, NULL, r);
  27988. if (ret != MP_VAL)
  27989. return -12922;
  27990. ret = mp_sub(a, b, NULL);
  27991. if (ret != MP_VAL)
  27992. return -12923;
  27993. ret = mp_sub(a, NULL, r);
  27994. if (ret != MP_VAL)
  27995. return -12924;
  27996. ret = mp_sub(NULL, b, r);
  27997. if (ret != MP_VAL)
  27998. return -12925;
  27999. #endif
  28000. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  28001. defined(WOLFSSL_CUSTOM_CURVES))
  28002. ret = mp_addmod(NULL, NULL, NULL, NULL);
  28003. if (ret != MP_VAL)
  28004. return -12926;
  28005. ret = mp_addmod(a, NULL, NULL, NULL);
  28006. if (ret != MP_VAL)
  28007. return -12927;
  28008. ret = mp_addmod(NULL, b, NULL, NULL);
  28009. if (ret != MP_VAL)
  28010. return -12928;
  28011. ret = mp_addmod(NULL, NULL, b, NULL);
  28012. if (ret != MP_VAL)
  28013. return -12929;
  28014. ret = mp_addmod(NULL, NULL, NULL, a);
  28015. if (ret != MP_VAL)
  28016. return -12930;
  28017. ret = mp_addmod(a, b, b, NULL);
  28018. if (ret != MP_VAL)
  28019. return -12931;
  28020. ret = mp_addmod(a, b, NULL, a);
  28021. if (ret != MP_VAL)
  28022. return -12932;
  28023. ret = mp_addmod(a, NULL, b, a);
  28024. if (ret != MP_VAL)
  28025. return -12933;
  28026. ret = mp_addmod(NULL, b, b, a);
  28027. if (ret != MP_VAL)
  28028. return -12934;
  28029. #endif
  28030. #ifdef WOLFSSL_SP_MATH_ALL
  28031. ret = mp_submod(NULL, NULL, NULL, NULL);
  28032. if (ret != MP_VAL)
  28033. return -12935;
  28034. ret = mp_submod(a, NULL, NULL, NULL);
  28035. if (ret != MP_VAL)
  28036. return -12936;
  28037. ret = mp_submod(NULL, b, NULL, NULL);
  28038. if (ret != MP_VAL)
  28039. return -12937;
  28040. ret = mp_submod(NULL, NULL, b, NULL);
  28041. if (ret != MP_VAL)
  28042. return -12938;
  28043. ret = mp_submod(NULL, NULL, NULL, a);
  28044. if (ret != MP_VAL)
  28045. return -12939;
  28046. ret = mp_submod(a, b, b, NULL);
  28047. if (ret != MP_VAL)
  28048. return -12940;
  28049. ret = mp_submod(a, b, NULL, a);
  28050. if (ret != MP_VAL)
  28051. return -12941;
  28052. ret = mp_submod(a, NULL, b, a);
  28053. if (ret != MP_VAL)
  28054. return -12942;
  28055. ret = mp_submod(NULL, b, b, a);
  28056. if (ret != MP_VAL)
  28057. return -12943;
  28058. #endif
  28059. #ifdef WOLFSSL_SP_MATH_ALL
  28060. ret = mp_div_2d(NULL, 1, a, b);
  28061. if (ret != MP_VAL)
  28062. return -12944;
  28063. ret = mp_mod_2d(NULL, 1, NULL);
  28064. if (ret != MP_VAL)
  28065. return -12945;
  28066. ret = mp_mod_2d(a, 1, NULL);
  28067. if (ret != MP_VAL)
  28068. return -12946;
  28069. ret = mp_mod_2d(NULL, 1, b);
  28070. if (ret != MP_VAL)
  28071. return -12947;
  28072. ret = mp_mul_2d(NULL, 1, NULL);
  28073. if (ret != MP_VAL)
  28074. return -12948;
  28075. ret = mp_mul_2d(a, 1, NULL);
  28076. if (ret != MP_VAL)
  28077. return -12949;
  28078. ret = mp_mul_2d(NULL, 1, b);
  28079. if (ret != MP_VAL)
  28080. return -12950;
  28081. #endif
  28082. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28083. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  28084. ret = mp_montgomery_reduce(NULL, NULL, 1);
  28085. if (ret != MP_VAL)
  28086. return -12951;
  28087. ret = mp_montgomery_reduce(a, NULL, 1);
  28088. if (ret != MP_VAL)
  28089. return -12952;
  28090. ret = mp_montgomery_reduce(NULL, b, 1);
  28091. if (ret != MP_VAL)
  28092. return -12953;
  28093. mp_zero(b);
  28094. ret = mp_montgomery_reduce(a, b, 1);
  28095. if (ret != MP_VAL)
  28096. return -12954;
  28097. #endif
  28098. #ifdef WOLFSSL_SP_MATH_ALL
  28099. ret = mp_montgomery_setup(NULL, NULL);
  28100. if (ret != MP_VAL)
  28101. return -12955;
  28102. ret = mp_montgomery_setup(a, NULL);
  28103. if (ret != MP_VAL)
  28104. return -12956;
  28105. ret = mp_montgomery_setup(NULL, &rho);
  28106. if (ret != MP_VAL)
  28107. return -12957;
  28108. ret = mp_montgomery_calc_normalization(NULL, NULL);
  28109. if (ret != MP_VAL)
  28110. return -12958;
  28111. ret = mp_montgomery_calc_normalization(a, NULL);
  28112. if (ret != MP_VAL)
  28113. return -12959;
  28114. ret = mp_montgomery_calc_normalization(NULL, b);
  28115. if (ret != MP_VAL)
  28116. return -12960;
  28117. #endif
  28118. ret = mp_unsigned_bin_size(NULL);
  28119. if (ret != 0)
  28120. return -12961;
  28121. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  28122. ret = mp_tohex(NULL, NULL);
  28123. if (ret != MP_VAL)
  28124. return -12962;
  28125. ret = mp_tohex(a, NULL);
  28126. if (ret != MP_VAL)
  28127. return -12963;
  28128. ret = mp_tohex(NULL, hexStr);
  28129. if (ret != MP_VAL)
  28130. return -12964;
  28131. #endif
  28132. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  28133. ret = mp_todecimal(NULL, NULL);
  28134. if (ret != MP_VAL)
  28135. return -12965;
  28136. ret = mp_todecimal(a, NULL);
  28137. if (ret != MP_VAL)
  28138. return -12966;
  28139. ret = mp_todecimal(NULL, decStr);
  28140. if (ret != MP_VAL)
  28141. return -12967;
  28142. #endif
  28143. #ifdef WOLFSSL_SP_MATH_ALL
  28144. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  28145. if (ret != MP_VAL)
  28146. return -12968;
  28147. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  28148. if (ret != MP_VAL)
  28149. return -12969;
  28150. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  28151. if (ret != MP_VAL)
  28152. return -12970;
  28153. ret = mp_toradix(a, hexStr, 3);
  28154. if (ret != MP_VAL)
  28155. return -12971;
  28156. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  28157. if (ret != MP_VAL)
  28158. return -12972;
  28159. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  28160. if (ret != MP_VAL)
  28161. return -12973;
  28162. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  28163. if (ret != MP_VAL)
  28164. return -12974;
  28165. ret = mp_radix_size(a, 3, &size);
  28166. if (ret != MP_VAL)
  28167. return -12975;
  28168. #endif
  28169. return 0;
  28170. }
  28171. #endif
  28172. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28173. static int mp_test_set_is_bit(mp_int* a)
  28174. {
  28175. int i, j;
  28176. mp_zero(a);
  28177. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28178. if (mp_is_bit_set(a, i))
  28179. return -12980;
  28180. for (j = 0; j < i; j++) {
  28181. if (!mp_is_bit_set(a, j))
  28182. return -12981;
  28183. }
  28184. if (mp_set_bit(a, i) != 0)
  28185. return -12982;
  28186. if (!mp_is_bit_set(a, i))
  28187. return -12983;
  28188. }
  28189. mp_zero(a);
  28190. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28191. if (mp_is_bit_set(a, i))
  28192. return -12984;
  28193. }
  28194. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28195. mp_zero(a);
  28196. if (mp_set_bit(a, i) != 0)
  28197. return -12985;
  28198. for (j = 0; j < i; j++) {
  28199. if (mp_is_bit_set(a, j))
  28200. return -12986;
  28201. }
  28202. if (!mp_is_bit_set(a, i))
  28203. return -12987;
  28204. }
  28205. #ifdef WOLFSSL_KEY_GEN
  28206. for (i = 0; i < DIGIT_BIT * 2; i++) {
  28207. mp_set(a, 1);
  28208. if (mp_2expt(a, i) != 0)
  28209. return -12988;
  28210. for (j = 0; j < i; j++) {
  28211. if (mp_is_bit_set(a, j))
  28212. return -12989;
  28213. }
  28214. if (!mp_is_bit_set(a, i))
  28215. return -12990;
  28216. }
  28217. #endif
  28218. #ifdef WOLFSSL_SP_MATH
  28219. mp_zero(a);
  28220. for (j = 1; j <= 3; j++) {
  28221. i = SP_INT_MAX_BITS - j;
  28222. if (mp_is_bit_set(a, i))
  28223. return -12991;
  28224. if (mp_set_bit(a, i) != 0)
  28225. return -12992;
  28226. if (!mp_is_bit_set(a, i))
  28227. return -12993;
  28228. #ifdef WOLFSSL_KEY_GEN
  28229. if (mp_2expt(a, i) != 0)
  28230. return -12994;
  28231. if (!mp_is_bit_set(a, i))
  28232. return -12995;
  28233. #endif
  28234. }
  28235. mp_zero(a);
  28236. for (j = 0; j <= 3; j++) {
  28237. i = SP_INT_MAX_BITS + j;
  28238. if (mp_is_bit_set(a, i))
  28239. return -12996;
  28240. if (mp_set_bit(a, i) != MP_VAL)
  28241. return -12997;
  28242. #ifdef WOLFSSL_KEY_GEN
  28243. if (mp_2expt(a, i) != MP_VAL)
  28244. return -12998;
  28245. #endif
  28246. }
  28247. #endif
  28248. return 0;
  28249. }
  28250. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  28251. static int mp_test_cmp(mp_int* a, mp_int* b)
  28252. {
  28253. int ret;
  28254. mp_zero(a);
  28255. mp_zero(b);
  28256. ret = mp_cmp_d(a, 0);
  28257. if (ret != MP_EQ)
  28258. return -13000;
  28259. ret = mp_cmp_d(a, 1);
  28260. if (ret != MP_LT)
  28261. return -13001;
  28262. ret = mp_cmp(a, b);
  28263. if (ret != MP_EQ)
  28264. return -13002;
  28265. mp_set(a, 1);
  28266. ret = mp_cmp_d(a, 0);
  28267. if (ret != MP_GT)
  28268. return -13003;
  28269. ret = mp_cmp_d(a, 1);
  28270. if (ret != MP_EQ)
  28271. return -13004;
  28272. ret = mp_cmp_d(a, 2);
  28273. if (ret != MP_LT)
  28274. return -13005;
  28275. ret = mp_cmp(a, b);
  28276. if (ret != MP_GT)
  28277. return -13006;
  28278. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  28279. ret = mp_cmp_d(b, -1);
  28280. if (ret != MP_GT)
  28281. return -13007;
  28282. ret = mp_cmp(a, b);
  28283. if (ret != MP_LT)
  28284. return -13008;
  28285. ret = mp_cmp(b, a);
  28286. if (ret != MP_GT)
  28287. return -13009;
  28288. ret = mp_cmp(b, b);
  28289. if (ret != MP_EQ)
  28290. return -13010;
  28291. return 0;
  28292. }
  28293. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  28294. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  28295. {
  28296. int ret;
  28297. int i, j, k;
  28298. for (i = 0; i < 10; i++) {
  28299. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  28300. ret = randNum(a, j, rng, NULL);
  28301. if (ret != MP_OKAY)
  28302. return -13020;
  28303. mp_copy(a, b);
  28304. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  28305. ret = mp_mul_2d(a, k, a);
  28306. if (ret != MP_OKAY)
  28307. return -13021;
  28308. mp_rshb(a, k);
  28309. if (mp_cmp(a, b) != MP_EQ)
  28310. return -13022;
  28311. }
  28312. }
  28313. }
  28314. for (i = 0; i < 10; i++) {
  28315. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  28316. ret = randNum(a, j, rng, NULL);
  28317. if (ret != MP_OKAY)
  28318. return -13023;
  28319. mp_copy(a, b);
  28320. for (k = 0; k < 10; k++) {
  28321. ret = mp_lshd(a, k);
  28322. if (ret != MP_OKAY)
  28323. return -13024;
  28324. mp_rshd(a, k);
  28325. if (mp_cmp(a, b) != MP_EQ)
  28326. return -13025;
  28327. }
  28328. }
  28329. }
  28330. mp_zero(a);
  28331. mp_rshd(a, 1);
  28332. if (!mp_iszero(a))
  28333. return -13026;
  28334. mp_set(a, 1);
  28335. mp_rshd(a, 1);
  28336. if (!mp_iszero(a))
  28337. return -13027;
  28338. mp_set(a, 1);
  28339. mp_rshd(a, 2);
  28340. if (!mp_iszero(a))
  28341. return -13028;
  28342. return 0;
  28343. }
  28344. #endif
  28345. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  28346. WC_RNG* rng)
  28347. {
  28348. int ret;
  28349. int i, j, k;
  28350. mp_zero(a);
  28351. mp_zero(d);
  28352. ret = mp_div(a, d, r, rem);
  28353. if (ret != MP_VAL)
  28354. return -13030;
  28355. mp_set(d, 1);
  28356. ret = mp_div(a, d, r, rem);
  28357. if (ret != MP_OKAY)
  28358. return -13031;
  28359. if (!mp_iszero(r))
  28360. return -13032;
  28361. if (!mp_iszero(rem))
  28362. return -13033;
  28363. mp_set(a, 1);
  28364. ret = mp_div(a, d, r, rem);
  28365. if (ret != MP_OKAY)
  28366. return -13034;
  28367. if (!mp_isone(r))
  28368. return -13035;
  28369. if (!mp_iszero(rem))
  28370. return -13036;
  28371. for (i = 0; i < 100; i++) {
  28372. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  28373. ret = randNum(d, j, rng, NULL);
  28374. if (ret != MP_OKAY)
  28375. return -13037;
  28376. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  28377. ret = randNum(a, k, rng, NULL);
  28378. if (ret != MP_OKAY)
  28379. return -13038;
  28380. ret = mp_div(a, d, NULL, rem);
  28381. if (ret != MP_OKAY)
  28382. return -13039;
  28383. ret = mp_div(a, d, r, NULL);
  28384. if (ret != MP_OKAY)
  28385. return -13040;
  28386. ret = mp_div(a, d, r, rem);
  28387. if (ret != MP_OKAY)
  28388. return -13041;
  28389. mp_mul(r, d, r);
  28390. mp_add(r, rem, r);
  28391. if (mp_cmp(r, a) != MP_EQ)
  28392. return -13042;
  28393. }
  28394. }
  28395. }
  28396. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  28397. if (ret != MP_OKAY)
  28398. return -13043;
  28399. mp_add(d, d, a);
  28400. mp_set(rem, 1);
  28401. mp_div(a, d, NULL, rem);
  28402. if (ret != MP_OKAY)
  28403. return -13044;
  28404. if (!mp_iszero(rem))
  28405. return -13045;
  28406. mp_set(r, 1);
  28407. mp_div(a, d, r, NULL);
  28408. if (ret != MP_OKAY)
  28409. return -13046;
  28410. if (mp_cmp_d(r, 2) != MP_EQ)
  28411. return -13047;
  28412. mp_set(r, 1);
  28413. mp_set(rem, 1);
  28414. mp_div(a, d, r, rem);
  28415. if (ret != MP_OKAY)
  28416. return -13048;
  28417. if (mp_cmp_d(r, 2) != MP_EQ)
  28418. return -13049;
  28419. if (!mp_iszero(rem))
  28420. return -13050;
  28421. mp_set(a, 0xfe);
  28422. mp_lshd(a, 3);
  28423. mp_add_d(a, 0xff, a);
  28424. mp_set(d, 0xfe);
  28425. mp_lshd(d, 2);
  28426. ret = mp_div(a, d, r, rem);
  28427. if (ret != MP_OKAY)
  28428. return -13051;
  28429. mp_mul(r, d, d);
  28430. mp_add(rem, d, d);
  28431. if (mp_cmp(a, d) != MP_EQ)
  28432. return -13052;
  28433. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  28434. * a digit. So mp_div must detect and handle.
  28435. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  28436. */
  28437. mp_set(a, 1);
  28438. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  28439. mp_set(d, 1);
  28440. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  28441. mp_add_d(d, 1, d);
  28442. ret = mp_div(a, d, r, rem);
  28443. if (ret != MP_OKAY)
  28444. return -13053;
  28445. return 0;
  28446. }
  28447. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  28448. !defined(WC_NO_RNG)
  28449. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  28450. {
  28451. int ret;
  28452. int res;
  28453. ret = mp_rand_prime(a, 1, rng, NULL);
  28454. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28455. if (ret != 0)
  28456. #else
  28457. if (ret != MP_VAL)
  28458. #endif
  28459. return -13060;
  28460. ret = mp_rand_prime(a, -5, rng, NULL);
  28461. if (ret != 0)
  28462. return -13061;
  28463. ret = mp_prime_is_prime(a, 1, &res);
  28464. if (ret != MP_OKAY)
  28465. return -13062;
  28466. if (res != MP_YES)
  28467. return -13063;
  28468. ret = mp_prime_is_prime(a, 0, &res);
  28469. if (ret != MP_VAL)
  28470. return -13064;
  28471. ret = mp_prime_is_prime(a, -1, &res);
  28472. if (ret != MP_VAL)
  28473. return -13065;
  28474. ret = mp_prime_is_prime(a, 257, &res);
  28475. if (ret != MP_VAL)
  28476. return -13066;
  28477. mp_set(a, 1);
  28478. ret = mp_prime_is_prime(a, 1, &res);
  28479. if (ret != MP_OKAY)
  28480. return -13067;
  28481. if (res != MP_NO)
  28482. return -13068;
  28483. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28484. if (ret != MP_OKAY)
  28485. return -13069;
  28486. if (res != MP_NO)
  28487. return -13070;
  28488. mp_set(a, 2);
  28489. ret = mp_prime_is_prime(a, 1, &res);
  28490. if (ret != MP_OKAY)
  28491. return -13071;
  28492. if (res != MP_YES)
  28493. return -13072;
  28494. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28495. if (ret != MP_OKAY)
  28496. return -13073;
  28497. if (res != MP_YES)
  28498. return -13074;
  28499. mp_set(a, 0xfb);
  28500. ret = mp_prime_is_prime(a, 1, &res);
  28501. if (ret != MP_OKAY)
  28502. return -13075;
  28503. if (res != MP_YES)
  28504. return -13076;
  28505. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28506. if (ret != MP_OKAY)
  28507. return -13077;
  28508. if (res != MP_YES)
  28509. return -13078;
  28510. mp_set(a, 0x6);
  28511. ret = mp_prime_is_prime(a, 1, &res);
  28512. if (ret != MP_OKAY)
  28513. return -13079;
  28514. if (res != MP_NO)
  28515. return -13080;
  28516. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28517. if (ret != MP_OKAY)
  28518. return -13081;
  28519. if (res != MP_NO)
  28520. return -13082;
  28521. mp_set_int(a, 0x655 * 0x65b);
  28522. ret = mp_prime_is_prime(a, 10, &res);
  28523. if (ret != MP_OKAY)
  28524. return -13083;
  28525. if (res != MP_NO)
  28526. return -13084;
  28527. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  28528. if (ret != MP_OKAY)
  28529. return -13085;
  28530. if (res != MP_NO)
  28531. return -13086;
  28532. return 0;
  28533. }
  28534. #endif
  28535. #if defined(WOLFSSL_SP_MATH_ALL) || (defined(WOLFSSL_SP_MATH) && \
  28536. defined(WOLFSSL_HAVE_SP_DH) || (defined(HAVE_ECC) && defined(FP_ECC)))
  28537. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  28538. WC_RNG* rng)
  28539. {
  28540. int ret;
  28541. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  28542. int i;
  28543. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  28544. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  28545. { 12, 56, 168 }, { 56, 12, 168 }
  28546. };
  28547. #endif
  28548. (void)exp;
  28549. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  28550. mp_set(a, 0);
  28551. mp_set(b, 1);
  28552. ret = mp_lcm(a, a, r);
  28553. if (ret != MP_VAL)
  28554. return -13090;
  28555. ret = mp_lcm(a, b, r);
  28556. if (ret != MP_VAL)
  28557. return -13091;
  28558. ret = mp_lcm(b, a, r);
  28559. if (ret != MP_VAL)
  28560. return -13092;
  28561. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  28562. mp_set(a, kat[i][0]);
  28563. mp_set(b, kat[i][1]);
  28564. ret = mp_lcm(a, b, r);
  28565. if (ret != MP_OKAY)
  28566. return -13093;
  28567. mp_set(exp, kat[i][2]);
  28568. if (mp_cmp(r, exp) != MP_EQ)
  28569. return -13094;
  28570. }
  28571. #endif
  28572. (void)rng;
  28573. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  28574. !defined(WC_NO_RNG)
  28575. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  28576. return -13095;
  28577. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  28578. return -13096;
  28579. if (mp_mul(a, b, exp) != MP_OKAY)
  28580. return -13097;
  28581. ret = mp_lcm(a, b, r);
  28582. if (ret != MP_OKAY)
  28583. return -13098;
  28584. if (mp_cmp(r, exp) != MP_EQ)
  28585. return -13099;
  28586. ret = mp_lcm(b, a, r);
  28587. if (ret != MP_OKAY)
  28588. return -13100;
  28589. if (mp_cmp(r, exp) != MP_EQ)
  28590. return -13101;
  28591. #endif
  28592. mp_set(a, 11);
  28593. mp_zero(b);
  28594. ret = mp_gcd(a, b, r);
  28595. if (ret != MP_OKAY)
  28596. return -13102;
  28597. if (mp_cmp_d(r, 11) != MP_EQ)
  28598. return -13103;
  28599. ret = mp_gcd(b, a, r);
  28600. if (ret != MP_OKAY)
  28601. return -13104;
  28602. if (mp_cmp_d(r, 11) != MP_EQ)
  28603. return -13105;
  28604. ret = mp_gcd(b, b, r);
  28605. if (ret != MP_VAL)
  28606. return -13106;
  28607. return 0;
  28608. }
  28609. #endif
  28610. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  28611. defined(WOLFSSL_SP_MATH_ALL)
  28612. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  28613. {
  28614. int ret;
  28615. int i;
  28616. int j;
  28617. mp_set(a, 10);
  28618. ret = mp_mod_2d(a, 0, r);
  28619. if (ret != MP_OKAY)
  28620. return -13110;
  28621. if (!mp_iszero(r))
  28622. return -13111;
  28623. ret = mp_mod_2d(a, 1, r);
  28624. if (ret != MP_OKAY)
  28625. return -13112;
  28626. if (!mp_iszero(r))
  28627. return -13113;
  28628. ret = mp_mod_2d(a, 2, r);
  28629. if (ret != MP_OKAY)
  28630. return -13114;
  28631. if (mp_cmp_d(r, 2))
  28632. return -13115;
  28633. for (i = 2; i < 20; i++) {
  28634. ret = randNum(a, i, rng, NULL);
  28635. if (ret != 0)
  28636. return -13116;
  28637. for (j = 1; j <= mp_count_bits(a); j++) {
  28638. /* Get top part */
  28639. ret = mp_div_2d(a, j, t, NULL);
  28640. if (ret != 0)
  28641. return -13117;
  28642. ret = mp_mul_2d(t, j, t);
  28643. if (ret != 0)
  28644. return -13118;
  28645. /* Get bottom part */
  28646. ret = mp_mod_2d(a, j, r);
  28647. if (ret != 0)
  28648. return -13119;
  28649. /* Reassemble */
  28650. ret = mp_add(t, r, r);
  28651. if (ret != 0)
  28652. return -13120;
  28653. if (mp_cmp(a, r) != MP_EQ)
  28654. return -13121;
  28655. }
  28656. }
  28657. return 0;
  28658. }
  28659. #endif
  28660. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  28661. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  28662. static int mp_test_mod_d(mp_int* a)
  28663. {
  28664. int ret;
  28665. mp_digit r;
  28666. if (mp_set(a, 1) != MP_OKAY)
  28667. return -13130;
  28668. ret = mp_mod_d(a, 0, &r);
  28669. if (ret != MP_VAL)
  28670. return -13131;
  28671. mp_zero(a);
  28672. ret = mp_mod_d(a, 1, &r);
  28673. if (ret != MP_OKAY)
  28674. return -13132;
  28675. ret = mp_mod_d(a, 3, &r);
  28676. if (ret != MP_OKAY)
  28677. return -13133;
  28678. ret = mp_mod_d(a, 5, &r);
  28679. if (ret != MP_OKAY)
  28680. return -13134;
  28681. return 0;
  28682. }
  28683. #endif
  28684. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  28685. WC_RNG* rng)
  28686. {
  28687. int ret;
  28688. int i;
  28689. for (i = 1; i < 16; i++) {
  28690. ret = randNum(a, i, rng, NULL);
  28691. if (ret != 0)
  28692. return -13140;
  28693. ret = mp_mul(a, a, r1);
  28694. if (ret != 0)
  28695. return -13141;
  28696. ret = mp_sqr(a, r2);
  28697. if (ret != 0)
  28698. return -13142;
  28699. if (mp_cmp(r1, r2) != MP_EQ)
  28700. return -13143;
  28701. }
  28702. ret = mp_set(b, 0);
  28703. if (ret != MP_OKAY)
  28704. return -13144;
  28705. ret = mp_mul(a, b, r1);
  28706. if (ret != MP_OKAY)
  28707. return -13145;
  28708. if (!mp_iszero(r1))
  28709. return -13146;
  28710. ret = mp_sqr(b, r1);
  28711. if (ret != MP_OKAY)
  28712. return -13147;
  28713. if (!mp_iszero(r1))
  28714. return -13148;
  28715. #ifdef WOLFSSL_SP_MATH
  28716. ret = mp_set(a, 1);
  28717. if (ret != MP_OKAY)
  28718. return -13149;
  28719. i = (SP_INT_DIGITS + 1) / 2;
  28720. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  28721. if (ret != MP_OKAY)
  28722. return -13150;
  28723. ret = mp_set(b, 1);
  28724. if (ret != MP_OKAY)
  28725. return -13151;
  28726. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  28727. if (ret != MP_OKAY)
  28728. return -13152;
  28729. ret = mp_mul(a, b, r1);
  28730. if (ret != MP_OKAY)
  28731. return -13153;
  28732. ret = mp_mul(a, a, r1);
  28733. if (ret == MP_OKAY)
  28734. return -13154;
  28735. ret = mp_sqr(a, r1);
  28736. if (ret == MP_OKAY)
  28737. return -13155;
  28738. ret = mp_sqr(b, r1);
  28739. if (ret != MP_OKAY)
  28740. return -13156;
  28741. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28742. (defined(HAVE_ECC) && defined(FP_ECC))
  28743. ret = mp_mulmod(a, b, b, r1);
  28744. if (ret != MP_OKAY)
  28745. return -13157;
  28746. ret = mp_mulmod(a, a, b, r1);
  28747. if (ret == MP_OKAY)
  28748. return -13158;
  28749. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  28750. ret = mp_sqrmod(a, b, r1);
  28751. if (ret == MP_OKAY)
  28752. return -13159;
  28753. ret = mp_sqrmod(b, a, r1);
  28754. if (ret != MP_OKAY)
  28755. return -13160;
  28756. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  28757. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  28758. #endif /* WOLFSSL_SP_MATH */
  28759. return 0;
  28760. }
  28761. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  28762. defined(OPENSSL_EXTRA)
  28763. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  28764. {
  28765. int ret;
  28766. mp_set(a, 0);
  28767. mp_set(m, 1);
  28768. ret = mp_invmod(a, m, r);
  28769. if (ret != MP_VAL)
  28770. return -13170;
  28771. ret = mp_invmod(m, a, r);
  28772. if (ret != MP_VAL)
  28773. return -13171;
  28774. mp_set(a, 2);
  28775. mp_set(m, 4);
  28776. ret = mp_invmod(a, m, r);
  28777. if (ret != MP_VAL)
  28778. return -13172;
  28779. mp_set(a, 1);
  28780. mp_set(m, 4);
  28781. ret = mp_invmod(a, m, r);
  28782. if (ret != MP_OKAY)
  28783. return -13173;
  28784. if (!mp_isone(r))
  28785. return -13174;
  28786. mp_set(a, 3);
  28787. mp_set(m, 4);
  28788. ret = mp_invmod(a, m, r);
  28789. if (ret != MP_OKAY)
  28790. return -13175;
  28791. mp_set(a, 3);
  28792. mp_set(m, 5);
  28793. ret = mp_invmod(a, m, r);
  28794. if (ret != MP_OKAY)
  28795. return -13176;
  28796. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  28797. mp_read_radix(a, "-3", 16);
  28798. ret = mp_invmod(a, m, r);
  28799. if (ret != MP_OKAY)
  28800. return -13177;
  28801. #endif
  28802. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28803. #ifdef HAVE_ECC
  28804. mp_set(a, 0);
  28805. mp_set(m, 3);
  28806. ret = mp_invmod_mont_ct(a, m, r, 1);
  28807. if (ret != MP_VAL)
  28808. return -13178;
  28809. mp_set(a, 1);
  28810. mp_set(m, 0);
  28811. ret = mp_invmod_mont_ct(a, m, r, 1);
  28812. if (ret != MP_VAL)
  28813. return -13179;
  28814. mp_set(a, 1);
  28815. mp_set(m, 1);
  28816. ret = mp_invmod_mont_ct(a, m, r, 1);
  28817. if (ret != MP_VAL)
  28818. return -13180;
  28819. mp_set(a, 1);
  28820. mp_set(m, 2);
  28821. ret = mp_invmod_mont_ct(a, m, r, 1);
  28822. if (ret != MP_VAL)
  28823. return -13181;
  28824. mp_set(a, 1);
  28825. mp_set(m, 3);
  28826. ret = mp_invmod_mont_ct(a, m, r, 1);
  28827. if (ret != MP_OKAY)
  28828. return -13182;
  28829. #endif
  28830. #endif
  28831. return 0;
  28832. }
  28833. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  28834. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  28835. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  28836. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  28837. {
  28838. int ret;
  28839. mp_set(b, 0x2);
  28840. mp_set(e, 0x3);
  28841. mp_set(m, 0x0);
  28842. ret = mp_exptmod_ex(b, e, 1, m, r);
  28843. if (ret != MP_VAL)
  28844. return -13190;
  28845. ret = mp_exptmod_nct(b, e, m, r);
  28846. if (ret != MP_VAL)
  28847. return -13191;
  28848. mp_set(b, 0x2);
  28849. mp_set(e, 0x3);
  28850. mp_set(m, 0x1);
  28851. ret = mp_exptmod_ex(b, e, 1, m, r);
  28852. if (ret != MP_OKAY)
  28853. return -13192;
  28854. if (!mp_iszero(r))
  28855. return -13193;
  28856. ret = mp_exptmod_nct(b, e, m, r);
  28857. if (ret != MP_OKAY)
  28858. return -13194;
  28859. if (!mp_iszero(r))
  28860. return -13195;
  28861. mp_set(b, 0x2);
  28862. mp_set(e, 0x0);
  28863. mp_set(m, 0x7);
  28864. ret = mp_exptmod_ex(b, e, 1, m, r);
  28865. if (ret != MP_OKAY)
  28866. return -13196;
  28867. if (!mp_isone(r))
  28868. return -13197;
  28869. ret = mp_exptmod_nct(b, e, m, r);
  28870. if (ret != MP_OKAY)
  28871. return -13198;
  28872. if (!mp_isone(r))
  28873. return -13199;
  28874. mp_set(b, 0x0);
  28875. mp_set(e, 0x3);
  28876. mp_set(m, 0x7);
  28877. ret = mp_exptmod_ex(b, e, 1, m, r);
  28878. if (ret != MP_OKAY)
  28879. return -13200;
  28880. if (!mp_iszero(r))
  28881. return -13201;
  28882. ret = mp_exptmod_nct(b, e, m, r);
  28883. if (ret != MP_OKAY)
  28884. return -13202;
  28885. if (!mp_iszero(r))
  28886. return -13203;
  28887. mp_set(b, 0x10);
  28888. mp_set(e, 0x3);
  28889. mp_set(m, 0x7);
  28890. ret = mp_exptmod_ex(b, e, 1, m, r);
  28891. if (ret != MP_OKAY)
  28892. return -13204;
  28893. ret = mp_exptmod_nct(b, e, m, r);
  28894. if (ret != MP_OKAY)
  28895. return -13205;
  28896. mp_set(b, 0x7);
  28897. mp_set(e, 0x3);
  28898. mp_set(m, 0x7);
  28899. ret = mp_exptmod_ex(b, e, 1, m, r);
  28900. if (ret != MP_OKAY)
  28901. return -13206;
  28902. if (!mp_iszero(r))
  28903. return -13207;
  28904. ret = mp_exptmod_nct(b, e, m, r);
  28905. if (ret != MP_OKAY)
  28906. return -13208;
  28907. if (!mp_iszero(r))
  28908. return -13209;
  28909. mp_set(b, 0x01);
  28910. mp_mul_2d(b, DIGIT_BIT, b);
  28911. mp_add_d(b, 1, b);
  28912. mp_set(e, 0x3);
  28913. mp_copy(b, m);
  28914. ret = mp_exptmod_ex(b, e, 1, m, r);
  28915. if (ret != MP_OKAY)
  28916. return -13210;
  28917. if (!mp_iszero(r))
  28918. return -13211;
  28919. ret = mp_exptmod_nct(b, e, m, r);
  28920. if (ret != MP_OKAY)
  28921. return -13212;
  28922. if (!mp_iszero(r))
  28923. return -13213;
  28924. mp_set(b, 0x2);
  28925. mp_set(e, 0x3);
  28926. mp_set(m, 0x7);
  28927. ret = mp_exptmod_ex(b, e, 1, m, r);
  28928. if (ret != MP_OKAY)
  28929. return -13214;
  28930. ret = mp_exptmod_nct(b, e, m, r);
  28931. if (ret != MP_OKAY)
  28932. return -13215;
  28933. #ifdef WOLFSSL_SP_MATH
  28934. mp_set(b, 0x2);
  28935. mp_set(e, 0x3);
  28936. mp_set(m, 0x01);
  28937. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  28938. mp_add_d(m, 0x01, m);
  28939. ret = mp_exptmod_ex(b, e, 1, m, r);
  28940. if (ret != MP_VAL)
  28941. return -13216;
  28942. ret = mp_exptmod_nct(b, e, m, r);
  28943. if (ret != MP_VAL)
  28944. return -13217;
  28945. #endif
  28946. return 0;
  28947. }
  28948. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  28949. * OPENSSL_EXTRA */
  28950. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28951. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  28952. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  28953. {
  28954. int ret;
  28955. mp_digit mp;
  28956. static int exp[] = { 7, 8, 16, 27, 32, 64,
  28957. 127, 128, 255, 256,
  28958. 383, 384, 2033, 2048
  28959. };
  28960. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  28961. 0x01, 0x9f, 0x13, 0xbd,
  28962. 0x1f, 0x13d, 0x45, 0x615
  28963. };
  28964. int i;
  28965. int j;
  28966. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  28967. if (exp[i] >= DIGIT_BIT)
  28968. continue;
  28969. mp_zero(m);
  28970. ret = mp_set_bit(m, exp[i]);
  28971. if (ret != MP_OKAY)
  28972. return -13220;
  28973. ret = mp_sub_d(m, sub[i], m);
  28974. if (ret != MP_OKAY)
  28975. return -13221;
  28976. ret = mp_montgomery_setup(m, &mp);
  28977. if (ret != MP_OKAY)
  28978. return -13222;
  28979. ret = mp_montgomery_calc_normalization(n, m);
  28980. if (ret != MP_OKAY)
  28981. return -13223;
  28982. for (j = 0; j < 10; j++) {
  28983. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  28984. if (ret != 0)
  28985. return -13224;
  28986. ret = mp_mod(a, m, a);
  28987. if (ret != 0)
  28988. return -13225;
  28989. /* r = a * a */
  28990. ret = mp_sqrmod(a, m, r);
  28991. if (ret != MP_OKAY)
  28992. return -13226;
  28993. /* Convert to Montgomery form = a*n */
  28994. ret = mp_mulmod(a, n, m, a);
  28995. if (ret != MP_OKAY)
  28996. return -13227;
  28997. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  28998. ret = mp_sqr(a, a);
  28999. if (ret != MP_OKAY)
  29000. return -13228;
  29001. ret = mp_montgomery_reduce(a, m, mp);
  29002. if (ret != MP_OKAY)
  29003. return -13229;
  29004. ret = mp_montgomery_reduce(a, m, mp);
  29005. if (ret != MP_OKAY)
  29006. return -13230;
  29007. if (mp_cmp(a, r) != MP_EQ)
  29008. return -13231;
  29009. }
  29010. }
  29011. return 0;
  29012. }
  29013. #endif
  29014. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  29015. {
  29016. WC_RNG rng;
  29017. int ret;
  29018. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  29019. int i, j, k;
  29020. mp_digit d;
  29021. #endif
  29022. mp_int a, b, r1, r2, p;
  29023. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  29024. if (ret != 0)
  29025. return -13300;
  29026. #ifdef WOLSSL_SP_MATH_ALL
  29027. mp_init_copy(&p, &a);
  29028. #else
  29029. ret = mp_init(&p);
  29030. if (ret != 0)
  29031. return -13301;
  29032. #endif
  29033. #ifndef HAVE_FIPS
  29034. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29035. #else
  29036. ret = wc_InitRng(&rng);
  29037. #endif
  29038. if (ret != 0)
  29039. goto done;
  29040. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  29041. mp_set_int(&a, 0);
  29042. if (a.used != 0 || a.dp[0] != 0)
  29043. return -13302;
  29044. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  29045. for (i = 0; i < 4 * j; i++) {
  29046. /* New values to use. */
  29047. ret = randNum(&p, j, &rng, NULL);
  29048. if (ret != 0)
  29049. return -13303;
  29050. ret = randNum(&a, j, &rng, NULL);
  29051. if (ret != 0)
  29052. return -13304;
  29053. ret = randNum(&b, j, &rng, NULL);
  29054. if (ret != 0)
  29055. return -13305;
  29056. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  29057. if (ret != 0)
  29058. return -13306;
  29059. d &= MP_MASK;
  29060. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  29061. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  29062. /* Ensure sqrmod produce same result as mulmod. */
  29063. ret = mp_sqrmod(&a, &p, &r1);
  29064. if (ret != 0)
  29065. return -13307;
  29066. ret = mp_mulmod(&a, &a, &p, &r2);
  29067. if (ret != 0)
  29068. return -13308;
  29069. if (mp_cmp(&r1, &r2) != 0)
  29070. return -13309;
  29071. #endif
  29072. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  29073. !defined(WOLFSSL_SP_INT_NEGATIVE))
  29074. ret = mp_addmod(&a, &b, &p, &r1);
  29075. if (ret != 0)
  29076. return -13310;
  29077. ret = mp_submod(&r1, &b, &p, &r2);
  29078. if (ret != 0)
  29079. return -13311;
  29080. ret = mp_mod(&a, &p, &r1);
  29081. if (ret != 0)
  29082. return -13312;
  29083. if (mp_cmp(&r1, &r2) != MP_EQ)
  29084. return -13313;
  29085. #else
  29086. /* Ensure add with mod produce same result as sub with mod. */
  29087. ret = mp_addmod(&a, &b, &p, &r1);
  29088. if (ret != 0)
  29089. return -13314;
  29090. b.sign ^= 1;
  29091. ret = mp_submod(&a, &b, &p, &r2);
  29092. if (ret != 0)
  29093. return -13315;
  29094. if (mp_cmp(&r1, &r2) != 0)
  29095. return -13316;
  29096. #endif
  29097. /* Ensure add digit produce same result as sub digit. */
  29098. ret = mp_add_d(&a, d, &r1);
  29099. if (ret != 0)
  29100. return -13317;
  29101. ret = mp_sub_d(&r1, d, &r2);
  29102. if (ret != 0)
  29103. return -13318;
  29104. if (mp_cmp(&a, &r2) != 0)
  29105. return -13319;
  29106. /* Invert - if p is even it will use the slow impl.
  29107. * - if p and a are even it will fail.
  29108. */
  29109. ret = mp_invmod(&a, &p, &r1);
  29110. if (ret != 0 && ret != MP_VAL)
  29111. return -13320;
  29112. ret = 0;
  29113. /* Shift up and down number all bits in a digit. */
  29114. for (k = 0; k < DIGIT_BIT; k++) {
  29115. mp_mul_2d(&a, k, &r1);
  29116. mp_div_2d(&r1, k, &r2, &p);
  29117. if (mp_cmp(&a, &r2) != 0)
  29118. return -13321;
  29119. if (!mp_iszero(&p))
  29120. return -13322;
  29121. mp_rshb(&r1, k);
  29122. if (mp_cmp(&a, &r1) != 0)
  29123. return -13323;
  29124. }
  29125. }
  29126. }
  29127. #if DIGIT_BIT >= 32
  29128. /* Check that setting a 32-bit digit works. */
  29129. d &= 0xffffffffU;
  29130. mp_set_int(&a, d);
  29131. if (a.used != 1 || a.dp[0] != d)
  29132. return -13324;
  29133. #endif
  29134. /* Check setting a bit and testing a bit works. */
  29135. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  29136. mp_zero(&a);
  29137. mp_set_bit(&a, i);
  29138. if (!mp_is_bit_set(&a, i))
  29139. return -13325;
  29140. }
  29141. #endif
  29142. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  29143. mp_zero(&a);
  29144. i = mp_cnt_lsb(&a);
  29145. if (i != 0)
  29146. return -13326;
  29147. mp_set(&a, 1);
  29148. i = mp_cnt_lsb(&a);
  29149. if (i != 0)
  29150. return -13327;
  29151. #endif
  29152. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29153. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  29154. return ret;
  29155. #endif
  29156. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  29157. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  29158. return ret;
  29159. #endif
  29160. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  29161. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  29162. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  29163. return ret;
  29164. #endif
  29165. #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC)
  29166. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  29167. return ret;
  29168. #endif
  29169. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  29170. return ret;
  29171. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  29172. return ret;
  29173. if ((ret = mp_test_read_to_bin(&a)) != 0)
  29174. return ret;
  29175. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29176. if ((ret = mp_test_set_int(&a)) != 0)
  29177. return ret;
  29178. #endif
  29179. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  29180. return ret;
  29181. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  29182. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  29183. return ret;
  29184. #endif
  29185. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29186. if ((ret = mp_test_set_is_bit(&a)) != 0)
  29187. return ret;
  29188. #endif
  29189. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  29190. return ret;
  29191. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  29192. !defined(WC_NO_RNG)
  29193. if ((ret = mp_test_prime(&a, &rng)) != 0)
  29194. return ret;
  29195. #endif
  29196. #if defined(WOLFSSL_SP_MATH_ALL) || (defined(WOLFSSL_SP_MATH) && \
  29197. defined(WOLFSSL_HAVE_SP_DH) || (defined(HAVE_ECC) && defined(FP_ECC)))
  29198. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  29199. return ret;
  29200. #endif
  29201. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  29202. defined(WOLFSSL_SP_MATH_ALL)
  29203. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  29204. return ret;
  29205. #endif
  29206. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  29207. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  29208. if ((ret = mp_test_mod_d(&a)) != 0)
  29209. return ret;
  29210. #endif
  29211. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  29212. return ret;
  29213. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  29214. defined(OPENSSL_EXTRA)
  29215. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  29216. return ret;
  29217. #endif
  29218. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  29219. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  29220. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  29221. return ret;
  29222. #endif
  29223. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  29224. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  29225. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  29226. return ret;
  29227. #endif
  29228. done:
  29229. mp_clear(&p);
  29230. mp_clear(&r2);
  29231. mp_clear(&r1);
  29232. mp_clear(&b);
  29233. mp_clear(&a);
  29234. wc_FreeRng(&rng);
  29235. return ret;
  29236. }
  29237. #endif
  29238. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  29239. typedef struct pairs_t {
  29240. const unsigned char* coeff;
  29241. int coeffSz;
  29242. int exp;
  29243. } pairs_t;
  29244. /*
  29245. n =p1p2p3, where pi = ki(p1−1)+1 with (k2,k3) = (173,293)
  29246. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  29247. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  29248. */
  29249. static const unsigned char c192a[] =
  29250. {
  29251. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  29252. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  29253. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  29254. };
  29255. static const unsigned char c0a[] =
  29256. {
  29257. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  29258. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  29259. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  29260. };
  29261. static const pairs_t ecPairsA[] =
  29262. {
  29263. {c192a, sizeof(c192a), 192},
  29264. {c0a, sizeof(c0a), 0}
  29265. };
  29266. static const int kA[] = {173, 293};
  29267. static const unsigned char controlPrime[] = {
  29268. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  29269. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  29270. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  29271. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  29272. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  29273. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  29274. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  29275. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  29276. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  29277. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  29278. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  29279. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  29280. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  29281. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  29282. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  29283. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  29284. };
  29285. static const unsigned char testOne[] = { 1 };
  29286. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  29287. {
  29288. int ret;
  29289. mp_int ki;
  29290. ret = mp_init(&ki);
  29291. if (ret == 0)
  29292. ret = mp_set(&ki, k);
  29293. if (ret == 0)
  29294. ret = mp_sub_d(p1, 1, p2);
  29295. if (ret == 0)
  29296. ret = mp_mul(p2, &ki, p2);
  29297. if (ret == 0)
  29298. ret = mp_add_d(p2, 1, p2);
  29299. mp_clear(&ki);
  29300. return ret;
  29301. }
  29302. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  29303. const pairs_t* ecPairs, int ecPairsSz,
  29304. const int* k)
  29305. {
  29306. mp_int x,y;
  29307. int ret, i;
  29308. ret = mp_init(&x);
  29309. if (ret == 0) {
  29310. ret = mp_init(&y);
  29311. if (ret != 0) {
  29312. mp_clear(&x);
  29313. return MP_MEM;
  29314. }
  29315. }
  29316. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  29317. ret = mp_read_unsigned_bin(&x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  29318. /* p1 = 2^exp */
  29319. if (ret == 0)
  29320. ret = mp_2expt(&y, ecPairs[i].exp);
  29321. /* p1 = p1 * m */
  29322. if (ret == 0)
  29323. ret = mp_mul(&x, &y, &x);
  29324. /* p1 += */
  29325. if (ret == 0)
  29326. ret = mp_add(p1, &x, p1);
  29327. mp_zero(&x);
  29328. mp_zero(&y);
  29329. }
  29330. mp_clear(&x);
  29331. mp_clear(&y);
  29332. if (ret == 0)
  29333. ret = GenerateNextP(p1, p2, k[0]);
  29334. if (ret == 0)
  29335. ret = GenerateNextP(p1, p3, k[1]);
  29336. return ret;
  29337. }
  29338. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  29339. {
  29340. #ifdef WOLFSSL_SMALL_STACK
  29341. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29342. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29343. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29344. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29345. #else
  29346. mp_int n[1],
  29347. p1[1],
  29348. p2[1],
  29349. p3[1];
  29350. #endif
  29351. int ret, isPrime = 0;
  29352. WC_RNG rng;
  29353. #ifdef WOLFSSL_SMALL_STACK
  29354. if ((n == NULL) ||
  29355. (p1 == NULL) ||
  29356. (p2 == NULL) ||
  29357. (p3 == NULL))
  29358. ERROR_OUT(MEMORY_E, out);
  29359. #endif
  29360. ret = wc_InitRng(&rng);
  29361. if (ret == 0)
  29362. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  29363. if (ret == 0)
  29364. ret = GenerateP(p1, p2, p3,
  29365. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  29366. if (ret == 0)
  29367. ret = mp_mul(p1, p2, n);
  29368. if (ret == 0)
  29369. ret = mp_mul(n, p3, n);
  29370. if (ret != 0)
  29371. ERROR_OUT(-13400, out);
  29372. /* Check the old prime test using the number that false positives.
  29373. * This test result should indicate as not prime. */
  29374. ret = mp_prime_is_prime(n, 40, &isPrime);
  29375. if (ret != 0)
  29376. ERROR_OUT(-13401, out);
  29377. if (isPrime)
  29378. ERROR_OUT(-13402, out);
  29379. /* This test result should fail. It should indicate the value as prime. */
  29380. ret = mp_prime_is_prime(n, 8, &isPrime);
  29381. if (ret != 0)
  29382. ERROR_OUT(-13403, out);
  29383. if (!isPrime)
  29384. ERROR_OUT(-13404, out);
  29385. /* This test result should indicate the value as not prime. */
  29386. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29387. if (ret != 0)
  29388. ERROR_OUT(-13405, out);
  29389. if (isPrime)
  29390. ERROR_OUT(-13406, out);
  29391. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  29392. if (ret != 0)
  29393. ERROR_OUT(-13407, out);
  29394. /* This test result should indicate the value as prime. */
  29395. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29396. if (ret != 0)
  29397. ERROR_OUT(-13408, out);
  29398. if (!isPrime)
  29399. ERROR_OUT(-13409, out);
  29400. /* This test result should indicate the value as prime. */
  29401. isPrime = -1;
  29402. ret = mp_prime_is_prime(n, 8, &isPrime);
  29403. if (ret != 0)
  29404. ERROR_OUT(-13410, out);
  29405. if (!isPrime)
  29406. ERROR_OUT(-13411, out);
  29407. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  29408. if (ret != 0)
  29409. ERROR_OUT(-13412, out);
  29410. /* This test result should indicate the value as not prime. */
  29411. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29412. if (ret != 0)
  29413. ERROR_OUT(-13413, out);
  29414. if (isPrime)
  29415. ERROR_OUT(-13414, out);
  29416. ret = mp_prime_is_prime(n, 8, &isPrime);
  29417. if (ret != 0)
  29418. ERROR_OUT(-13415, out);
  29419. if (isPrime)
  29420. ERROR_OUT(-13416, out);
  29421. ret = 0;
  29422. out:
  29423. #ifdef WOLFSSL_SMALL_STACK
  29424. if (n != NULL) {
  29425. mp_clear(n);
  29426. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29427. }
  29428. if (p1 != NULL) {
  29429. mp_clear(p1);
  29430. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29431. }
  29432. if (p2 != NULL) {
  29433. mp_clear(p2);
  29434. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29435. }
  29436. if (p3 != NULL) {
  29437. mp_clear(p3);
  29438. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29439. }
  29440. #else
  29441. mp_clear(p3);
  29442. mp_clear(p2);
  29443. mp_clear(p1);
  29444. mp_clear(n);
  29445. #endif
  29446. wc_FreeRng(&rng);
  29447. return ret;
  29448. }
  29449. #endif /* WOLFSSL_PUBLIC_MP */
  29450. #if defined(ASN_BER_TO_DER) && \
  29451. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  29452. defined(OPENSSL_EXTRA_X509_SMALL))
  29453. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  29454. typedef struct berDerTestData {
  29455. const byte *in;
  29456. word32 inSz;
  29457. const byte *out;
  29458. word32 outSz;
  29459. } berDerTestData;
  29460. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  29461. {
  29462. int ret;
  29463. int i;
  29464. word32 len = 0, l;
  29465. byte out[32];
  29466. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  29467. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  29468. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  29469. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  29470. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  29471. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  29472. };
  29473. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  29474. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  29475. 0x30, 0x80,
  29476. 0x02, 0x01, 0x01,
  29477. 0x30, 0x80,
  29478. 0x24, 0x80,
  29479. 0x04, 0x01, 0x01,
  29480. 0x04, 0x02, 0x02, 0x03,
  29481. 0x00, 0x00,
  29482. 0x06, 0x01, 0x01,
  29483. 0x00, 0x00,
  29484. 0x31, 0x80,
  29485. 0x06, 0x01, 0x01,
  29486. 0x00, 0x00,
  29487. 0x00, 0x00,
  29488. };
  29489. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  29490. 0x30, 0x12,
  29491. 0x02, 0x01, 0x01,
  29492. 0x30, 0x08,
  29493. 0x04, 0x03, 0x01, 0x02, 0x03,
  29494. 0x06, 0x01, 0x01,
  29495. 0x31, 0x03,
  29496. 0x06, 0x01, 0x01
  29497. };
  29498. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  29499. berDerTestData testData[] = {
  29500. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  29501. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  29502. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  29503. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  29504. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  29505. };
  29506. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  29507. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  29508. if (ret != LENGTH_ONLY_E)
  29509. return -13500 - i;
  29510. if (len != testData[i].outSz)
  29511. return -13510 - i;
  29512. len = testData[i].outSz;
  29513. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  29514. if (ret != 0)
  29515. return -13520 - i;
  29516. if (XMEMCMP(out, testData[i].out, len) != 0)
  29517. return -13530 - i;
  29518. for (l = 1; l < testData[i].inSz; l++) {
  29519. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  29520. if (ret != ASN_PARSE_E)
  29521. return -13540;
  29522. len = testData[i].outSz;
  29523. ret = wc_BerToDer(testData[i].in, l, out, &len);
  29524. if (ret != ASN_PARSE_E)
  29525. return -13541;
  29526. }
  29527. for (l = 0; l < testData[i].outSz-1; l++) {
  29528. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  29529. if (ret != BUFFER_E)
  29530. return -13542;
  29531. }
  29532. }
  29533. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  29534. if (ret != BAD_FUNC_ARG)
  29535. return -13543;
  29536. ret = wc_BerToDer(out, 4, NULL, NULL);
  29537. if (ret != BAD_FUNC_ARG)
  29538. return -13544;
  29539. ret = wc_BerToDer(NULL, 4, NULL, &len);
  29540. if (ret != BAD_FUNC_ARG)
  29541. return -13545;
  29542. ret = wc_BerToDer(NULL, 4, out, NULL);
  29543. if (ret != BAD_FUNC_ARG)
  29544. return -13546;
  29545. ret = wc_BerToDer(out, 4, out, NULL);
  29546. if (ret != BAD_FUNC_ARG)
  29547. return -13547;
  29548. ret = wc_BerToDer(NULL, 4, out, &len);
  29549. if (ret != BAD_FUNC_ARG)
  29550. return -13548;
  29551. for (l = 1; l < sizeof(good4_out); l++) {
  29552. len = l;
  29553. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  29554. if (ret != BUFFER_E)
  29555. return -13549;
  29556. }
  29557. return 0;
  29558. }
  29559. #endif
  29560. #ifdef DEBUG_WOLFSSL
  29561. static THREAD_LS_T int log_cnt = 0;
  29562. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  29563. {
  29564. (void)logLevel;
  29565. (void)logMessage;
  29566. log_cnt++;
  29567. }
  29568. #endif /* DEBUG_WOLFSSL */
  29569. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  29570. {
  29571. #ifdef DEBUG_WOLFSSL
  29572. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  29573. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  29574. byte b[256];
  29575. int i;
  29576. for (i = 0; i < (int)sizeof(b); i++)
  29577. b[i] = i;
  29578. if (wolfSSL_Debugging_ON() != 0)
  29579. return -13600;
  29580. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  29581. return -13601;
  29582. WOLFSSL_MSG(msg);
  29583. WOLFSSL_BUFFER(a, sizeof(a));
  29584. WOLFSSL_BUFFER(b, sizeof(b));
  29585. WOLFSSL_BUFFER(NULL, 0);
  29586. WOLFSSL_ERROR(MEMORY_E);
  29587. WOLFSSL_ERROR_MSG(msg);
  29588. /* turn off logs */
  29589. wolfSSL_Debugging_OFF();
  29590. /* capture log count */
  29591. i = log_cnt;
  29592. /* validate no logs are output when disabled */
  29593. WOLFSSL_MSG(msg);
  29594. WOLFSSL_BUFFER(a, sizeof(a));
  29595. WOLFSSL_BUFFER(b, sizeof(b));
  29596. WOLFSSL_BUFFER(NULL, 0);
  29597. WOLFSSL_ERROR(MEMORY_E);
  29598. WOLFSSL_ERROR_MSG(msg);
  29599. /* check the logs were disabled */
  29600. if (i != log_cnt)
  29601. return -13602;
  29602. /* restore callback and leave logging enabled */
  29603. wolfSSL_SetLoggingCb(NULL);
  29604. wolfSSL_Debugging_ON();
  29605. /* suppress unused args */
  29606. (void)a;
  29607. (void)b;
  29608. #else
  29609. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  29610. return -13603;
  29611. wolfSSL_Debugging_OFF();
  29612. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  29613. return -13604;
  29614. #endif /* DEBUG_WOLFSSL */
  29615. return 0;
  29616. }
  29617. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  29618. {
  29619. #ifdef WOLFSSL_PTHREADS
  29620. wolfSSL_Mutex m;
  29621. #endif
  29622. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  29623. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  29624. if (mm == NULL)
  29625. return -13700;
  29626. wc_FreeMutex(mm);
  29627. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  29628. #endif
  29629. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  29630. #ifdef WOLFSSL_PTHREADS
  29631. if (wc_InitMutex(&m) != 0)
  29632. return -13701;
  29633. if (wc_LockMutex(&m) != 0)
  29634. return -13702;
  29635. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  29636. /* trying to free a locked mutex is not portable behavior with pthread */
  29637. /* Attempting to destroy a locked mutex results in undefined behavior */
  29638. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  29639. return -13703;
  29640. #endif
  29641. if (wc_UnLockMutex(&m) != 0)
  29642. return -13704;
  29643. if (wc_FreeMutex(&m) != 0)
  29644. return -13705;
  29645. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  29646. /* Trying to use a pthread after free'ing is not portable behavior */
  29647. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  29648. return -13706;
  29649. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  29650. return -13707;
  29651. #endif
  29652. #endif
  29653. return 0;
  29654. }
  29655. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  29656. #ifndef WOLFSSL_NO_MALLOC
  29657. static int malloc_cnt = 0;
  29658. static int realloc_cnt = 0;
  29659. static int free_cnt = 0;
  29660. static void *my_Malloc_cb(size_t size)
  29661. {
  29662. malloc_cnt++;
  29663. #ifndef WOLFSSL_NO_MALLOC
  29664. return malloc(size);
  29665. #else
  29666. WOLFSSL_MSG("No malloc available");
  29667. (void)size;
  29668. return NULL;
  29669. #endif
  29670. }
  29671. static void my_Free_cb(void *ptr)
  29672. {
  29673. free_cnt++;
  29674. #ifndef WOLFSSL_NO_MALLOC
  29675. free(ptr);
  29676. #else
  29677. WOLFSSL_MSG("No free available");
  29678. (void)ptr;
  29679. #endif
  29680. }
  29681. static void *my_Realloc_cb(void *ptr, size_t size)
  29682. {
  29683. realloc_cnt++;
  29684. #ifndef WOLFSSL_NO_MALLOC
  29685. return realloc(ptr, size);
  29686. #else
  29687. WOLFSSL_MSG("No realloc available");
  29688. (void)ptr;
  29689. (void)size;
  29690. return NULL;
  29691. #endif
  29692. }
  29693. #endif /* !WOLFSSL_NO_MALLOC */
  29694. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  29695. {
  29696. int ret = 0;
  29697. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29698. byte* b = NULL;
  29699. #endif
  29700. wolfSSL_Malloc_cb mc;
  29701. wolfSSL_Free_cb fc;
  29702. wolfSSL_Realloc_cb rc;
  29703. /* Save existing memory callbacks */
  29704. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  29705. return -13800;
  29706. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29707. /* test realloc */
  29708. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29709. if (b == NULL) {
  29710. ERROR_OUT(-13801, exit_memcb);
  29711. }
  29712. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29713. b = NULL;
  29714. /* Use API. */
  29715. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)(void*)&my_Malloc_cb,
  29716. (wolfSSL_Free_cb)(void*)&my_Free_cb,
  29717. (wolfSSL_Realloc_cb)(void*)&my_Realloc_cb) != 0) {
  29718. ERROR_OUT(-13802, exit_memcb);
  29719. }
  29720. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29721. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29722. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29723. #ifndef WOLFSSL_STATIC_MEMORY
  29724. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  29725. #else
  29726. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  29727. #endif
  29728. ret = -13803;
  29729. #endif /* !WOLFSSL_NO_MALLOC */
  29730. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29731. exit_memcb:
  29732. #endif
  29733. /* restore memory callbacks */
  29734. wolfSSL_SetAllocators(mc, fc, rc);
  29735. return ret;
  29736. }
  29737. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  29738. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  29739. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  29740. {
  29741. int ret = 0;
  29742. byte out[112];
  29743. byte blob[112];
  29744. word32 outSz;
  29745. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  29746. {
  29747. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  29748. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  29749. };
  29750. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  29751. {
  29752. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  29753. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  29754. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  29755. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  29756. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  29757. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  29758. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  29759. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  29760. };
  29761. XMEMSET(blob, 0, sizeof(blob));
  29762. outSz = sizeof(blob);
  29763. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  29764. if (ret != 0) {
  29765. ERROR_OUT(-13900, exit_blob);
  29766. }
  29767. blob[outSz - 2] += 1;
  29768. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29769. if (ret == 0) { /* should fail with altered blob */
  29770. ERROR_OUT(-13901, exit_blob);
  29771. }
  29772. XMEMSET(blob, 0, sizeof(blob));
  29773. outSz = sizeof(blob);
  29774. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  29775. if (ret != 0) {
  29776. ERROR_OUT(-13902, exit_blob);
  29777. }
  29778. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29779. if (ret != 0) {
  29780. ERROR_OUT(-13903, exit_blob);
  29781. }
  29782. if (XMEMCMP(out, iv, sizeof(iv))) {
  29783. ERROR_OUT(-13904, exit_blob);
  29784. }
  29785. XMEMSET(blob, 0, sizeof(blob));
  29786. outSz = sizeof(blob);
  29787. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  29788. if (ret != 0) {
  29789. ERROR_OUT(-13905, exit_blob);
  29790. }
  29791. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29792. if (ret != 0) {
  29793. ERROR_OUT(-13906, exit_blob);
  29794. }
  29795. if (XMEMCMP(out, text, sizeof(text))) {
  29796. ERROR_OUT(-13907, exit_blob);
  29797. }
  29798. exit_blob:
  29799. return ret;
  29800. }
  29801. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  29802. #ifdef WOLF_CRYPTO_CB
  29803. /* Example custom context for crypto callback */
  29804. typedef struct {
  29805. int exampleVar; /* example, not used */
  29806. } myCryptoDevCtx;
  29807. /* Example crypto dev callback function that calls software version */
  29808. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  29809. {
  29810. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  29811. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  29812. if (info == NULL)
  29813. return BAD_FUNC_ARG;
  29814. #ifdef DEBUG_WOLFSSL
  29815. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  29816. #endif
  29817. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  29818. #ifndef WC_NO_RNG
  29819. /* set devId to invalid, so software is used */
  29820. info->rng.rng->devId = INVALID_DEVID;
  29821. ret = wc_RNG_GenerateBlock(info->rng.rng,
  29822. info->rng.out, info->rng.sz);
  29823. /* reset devId */
  29824. info->rng.rng->devId = devIdArg;
  29825. #endif
  29826. }
  29827. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  29828. #ifndef WC_NO_RNG
  29829. static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  29830. word32* seedWord32 = (word32*)seed;
  29831. word32 len;
  29832. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  29833. while (info->seed.sz > 0) {
  29834. len = (word32)sizeof(seed);
  29835. if (info->seed.sz < len)
  29836. len = info->seed.sz;
  29837. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  29838. info->seed.seed += len;
  29839. info->seed.sz -= len;
  29840. (*seedWord32)++;
  29841. }
  29842. ret = 0;
  29843. #endif
  29844. }
  29845. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  29846. #ifdef DEBUG_WOLFSSL
  29847. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  29848. #endif
  29849. #ifndef NO_RSA
  29850. if (info->pk.type == WC_PK_TYPE_RSA) {
  29851. /* set devId to invalid, so software is used */
  29852. info->pk.rsa.key->devId = INVALID_DEVID;
  29853. switch (info->pk.rsa.type) {
  29854. case RSA_PUBLIC_ENCRYPT:
  29855. case RSA_PUBLIC_DECRYPT:
  29856. /* perform software based RSA public op */
  29857. ret = wc_RsaFunction(
  29858. info->pk.rsa.in, info->pk.rsa.inLen,
  29859. info->pk.rsa.out, info->pk.rsa.outLen,
  29860. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  29861. break;
  29862. case RSA_PRIVATE_ENCRYPT:
  29863. case RSA_PRIVATE_DECRYPT:
  29864. /* perform software based RSA private op */
  29865. ret = wc_RsaFunction(
  29866. info->pk.rsa.in, info->pk.rsa.inLen,
  29867. info->pk.rsa.out, info->pk.rsa.outLen,
  29868. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  29869. break;
  29870. }
  29871. /* reset devId */
  29872. info->pk.rsa.key->devId = devIdArg;
  29873. }
  29874. #ifdef WOLFSSL_KEY_GEN
  29875. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  29876. info->pk.rsakg.key->devId = INVALID_DEVID;
  29877. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  29878. info->pk.rsakg.e, info->pk.rsakg.rng);
  29879. /* reset devId */
  29880. info->pk.rsakg.key->devId = devIdArg;
  29881. }
  29882. #endif
  29883. #endif /* !NO_RSA */
  29884. #ifdef HAVE_ECC
  29885. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  29886. /* set devId to invalid, so software is used */
  29887. info->pk.eckg.key->devId = INVALID_DEVID;
  29888. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  29889. info->pk.eckg.key, info->pk.eckg.curveId);
  29890. /* reset devId */
  29891. info->pk.eckg.key->devId = devIdArg;
  29892. }
  29893. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  29894. /* set devId to invalid, so software is used */
  29895. info->pk.eccsign.key->devId = INVALID_DEVID;
  29896. ret = wc_ecc_sign_hash(
  29897. info->pk.eccsign.in, info->pk.eccsign.inlen,
  29898. info->pk.eccsign.out, info->pk.eccsign.outlen,
  29899. info->pk.eccsign.rng, info->pk.eccsign.key);
  29900. /* reset devId */
  29901. info->pk.eccsign.key->devId = devIdArg;
  29902. }
  29903. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  29904. /* set devId to invalid, so software is used */
  29905. info->pk.eccverify.key->devId = INVALID_DEVID;
  29906. ret = wc_ecc_verify_hash(
  29907. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  29908. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  29909. info->pk.eccverify.res, info->pk.eccverify.key);
  29910. /* reset devId */
  29911. info->pk.eccverify.key->devId = devIdArg;
  29912. }
  29913. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  29914. /* set devId to invalid, so software is used */
  29915. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  29916. ret = wc_ecc_shared_secret(
  29917. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  29918. info->pk.ecdh.out, info->pk.ecdh.outlen);
  29919. /* reset devId */
  29920. info->pk.ecdh.private_key->devId = devIdArg;
  29921. }
  29922. #endif /* HAVE_ECC */
  29923. }
  29924. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  29925. #if !defined(NO_AES) || !defined(NO_DES3)
  29926. #ifdef HAVE_AESGCM
  29927. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  29928. if (info->cipher.enc) {
  29929. /* set devId to invalid, so software is used */
  29930. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  29931. ret = wc_AesGcmEncrypt(
  29932. info->cipher.aesgcm_enc.aes,
  29933. info->cipher.aesgcm_enc.out,
  29934. info->cipher.aesgcm_enc.in,
  29935. info->cipher.aesgcm_enc.sz,
  29936. info->cipher.aesgcm_enc.iv,
  29937. info->cipher.aesgcm_enc.ivSz,
  29938. info->cipher.aesgcm_enc.authTag,
  29939. info->cipher.aesgcm_enc.authTagSz,
  29940. info->cipher.aesgcm_enc.authIn,
  29941. info->cipher.aesgcm_enc.authInSz);
  29942. /* reset devId */
  29943. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  29944. }
  29945. else {
  29946. /* set devId to invalid, so software is used */
  29947. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  29948. ret = wc_AesGcmDecrypt(
  29949. info->cipher.aesgcm_dec.aes,
  29950. info->cipher.aesgcm_dec.out,
  29951. info->cipher.aesgcm_dec.in,
  29952. info->cipher.aesgcm_dec.sz,
  29953. info->cipher.aesgcm_dec.iv,
  29954. info->cipher.aesgcm_dec.ivSz,
  29955. info->cipher.aesgcm_dec.authTag,
  29956. info->cipher.aesgcm_dec.authTagSz,
  29957. info->cipher.aesgcm_dec.authIn,
  29958. info->cipher.aesgcm_dec.authInSz);
  29959. /* reset devId */
  29960. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  29961. }
  29962. }
  29963. #endif /* HAVE_AESGCM */
  29964. #ifdef HAVE_AES_CBC
  29965. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  29966. if (info->cipher.enc) {
  29967. /* set devId to invalid, so software is used */
  29968. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  29969. ret = wc_AesCbcEncrypt(
  29970. info->cipher.aescbc.aes,
  29971. info->cipher.aescbc.out,
  29972. info->cipher.aescbc.in,
  29973. info->cipher.aescbc.sz);
  29974. /* reset devId */
  29975. info->cipher.aescbc.aes->devId = devIdArg;
  29976. }
  29977. else {
  29978. /* set devId to invalid, so software is used */
  29979. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  29980. ret = wc_AesCbcDecrypt(
  29981. info->cipher.aescbc.aes,
  29982. info->cipher.aescbc.out,
  29983. info->cipher.aescbc.in,
  29984. info->cipher.aescbc.sz);
  29985. /* reset devId */
  29986. info->cipher.aescbc.aes->devId = devIdArg;
  29987. }
  29988. }
  29989. #endif /* HAVE_AES_CBC */
  29990. #ifndef NO_DES3
  29991. if (info->cipher.type == WC_CIPHER_DES3) {
  29992. if (info->cipher.enc) {
  29993. /* set devId to invalid, so software is used */
  29994. info->cipher.des3.des->devId = INVALID_DEVID;
  29995. ret = wc_Des3_CbcEncrypt(
  29996. info->cipher.des3.des,
  29997. info->cipher.des3.out,
  29998. info->cipher.des3.in,
  29999. info->cipher.des3.sz);
  30000. /* reset devId */
  30001. info->cipher.des3.des->devId = devIdArg;
  30002. }
  30003. else {
  30004. /* set devId to invalid, so software is used */
  30005. info->cipher.des3.des->devId = INVALID_DEVID;
  30006. ret = wc_Des3_CbcDecrypt(
  30007. info->cipher.des3.des,
  30008. info->cipher.des3.out,
  30009. info->cipher.des3.in,
  30010. info->cipher.des3.sz);
  30011. /* reset devId */
  30012. info->cipher.des3.des->devId = devIdArg;
  30013. }
  30014. }
  30015. #endif /* !NO_DES3 */
  30016. #endif /* !NO_AES || !NO_DES3 */
  30017. }
  30018. #if !defined(NO_SHA) || !defined(NO_SHA256)
  30019. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  30020. #if !defined(NO_SHA)
  30021. if (info->hash.type == WC_HASH_TYPE_SHA) {
  30022. if (info->hash.sha1 == NULL)
  30023. return NOT_COMPILED_IN;
  30024. /* set devId to invalid, so software is used */
  30025. info->hash.sha1->devId = INVALID_DEVID;
  30026. if (info->hash.in != NULL) {
  30027. ret = wc_ShaUpdate(
  30028. info->hash.sha1,
  30029. info->hash.in,
  30030. info->hash.inSz);
  30031. }
  30032. if (info->hash.digest != NULL) {
  30033. ret = wc_ShaFinal(
  30034. info->hash.sha1,
  30035. info->hash.digest);
  30036. }
  30037. /* reset devId */
  30038. info->hash.sha1->devId = devIdArg;
  30039. }
  30040. else
  30041. #endif
  30042. #if !defined(NO_SHA256)
  30043. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  30044. if (info->hash.sha256 == NULL)
  30045. return NOT_COMPILED_IN;
  30046. /* set devId to invalid, so software is used */
  30047. info->hash.sha256->devId = INVALID_DEVID;
  30048. if (info->hash.in != NULL) {
  30049. ret = wc_Sha256Update(
  30050. info->hash.sha256,
  30051. info->hash.in,
  30052. info->hash.inSz);
  30053. }
  30054. if (info->hash.digest != NULL) {
  30055. ret = wc_Sha256Final(
  30056. info->hash.sha256,
  30057. info->hash.digest);
  30058. }
  30059. /* reset devId */
  30060. info->hash.sha256->devId = devIdArg;
  30061. }
  30062. else
  30063. #endif
  30064. {
  30065. }
  30066. }
  30067. #endif /* !NO_SHA || !NO_SHA256 */
  30068. #ifndef NO_HMAC
  30069. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  30070. if (info->hmac.hmac == NULL)
  30071. return NOT_COMPILED_IN;
  30072. /* set devId to invalid, so software is used */
  30073. info->hmac.hmac->devId = INVALID_DEVID;
  30074. if (info->hash.in != NULL) {
  30075. ret = wc_HmacUpdate(
  30076. info->hmac.hmac,
  30077. info->hmac.in,
  30078. info->hmac.inSz);
  30079. }
  30080. else if (info->hash.digest != NULL) {
  30081. ret = wc_HmacFinal(
  30082. info->hmac.hmac,
  30083. info->hmac.digest);
  30084. }
  30085. /* reset devId */
  30086. info->hmac.hmac->devId = devIdArg;
  30087. }
  30088. #endif
  30089. (void)devIdArg;
  30090. (void)myCtx;
  30091. return ret;
  30092. }
  30093. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  30094. {
  30095. int ret = 0;
  30096. myCryptoDevCtx myCtx;
  30097. /* example data for callback */
  30098. myCtx.exampleVar = 1;
  30099. /* set devId to something other than INVALID_DEVID */
  30100. devId = 1;
  30101. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  30102. #ifndef WC_NO_RNG
  30103. if (ret == 0)
  30104. ret = random_test();
  30105. #endif /* WC_NO_RNG */
  30106. #ifndef NO_RSA
  30107. if (ret == 0)
  30108. ret = rsa_test();
  30109. #endif
  30110. #ifdef HAVE_ECC
  30111. if (ret == 0)
  30112. ret = ecc_test();
  30113. #endif
  30114. #ifndef NO_AES
  30115. #ifdef HAVE_AESGCM
  30116. if (ret == 0)
  30117. ret = aesgcm_test();
  30118. #endif
  30119. #ifdef HAVE_AES_CBC
  30120. if (ret == 0)
  30121. ret = aes_test();
  30122. #endif
  30123. #endif /* !NO_AES */
  30124. #ifndef NO_DES3
  30125. if (ret == 0)
  30126. ret = des3_test();
  30127. #endif /* !NO_DES3 */
  30128. #if !defined(NO_SHA) || !defined(NO_SHA256)
  30129. #ifndef NO_SHA
  30130. if (ret == 0)
  30131. ret = sha_test();
  30132. #endif
  30133. #ifndef NO_SHA256
  30134. if (ret == 0)
  30135. ret = sha256_test();
  30136. #endif
  30137. #endif
  30138. #ifndef NO_HMAC
  30139. #ifndef NO_SHA
  30140. if (ret == 0)
  30141. ret = hmac_sha_test();
  30142. #endif
  30143. #ifndef NO_SHA256
  30144. if (ret == 0)
  30145. ret = hmac_sha256_test();
  30146. #endif
  30147. #endif
  30148. #ifndef NO_PWDBASED
  30149. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  30150. if (ret == 0)
  30151. ret = pbkdf2_test();
  30152. #endif
  30153. #endif
  30154. /* reset devId */
  30155. devId = INVALID_DEVID;
  30156. return ret;
  30157. }
  30158. #endif /* WOLF_CRYPTO_CB */
  30159. #ifdef WOLFSSL_CERT_PIV
  30160. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  30161. {
  30162. int ret;
  30163. wc_CertPIV piv;
  30164. /* Template for Identiv PIV cert, nonce and signature */
  30165. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  30166. 0x0A, 0x0D,
  30167. 0x53, 0x04, /* NIST PIV Cert */
  30168. 0x70, 0x02, /* Certificate */
  30169. 0x30, 0x00,
  30170. 0x71, 0x01, 0x00, /* Cert Info */
  30171. 0xFE, 0x00, /* Error Detection */
  30172. 0x0B, 0x01, 0x00, /* Nonce */
  30173. 0x0C, 0x01, 0x00, /* Signed Nonce */
  30174. };
  30175. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  30176. 0x53, 0x04, /* NIST PIV Cert */
  30177. 0x70, 0x02, /* Certificate */
  30178. 0x30, 0x00,
  30179. 0x71, 0x01, 0x00, /* Cert Info */
  30180. 0xFE, 0x00, /* Error Detection */
  30181. };
  30182. /* Test with identiv 0x0A, 0x0B and 0x0C markers */
  30183. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  30184. if (ret == 0) {
  30185. /* Test with NIST PIV format */
  30186. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  30187. }
  30188. return ret;
  30189. }
  30190. #endif /* WOLFSSL_CERT_PIV */
  30191. #undef ERROR_OUT
  30192. #else
  30193. #ifndef NO_MAIN_DRIVER
  30194. int main() { return 0; }
  30195. #endif
  30196. #endif /* NO_CRYPT_TEST */