test.c 1.1 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625
  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) || defined(WC_NO_RNG)
  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. #ifdef WOLFSSL_QNX_CAAM
  305. static int devId = WOLFSSL_CAAM_DEVID;
  306. #else
  307. static int devId = INVALID_DEVID;
  308. #endif
  309. #ifdef HAVE_WNR
  310. const char* wnrConfigFile = "wnr-example.conf";
  311. #endif
  312. #define TEST_STRING "Everyone gets Friday off."
  313. #define TEST_STRING_SZ 25
  314. typedef struct testVector {
  315. const char* input;
  316. const char* output;
  317. size_t inLen;
  318. size_t outLen;
  319. } testVector;
  320. #ifndef WOLFSSL_TEST_SUBROUTINE
  321. #define WOLFSSL_TEST_SUBROUTINE
  322. #endif
  323. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  324. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  325. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  326. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  327. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  328. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  329. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  330. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  331. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  332. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  333. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  334. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  335. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  336. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  337. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  338. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  339. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  340. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  341. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  342. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  343. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  344. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  345. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void);
  346. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  347. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  348. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  349. WOLFSSL_TEST_SUBROUTINE int hc128_test(void);
  350. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void);
  351. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  352. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  353. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  354. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  355. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  356. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  357. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  358. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  359. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  360. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  361. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  362. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  363. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  364. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  365. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  366. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  367. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  368. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  369. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  370. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  371. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  372. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  373. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  374. #ifndef WC_NO_RNG
  375. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  376. #endif /* WC_NO_RNG */
  377. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  379. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  380. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  381. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  385. #endif
  386. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  390. #ifdef HAVE_ECC
  391. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  392. #ifdef HAVE_ECC_ENCRYPT
  393. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  394. #endif
  395. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  396. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  397. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  398. /* skip for ATECC508/608A, cannot import private key buffers */
  399. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  400. #endif
  401. #endif
  402. #ifdef HAVE_CURVE25519
  403. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  404. #endif
  405. #ifdef HAVE_ED25519
  406. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  407. #endif
  408. #ifdef HAVE_CURVE448
  409. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  410. #endif
  411. #ifdef HAVE_ED448
  412. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  413. #endif
  414. #ifdef HAVE_BLAKE2
  415. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  416. #endif
  417. #ifdef HAVE_BLAKE2S
  418. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  419. #endif
  420. #ifdef HAVE_LIBZ
  421. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  422. #endif
  423. #ifdef HAVE_PKCS7
  424. #ifndef NO_PKCS7_ENCRYPTED_DATA
  425. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  426. #endif
  427. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  428. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  429. #endif
  430. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  431. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  432. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  433. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  434. #endif
  435. #ifndef NO_AES
  436. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  437. word32 keySz);
  438. #endif
  439. #endif
  440. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  441. !defined(NO_FILESYSTEM)
  442. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  443. #endif
  444. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  445. !defined(NO_FILESYSTEM)
  446. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  447. #endif
  448. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  449. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  450. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  451. #endif
  452. #ifdef HAVE_IDEA
  453. WOLFSSL_TEST_SUBROUTINE int idea_test(void);
  454. #endif
  455. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  456. #ifdef HAVE_VALGRIND
  457. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  458. #endif
  459. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  460. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  461. #endif
  462. #ifdef ASN_BER_TO_DER
  463. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  464. #endif
  465. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  466. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  467. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  468. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  469. #endif
  470. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  471. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  472. #endif
  473. #ifdef WOLF_CRYPTO_CB
  474. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  475. #endif
  476. #ifdef WOLFSSL_CERT_PIV
  477. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  478. #endif
  479. /* General big buffer size for many tests. */
  480. #define FOURK_BUF 4096
  481. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  482. #ifdef HAVE_STACK_SIZE
  483. static THREAD_RETURN err_sys(const char* msg, int es)
  484. #else
  485. static int err_sys(const char* msg, int es)
  486. #endif
  487. {
  488. (void)msg;
  489. (void)es;
  490. #ifdef WOLFSSL_LINUXKM
  491. lkm_printf("%s error = %d\n", msg, es);
  492. EXIT_TEST(es);
  493. #else
  494. printf("%s error = %d\n", msg, es);
  495. EXIT_TEST(-1);
  496. #endif
  497. }
  498. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  499. /* func_args from test.h, so don't have to pull in other stuff */
  500. typedef struct func_args {
  501. int argc;
  502. char** argv;
  503. int return_code;
  504. } func_args;
  505. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  506. #ifdef HAVE_FIPS
  507. static void myFipsCb(int ok, int err, const char* hash)
  508. {
  509. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  510. printf("message = %s\n", wc_GetErrorString(err));
  511. printf("hash = %s\n", hash);
  512. if (err == IN_CORE_FIPS_E) {
  513. printf("In core integrity hash check failure, copy above hash\n");
  514. printf("into verifyCore[] in fips_test.c and rebuild\n");
  515. }
  516. }
  517. #endif /* HAVE_FIPS */
  518. #ifdef WOLFSSL_STATIC_MEMORY
  519. #ifdef BENCH_EMBEDDED
  520. static byte gTestMemory[14000];
  521. #elif defined(WOLFSSL_CERT_EXT)
  522. static byte gTestMemory[140000];
  523. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  524. static byte gTestMemory[160000];
  525. #else
  526. static byte gTestMemory[80000];
  527. #endif
  528. #endif
  529. #ifdef WOLFSSL_PB
  530. static int wolfssl_pb_print(const char* msg, ...)
  531. {
  532. int ret;
  533. va_list args;
  534. char tmpBuf[80];
  535. va_start(args, msg);
  536. ret = vsprint(tmpBuf, msg, args);
  537. va_end(args);
  538. fnDumpStringToSystemLog(tmpBuf);
  539. return ret;
  540. }
  541. #endif /* WOLFSSL_PB */
  542. /* optional macro to add sleep between tests */
  543. #ifdef TEST_SLEEP
  544. #include <stdarg.h> /* for var args */
  545. static WC_INLINE void test_pass(const char* fmt, ...)
  546. {
  547. va_list args;
  548. va_start(args, fmt);
  549. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  550. va_end(args);
  551. PRINT_HEAP_CHECKPOINT();
  552. TEST_SLEEP();
  553. }
  554. #else
  555. /* redirect to printf */
  556. #define test_pass(...) { \
  557. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  558. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  559. return err_sys("post-test check failed", -1); \
  560. } \
  561. PRINT_HEAP_CHECKPOINT(); \
  562. }
  563. /* stub the sleep macro */
  564. #define TEST_SLEEP()
  565. #endif
  566. #ifdef HAVE_STACK_SIZE
  567. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  568. #else
  569. int wolfcrypt_test(void* args)
  570. #endif
  571. {
  572. int ret;
  573. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  574. long heap_baselineAllocs, heap_baselineBytes;
  575. #endif
  576. STACK_SIZE_INIT();
  577. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  578. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  579. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  580. (void)wolfCrypt_heap_peakBytes_checkpoint();
  581. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  582. #endif
  583. printf("------------------------------------------------------------------------------\n");
  584. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  585. printf("------------------------------------------------------------------------------\n");
  586. if (args) {
  587. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  588. int ch;
  589. #endif
  590. ((func_args*)args)->return_code = -1; /* error state */
  591. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  592. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  593. switch(ch) {
  594. case 's':
  595. #ifdef HAVE_STACK_SIZE_VERBOSE
  596. max_relative_stack = (ssize_t)atoi(myoptarg);
  597. break;
  598. #else
  599. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  600. #endif
  601. case 'm':
  602. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  603. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  604. break;
  605. #else
  606. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  607. #endif
  608. case 'a':
  609. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  610. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  611. break;
  612. #else
  613. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  614. #endif
  615. case 'h':
  616. return err_sys("\
  617. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  618. [-a max_relative_heap_allocs] [-h]\n", 0);
  619. default:
  620. return err_sys("unknown test option. try -h.", -1);
  621. }
  622. }
  623. #endif
  624. }
  625. #ifdef WOLFSSL_STATIC_MEMORY
  626. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  627. WOLFMEM_GENERAL, 1) != 0) {
  628. printf("unable to load static memory.\n");
  629. return(EXIT_FAILURE);
  630. }
  631. #endif
  632. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  633. wolfSSL_Debugging_ON();
  634. #endif
  635. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  636. wc_SetLoggingHeap(HEAP_HINT);
  637. #endif
  638. #ifdef HAVE_FIPS
  639. wolfCrypt_SetCb_fips(myFipsCb);
  640. #endif
  641. #if !defined(NO_BIG_INT)
  642. if (CheckCtcSettings() != 1) {
  643. printf("Sizeof mismatch (build) %x != (run) %x\n",
  644. CTC_SETTINGS, CheckRunTimeSettings());
  645. return err_sys("Build vs runtime math mismatch\n", -1000);
  646. }
  647. #if defined(USE_FAST_MATH) && \
  648. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  649. if (CheckFastMathSettings() != 1)
  650. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  651. -1001);
  652. #endif /* USE_FAST_MATH */
  653. #endif /* !NO_BIG_INT */
  654. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  655. initDefaultName();
  656. #endif
  657. #ifdef WOLFSSL_ASYNC_CRYPT
  658. ret = wolfAsync_DevOpen(&devId);
  659. if (ret < 0) {
  660. printf("Async device open failed\nRunning without async\n");
  661. }
  662. #else
  663. (void)devId;
  664. #endif /* WOLFSSL_ASYNC_CRYPT */
  665. #ifdef WOLF_CRYPTO_CB
  666. #ifdef HAVE_INTEL_QA_SYNC
  667. devId = wc_CryptoCb_InitIntelQa();
  668. if (INVALID_DEVID == devId) {
  669. printf("Couldn't init the Intel QA\n");
  670. }
  671. #endif
  672. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  673. devId = wc_CryptoCb_InitOcteon();
  674. if (INVALID_DEVID == devId) {
  675. printf("Couldn't init the Cavium Octeon\n");
  676. }
  677. #endif
  678. #endif
  679. #ifdef HAVE_SELFTEST
  680. if ( (ret = wolfCrypt_SelfTest()) != 0)
  681. return err_sys("CAVP selftest failed!\n", ret);
  682. else
  683. test_pass("CAVP selftest passed!\n");
  684. #endif
  685. if ( (ret = error_test()) != 0)
  686. return err_sys("error test failed!\n", ret);
  687. else
  688. test_pass("error test passed!\n");
  689. if ( (ret = memory_test()) != 0)
  690. return err_sys("MEMORY test failed!\n", ret);
  691. else
  692. test_pass("MEMORY test passed!\n");
  693. #ifndef NO_CODING
  694. if ( (ret = base64_test()) != 0)
  695. return err_sys("base64 test failed!\n", ret);
  696. else
  697. test_pass("base64 test passed!\n");
  698. #ifdef WOLFSSL_BASE16
  699. if ( (ret = base16_test()) != 0)
  700. return err_sys("base16 test failed!\n", ret);
  701. else
  702. test_pass("base16 test passed!\n");
  703. #endif
  704. #endif /* !NO_CODING */
  705. #ifndef NO_ASN
  706. if ( (ret = asn_test()) != 0)
  707. return err_sys("asn test failed!\n", ret);
  708. else
  709. test_pass("asn test passed!\n");
  710. #endif
  711. #ifndef WC_NO_RNG
  712. if ( (ret = random_test()) != 0)
  713. return err_sys("RANDOM test failed!\n", ret);
  714. else
  715. test_pass("RANDOM test passed!\n");
  716. #endif /* WC_NO_RNG */
  717. #ifndef NO_MD5
  718. if ( (ret = md5_test()) != 0)
  719. return err_sys("MD5 test failed!\n", ret);
  720. else
  721. test_pass("MD5 test passed!\n");
  722. #endif
  723. #ifdef WOLFSSL_MD2
  724. if ( (ret = md2_test()) != 0)
  725. return err_sys("MD2 test failed!\n", ret);
  726. else
  727. test_pass("MD2 test passed!\n");
  728. #endif
  729. #ifndef NO_MD4
  730. if ( (ret = md4_test()) != 0)
  731. return err_sys("MD4 test failed!\n", ret);
  732. else
  733. test_pass("MD4 test passed!\n");
  734. #endif
  735. #ifndef NO_SHA
  736. if ( (ret = sha_test()) != 0)
  737. return err_sys("SHA test failed!\n", ret);
  738. else
  739. test_pass("SHA test passed!\n");
  740. #endif
  741. #ifdef WOLFSSL_SHA224
  742. if ( (ret = sha224_test()) != 0)
  743. return err_sys("SHA-224 test failed!\n", ret);
  744. else
  745. test_pass("SHA-224 test passed!\n");
  746. #endif
  747. #ifndef NO_SHA256
  748. if ( (ret = sha256_test()) != 0)
  749. return err_sys("SHA-256 test failed!\n", ret);
  750. else
  751. test_pass("SHA-256 test passed!\n");
  752. #endif
  753. #ifdef WOLFSSL_SHA384
  754. if ( (ret = sha384_test()) != 0)
  755. return err_sys("SHA-384 test failed!\n", ret);
  756. else
  757. test_pass("SHA-384 test passed!\n");
  758. #endif
  759. #ifdef WOLFSSL_SHA512
  760. if ( (ret = sha512_test()) != 0)
  761. return err_sys("SHA-512 test failed!\n", ret);
  762. else
  763. test_pass("SHA-512 test passed!\n");
  764. #endif
  765. #ifdef WOLFSSL_SHA3
  766. if ( (ret = sha3_test()) != 0)
  767. return err_sys("SHA-3 test failed!\n", ret);
  768. else
  769. test_pass("SHA-3 test passed!\n");
  770. #endif
  771. #ifdef WOLFSSL_SHAKE256
  772. if ( (ret = shake256_test()) != 0)
  773. return err_sys("SHAKE256 test failed!\n", ret);
  774. else
  775. test_pass("SHAKE256 test passed!\n");
  776. #endif
  777. #ifndef NO_HASH_WRAPPER
  778. if ( (ret = hash_test()) != 0)
  779. return err_sys("Hash test failed!\n", ret);
  780. else
  781. test_pass("Hash test passed!\n");
  782. #endif
  783. #ifdef WOLFSSL_RIPEMD
  784. if ( (ret = ripemd_test()) != 0)
  785. return err_sys("RIPEMD test failed!\n", ret);
  786. else
  787. test_pass("RIPEMD test passed!\n");
  788. #endif
  789. #ifdef HAVE_BLAKE2
  790. if ( (ret = blake2b_test()) != 0)
  791. return err_sys("BLAKE2b test failed!\n", ret);
  792. else
  793. test_pass("BLAKE2b test passed!\n");
  794. #endif
  795. #ifdef HAVE_BLAKE2S
  796. if ( (ret = blake2s_test()) != 0)
  797. return err_sys("BLAKE2s test failed!\n", ret);
  798. else
  799. test_pass("BLAKE2s test passed!\n");
  800. #endif
  801. #ifndef NO_HMAC
  802. #ifndef NO_MD5
  803. if ( (ret = hmac_md5_test()) != 0)
  804. return err_sys("HMAC-MD5 test failed!\n", ret);
  805. else
  806. test_pass("HMAC-MD5 test passed!\n");
  807. #endif
  808. #ifndef NO_SHA
  809. if ( (ret = hmac_sha_test()) != 0)
  810. return err_sys("HMAC-SHA test failed!\n", ret);
  811. else
  812. test_pass("HMAC-SHA test passed!\n");
  813. #endif
  814. #ifdef WOLFSSL_SHA224
  815. if ( (ret = hmac_sha224_test()) != 0)
  816. return err_sys("HMAC-SHA224 test failed!\n", ret);
  817. else
  818. test_pass("HMAC-SHA224 test passed!\n");
  819. #endif
  820. #ifndef NO_SHA256
  821. if ( (ret = hmac_sha256_test()) != 0)
  822. return err_sys("HMAC-SHA256 test failed!\n", ret);
  823. else
  824. test_pass("HMAC-SHA256 test passed!\n");
  825. #endif
  826. #ifdef WOLFSSL_SHA384
  827. if ( (ret = hmac_sha384_test()) != 0)
  828. return err_sys("HMAC-SHA384 test failed!\n", ret);
  829. else
  830. test_pass("HMAC-SHA384 test passed!\n");
  831. #endif
  832. #ifdef WOLFSSL_SHA512
  833. if ( (ret = hmac_sha512_test()) != 0)
  834. return err_sys("HMAC-SHA512 test failed!\n", ret);
  835. else
  836. test_pass("HMAC-SHA512 test passed!\n");
  837. #endif
  838. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  839. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  840. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  841. if ( (ret = hmac_sha3_test()) != 0)
  842. return err_sys("HMAC-SHA3 test failed!\n", ret);
  843. else
  844. test_pass("HMAC-SHA3 test passed!\n");
  845. #endif
  846. #ifdef HAVE_HKDF
  847. if ( (ret = hkdf_test()) != 0)
  848. return err_sys("HMAC-KDF test failed!\n", ret);
  849. else
  850. test_pass("HMAC-KDF test passed!\n");
  851. #endif
  852. #endif /* !NO_HMAC */
  853. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  854. if ( (ret = x963kdf_test()) != 0)
  855. return err_sys("X963-KDF test failed!\n", ret);
  856. else
  857. test_pass("X963-KDF test passed!\n");
  858. #endif
  859. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  860. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  861. if ( (ret = gmac_test()) != 0)
  862. return err_sys("GMAC test failed!\n", ret);
  863. else
  864. test_pass("GMAC test passed!\n");
  865. #endif
  866. #ifdef WC_RC2
  867. if ( (ret = rc2_test()) != 0)
  868. return err_sys("RC2 test failed!\n", ret);
  869. else
  870. test_pass("RC2 test passed!\n");
  871. #endif
  872. #ifndef NO_RC4
  873. if ( (ret = arc4_test()) != 0)
  874. return err_sys("ARC4 test failed!\n", ret);
  875. else
  876. test_pass("ARC4 test passed!\n");
  877. #endif
  878. #ifndef NO_HC128
  879. if ( (ret = hc128_test()) != 0)
  880. return err_sys("HC-128 test failed!\n", ret);
  881. else
  882. test_pass("HC-128 test passed!\n");
  883. #endif
  884. #ifndef NO_RABBIT
  885. if ( (ret = rabbit_test()) != 0)
  886. return err_sys("Rabbit test failed!\n", ret);
  887. else
  888. test_pass("Rabbit test passed!\n");
  889. #endif
  890. #ifdef HAVE_CHACHA
  891. if ( (ret = chacha_test()) != 0)
  892. return err_sys("Chacha test failed!\n", ret);
  893. else
  894. test_pass("Chacha test passed!\n");
  895. #endif
  896. #ifdef HAVE_XCHACHA
  897. if ( (ret = XChaCha_test()) != 0)
  898. return err_sys("XChacha test failed!\n", ret);
  899. else
  900. test_pass("XChacha test passed!\n");
  901. #endif
  902. #ifdef HAVE_POLY1305
  903. if ( (ret = poly1305_test()) != 0)
  904. return err_sys("POLY1305 test failed!\n", ret);
  905. else
  906. test_pass("POLY1305 test passed!\n");
  907. #endif
  908. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  909. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  910. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  911. else
  912. test_pass("ChaCha20-Poly1305 AEAD test passed!\n");
  913. #endif
  914. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  915. if ( (ret = XChaCha20Poly1305_test()) != 0)
  916. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  917. else
  918. test_pass("XChaCha20-Poly1305 AEAD test passed!\n");
  919. #endif
  920. #ifndef NO_DES3
  921. if ( (ret = des_test()) != 0)
  922. return err_sys("DES test failed!\n", ret);
  923. else
  924. test_pass("DES test passed!\n");
  925. #endif
  926. #ifndef NO_DES3
  927. if ( (ret = des3_test()) != 0)
  928. return err_sys("DES3 test failed!\n", ret);
  929. else
  930. test_pass("DES3 test passed!\n");
  931. #endif
  932. #ifndef NO_AES
  933. if ( (ret = aes_test()) != 0)
  934. return err_sys("AES test failed!\n", ret);
  935. else
  936. test_pass("AES test passed!\n");
  937. #ifdef WOLFSSL_AES_192
  938. if ( (ret = aes192_test()) != 0)
  939. return err_sys("AES192 test failed!\n", ret);
  940. else
  941. test_pass("AES192 test passed!\n");
  942. #endif
  943. #ifdef WOLFSSL_AES_256
  944. if ( (ret = aes256_test()) != 0)
  945. return err_sys("AES256 test failed!\n", ret);
  946. else
  947. test_pass("AES256 test passed!\n");
  948. #endif
  949. #ifdef WOLFSSL_AES_OFB
  950. if ( (ret = aesofb_test()) != 0)
  951. return err_sys("AES-OFB test failed!\n", ret);
  952. else
  953. test_pass("AESOFB test passed!\n");
  954. #endif
  955. #ifdef HAVE_AESGCM
  956. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  957. if ( (ret = aesgcm_test()) != 0)
  958. return err_sys("AES-GCM test failed!\n", ret);
  959. #endif
  960. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  961. !(defined(WOLF_CRYPTO_CB) && \
  962. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  963. if ((ret = aesgcm_default_test()) != 0) {
  964. return err_sys("AES-GCM test failed!\n", ret);
  965. }
  966. #endif
  967. test_pass("AES-GCM test passed!\n");
  968. #endif
  969. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  970. if ( (ret = aesccm_test()) != 0)
  971. return err_sys("AES-CCM test failed!\n", ret);
  972. else
  973. test_pass("AES-CCM test passed!\n");
  974. #endif
  975. #ifdef HAVE_AES_KEYWRAP
  976. if ( (ret = aeskeywrap_test()) != 0)
  977. return err_sys("AES Key Wrap test failed!\n", ret);
  978. else
  979. test_pass("AES Key Wrap test passed!\n");
  980. #endif
  981. #endif
  982. #ifdef HAVE_CAMELLIA
  983. if ( (ret = camellia_test()) != 0)
  984. return err_sys("CAMELLIA test failed!\n", ret);
  985. else
  986. test_pass("CAMELLIA test passed!\n");
  987. #endif
  988. #ifdef HAVE_IDEA
  989. if ( (ret = idea_test()) != 0)
  990. return err_sys("IDEA test failed!\n", ret);
  991. else
  992. test_pass("IDEA test passed!\n");
  993. #endif
  994. #ifndef NO_RSA
  995. #ifdef WC_RSA_NO_PADDING
  996. if ( (ret = rsa_no_pad_test()) != 0)
  997. return err_sys("RSA NOPAD test failed!\n", ret);
  998. else
  999. test_pass("RSA NOPAD test passed!\n");
  1000. #endif
  1001. if ( (ret = rsa_test()) != 0)
  1002. return err_sys("RSA test failed!\n", ret);
  1003. else
  1004. test_pass("RSA test passed!\n");
  1005. #endif
  1006. #ifndef NO_DH
  1007. if ( (ret = dh_test()) != 0)
  1008. return err_sys("DH test failed!\n", ret);
  1009. else
  1010. test_pass("DH test passed!\n");
  1011. #endif
  1012. #ifndef NO_DSA
  1013. if ( (ret = dsa_test()) != 0)
  1014. return err_sys("DSA test failed!\n", ret);
  1015. else
  1016. test_pass("DSA test passed!\n");
  1017. #endif
  1018. #ifdef WOLFCRYPT_HAVE_SRP
  1019. if ( (ret = srp_test()) != 0)
  1020. return err_sys("SRP test failed!\n", ret);
  1021. else
  1022. test_pass("SRP test passed!\n");
  1023. #endif
  1024. #ifndef NO_PWDBASED
  1025. if ( (ret = pwdbased_test()) != 0)
  1026. return err_sys("PWDBASED test failed!\n", ret);
  1027. else
  1028. test_pass("PWDBASED test passed!\n");
  1029. #endif
  1030. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1031. if ( (ret = openssl_test()) != 0)
  1032. return err_sys("OPENSSL test failed!\n", ret);
  1033. else
  1034. test_pass("OPENSSL test passed!\n");
  1035. if ( (ret = openSSL_evpMD_test()) != 0)
  1036. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1037. else
  1038. test_pass("OPENSSL (EVP MD) passed!\n");
  1039. if ( (ret = openssl_pkey0_test()) != 0)
  1040. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1041. else
  1042. test_pass("OPENSSL (PKEY0) passed!\n");
  1043. if ( (ret = openssl_pkey1_test()) != 0)
  1044. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1045. else
  1046. test_pass("OPENSSL (PKEY1) passed!\n");
  1047. if ( (ret = openssl_evpSig_test()) != 0)
  1048. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1049. else
  1050. test_pass("OPENSSL (EVP Sign/Verify) passed!\n");
  1051. #endif
  1052. #ifdef HAVE_ECC
  1053. if ( (ret = ecc_test()) != 0)
  1054. return err_sys("ECC test failed!\n", ret);
  1055. else
  1056. test_pass("ECC test passed!\n");
  1057. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  1058. if ( (ret = ecc_encrypt_test()) != 0)
  1059. return err_sys("ECC Enc test failed!\n", ret);
  1060. else
  1061. test_pass("ECC Enc test passed!\n");
  1062. #endif
  1063. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1064. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1065. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  1066. /* skip for ATECC508/608A, cannot import private key buffers */
  1067. if ( (ret = ecc_test_buffers()) != 0)
  1068. return err_sys("ECC buffer test failed!\n", ret);
  1069. else
  1070. test_pass("ECC buffer test passed!\n");
  1071. #endif
  1072. #endif
  1073. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1074. !defined(NO_FILESYSTEM)
  1075. if ( (ret = cert_test()) != 0)
  1076. return err_sys("CERT test failed!\n", ret);
  1077. else
  1078. test_pass("CERT test passed!\n");
  1079. #endif
  1080. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1081. !defined(NO_FILESYSTEM)
  1082. if ( (ret = certext_test()) != 0)
  1083. return err_sys("CERT EXT test failed!\n", ret);
  1084. else
  1085. test_pass("CERT EXT test passed!\n");
  1086. #endif
  1087. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1088. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1089. if ( (ret = decodedCertCache_test()) != 0)
  1090. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1091. else
  1092. test_pass("DECODED CERT CACHE test passed!\n");
  1093. #endif
  1094. #ifdef HAVE_CURVE25519
  1095. if ( (ret = curve25519_test()) != 0)
  1096. return err_sys("CURVE25519 test failed!\n", ret);
  1097. else
  1098. test_pass("CURVE25519 test passed!\n");
  1099. #endif
  1100. #ifdef HAVE_ED25519
  1101. if ( (ret = ed25519_test()) != 0)
  1102. return err_sys("ED25519 test failed!\n", ret);
  1103. else
  1104. test_pass("ED25519 test passed!\n");
  1105. #endif
  1106. #ifdef HAVE_CURVE448
  1107. if ( (ret = curve448_test()) != 0)
  1108. return err_sys("CURVE448 test failed!\n", ret);
  1109. else
  1110. test_pass("CURVE448 test passed!\n");
  1111. #endif
  1112. #ifdef HAVE_ED448
  1113. if ( (ret = ed448_test()) != 0)
  1114. return err_sys("ED448 test failed!\n", ret);
  1115. else
  1116. test_pass("ED448 test passed!\n");
  1117. #endif
  1118. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1119. if ( (ret = cmac_test()) != 0)
  1120. return err_sys("CMAC test failed!\n", ret);
  1121. else
  1122. test_pass("CMAC test passed!\n");
  1123. #endif
  1124. #ifdef HAVE_LIBZ
  1125. if ( (ret = compress_test()) != 0)
  1126. return err_sys("COMPRESS test failed!\n", ret);
  1127. else
  1128. test_pass("COMPRESS test passed!\n");
  1129. #endif
  1130. #ifdef HAVE_PKCS7
  1131. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1132. if ( (ret = pkcs7encrypted_test()) != 0)
  1133. return err_sys("PKCS7encrypted test failed!\n", ret);
  1134. else
  1135. test_pass("PKCS7encrypted test passed!\n");
  1136. #endif
  1137. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1138. if ( (ret = pkcs7compressed_test()) != 0)
  1139. return err_sys("PKCS7compressed test failed!\n", ret);
  1140. else
  1141. test_pass("PKCS7compressed test passed!\n");
  1142. #endif
  1143. if ( (ret = pkcs7signed_test()) != 0)
  1144. return err_sys("PKCS7signed test failed!\n", ret);
  1145. else
  1146. test_pass("PKCS7signed test passed!\n");
  1147. if ( (ret = pkcs7enveloped_test()) != 0)
  1148. return err_sys("PKCS7enveloped test failed!\n", ret);
  1149. else
  1150. test_pass("PKCS7enveloped test passed!\n");
  1151. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1152. if ( (ret = pkcs7authenveloped_test()) != 0)
  1153. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1154. else
  1155. test_pass("PKCS7authenveloped test passed!\n");
  1156. #endif
  1157. #endif
  1158. #ifdef HAVE_VALGRIND
  1159. if ( (ret = mp_test()) != 0)
  1160. return err_sys("mp test failed!\n", ret);
  1161. else
  1162. test_pass("mp test passed!\n");
  1163. #endif
  1164. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1165. if ( (ret = prime_test()) != 0)
  1166. return err_sys("prime test failed!\n", ret);
  1167. else
  1168. test_pass("prime test passed!\n");
  1169. #endif
  1170. #if defined(ASN_BER_TO_DER) && \
  1171. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1172. defined(OPENSSL_EXTRA_X509_SMALL))
  1173. if ( (ret = berder_test()) != 0)
  1174. return err_sys("ber-der test failed!\n", ret);
  1175. else
  1176. test_pass("ber-der test passed!\n");
  1177. #endif
  1178. if ( (ret = logging_test()) != 0)
  1179. return err_sys("logging test failed!\n", ret);
  1180. else
  1181. test_pass("logging test passed!\n");
  1182. if ( (ret = mutex_test()) != 0)
  1183. return err_sys("mutex test failed!\n", ret);
  1184. else
  1185. test_pass("mutex test passed!\n");
  1186. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1187. if ( (ret = memcb_test()) != 0)
  1188. return err_sys("memcb test failed!\n", ret);
  1189. else
  1190. test_pass("memcb test passed!\n");
  1191. #endif
  1192. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1193. if ( (ret = blob_test()) != 0)
  1194. return err_sys("blob test failed!\n", ret);
  1195. else
  1196. test_pass("blob test passed!\n");
  1197. #endif
  1198. #if defined(WOLF_CRYPTO_CB) && \
  1199. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1200. defined(WOLFSSL_QNX_CAAM))
  1201. if ( (ret = cryptocb_test()) != 0)
  1202. return err_sys("crypto callback test failed!\n", ret);
  1203. else
  1204. test_pass("crypto callback test passed!\n");
  1205. #endif
  1206. #ifdef WOLFSSL_CERT_PIV
  1207. if ( (ret = certpiv_test()) != 0)
  1208. return err_sys("cert piv test failed!\n", ret);
  1209. else
  1210. test_pass("cert piv test passed!\n");
  1211. #endif
  1212. #ifdef WOLF_CRYPTO_CB
  1213. #ifdef HAVE_INTEL_QA_SYNC
  1214. wc_CryptoCb_CleanupIntelQa(&devId);
  1215. #endif
  1216. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1217. wc_CryptoCb_CleanupOcteon(&devId);
  1218. #endif
  1219. #endif
  1220. #ifdef WOLFSSL_ASYNC_CRYPT
  1221. wolfAsync_DevClose(&devId);
  1222. #endif
  1223. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1224. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1225. wc_ecc_fp_free();
  1226. #endif
  1227. if (args)
  1228. ((func_args*)args)->return_code = ret;
  1229. test_pass("Test complete\n");
  1230. EXIT_TEST(ret);
  1231. }
  1232. #ifndef NO_MAIN_DRIVER
  1233. /* so overall tests can pull in test function */
  1234. #ifdef WOLFSSL_ESPIDF
  1235. void app_main( )
  1236. #else
  1237. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1238. int myoptind = 0;
  1239. char* myoptarg = NULL;
  1240. #endif
  1241. int main(int argc, char** argv)
  1242. #endif
  1243. {
  1244. int ret;
  1245. func_args args;
  1246. #ifdef WOLFSSL_ESPIDF
  1247. /* set dummy wallclock time. */
  1248. struct timeval utctime;
  1249. struct timezone tz;
  1250. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1251. utctime.tv_usec = 0;
  1252. tz.tz_minuteswest = 0;
  1253. tz.tz_dsttime = 0;
  1254. settimeofday(&utctime, &tz);
  1255. #endif
  1256. #ifdef WOLFSSL_APACHE_MYNEWT
  1257. #ifdef ARCH_sim
  1258. mcu_sim_parse_args(argc, argv);
  1259. #endif
  1260. sysinit();
  1261. /* set dummy wallclock time. */
  1262. struct os_timeval utctime;
  1263. struct os_timezone tz;
  1264. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1265. utctime.tv_usec = 0;
  1266. tz.tz_minuteswest = 0;
  1267. tz.tz_dsttime = 0;
  1268. os_settimeofday(&utctime, &tz);
  1269. #endif
  1270. #ifdef HAVE_WNR
  1271. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1272. err_sys("Whitewood netRandom global config failed", -1001);
  1273. return -1002;
  1274. }
  1275. #endif
  1276. #ifndef WOLFSSL_ESPIDF
  1277. args.argc = argc;
  1278. args.argv = argv;
  1279. #endif
  1280. if ((ret = wolfCrypt_Init()) != 0) {
  1281. printf("wolfCrypt_Init failed %d\n", ret);
  1282. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1283. }
  1284. #ifdef HAVE_STACK_SIZE
  1285. StackSizeCheck(&args, wolfcrypt_test);
  1286. #else
  1287. wolfcrypt_test(&args);
  1288. #endif
  1289. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1290. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1291. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1292. }
  1293. #ifdef HAVE_WNR
  1294. if (wc_FreeNetRandom() < 0)
  1295. err_sys("Failed to free netRandom context", -1005);
  1296. #endif /* HAVE_WNR */
  1297. #ifndef WOLFSSL_ESPIDF
  1298. return args.return_code;
  1299. #endif
  1300. }
  1301. #endif /* NO_MAIN_DRIVER */
  1302. /* helper to save DER, convert to PEM and save PEM */
  1303. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1304. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))))
  1305. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1306. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1307. #else
  1308. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1309. #endif
  1310. static int _SaveDerAndPem(const byte* der, int derSz,
  1311. const char* fileDer, const char* filePem, int pemType, int errBase)
  1312. {
  1313. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1314. int ret;
  1315. XFILE derFile;
  1316. derFile = XFOPEN(fileDer, "wb");
  1317. if (!derFile) {
  1318. return errBase + 0;
  1319. }
  1320. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1321. XFCLOSE(derFile);
  1322. if (ret != derSz) {
  1323. return errBase + 1;
  1324. }
  1325. #endif
  1326. #ifdef WOLFSSL_DER_TO_PEM
  1327. if (filePem) {
  1328. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1329. XFILE pemFile;
  1330. #endif
  1331. byte* pem;
  1332. int pemSz;
  1333. /* calculate PEM size */
  1334. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1335. if (pemSz < 0) {
  1336. return pemSz;
  1337. }
  1338. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1339. if (pem == NULL) {
  1340. return MEMORY_E;
  1341. }
  1342. /* Convert to PEM */
  1343. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1344. if (pemSz < 0) {
  1345. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1346. return errBase + 2;
  1347. }
  1348. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1349. pemFile = XFOPEN(filePem, "wb");
  1350. if (!pemFile) {
  1351. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1352. return errBase + 3;
  1353. }
  1354. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1355. XFCLOSE(pemFile);
  1356. if (ret != pemSz) {
  1357. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1358. return errBase + 4;
  1359. }
  1360. #endif
  1361. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1362. }
  1363. #endif /* WOLFSSL_DER_TO_PEM */
  1364. /* suppress unused variable warnings */
  1365. (void)der;
  1366. (void)derSz;
  1367. (void)filePem;
  1368. (void)fileDer;
  1369. (void)pemType;
  1370. (void)errBase;
  1371. return 0;
  1372. }
  1373. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1374. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1375. {
  1376. const char* errStr;
  1377. char out[WOLFSSL_MAX_ERROR_SZ];
  1378. const char* unknownStr = wc_GetErrorString(0);
  1379. #ifdef NO_ERROR_STRINGS
  1380. /* Ensure a valid error code's string matches an invalid code's.
  1381. * The string is that error strings are not available.
  1382. */
  1383. errStr = wc_GetErrorString(OPEN_RAN_E);
  1384. wc_ErrorString(OPEN_RAN_E, out);
  1385. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1386. return -1100;
  1387. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1388. return -1101;
  1389. #else
  1390. int i;
  1391. int j = 0;
  1392. /* Values that are not or no longer error codes. */
  1393. int missing[] = { -122, -123, -124, -127, -128, -129,
  1394. -163, -164, -165, -166, -167, -168, -169,
  1395. -179, -233,
  1396. 0 };
  1397. /* Check that all errors have a string and it's the same through the two
  1398. * APIs. Check that the values that are not errors map to the unknown
  1399. * string.
  1400. */
  1401. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1402. errStr = wc_GetErrorString(i);
  1403. wc_ErrorString(i, out);
  1404. if (i != missing[j]) {
  1405. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  1406. return -1102;
  1407. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  1408. return -1103;
  1409. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  1410. return -1104;
  1411. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1412. return -1105;
  1413. }
  1414. else {
  1415. j++;
  1416. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1417. return -1106;
  1418. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1419. return -1107;
  1420. }
  1421. }
  1422. /* Check if the next possible value has been given a string. */
  1423. errStr = wc_GetErrorString(i);
  1424. wc_ErrorString(i, out);
  1425. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1426. return -1108;
  1427. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1428. return -1109;
  1429. #endif
  1430. return 0;
  1431. }
  1432. #ifndef NO_CODING
  1433. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1434. {
  1435. int ret;
  1436. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1437. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1438. byte out[128];
  1439. word32 outLen;
  1440. #ifdef WOLFSSL_BASE64_ENCODE
  1441. byte data[3];
  1442. word32 dataLen;
  1443. byte longData[79] = { 0 };
  1444. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1445. #endif
  1446. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA Gdj=";
  1447. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1448. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd AA";
  1449. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1450. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1451. byte goodChar[] =
  1452. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1453. "abcdefghijklmnopqrstuvwxyz"
  1454. "0123456789+/;";
  1455. byte charTest[] = "A+Gd\0\0\0";
  1456. int i;
  1457. /* Good Base64 encodings. */
  1458. outLen = sizeof(out);
  1459. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1460. if (ret != 0)
  1461. return -1200;
  1462. outLen = sizeof(out);
  1463. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1464. if (ret != 0)
  1465. return -1201;
  1466. outLen = sizeof(goodChar);
  1467. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1468. if (ret != 0)
  1469. return -1235;
  1470. if (outLen != 64 / 4 * 3)
  1471. return -1236;
  1472. /* Bad parameters. */
  1473. outLen = 1;
  1474. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1475. if (ret != BAD_FUNC_ARG)
  1476. return -1202;
  1477. outLen = sizeof(out);
  1478. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1479. if (ret != ASN_INPUT_E)
  1480. return -1203;
  1481. outLen = sizeof(out);
  1482. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1483. if (ret != ASN_INPUT_E)
  1484. return -1203;
  1485. /* Bad character at each offset 0-3. */
  1486. for (i = 0; i < 4; i++) {
  1487. outLen = sizeof(out);
  1488. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1489. if (ret != ASN_INPUT_E)
  1490. return -1204 - i;
  1491. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1492. if (ret != ASN_INPUT_E)
  1493. return -1214 - i;
  1494. }
  1495. /* Invalid character less than 0x2b */
  1496. for (i = 1; i < 0x2b; i++) {
  1497. outLen = sizeof(out);
  1498. charTest[0] = i;
  1499. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1500. if (ret != ASN_INPUT_E)
  1501. return -1240 - i;
  1502. }
  1503. /* Bad characters in range 0x2b - 0x7a. */
  1504. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1505. outLen = sizeof(out);
  1506. charTest[0] = badChar[i];
  1507. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1508. if (ret != ASN_INPUT_E)
  1509. return -1270 - i;
  1510. }
  1511. /* Invalid character greater than 0x7a */
  1512. for (i = 0x7b; i < 0x100; i++) {
  1513. outLen = sizeof(out);
  1514. charTest[0] = i;
  1515. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1516. if (ret != ASN_INPUT_E)
  1517. return -1290 - i;
  1518. }
  1519. #ifdef WOLFSSL_BASE64_ENCODE
  1520. /* Decode and encode all symbols - non-alphanumeric. */
  1521. dataLen = sizeof(data);
  1522. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1523. if (ret != 0)
  1524. return -1224;
  1525. outLen = sizeof(out);
  1526. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1527. if (ret != LENGTH_ONLY_E)
  1528. return -1225;
  1529. outLen = sizeof(out);
  1530. ret = Base64_Encode(data, dataLen, out, &outLen);
  1531. if (ret != 0)
  1532. return -1226;
  1533. outLen = 7;
  1534. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1535. if (ret != BUFFER_E)
  1536. return -1227;
  1537. outLen = sizeof(out);
  1538. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1539. if (ret != LENGTH_ONLY_E)
  1540. return -1228;
  1541. outLen = sizeof(out);
  1542. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1543. if (ret != 0)
  1544. return -1229;
  1545. outLen = sizeof(out);
  1546. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1547. if (ret != 0)
  1548. return -1230;
  1549. /* Data that results in an encoding longer than one line. */
  1550. outLen = sizeof(out);
  1551. dataLen = sizeof(longData);
  1552. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1553. if (ret != 0)
  1554. return -1231;
  1555. outLen = sizeof(out);
  1556. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1557. if (ret != 0)
  1558. return -1232;
  1559. outLen = sizeof(out);
  1560. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1561. if (ret != 0)
  1562. return -1233;
  1563. #endif
  1564. return 0;
  1565. }
  1566. #ifdef WOLFSSL_BASE16
  1567. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1568. {
  1569. int ret;
  1570. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1571. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1572. byte encoded[40];
  1573. word32 encodedLen;
  1574. byte plain[40];
  1575. word32 len;
  1576. /* length returned includes null termination */
  1577. encodedLen = sizeof(encoded);
  1578. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1579. if (ret != 0)
  1580. return -1300;
  1581. len = (word32)XSTRLEN((char*)encoded);
  1582. if (len != encodedLen - 1)
  1583. return -1301;
  1584. len = sizeof(plain);
  1585. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1586. if (ret != 0)
  1587. return -1302;
  1588. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1589. return -1303;
  1590. if (encodedLen != sizeof(encodedTestData) ||
  1591. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1592. return -1304;
  1593. }
  1594. return 0;
  1595. }
  1596. #endif /* WOLFSSL_BASE16 */
  1597. #endif /* !NO_CODING */
  1598. #ifndef NO_ASN
  1599. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1600. {
  1601. int ret;
  1602. /* ASN1 encoded date buffer */
  1603. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1604. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1605. byte format;
  1606. int length;
  1607. const byte* datePart;
  1608. #ifndef NO_ASN_TIME
  1609. struct tm timearg;
  1610. time_t now;
  1611. #endif
  1612. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1613. &length);
  1614. if (ret != 0)
  1615. return -1400;
  1616. #ifndef NO_ASN_TIME
  1617. /* Parameter Validation tests. */
  1618. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1619. return -1401;
  1620. if (wc_GetTime(&now, 0) != BUFFER_E)
  1621. return -1402;
  1622. now = 0;
  1623. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1624. return -1403;
  1625. }
  1626. if (now == 0) {
  1627. printf("RTC/Time not set!\n");
  1628. return -1404;
  1629. }
  1630. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1631. if (ret != 0)
  1632. return -1405;
  1633. #endif /* !NO_ASN_TIME */
  1634. return 0;
  1635. }
  1636. #endif /* !NO_ASN */
  1637. #ifdef WOLFSSL_MD2
  1638. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1639. {
  1640. Md2 md2;
  1641. byte hash[MD2_DIGEST_SIZE];
  1642. testVector a, b, c, d, e, f, g;
  1643. testVector test_md2[7];
  1644. int times = sizeof(test_md2) / sizeof(testVector), i;
  1645. a.input = "";
  1646. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1647. "\x27\x73";
  1648. a.inLen = XSTRLEN(a.input);
  1649. a.outLen = MD2_DIGEST_SIZE;
  1650. b.input = "a";
  1651. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1652. "\xb5\xd1";
  1653. b.inLen = XSTRLEN(b.input);
  1654. b.outLen = MD2_DIGEST_SIZE;
  1655. c.input = "abc";
  1656. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1657. "\xd6\xbb";
  1658. c.inLen = XSTRLEN(c.input);
  1659. c.outLen = MD2_DIGEST_SIZE;
  1660. d.input = "message digest";
  1661. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1662. "\x06\xb0";
  1663. d.inLen = XSTRLEN(d.input);
  1664. d.outLen = MD2_DIGEST_SIZE;
  1665. e.input = "abcdefghijklmnopqrstuvwxyz";
  1666. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1667. "\x94\x0b";
  1668. e.inLen = XSTRLEN(e.input);
  1669. e.outLen = MD2_DIGEST_SIZE;
  1670. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1671. "6789";
  1672. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1673. "\x38\xcd";
  1674. f.inLen = XSTRLEN(f.input);
  1675. f.outLen = MD2_DIGEST_SIZE;
  1676. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1677. "9012345678901234567890";
  1678. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1679. "\xef\xd8";
  1680. g.inLen = XSTRLEN(g.input);
  1681. g.outLen = MD2_DIGEST_SIZE;
  1682. test_md2[0] = a;
  1683. test_md2[1] = b;
  1684. test_md2[2] = c;
  1685. test_md2[3] = d;
  1686. test_md2[4] = e;
  1687. test_md2[5] = f;
  1688. test_md2[6] = g;
  1689. wc_InitMd2(&md2);
  1690. for (i = 0; i < times; ++i) {
  1691. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1692. wc_Md2Final(&md2, hash);
  1693. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1694. return -1500 - i;
  1695. }
  1696. return 0;
  1697. }
  1698. #endif
  1699. #ifndef NO_MD5
  1700. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1701. {
  1702. int ret = 0;
  1703. wc_Md5 md5, md5Copy;
  1704. byte hash[WC_MD5_DIGEST_SIZE];
  1705. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1706. testVector a, b, c, d, e, f;
  1707. testVector test_md5[6];
  1708. int times = sizeof(test_md5) / sizeof(testVector), i;
  1709. a.input = "";
  1710. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1711. "\x7e";
  1712. a.inLen = XSTRLEN(a.input);
  1713. a.outLen = WC_MD5_DIGEST_SIZE;
  1714. b.input = "abc";
  1715. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1716. "\x72";
  1717. b.inLen = XSTRLEN(b.input);
  1718. b.outLen = WC_MD5_DIGEST_SIZE;
  1719. c.input = "message digest";
  1720. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1721. "\xd0";
  1722. c.inLen = XSTRLEN(c.input);
  1723. c.outLen = WC_MD5_DIGEST_SIZE;
  1724. d.input = "abcdefghijklmnopqrstuvwxyz";
  1725. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1726. "\x3b";
  1727. d.inLen = XSTRLEN(d.input);
  1728. d.outLen = WC_MD5_DIGEST_SIZE;
  1729. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1730. "6789";
  1731. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1732. "\x9f";
  1733. e.inLen = XSTRLEN(e.input);
  1734. e.outLen = WC_MD5_DIGEST_SIZE;
  1735. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1736. "9012345678901234567890";
  1737. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1738. "\x7a";
  1739. f.inLen = XSTRLEN(f.input);
  1740. f.outLen = WC_MD5_DIGEST_SIZE;
  1741. test_md5[0] = a;
  1742. test_md5[1] = b;
  1743. test_md5[2] = c;
  1744. test_md5[3] = d;
  1745. test_md5[4] = e;
  1746. test_md5[5] = f;
  1747. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1748. if (ret != 0)
  1749. return -1600;
  1750. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1751. if (ret != 0) {
  1752. wc_Md5Free(&md5);
  1753. return -1601;
  1754. }
  1755. for (i = 0; i < times; ++i) {
  1756. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1757. (word32)test_md5[i].inLen);
  1758. if (ret != 0)
  1759. ERROR_OUT(-1602 - i, exit);
  1760. ret = wc_Md5GetHash(&md5, hashcopy);
  1761. if (ret != 0)
  1762. ERROR_OUT(-1603 - i, exit);
  1763. ret = wc_Md5Copy(&md5, &md5Copy);
  1764. if (ret != 0)
  1765. ERROR_OUT(-1604 - i, exit);
  1766. ret = wc_Md5Final(&md5, hash);
  1767. if (ret != 0)
  1768. ERROR_OUT(-1605 - i, exit);
  1769. wc_Md5Free(&md5Copy);
  1770. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1771. ERROR_OUT(-1606 - i, exit);
  1772. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1773. ERROR_OUT(-1607 - i, exit);
  1774. }
  1775. /* BEGIN LARGE HASH TEST */ {
  1776. byte large_input[1024];
  1777. const char* large_digest =
  1778. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1779. for (i = 0; i < (int)sizeof(large_input); i++) {
  1780. large_input[i] = (byte)(i & 0xFF);
  1781. }
  1782. times = 100;
  1783. #ifdef WOLFSSL_PIC32MZ_HASH
  1784. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1785. #endif
  1786. for (i = 0; i < times; ++i) {
  1787. ret = wc_Md5Update(&md5, (byte*)large_input,
  1788. (word32)sizeof(large_input));
  1789. if (ret != 0)
  1790. ERROR_OUT(-1608, exit);
  1791. }
  1792. ret = wc_Md5Final(&md5, hash);
  1793. if (ret != 0)
  1794. ERROR_OUT(-1609, exit);
  1795. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1796. ERROR_OUT(-1610, exit);
  1797. } /* END LARGE HASH TEST */
  1798. exit:
  1799. wc_Md5Free(&md5);
  1800. wc_Md5Free(&md5Copy);
  1801. return ret;
  1802. }
  1803. #endif /* NO_MD5 */
  1804. #ifndef NO_MD4
  1805. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  1806. {
  1807. Md4 md4;
  1808. byte hash[MD4_DIGEST_SIZE];
  1809. testVector a, b, c, d, e, f, g;
  1810. testVector test_md4[7];
  1811. int times = sizeof(test_md4) / sizeof(testVector), i;
  1812. a.input = "";
  1813. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1814. "\xc0";
  1815. a.inLen = XSTRLEN(a.input);
  1816. a.outLen = MD4_DIGEST_SIZE;
  1817. b.input = "a";
  1818. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1819. "\x24";
  1820. b.inLen = XSTRLEN(b.input);
  1821. b.outLen = MD4_DIGEST_SIZE;
  1822. c.input = "abc";
  1823. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1824. "\x9d";
  1825. c.inLen = XSTRLEN(c.input);
  1826. c.outLen = MD4_DIGEST_SIZE;
  1827. d.input = "message digest";
  1828. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1829. "\x4b";
  1830. d.inLen = XSTRLEN(d.input);
  1831. d.outLen = MD4_DIGEST_SIZE;
  1832. e.input = "abcdefghijklmnopqrstuvwxyz";
  1833. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1834. "\xa9";
  1835. e.inLen = XSTRLEN(e.input);
  1836. e.outLen = MD4_DIGEST_SIZE;
  1837. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1838. "6789";
  1839. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1840. "\xe4";
  1841. f.inLen = XSTRLEN(f.input);
  1842. f.outLen = MD4_DIGEST_SIZE;
  1843. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1844. "9012345678901234567890";
  1845. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1846. "\x36";
  1847. g.inLen = XSTRLEN(g.input);
  1848. g.outLen = MD4_DIGEST_SIZE;
  1849. test_md4[0] = a;
  1850. test_md4[1] = b;
  1851. test_md4[2] = c;
  1852. test_md4[3] = d;
  1853. test_md4[4] = e;
  1854. test_md4[5] = f;
  1855. test_md4[6] = g;
  1856. wc_InitMd4(&md4);
  1857. for (i = 0; i < times; ++i) {
  1858. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1859. wc_Md4Final(&md4, hash);
  1860. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1861. return -1700 - i;
  1862. }
  1863. return 0;
  1864. }
  1865. #endif /* NO_MD4 */
  1866. #ifndef NO_SHA
  1867. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  1868. {
  1869. int ret = 0;
  1870. wc_Sha sha, shaCopy;
  1871. byte hash[WC_SHA_DIGEST_SIZE];
  1872. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1873. testVector a, b, c, d, e;
  1874. testVector test_sha[5];
  1875. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1876. a.input = "";
  1877. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1878. "\x90\xaf\xd8\x07\x09";
  1879. a.inLen = XSTRLEN(a.input);
  1880. a.outLen = WC_SHA_DIGEST_SIZE;
  1881. b.input = "abc";
  1882. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1883. "\x6C\x9C\xD0\xD8\x9D";
  1884. b.inLen = XSTRLEN(b.input);
  1885. b.outLen = WC_SHA_DIGEST_SIZE;
  1886. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1887. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1888. "\xE5\xE5\x46\x70\xF1";
  1889. c.inLen = XSTRLEN(c.input);
  1890. c.outLen = WC_SHA_DIGEST_SIZE;
  1891. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1892. "aaaaaa";
  1893. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1894. "\x2A\x25\xEC\x64\x4D";
  1895. d.inLen = XSTRLEN(d.input);
  1896. d.outLen = WC_SHA_DIGEST_SIZE;
  1897. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1898. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1899. "aaaaaaaaaa";
  1900. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1901. "\x53\x99\x5E\x26\xA0";
  1902. e.inLen = XSTRLEN(e.input);
  1903. e.outLen = WC_SHA_DIGEST_SIZE;
  1904. test_sha[0] = a;
  1905. test_sha[1] = b;
  1906. test_sha[2] = c;
  1907. test_sha[3] = d;
  1908. test_sha[4] = e;
  1909. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  1910. if (ret != 0)
  1911. return -1800;
  1912. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  1913. if (ret != 0) {
  1914. wc_ShaFree(&sha);
  1915. return -1801;
  1916. }
  1917. for (i = 0; i < times; ++i) {
  1918. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  1919. (word32)test_sha[i].inLen);
  1920. if (ret != 0)
  1921. ERROR_OUT(-1802 - i, exit);
  1922. ret = wc_ShaGetHash(&sha, hashcopy);
  1923. if (ret != 0)
  1924. ERROR_OUT(-1803 - i, exit);
  1925. ret = wc_ShaCopy(&sha, &shaCopy);
  1926. if (ret != 0)
  1927. ERROR_OUT(-1804 - i, exit);
  1928. ret = wc_ShaFinal(&sha, hash);
  1929. if (ret != 0)
  1930. ERROR_OUT(-1805 - i, exit);
  1931. wc_ShaFree(&shaCopy);
  1932. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  1933. ERROR_OUT(-1806 - i, exit);
  1934. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  1935. ERROR_OUT(-1807 - i, exit);
  1936. }
  1937. /* BEGIN LARGE HASH TEST */ {
  1938. byte large_input[1024];
  1939. #ifdef WOLFSSL_RENESAS_TSIP
  1940. const char* large_digest =
  1941. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  1942. "\x15\x8c\x6d\xb6";
  1943. #else
  1944. const char* large_digest =
  1945. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  1946. "\xc5\xd9\x0a\xac";
  1947. #endif
  1948. for (i = 0; i < (int)sizeof(large_input); i++) {
  1949. large_input[i] = (byte)(i & 0xFF);
  1950. }
  1951. #ifdef WOLFSSL_RENESAS_TSIP
  1952. times = 20;
  1953. #else
  1954. times = 100;
  1955. #endif
  1956. #ifdef WOLFSSL_PIC32MZ_HASH
  1957. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  1958. #endif
  1959. for (i = 0; i < times; ++i) {
  1960. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  1961. (word32)sizeof(large_input));
  1962. if (ret != 0)
  1963. ERROR_OUT(-1808, exit);
  1964. }
  1965. ret = wc_ShaFinal(&sha, hash);
  1966. if (ret != 0)
  1967. ERROR_OUT(-1809, exit);
  1968. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  1969. ERROR_OUT(-1810, exit);
  1970. } /* END LARGE HASH TEST */
  1971. exit:
  1972. wc_ShaFree(&sha);
  1973. wc_ShaFree(&shaCopy);
  1974. return ret;
  1975. }
  1976. #endif /* NO_SHA */
  1977. #ifdef WOLFSSL_RIPEMD
  1978. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  1979. {
  1980. RipeMd ripemd;
  1981. int ret;
  1982. byte hash[RIPEMD_DIGEST_SIZE];
  1983. testVector a, b, c, d;
  1984. testVector test_ripemd[4];
  1985. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  1986. a.input = "abc";
  1987. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  1988. "\xb0\x87\xf1\x5a\x0b\xfc";
  1989. a.inLen = XSTRLEN(a.input);
  1990. a.outLen = RIPEMD_DIGEST_SIZE;
  1991. b.input = "message digest";
  1992. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  1993. "\x5f\xfa\x21\x59\x5f\x36";
  1994. b.inLen = XSTRLEN(b.input);
  1995. b.outLen = RIPEMD_DIGEST_SIZE;
  1996. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1997. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  1998. "\xf4\x9a\xda\x62\xeb\x2b";
  1999. c.inLen = XSTRLEN(c.input);
  2000. c.outLen = RIPEMD_DIGEST_SIZE;
  2001. d.input = "12345678901234567890123456789012345678901234567890123456"
  2002. "789012345678901234567890";
  2003. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2004. "\x82\xbf\x63\x32\x6b\xfb";
  2005. d.inLen = XSTRLEN(d.input);
  2006. d.outLen = RIPEMD_DIGEST_SIZE;
  2007. test_ripemd[0] = a;
  2008. test_ripemd[1] = b;
  2009. test_ripemd[2] = c;
  2010. test_ripemd[3] = d;
  2011. ret = wc_InitRipeMd(&ripemd);
  2012. if (ret != 0) {
  2013. return -1900;
  2014. }
  2015. for (i = 0; i < times; ++i) {
  2016. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2017. (word32)test_ripemd[i].inLen);
  2018. if (ret != 0) {
  2019. return -1901 - i;
  2020. }
  2021. ret = wc_RipeMdFinal(&ripemd, hash);
  2022. if (ret != 0) {
  2023. return -1911 - i;
  2024. }
  2025. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2026. return -1921 - i;
  2027. }
  2028. return 0;
  2029. }
  2030. #endif /* WOLFSSL_RIPEMD */
  2031. #ifdef HAVE_BLAKE2
  2032. #define BLAKE2B_TESTS 3
  2033. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2034. {
  2035. {
  2036. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2037. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2038. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2039. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2040. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2041. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2042. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2043. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2044. },
  2045. {
  2046. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2047. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2048. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2049. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2050. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2051. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2052. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2053. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2054. },
  2055. {
  2056. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2057. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2058. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2059. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2060. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2061. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2062. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2063. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2064. }
  2065. };
  2066. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2067. {
  2068. Blake2b b2b;
  2069. byte digest[64];
  2070. byte input[64];
  2071. int i, ret;
  2072. for (i = 0; i < (int)sizeof(input); i++)
  2073. input[i] = (byte)i;
  2074. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2075. ret = wc_InitBlake2b(&b2b, 64);
  2076. if (ret != 0)
  2077. return -2000 - i;
  2078. ret = wc_Blake2bUpdate(&b2b, input, i);
  2079. if (ret != 0)
  2080. return -2010 - 1;
  2081. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2082. if (ret != 0)
  2083. return -2020 - i;
  2084. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2085. return -2030 - i;
  2086. }
  2087. }
  2088. return 0;
  2089. }
  2090. #endif /* HAVE_BLAKE2 */
  2091. #ifdef HAVE_BLAKE2S
  2092. #define BLAKE2S_TESTS 3
  2093. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2094. {
  2095. {
  2096. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2097. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2098. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2099. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2100. },
  2101. {
  2102. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2103. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2104. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2105. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2106. },
  2107. {
  2108. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2109. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2110. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2111. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2112. }
  2113. };
  2114. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2115. {
  2116. Blake2s b2s;
  2117. byte digest[32];
  2118. byte input[64];
  2119. int i, ret;
  2120. for (i = 0; i < (int)sizeof(input); i++)
  2121. input[i] = (byte)i;
  2122. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2123. ret = wc_InitBlake2s(&b2s, 32);
  2124. if (ret != 0)
  2125. return -2100 - i;
  2126. ret = wc_Blake2sUpdate(&b2s, input, i);
  2127. if (ret != 0)
  2128. return -2110 - 1;
  2129. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2130. if (ret != 0)
  2131. return -2120 - i;
  2132. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2133. return -2130 - i;
  2134. }
  2135. }
  2136. return 0;
  2137. }
  2138. #endif /* HAVE_BLAKE2S */
  2139. #ifdef WOLFSSL_SHA224
  2140. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2141. {
  2142. wc_Sha224 sha, shaCopy;
  2143. byte hash[WC_SHA224_DIGEST_SIZE];
  2144. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2145. int ret = 0;
  2146. testVector a, b, c;
  2147. testVector test_sha[3];
  2148. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2149. a.input = "";
  2150. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2151. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2152. a.inLen = XSTRLEN(a.input);
  2153. a.outLen = WC_SHA224_DIGEST_SIZE;
  2154. b.input = "abc";
  2155. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2156. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2157. b.inLen = XSTRLEN(b.input);
  2158. b.outLen = WC_SHA224_DIGEST_SIZE;
  2159. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2160. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2161. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2162. c.inLen = XSTRLEN(c.input);
  2163. c.outLen = WC_SHA224_DIGEST_SIZE;
  2164. test_sha[0] = a;
  2165. test_sha[1] = b;
  2166. test_sha[2] = c;
  2167. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2168. if (ret != 0)
  2169. return -2200;
  2170. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2171. if (ret != 0) {
  2172. wc_Sha224Free(&sha);
  2173. return -2201;
  2174. }
  2175. for (i = 0; i < times; ++i) {
  2176. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2177. (word32)test_sha[i].inLen);
  2178. if (ret != 0)
  2179. ERROR_OUT(-2202 - i, exit);
  2180. ret = wc_Sha224GetHash(&sha, hashcopy);
  2181. if (ret != 0)
  2182. ERROR_OUT(-2203 - i, exit);
  2183. ret = wc_Sha224Copy(&sha, &shaCopy);
  2184. if (ret != 0)
  2185. ERROR_OUT(-2204 - i, exit);
  2186. ret = wc_Sha224Final(&sha, hash);
  2187. if (ret != 0)
  2188. ERROR_OUT(-2205 - i, exit);
  2189. wc_Sha224Free(&shaCopy);
  2190. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2191. ERROR_OUT(-2206 - i, exit);
  2192. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2193. ERROR_OUT(-2207 - i, exit);
  2194. }
  2195. exit:
  2196. wc_Sha224Free(&sha);
  2197. wc_Sha224Free(&shaCopy);
  2198. return ret;
  2199. }
  2200. #endif
  2201. #ifndef NO_SHA256
  2202. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2203. {
  2204. wc_Sha256 sha, shaCopy;
  2205. byte hash[WC_SHA256_DIGEST_SIZE];
  2206. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2207. int ret = 0;
  2208. testVector a, b, c;
  2209. testVector test_sha[3];
  2210. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2211. a.input = "";
  2212. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2213. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2214. "\xb8\x55";
  2215. a.inLen = XSTRLEN(a.input);
  2216. a.outLen = WC_SHA256_DIGEST_SIZE;
  2217. b.input = "abc";
  2218. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2219. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2220. "\x15\xAD";
  2221. b.inLen = XSTRLEN(b.input);
  2222. b.outLen = WC_SHA256_DIGEST_SIZE;
  2223. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2224. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2225. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2226. "\x06\xC1";
  2227. c.inLen = XSTRLEN(c.input);
  2228. c.outLen = WC_SHA256_DIGEST_SIZE;
  2229. test_sha[0] = a;
  2230. test_sha[1] = b;
  2231. test_sha[2] = c;
  2232. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2233. if (ret != 0)
  2234. return -2300;
  2235. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2236. if (ret != 0) {
  2237. wc_Sha256Free(&sha);
  2238. return -2301;
  2239. }
  2240. for (i = 0; i < times; ++i) {
  2241. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2242. (word32)test_sha[i].inLen);
  2243. if (ret != 0) {
  2244. ERROR_OUT(-2302 - i, exit);
  2245. }
  2246. ret = wc_Sha256GetHash(&sha, hashcopy);
  2247. if (ret != 0)
  2248. ERROR_OUT(-2303 - i, exit);
  2249. ret = wc_Sha256Copy(&sha, &shaCopy);
  2250. if (ret != 0)
  2251. ERROR_OUT(-2304 - i, exit);
  2252. ret = wc_Sha256Final(&sha, hash);
  2253. if (ret != 0)
  2254. ERROR_OUT(-2305 - i, exit);
  2255. wc_Sha256Free(&shaCopy);
  2256. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2257. ERROR_OUT(-2306 - i, exit);
  2258. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2259. ERROR_OUT(-2307 - i, exit);
  2260. }
  2261. /* BEGIN LARGE HASH TEST */ {
  2262. byte large_input[1024];
  2263. #ifdef WOLFSSL_RENESAS_TSIP_CRYPT
  2264. const char* large_digest =
  2265. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2266. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2267. #else
  2268. const char* large_digest =
  2269. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2270. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2271. #endif
  2272. for (i = 0; i < (int)sizeof(large_input); i++) {
  2273. large_input[i] = (byte)(i & 0xFF);
  2274. }
  2275. #ifdef WOLFSSL_RENESAS_TSIP
  2276. times = 20;
  2277. #else
  2278. times = 100;
  2279. #endif
  2280. #ifdef WOLFSSL_PIC32MZ_HASH
  2281. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2282. #endif
  2283. for (i = 0; i < times; ++i) {
  2284. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2285. (word32)sizeof(large_input));
  2286. if (ret != 0)
  2287. ERROR_OUT(-2308, exit);
  2288. }
  2289. ret = wc_Sha256Final(&sha, hash);
  2290. if (ret != 0)
  2291. ERROR_OUT(-2309, exit);
  2292. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2293. ERROR_OUT(-2310, exit);
  2294. } /* END LARGE HASH TEST */
  2295. exit:
  2296. wc_Sha256Free(&sha);
  2297. wc_Sha256Free(&shaCopy);
  2298. return ret;
  2299. }
  2300. #endif
  2301. #ifdef WOLFSSL_SHA512
  2302. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2303. {
  2304. wc_Sha512 sha, shaCopy;
  2305. byte hash[WC_SHA512_DIGEST_SIZE];
  2306. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2307. int ret = 0;
  2308. testVector a, b, c;
  2309. testVector test_sha[3];
  2310. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2311. a.input = "";
  2312. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2313. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2314. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2315. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2316. "\xf9\x27\xda\x3e";
  2317. a.inLen = XSTRLEN(a.input);
  2318. a.outLen = WC_SHA512_DIGEST_SIZE;
  2319. b.input = "abc";
  2320. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2321. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2322. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2323. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2324. "\xa5\x4c\xa4\x9f";
  2325. b.inLen = XSTRLEN(b.input);
  2326. b.outLen = WC_SHA512_DIGEST_SIZE;
  2327. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2328. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2329. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2330. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2331. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2332. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2333. "\x87\x4b\xe9\x09";
  2334. c.inLen = XSTRLEN(c.input);
  2335. c.outLen = WC_SHA512_DIGEST_SIZE;
  2336. test_sha[0] = a;
  2337. test_sha[1] = b;
  2338. test_sha[2] = c;
  2339. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2340. if (ret != 0)
  2341. return -2400;
  2342. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2343. if (ret != 0) {
  2344. wc_Sha512Free(&sha);
  2345. return -2401;
  2346. }
  2347. for (i = 0; i < times; ++i) {
  2348. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2349. (word32)test_sha[i].inLen);
  2350. if (ret != 0)
  2351. ERROR_OUT(-2402 - i, exit);
  2352. ret = wc_Sha512GetHash(&sha, hashcopy);
  2353. if (ret != 0)
  2354. ERROR_OUT(-2403 - i, exit);
  2355. ret = wc_Sha512Copy(&sha, &shaCopy);
  2356. if (ret != 0)
  2357. ERROR_OUT(-2404 - i, exit);
  2358. ret = wc_Sha512Final(&sha, hash);
  2359. if (ret != 0)
  2360. ERROR_OUT(-2405 - i, exit);
  2361. wc_Sha512Free(&shaCopy);
  2362. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2363. ERROR_OUT(-2406 - i, exit);
  2364. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2365. ERROR_OUT(-2407 - i, exit);
  2366. }
  2367. /* BEGIN LARGE HASH TEST */ {
  2368. byte large_input[1024];
  2369. const char* large_digest =
  2370. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2371. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2372. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2373. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2374. for (i = 0; i < (int)sizeof(large_input); i++) {
  2375. large_input[i] = (byte)(i & 0xFF);
  2376. }
  2377. times = 100;
  2378. for (i = 0; i < times; ++i) {
  2379. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2380. (word32)sizeof(large_input));
  2381. if (ret != 0)
  2382. ERROR_OUT(-2408, exit);
  2383. }
  2384. ret = wc_Sha512Final(&sha, hash);
  2385. if (ret != 0)
  2386. ERROR_OUT(-2409, exit);
  2387. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2388. ERROR_OUT(-2410, exit);
  2389. } /* END LARGE HASH TEST */
  2390. exit:
  2391. wc_Sha512Free(&sha);
  2392. wc_Sha512Free(&shaCopy);
  2393. return ret;
  2394. }
  2395. #endif
  2396. #ifdef WOLFSSL_SHA384
  2397. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2398. {
  2399. wc_Sha384 sha, shaCopy;
  2400. byte hash[WC_SHA384_DIGEST_SIZE];
  2401. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2402. int ret = 0;
  2403. testVector a, b, c;
  2404. testVector test_sha[3];
  2405. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2406. a.input = "";
  2407. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2408. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2409. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2410. "\x98\xb9\x5b";
  2411. a.inLen = XSTRLEN(a.input);
  2412. a.outLen = WC_SHA384_DIGEST_SIZE;
  2413. b.input = "abc";
  2414. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2415. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2416. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2417. "\xc8\x25\xa7";
  2418. b.inLen = XSTRLEN(b.input);
  2419. b.outLen = WC_SHA384_DIGEST_SIZE;
  2420. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2421. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2422. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2423. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2424. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2425. "\x74\x60\x39";
  2426. c.inLen = XSTRLEN(c.input);
  2427. c.outLen = WC_SHA384_DIGEST_SIZE;
  2428. test_sha[0] = a;
  2429. test_sha[1] = b;
  2430. test_sha[2] = c;
  2431. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2432. if (ret != 0)
  2433. return -2500;
  2434. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2435. if (ret != 0) {
  2436. wc_Sha384Free(&sha);
  2437. return -2501;
  2438. }
  2439. for (i = 0; i < times; ++i) {
  2440. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2441. (word32)test_sha[i].inLen);
  2442. if (ret != 0)
  2443. ERROR_OUT(-2502 - i, exit);
  2444. ret = wc_Sha384GetHash(&sha, hashcopy);
  2445. if (ret != 0)
  2446. ERROR_OUT(-2503 - i, exit);
  2447. ret = wc_Sha384Copy(&sha, &shaCopy);
  2448. if (ret != 0)
  2449. ERROR_OUT(-2504 - i, exit);
  2450. ret = wc_Sha384Final(&sha, hash);
  2451. if (ret != 0)
  2452. ERROR_OUT(-2505 - i, exit);
  2453. wc_Sha384Free(&shaCopy);
  2454. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2455. ERROR_OUT(-2506 - i, exit);
  2456. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2457. ERROR_OUT(-2507 - i, exit);
  2458. }
  2459. /* BEGIN LARGE HASH TEST */ {
  2460. byte large_input[1024];
  2461. const char* large_digest =
  2462. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2463. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2464. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2465. for (i = 0; i < (int)sizeof(large_input); i++) {
  2466. large_input[i] = (byte)(i & 0xFF);
  2467. }
  2468. times = 100;
  2469. for (i = 0; i < times; ++i) {
  2470. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2471. (word32)sizeof(large_input));
  2472. if (ret != 0)
  2473. ERROR_OUT(-2508, exit);
  2474. }
  2475. ret = wc_Sha384Final(&sha, hash);
  2476. if (ret != 0)
  2477. ERROR_OUT(-2509, exit);
  2478. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2479. ERROR_OUT(-2510, exit);
  2480. } /* END LARGE HASH TEST */
  2481. exit:
  2482. wc_Sha384Free(&sha);
  2483. wc_Sha384Free(&shaCopy);
  2484. return ret;
  2485. }
  2486. #endif /* WOLFSSL_SHA384 */
  2487. #ifdef WOLFSSL_SHA3
  2488. #ifndef WOLFSSL_NOSHA3_224
  2489. static int sha3_224_test(void)
  2490. {
  2491. wc_Sha3 sha;
  2492. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2493. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2494. testVector a, b, c;
  2495. testVector test_sha[3];
  2496. int ret = 0;
  2497. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2498. a.input = "";
  2499. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2500. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2501. a.inLen = XSTRLEN(a.input);
  2502. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2503. b.input = "abc";
  2504. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2505. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2506. b.inLen = XSTRLEN(b.input);
  2507. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2508. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2509. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2510. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2511. c.inLen = XSTRLEN(c.input);
  2512. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2513. test_sha[0] = a;
  2514. test_sha[1] = b;
  2515. test_sha[2] = c;
  2516. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2517. if (ret != 0)
  2518. return -2600;
  2519. for (i = 0; i < times; ++i) {
  2520. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2521. (word32)test_sha[i].inLen);
  2522. if (ret != 0)
  2523. ERROR_OUT(-2601 - i, exit);
  2524. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2525. if (ret != 0)
  2526. ERROR_OUT(-2602 - i, exit);
  2527. ret = wc_Sha3_224_Final(&sha, hash);
  2528. if (ret != 0)
  2529. ERROR_OUT(-2603 - i, exit);
  2530. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2531. ERROR_OUT(-2604 - i, exit);
  2532. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2533. ERROR_OUT(-2605 - i, exit);
  2534. }
  2535. /* BEGIN LARGE HASH TEST */ {
  2536. byte large_input[1024];
  2537. const char* large_digest =
  2538. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2539. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2540. for (i = 0; i < (int)sizeof(large_input); i++) {
  2541. large_input[i] = (byte)(i & 0xFF);
  2542. }
  2543. times = 100;
  2544. for (i = 0; i < times; ++i) {
  2545. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2546. (word32)sizeof(large_input));
  2547. if (ret != 0)
  2548. ERROR_OUT(-2606, exit);
  2549. }
  2550. ret = wc_Sha3_224_Final(&sha, hash);
  2551. if (ret != 0)
  2552. ERROR_OUT(-2607, exit);
  2553. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2554. ERROR_OUT(-2608, exit);
  2555. } /* END LARGE HASH TEST */
  2556. exit:
  2557. wc_Sha3_224_Free(&sha);
  2558. return ret;
  2559. }
  2560. #endif /* WOLFSSL_NOSHA3_224 */
  2561. #ifndef WOLFSSL_NOSHA3_256
  2562. static int sha3_256_test(void)
  2563. {
  2564. wc_Sha3 sha;
  2565. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2566. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2567. testVector a, b, c;
  2568. testVector test_sha[3];
  2569. int ret = 0;
  2570. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2571. byte large_input[1024];
  2572. const char* large_digest =
  2573. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2574. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2575. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2576. /* test vector with hash of empty string */
  2577. const char* Keccak256EmptyOut =
  2578. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2579. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2580. #endif
  2581. a.input = "";
  2582. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2583. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2584. "\x43\x4a";
  2585. a.inLen = XSTRLEN(a.input);
  2586. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2587. b.input = "abc";
  2588. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2589. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2590. "\x15\x32";
  2591. b.inLen = XSTRLEN(b.input);
  2592. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2593. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2594. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2595. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2596. "\x33\x76";
  2597. c.inLen = XSTRLEN(c.input);
  2598. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2599. test_sha[0] = a;
  2600. test_sha[1] = b;
  2601. test_sha[2] = c;
  2602. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2603. if (ret != 0)
  2604. return -2700;
  2605. for (i = 0; i < times; ++i) {
  2606. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2607. (word32)test_sha[i].inLen);
  2608. if (ret != 0)
  2609. ERROR_OUT(-2701 - i, exit);
  2610. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2611. if (ret != 0)
  2612. ERROR_OUT(-2702 - i, exit);
  2613. ret = wc_Sha3_256_Final(&sha, hash);
  2614. if (ret != 0)
  2615. ERROR_OUT(-2703 - i, exit);
  2616. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2617. ERROR_OUT(-2704 - i, exit);
  2618. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2619. ERROR_OUT(-2705 - i, exit);
  2620. }
  2621. /* BEGIN LARGE HASH TEST */ {
  2622. for (i = 0; i < (int)sizeof(large_input); i++) {
  2623. large_input[i] = (byte)(i & 0xFF);
  2624. }
  2625. times = 100;
  2626. for (i = 0; i < times; ++i) {
  2627. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2628. (word32)sizeof(large_input));
  2629. if (ret != 0)
  2630. ERROR_OUT(-2706, exit);
  2631. }
  2632. ret = wc_Sha3_256_Final(&sha, hash);
  2633. if (ret != 0)
  2634. ERROR_OUT(-2707, exit);
  2635. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2636. ERROR_OUT(-2708, exit);
  2637. } /* END LARGE HASH TEST */
  2638. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2639. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2640. /* Test for Keccak256 */
  2641. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2642. if (ret != 0) {
  2643. ERROR_OUT(-2709, exit);
  2644. }
  2645. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2646. if (ret != 0) {
  2647. ERROR_OUT(-2710, exit);
  2648. }
  2649. ret = wc_Sha3_256_Final(&sha, hash);
  2650. if (ret != 0) {
  2651. ERROR_OUT(-2711, exit);
  2652. }
  2653. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2654. ERROR_OUT(-2712, exit);
  2655. }
  2656. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2657. exit:
  2658. wc_Sha3_256_Free(&sha);
  2659. return ret;
  2660. }
  2661. #endif /* WOLFSSL_NOSHA3_256 */
  2662. #ifndef WOLFSSL_NOSHA3_384
  2663. static int sha3_384_test(void)
  2664. {
  2665. wc_Sha3 sha;
  2666. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2667. #ifndef NO_INTM_HASH_TEST
  2668. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2669. #endif
  2670. testVector a, b, c;
  2671. testVector test_sha[3];
  2672. int ret;
  2673. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2674. a.input = "";
  2675. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2676. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2677. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2678. "\xd5\xf0\x04";
  2679. a.inLen = XSTRLEN(a.input);
  2680. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2681. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
  2682. /* NIST test vector with a length that is a multiple of 4 */
  2683. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2684. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2685. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2686. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2687. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2688. "\x19\x87\x22";
  2689. b.inLen = XSTRLEN(b.input);
  2690. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2691. #else
  2692. b.input = "abc";
  2693. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2694. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2695. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2696. "\x37\x6d\x25";
  2697. b.inLen = XSTRLEN(b.input);
  2698. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2699. #endif
  2700. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2701. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2702. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2703. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2704. "\x65\x7c\x22";
  2705. c.inLen = XSTRLEN(c.input);
  2706. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2707. #ifdef WOLFSSL_XILINX_CRYPT
  2708. test_sha[0] = b; /* hardware acc. can not handle "" string */
  2709. #else
  2710. test_sha[0] = a;
  2711. #endif
  2712. test_sha[1] = b;
  2713. test_sha[2] = c;
  2714. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2715. if (ret != 0)
  2716. return -2800;
  2717. for (i = 0; i < times; ++i) {
  2718. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2719. (word32)test_sha[i].inLen);
  2720. if (ret != 0)
  2721. ERROR_OUT(-2801 - i, exit);
  2722. #ifndef NO_INTM_HASH_TEST
  2723. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2724. if (ret != 0)
  2725. ERROR_OUT(-2802 - i, exit);
  2726. #endif
  2727. ret = wc_Sha3_384_Final(&sha, hash);
  2728. if (ret != 0)
  2729. ERROR_OUT(-2803 - i, exit);
  2730. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2731. ERROR_OUT(-2804 - i, exit);
  2732. #ifndef NO_INTM_HASH_TEST
  2733. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2734. ERROR_OUT(-2805 - i, exit);
  2735. #endif
  2736. }
  2737. /* BEGIN LARGE HASH TEST */ {
  2738. byte large_input[1024];
  2739. const char* large_digest =
  2740. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2741. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2742. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2743. for (i = 0; i < (int)sizeof(large_input); i++) {
  2744. large_input[i] = (byte)(i & 0xFF);
  2745. }
  2746. times = 100;
  2747. for (i = 0; i < times; ++i) {
  2748. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2749. (word32)sizeof(large_input));
  2750. if (ret != 0)
  2751. ERROR_OUT(-2806, exit);
  2752. }
  2753. ret = wc_Sha3_384_Final(&sha, hash);
  2754. if (ret != 0)
  2755. ERROR_OUT(-2807, exit);
  2756. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2757. ERROR_OUT(-2808, exit);
  2758. } /* END LARGE HASH TEST */
  2759. exit:
  2760. wc_Sha3_384_Free(&sha);
  2761. return ret;
  2762. }
  2763. #endif /* WOLFSSL_NOSHA3_384 */
  2764. #ifndef WOLFSSL_NOSHA3_512
  2765. static int sha3_512_test(void)
  2766. {
  2767. wc_Sha3 sha;
  2768. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2769. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  2770. testVector a, b, c;
  2771. testVector test_sha[3];
  2772. int ret;
  2773. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2774. a.input = "";
  2775. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2776. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2777. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2778. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2779. "\x28\x1d\xcd\x26";
  2780. a.inLen = XSTRLEN(a.input);
  2781. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2782. b.input = "abc";
  2783. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2784. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2785. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2786. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2787. "\x4e\xec\x53\xf0";
  2788. b.inLen = XSTRLEN(b.input);
  2789. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2790. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2791. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2792. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2793. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2794. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2795. "\x39\xd6\xe7\x5e";
  2796. c.inLen = XSTRLEN(c.input);
  2797. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  2798. test_sha[0] = a;
  2799. test_sha[1] = b;
  2800. test_sha[2] = c;
  2801. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2802. if (ret != 0)
  2803. return -2900;
  2804. for (i = 0; i < times; ++i) {
  2805. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2806. (word32)test_sha[i].inLen);
  2807. if (ret != 0)
  2808. ERROR_OUT(-2901 - i, exit);
  2809. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2810. if (ret != 0)
  2811. ERROR_OUT(-2902 - i, exit);
  2812. ret = wc_Sha3_512_Final(&sha, hash);
  2813. if (ret != 0)
  2814. ERROR_OUT(-2903 - i, exit);
  2815. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  2816. ERROR_OUT(-2904 - i, exit);
  2817. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  2818. ERROR_OUT(-2905 - i, exit);
  2819. }
  2820. /* BEGIN LARGE HASH TEST */ {
  2821. byte large_input[1024];
  2822. const char* large_digest =
  2823. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2824. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2825. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2826. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2827. for (i = 0; i < (int)sizeof(large_input); i++) {
  2828. large_input[i] = (byte)(i & 0xFF);
  2829. }
  2830. times = 100;
  2831. for (i = 0; i < times; ++i) {
  2832. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2833. (word32)sizeof(large_input));
  2834. if (ret != 0)
  2835. ERROR_OUT(-2906, exit);
  2836. }
  2837. ret = wc_Sha3_512_Final(&sha, hash);
  2838. if (ret != 0)
  2839. ERROR_OUT(-2907, exit);
  2840. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  2841. ERROR_OUT(-2908, exit);
  2842. } /* END LARGE HASH TEST */
  2843. exit:
  2844. wc_Sha3_512_Free(&sha);
  2845. return ret;
  2846. }
  2847. #endif /* WOLFSSL_NOSHA3_512 */
  2848. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  2849. {
  2850. int ret;
  2851. (void)ret;
  2852. #ifndef WOLFSSL_NOSHA3_224
  2853. if ((ret = sha3_224_test()) != 0)
  2854. return ret;
  2855. #endif
  2856. #ifndef WOLFSSL_NOSHA3_256
  2857. if ((ret = sha3_256_test()) != 0)
  2858. return ret;
  2859. #endif
  2860. #ifndef WOLFSSL_NOSHA3_384
  2861. if ((ret = sha3_384_test()) != 0)
  2862. return ret;
  2863. #endif
  2864. #ifndef WOLFSSL_NOSHA3_512
  2865. if ((ret = sha3_512_test()) != 0)
  2866. return ret;
  2867. #endif
  2868. return 0;
  2869. }
  2870. #endif /* WOLFSSL_SHA3 */
  2871. #ifdef WOLFSSL_SHAKE256
  2872. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  2873. {
  2874. #ifndef WOLFSSL_NO_SHAKE256
  2875. wc_Shake sha;
  2876. byte hash[114];
  2877. testVector a, b, c;
  2878. testVector test_sha[3];
  2879. int ret = 0;
  2880. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2881. byte large_input[1024];
  2882. const char* large_digest =
  2883. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  2884. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  2885. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  2886. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  2887. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  2888. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  2889. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  2890. "\xea\x26";
  2891. a.input = "";
  2892. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  2893. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  2894. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  2895. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  2896. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  2897. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  2898. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  2899. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  2900. a.inLen = XSTRLEN(a.input);
  2901. a.outLen = sizeof(hash);
  2902. b.input = "abc";
  2903. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  2904. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  2905. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  2906. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  2907. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  2908. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  2909. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  2910. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  2911. b.inLen = XSTRLEN(b.input);
  2912. b.outLen = sizeof(hash);
  2913. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2914. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  2915. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  2916. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  2917. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  2918. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  2919. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  2920. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  2921. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  2922. c.inLen = XSTRLEN(c.input);
  2923. c.outLen = sizeof(hash);
  2924. test_sha[0] = a;
  2925. test_sha[1] = b;
  2926. test_sha[2] = c;
  2927. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  2928. if (ret != 0)
  2929. return -3100;
  2930. for (i = 0; i < times; ++i) {
  2931. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  2932. (word32)test_sha[i].inLen);
  2933. if (ret != 0)
  2934. ERROR_OUT(-3101 - i, exit);
  2935. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  2936. if (ret != 0)
  2937. ERROR_OUT(-3102 - i, exit);
  2938. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  2939. ERROR_OUT(-3103 - i, exit);
  2940. }
  2941. /* BEGIN LARGE HASH TEST */ {
  2942. for (i = 0; i < (int)sizeof(large_input); i++) {
  2943. large_input[i] = (byte)(i & 0xFF);
  2944. }
  2945. times = 100;
  2946. for (i = 0; i < times; ++i) {
  2947. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  2948. (word32)sizeof(large_input));
  2949. if (ret != 0)
  2950. ERROR_OUT(-3104, exit);
  2951. }
  2952. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  2953. if (ret != 0)
  2954. ERROR_OUT(-3105, exit);
  2955. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  2956. ERROR_OUT(-3106, exit);
  2957. } /* END LARGE HASH TEST */
  2958. exit:
  2959. wc_Shake256_Free(&sha);
  2960. return ret;
  2961. #else
  2962. return 0;
  2963. #endif
  2964. }
  2965. #endif
  2966. #ifndef NO_HASH_WRAPPER
  2967. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  2968. {
  2969. wc_HashAlg hash;
  2970. int ret, exp_ret;
  2971. int i, j;
  2972. int digestSz;
  2973. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  2974. byte out[WC_MAX_DIGEST_SIZE];
  2975. byte hashOut[WC_MAX_DIGEST_SIZE];
  2976. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2977. enum wc_HashType hashType;
  2978. #endif
  2979. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  2980. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  2981. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  2982. WC_HASH_TYPE_SHA3_224,
  2983. WC_HASH_TYPE_SHA3_256,
  2984. WC_HASH_TYPE_SHA3_384,
  2985. WC_HASH_TYPE_SHA3_512 };
  2986. enum wc_HashType typesNoImpl[] = {
  2987. #ifdef NO_MD5
  2988. WC_HASH_TYPE_MD5,
  2989. #endif
  2990. #ifdef NO_SHA
  2991. WC_HASH_TYPE_SHA,
  2992. #endif
  2993. #ifndef WOLFSSL_SHA224
  2994. WC_HASH_TYPE_SHA224,
  2995. #endif
  2996. #ifdef NO_SHA256
  2997. WC_HASH_TYPE_SHA256,
  2998. #endif
  2999. #ifndef WOLFSSL_SHA384
  3000. WC_HASH_TYPE_SHA384,
  3001. #endif
  3002. #ifndef WOLFSSL_SHA512
  3003. WC_HASH_TYPE_SHA512,
  3004. #endif
  3005. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3006. WC_HASH_TYPE_SHA3_224,
  3007. #endif
  3008. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3009. WC_HASH_TYPE_SHA3_256,
  3010. #endif
  3011. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3012. WC_HASH_TYPE_SHA3_384,
  3013. #endif
  3014. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3015. WC_HASH_TYPE_SHA3_512,
  3016. #endif
  3017. WC_HASH_TYPE_NONE
  3018. };
  3019. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3020. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3021. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3022. WC_HASH_TYPE_BLAKE2B,
  3023. WC_HASH_TYPE_NONE };
  3024. /* Parameter Validation testing. */
  3025. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3026. if (ret != BAD_FUNC_ARG)
  3027. return -3200;
  3028. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3029. if (ret != BAD_FUNC_ARG)
  3030. return -3201;
  3031. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3032. if (ret != BAD_FUNC_ARG)
  3033. return -3202;
  3034. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3035. if (ret != BAD_FUNC_ARG)
  3036. return -3203;
  3037. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3038. if (ret != BAD_FUNC_ARG)
  3039. return -3204;
  3040. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3041. if (ret != BAD_FUNC_ARG)
  3042. return -3205;
  3043. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3044. if (ret != BAD_FUNC_ARG)
  3045. return -3206;
  3046. /* Try invalid hash algorithms. */
  3047. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3048. ret = wc_HashInit(&hash, typesBad[i]);
  3049. if (ret != BAD_FUNC_ARG)
  3050. return -3207 - i;
  3051. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3052. if (ret != BAD_FUNC_ARG)
  3053. return -3217 - i;
  3054. ret = wc_HashFinal(&hash, typesBad[i], out);
  3055. if (ret != BAD_FUNC_ARG)
  3056. return -3227 - i;
  3057. wc_HashFree(&hash, typesBad[i]);
  3058. }
  3059. /* Try valid hash algorithms. */
  3060. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3061. exp_ret = 0;
  3062. if (typesGood[i] == typesNoImpl[j]) {
  3063. /* Recognized but no implementation compiled in. */
  3064. exp_ret = HASH_TYPE_E;
  3065. j++;
  3066. }
  3067. ret = wc_HashInit(&hash, typesGood[i]);
  3068. if (ret != exp_ret)
  3069. return -3237 - i;
  3070. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3071. if (ret != exp_ret)
  3072. return -3247 - i;
  3073. ret = wc_HashFinal(&hash, typesGood[i], out);
  3074. if (ret != exp_ret)
  3075. return -3257 - i;
  3076. wc_HashFree(&hash, typesGood[i]);
  3077. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3078. if (exp_ret < 0 && digestSz != exp_ret)
  3079. return -3267 - i;
  3080. if (exp_ret == 0 && digestSz < 0)
  3081. return -3277 - i;
  3082. if (exp_ret == 0) {
  3083. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3084. digestSz - 1);
  3085. if (ret != BUFFER_E)
  3086. return -3287 - i;
  3087. }
  3088. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3089. if (ret != exp_ret)
  3090. return -3297 - i;
  3091. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3092. return -3307 -i;
  3093. ret = wc_HashGetBlockSize(typesGood[i]);
  3094. if (exp_ret < 0 && ret != exp_ret)
  3095. return -3308 - i;
  3096. if (exp_ret == 0 && ret < 0)
  3097. return -3318 - i;
  3098. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3099. ret = wc_HashGetOID(typesGood[i]);
  3100. if (ret == BAD_FUNC_ARG ||
  3101. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3102. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3103. return -3328 - i;
  3104. }
  3105. hashType = wc_OidGetHash(ret);
  3106. if (exp_ret == 0 && hashType != typesGood[i])
  3107. return -3338 - i;
  3108. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3109. }
  3110. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3111. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3112. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3113. return -3348 - i;
  3114. }
  3115. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3116. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3117. #ifdef WOLFSSL_MD2
  3118. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3119. return -3358;
  3120. #else
  3121. if (ret != HASH_TYPE_E)
  3122. return -3359;
  3123. #endif
  3124. hashType = wc_OidGetHash(646); /* Md2h */
  3125. #ifdef WOLFSSL_MD2
  3126. if (hashType != WC_HASH_TYPE_MD2)
  3127. return -3360;
  3128. #else
  3129. if (hashType != WC_HASH_TYPE_NONE)
  3130. return -3361;
  3131. #endif
  3132. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3133. #ifndef NO_MD5
  3134. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3135. return -3362;
  3136. #else
  3137. if (ret != HASH_TYPE_E)
  3138. return -3363;
  3139. #endif
  3140. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3141. if (ret != BAD_FUNC_ARG)
  3142. return -3364;
  3143. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3144. if (ret != BAD_FUNC_ARG)
  3145. return -3365;
  3146. hashType = wc_OidGetHash(0);
  3147. if (hashType != WC_HASH_TYPE_NONE)
  3148. return -3366;
  3149. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3150. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3151. #ifdef WOLFSSL_MD2
  3152. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3153. return -3367;
  3154. #else
  3155. if (ret != HASH_TYPE_E)
  3156. return -3368;
  3157. #endif
  3158. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3159. #ifdef WOLFSSL_MD2
  3160. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3161. return -3369;
  3162. #else
  3163. if (ret != HASH_TYPE_E)
  3164. return -3370;
  3165. #endif
  3166. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3167. #ifndef NO_MD4
  3168. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3169. return -3371;
  3170. #else
  3171. if (ret != HASH_TYPE_E)
  3172. return -3372;
  3173. #endif
  3174. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3175. #ifndef NO_MD4
  3176. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3177. return -3373;
  3178. #else
  3179. if (ret != HASH_TYPE_E)
  3180. return -3374;
  3181. #endif
  3182. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3183. #if !defined(NO_MD5) && !defined(NO_SHA)
  3184. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3185. return -3375;
  3186. #else
  3187. if (ret != HASH_TYPE_E)
  3188. return -3376;
  3189. #endif
  3190. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3191. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3192. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3193. return -3377;
  3194. #else
  3195. if (ret != HASH_TYPE_E)
  3196. return -3378;
  3197. #endif
  3198. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3199. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3200. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3201. return -3379;
  3202. #else
  3203. if (ret != HASH_TYPE_E)
  3204. return -3380;
  3205. #endif
  3206. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3207. if (ret != BAD_FUNC_ARG)
  3208. return -3381;
  3209. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3210. if (ret != BAD_FUNC_ARG)
  3211. return -3382;
  3212. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3213. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3214. ret = wc_GetCTC_HashOID(MD2);
  3215. if (ret == 0)
  3216. return -3383;
  3217. #endif
  3218. #ifndef NO_MD5
  3219. ret = wc_GetCTC_HashOID(WC_MD5);
  3220. if (ret == 0)
  3221. return -3384;
  3222. #endif
  3223. #ifndef NO_SHA
  3224. ret = wc_GetCTC_HashOID(WC_SHA);
  3225. if (ret == 0)
  3226. return -3385;
  3227. #endif
  3228. #ifdef WOLFSSL_SHA224
  3229. ret = wc_GetCTC_HashOID(WC_SHA224);
  3230. if (ret == 0)
  3231. return -3386;
  3232. #endif
  3233. #ifndef NO_SHA256
  3234. ret = wc_GetCTC_HashOID(WC_SHA256);
  3235. if (ret == 0)
  3236. return -3387;
  3237. #endif
  3238. #ifdef WOLFSSL_SHA384
  3239. ret = wc_GetCTC_HashOID(WC_SHA384);
  3240. if (ret == 0)
  3241. return -3388;
  3242. #endif
  3243. #ifdef WOLFSSL_SHA512
  3244. ret = wc_GetCTC_HashOID(WC_SHA512);
  3245. if (ret == 0)
  3246. return -3389;
  3247. #endif
  3248. ret = wc_GetCTC_HashOID(-1);
  3249. if (ret != 0)
  3250. return -3390;
  3251. #endif
  3252. return 0;
  3253. }
  3254. #endif /* !NO_HASH_WRAPPER */
  3255. #if !defined(NO_HMAC) && !defined(NO_MD5)
  3256. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  3257. {
  3258. Hmac hmac;
  3259. byte hash[WC_MD5_DIGEST_SIZE];
  3260. const char* keys[]=
  3261. {
  3262. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3263. "Jefe",
  3264. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3265. };
  3266. testVector a, b, c;
  3267. testVector test_hmac[3];
  3268. int ret;
  3269. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3270. a.input = "Hi There";
  3271. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  3272. "\x9d";
  3273. a.inLen = XSTRLEN(a.input);
  3274. a.outLen = WC_MD5_DIGEST_SIZE;
  3275. b.input = "what do ya want for nothing?";
  3276. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  3277. "\x38";
  3278. b.inLen = XSTRLEN(b.input);
  3279. b.outLen = WC_MD5_DIGEST_SIZE;
  3280. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3281. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3282. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3283. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3284. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  3285. "\xf6";
  3286. c.inLen = XSTRLEN(c.input);
  3287. c.outLen = WC_MD5_DIGEST_SIZE;
  3288. test_hmac[0] = a;
  3289. test_hmac[1] = b;
  3290. test_hmac[2] = c;
  3291. for (i = 0; i < times; ++i) {
  3292. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3293. if (i == 1) {
  3294. continue; /* cavium can't handle short keys, fips not allowed */
  3295. }
  3296. #endif
  3297. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  3298. return -3400;
  3299. }
  3300. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  3301. (word32)XSTRLEN(keys[i]));
  3302. if (ret != 0)
  3303. return -3401;
  3304. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3305. (word32)test_hmac[i].inLen);
  3306. if (ret != 0)
  3307. return -3402;
  3308. ret = wc_HmacFinal(&hmac, hash);
  3309. if (ret != 0)
  3310. return -3403;
  3311. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  3312. return -3404 - i;
  3313. wc_HmacFree(&hmac);
  3314. }
  3315. #ifndef HAVE_FIPS
  3316. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  3317. return -3414;
  3318. #endif
  3319. return 0;
  3320. }
  3321. #endif /* NO_HMAC && NO_MD5 */
  3322. #if !defined(NO_HMAC) && !defined(NO_SHA)
  3323. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  3324. {
  3325. Hmac hmac;
  3326. byte hash[WC_SHA_DIGEST_SIZE];
  3327. const char* keys[]=
  3328. {
  3329. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3330. "\x0b\x0b\x0b",
  3331. "Jefe",
  3332. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3333. "\xAA\xAA\xAA"
  3334. };
  3335. testVector a, b, c;
  3336. testVector test_hmac[3];
  3337. int ret;
  3338. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3339. a.input = "Hi There";
  3340. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  3341. "\x8e\xf1\x46\xbe\x00";
  3342. a.inLen = XSTRLEN(a.input);
  3343. a.outLen = WC_SHA_DIGEST_SIZE;
  3344. b.input = "what do ya want for nothing?";
  3345. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  3346. "\x9c\x25\x9a\x7c\x79";
  3347. b.inLen = XSTRLEN(b.input);
  3348. b.outLen = WC_SHA_DIGEST_SIZE;
  3349. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3350. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3351. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3352. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3353. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  3354. "\x4f\x63\xf1\x75\xd3";
  3355. c.inLen = XSTRLEN(c.input);
  3356. c.outLen = WC_SHA_DIGEST_SIZE;
  3357. test_hmac[0] = a;
  3358. test_hmac[1] = b;
  3359. test_hmac[2] = c;
  3360. for (i = 0; i < times; ++i) {
  3361. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3362. if (i == 1)
  3363. continue; /* cavium can't handle short keys, fips not allowed */
  3364. #endif
  3365. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3366. return -3500;
  3367. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  3368. (word32)XSTRLEN(keys[i]));
  3369. if (ret != 0)
  3370. return -3501;
  3371. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3372. (word32)test_hmac[i].inLen);
  3373. if (ret != 0)
  3374. return -3502;
  3375. ret = wc_HmacFinal(&hmac, hash);
  3376. if (ret != 0)
  3377. return -3503;
  3378. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  3379. return -3504 - i;
  3380. wc_HmacFree(&hmac);
  3381. }
  3382. #ifndef HAVE_FIPS
  3383. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  3384. return -3514;
  3385. #endif
  3386. return 0;
  3387. }
  3388. #endif
  3389. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  3390. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  3391. {
  3392. Hmac hmac;
  3393. byte hash[WC_SHA224_DIGEST_SIZE];
  3394. const char* keys[]=
  3395. {
  3396. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3397. "\x0b\x0b\x0b",
  3398. "Jefe",
  3399. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3400. "\xAA\xAA\xAA",
  3401. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3402. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3403. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3404. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3405. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3406. };
  3407. testVector a, b, c, d;
  3408. testVector test_hmac[4];
  3409. int ret;
  3410. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3411. a.input = "Hi There";
  3412. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  3413. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  3414. a.inLen = XSTRLEN(a.input);
  3415. a.outLen = WC_SHA224_DIGEST_SIZE;
  3416. b.input = "what do ya want for nothing?";
  3417. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  3418. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  3419. b.inLen = XSTRLEN(b.input);
  3420. b.outLen = WC_SHA224_DIGEST_SIZE;
  3421. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3422. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3423. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3424. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3425. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  3426. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  3427. c.inLen = XSTRLEN(c.input);
  3428. c.outLen = WC_SHA224_DIGEST_SIZE;
  3429. d.input = "Big Key Input";
  3430. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  3431. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  3432. d.inLen = XSTRLEN(d.input);
  3433. d.outLen = WC_SHA224_DIGEST_SIZE;
  3434. test_hmac[0] = a;
  3435. test_hmac[1] = b;
  3436. test_hmac[2] = c;
  3437. test_hmac[3] = d;
  3438. for (i = 0; i < times; ++i) {
  3439. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3440. if (i == 1)
  3441. continue; /* cavium can't handle short keys, fips not allowed */
  3442. #endif
  3443. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3444. return -3600;
  3445. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  3446. (word32)XSTRLEN(keys[i]));
  3447. if (ret != 0)
  3448. return -3601;
  3449. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3450. (word32)test_hmac[i].inLen);
  3451. if (ret != 0)
  3452. return -3602;
  3453. ret = wc_HmacFinal(&hmac, hash);
  3454. if (ret != 0)
  3455. return -3603;
  3456. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  3457. return -3604 - i;
  3458. wc_HmacFree(&hmac);
  3459. }
  3460. #ifndef HAVE_FIPS
  3461. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  3462. return -3614;
  3463. #endif
  3464. return 0;
  3465. }
  3466. #endif
  3467. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  3468. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  3469. {
  3470. Hmac hmac;
  3471. byte hash[WC_SHA256_DIGEST_SIZE];
  3472. const char* keys[]=
  3473. {
  3474. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3475. "\x0b\x0b\x0b",
  3476. "Jefe",
  3477. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3478. "\xAA\xAA\xAA",
  3479. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3480. "\xAA\xAA\xAA",
  3481. };
  3482. testVector a, b, c, d;
  3483. testVector test_hmac[4];
  3484. int ret;
  3485. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3486. a.input = "Hi There";
  3487. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  3488. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  3489. "\xcf\xf7";
  3490. a.inLen = XSTRLEN(a.input);
  3491. a.outLen = WC_SHA256_DIGEST_SIZE;
  3492. b.input = "what do ya want for nothing?";
  3493. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  3494. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  3495. "\x38\x43";
  3496. b.inLen = XSTRLEN(b.input);
  3497. b.outLen = WC_SHA256_DIGEST_SIZE;
  3498. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3499. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3500. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3501. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3502. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  3503. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  3504. "\x65\xfe";
  3505. c.inLen = XSTRLEN(c.input);
  3506. c.outLen = WC_SHA256_DIGEST_SIZE;
  3507. d.input = 0;
  3508. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  3509. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  3510. "\x3e\x46";
  3511. d.inLen = 0;
  3512. d.outLen = WC_SHA256_DIGEST_SIZE;
  3513. test_hmac[0] = a;
  3514. test_hmac[1] = b;
  3515. test_hmac[2] = c;
  3516. test_hmac[3] = d;
  3517. for (i = 0; i < times; ++i) {
  3518. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3519. if (i == 1)
  3520. continue; /* cavium can't handle short keys, fips not allowed */
  3521. #endif
  3522. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  3523. if (i == 3)
  3524. continue; /* QuickAssist can't handle empty HMAC */
  3525. #endif
  3526. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3527. return -3700 - i;
  3528. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  3529. (word32)XSTRLEN(keys[i]));
  3530. if (ret != 0)
  3531. return -3710 - i;
  3532. if (test_hmac[i].input != NULL) {
  3533. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3534. (word32)test_hmac[i].inLen);
  3535. if (ret != 0)
  3536. return -3720 - i;
  3537. }
  3538. ret = wc_HmacFinal(&hmac, hash);
  3539. if (ret != 0)
  3540. return -3730 - i;
  3541. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  3542. return -3740 - i;
  3543. wc_HmacFree(&hmac);
  3544. }
  3545. #ifndef HAVE_FIPS
  3546. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  3547. return -3750;
  3548. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  3549. return -3751;
  3550. #endif
  3551. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  3552. return -3752;
  3553. return 0;
  3554. }
  3555. #endif
  3556. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  3557. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  3558. {
  3559. Hmac hmac;
  3560. byte hash[WC_SHA384_DIGEST_SIZE];
  3561. const char* keys[]=
  3562. {
  3563. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3564. "\x0b\x0b\x0b",
  3565. "Jefe",
  3566. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3567. "\xAA\xAA\xAA",
  3568. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3569. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3570. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3571. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3572. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3573. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3574. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3575. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3576. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3577. };
  3578. testVector a, b, c, d;
  3579. testVector test_hmac[4];
  3580. int ret;
  3581. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3582. a.input = "Hi There";
  3583. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  3584. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  3585. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  3586. "\xfa\x9c\xb6";
  3587. a.inLen = XSTRLEN(a.input);
  3588. a.outLen = WC_SHA384_DIGEST_SIZE;
  3589. b.input = "what do ya want for nothing?";
  3590. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  3591. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  3592. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  3593. "\xb2\x16\x49";
  3594. b.inLen = XSTRLEN(b.input);
  3595. b.outLen = WC_SHA384_DIGEST_SIZE;
  3596. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3597. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3598. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3599. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3600. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  3601. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  3602. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  3603. "\xa3\x4f\x27";
  3604. c.inLen = XSTRLEN(c.input);
  3605. c.outLen = WC_SHA384_DIGEST_SIZE;
  3606. d.input = "Big Key Input";
  3607. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  3608. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  3609. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  3610. "\x57\x41\x69";
  3611. d.inLen = XSTRLEN(d.input);
  3612. d.outLen = WC_SHA384_DIGEST_SIZE;
  3613. test_hmac[0] = a;
  3614. test_hmac[1] = b;
  3615. test_hmac[2] = c;
  3616. test_hmac[3] = d;
  3617. for (i = 0; i < times; ++i) {
  3618. #if defined(HAVE_FIPS)
  3619. if (i == 1)
  3620. continue; /* fips not allowed */
  3621. #endif
  3622. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3623. return -3800;
  3624. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  3625. (word32)XSTRLEN(keys[i]));
  3626. if (ret != 0)
  3627. return -3801;
  3628. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3629. (word32)test_hmac[i].inLen);
  3630. if (ret != 0)
  3631. return -3802;
  3632. ret = wc_HmacFinal(&hmac, hash);
  3633. if (ret != 0)
  3634. return -3803;
  3635. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3636. return -3804 - i;
  3637. wc_HmacFree(&hmac);
  3638. }
  3639. #ifndef HAVE_FIPS
  3640. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  3641. return -3814;
  3642. #endif
  3643. return 0;
  3644. }
  3645. #endif
  3646. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  3647. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  3648. {
  3649. Hmac hmac;
  3650. byte hash[WC_SHA512_DIGEST_SIZE];
  3651. const char* keys[]=
  3652. {
  3653. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3654. "\x0b\x0b\x0b",
  3655. "Jefe",
  3656. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3657. "\xAA\xAA\xAA",
  3658. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3659. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3660. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3661. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3662. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3663. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3664. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3665. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3666. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3667. };
  3668. testVector a, b, c, d;
  3669. testVector test_hmac[4];
  3670. int ret;
  3671. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3672. a.input = "Hi There";
  3673. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  3674. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  3675. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  3676. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  3677. "\x3a\x12\x68\x54";
  3678. a.inLen = XSTRLEN(a.input);
  3679. a.outLen = WC_SHA512_DIGEST_SIZE;
  3680. b.input = "what do ya want for nothing?";
  3681. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  3682. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  3683. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  3684. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  3685. "\x38\xbc\xe7\x37";
  3686. b.inLen = XSTRLEN(b.input);
  3687. b.outLen = WC_SHA512_DIGEST_SIZE;
  3688. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3689. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3690. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3691. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3692. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  3693. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  3694. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  3695. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  3696. "\xe1\x32\x92\xfb";
  3697. c.inLen = XSTRLEN(c.input);
  3698. c.outLen = WC_SHA512_DIGEST_SIZE;
  3699. d.input = "Big Key Input";
  3700. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  3701. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  3702. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  3703. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  3704. "\x1e\x18\xfe\xfa";
  3705. d.inLen = XSTRLEN(d.input);
  3706. d.outLen = WC_SHA512_DIGEST_SIZE;
  3707. test_hmac[0] = a;
  3708. test_hmac[1] = b;
  3709. test_hmac[2] = c;
  3710. test_hmac[3] = d;
  3711. for (i = 0; i < times; ++i) {
  3712. #if defined(HAVE_FIPS)
  3713. if (i == 1)
  3714. continue; /* fips not allowed */
  3715. #endif
  3716. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3717. return -3900;
  3718. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  3719. (word32)XSTRLEN(keys[i]));
  3720. if (ret != 0)
  3721. return -3901;
  3722. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3723. (word32)test_hmac[i].inLen);
  3724. if (ret != 0)
  3725. return -3902;
  3726. ret = wc_HmacFinal(&hmac, hash);
  3727. if (ret != 0)
  3728. return -3903;
  3729. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3730. return -3904 - i;
  3731. wc_HmacFree(&hmac);
  3732. }
  3733. #ifndef HAVE_FIPS
  3734. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  3735. return -3914;
  3736. #endif
  3737. return 0;
  3738. }
  3739. #endif
  3740. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  3741. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  3742. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  3743. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  3744. {
  3745. Hmac hmac;
  3746. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3747. const char* key[4] =
  3748. {
  3749. "Jefe",
  3750. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3751. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3752. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3753. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3754. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3755. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3756. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3757. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3758. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3759. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3760. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3761. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3762. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3763. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3764. };
  3765. const char* input[4] =
  3766. {
  3767. "what do ya want for nothing?",
  3768. "Hi There",
  3769. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3770. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3771. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3772. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3773. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3774. "Big Key Input"
  3775. };
  3776. const int hashType[4] =
  3777. {
  3778. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  3779. };
  3780. const int hashSz[4] =
  3781. {
  3782. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  3783. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  3784. };
  3785. const char* output[16] =
  3786. {
  3787. /* key = jefe, input = what do ya want for nothing? */
  3788. /* HMAC-SHA3-224 */
  3789. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  3790. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  3791. /* HMAC-SHA3-256 */
  3792. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  3793. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  3794. /* HMAC-SHA3-384 */
  3795. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  3796. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  3797. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  3798. /* HMAC-SHA3-512 */
  3799. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  3800. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  3801. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  3802. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  3803. /* key = 0b..., input = Hi There */
  3804. /* HMAC-SHA3-224 */
  3805. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  3806. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  3807. /* HMAC-SHA3-256 */
  3808. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  3809. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  3810. /* HMAC-SHA3-384 */
  3811. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  3812. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  3813. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  3814. /* HMAC-SHA3-512 */
  3815. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  3816. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  3817. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  3818. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  3819. /* key = aa..., output = dd... */
  3820. /* HMAC-SHA3-224 */
  3821. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  3822. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  3823. /* HMAC-SHA3-256 */
  3824. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  3825. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  3826. /* HMAC-SHA3-384 */
  3827. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  3828. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  3829. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  3830. /* HMAC-SHA3-512 */
  3831. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  3832. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  3833. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  3834. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  3835. /* key = big key, input = Big Key Input */
  3836. /* HMAC-SHA3-224 */
  3837. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  3838. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  3839. /* HMAC-SHA3-256 */
  3840. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  3841. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  3842. /* HMAC-SHA3-384 */
  3843. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  3844. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  3845. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  3846. /* HMAC-SHA3-512 */
  3847. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  3848. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  3849. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  3850. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  3851. };
  3852. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  3853. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  3854. ret;
  3855. #ifdef HAVE_FIPS
  3856. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  3857. * short. Skip it in FIPS builds. */
  3858. i = 1;
  3859. #endif
  3860. for (; i < iMax; i++) {
  3861. for (j = 0; j < jMax; j++) {
  3862. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3863. return -4000;
  3864. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  3865. (word32)XSTRLEN(key[i]));
  3866. if (ret != 0)
  3867. return -4001;
  3868. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  3869. (word32)XSTRLEN(input[i]));
  3870. if (ret != 0)
  3871. return -4002;
  3872. ret = wc_HmacFinal(&hmac, hash);
  3873. if (ret != 0)
  3874. return -4003;
  3875. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  3876. return -4004;
  3877. wc_HmacFree(&hmac);
  3878. if (i > 0)
  3879. continue;
  3880. #ifndef HAVE_FIPS
  3881. ret = wc_HmacSizeByType(hashType[j]);
  3882. if (ret != hashSz[j])
  3883. return -4005;
  3884. #endif
  3885. }
  3886. }
  3887. return 0;
  3888. }
  3889. #endif
  3890. #ifdef WC_RC2
  3891. typedef struct rc2TestVector {
  3892. const char* input;
  3893. const char* output;
  3894. const char* key; /* Key, variable up to 128 bytes */
  3895. const char* iv; /* IV, 8-bytes */
  3896. int inLen;
  3897. int outLen;
  3898. int keyLen;
  3899. int effectiveKeyBits; /* Up to 1024 bits supported */
  3900. } rc2TestVector;
  3901. static int rc2_ecb_test(void)
  3902. {
  3903. int ret = 0;
  3904. byte cipher[RC2_BLOCK_SIZE];
  3905. byte plain[RC2_BLOCK_SIZE];
  3906. rc2TestVector a, b, c, d, e, f, g, h;
  3907. rc2TestVector test_rc2[8];
  3908. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  3909. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3910. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  3911. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3912. a.inLen = RC2_BLOCK_SIZE;
  3913. a.outLen = RC2_BLOCK_SIZE;
  3914. a.keyLen = 8;
  3915. a.effectiveKeyBits = 63;
  3916. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  3917. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  3918. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  3919. b.inLen = RC2_BLOCK_SIZE;
  3920. b.outLen = RC2_BLOCK_SIZE;
  3921. b.keyLen = 8;
  3922. b.effectiveKeyBits = 64;
  3923. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  3924. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  3925. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  3926. c.inLen = RC2_BLOCK_SIZE;
  3927. c.outLen = RC2_BLOCK_SIZE;
  3928. c.keyLen = 8;
  3929. c.effectiveKeyBits = 64;
  3930. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3931. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  3932. d.key = "\x88";
  3933. d.inLen = RC2_BLOCK_SIZE;
  3934. d.outLen = RC2_BLOCK_SIZE;
  3935. d.keyLen = 1;
  3936. d.effectiveKeyBits = 64;
  3937. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3938. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  3939. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  3940. e.inLen = RC2_BLOCK_SIZE;
  3941. e.outLen = RC2_BLOCK_SIZE;
  3942. e.keyLen = 7;
  3943. e.effectiveKeyBits = 64;
  3944. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3945. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  3946. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3947. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  3948. f.inLen = RC2_BLOCK_SIZE;
  3949. f.outLen = RC2_BLOCK_SIZE;
  3950. f.keyLen = 16;
  3951. f.effectiveKeyBits = 64;
  3952. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3953. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  3954. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3955. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  3956. g.inLen = RC2_BLOCK_SIZE;
  3957. g.outLen = RC2_BLOCK_SIZE;
  3958. g.keyLen = 16;
  3959. g.effectiveKeyBits = 128;
  3960. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3961. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  3962. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  3963. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  3964. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  3965. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  3966. "\x1e";
  3967. h.inLen = RC2_BLOCK_SIZE;
  3968. h.outLen = RC2_BLOCK_SIZE;
  3969. h.keyLen = 33;
  3970. h.effectiveKeyBits = 129;
  3971. test_rc2[0] = a;
  3972. test_rc2[1] = b;
  3973. test_rc2[2] = c;
  3974. test_rc2[3] = d;
  3975. test_rc2[4] = e;
  3976. test_rc2[5] = f;
  3977. test_rc2[6] = g;
  3978. test_rc2[7] = h;
  3979. for (i = 0; i < times; ++i) {
  3980. Rc2 enc;
  3981. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  3982. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  3983. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  3984. NULL, test_rc2[i].effectiveKeyBits);
  3985. if (ret != 0) {
  3986. return -4100;
  3987. }
  3988. /* ECB encrypt */
  3989. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  3990. (word32)test_rc2[i].outLen);
  3991. if (ret != 0) {
  3992. return -4101;
  3993. }
  3994. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  3995. return -4102;
  3996. }
  3997. /* ECB decrypt */
  3998. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  3999. if (ret != 0) {
  4000. return -4103;
  4001. }
  4002. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4003. return -4104;
  4004. }
  4005. }
  4006. return 0;
  4007. }
  4008. static int rc2_cbc_test(void)
  4009. {
  4010. int ret = 0;
  4011. byte cipher[128];
  4012. byte plain[128];
  4013. rc2TestVector a, b, c, d, e, f, g, h, i;
  4014. rc2TestVector test_rc2[9];
  4015. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4016. /* key length = 7, effective key bits = 63 */
  4017. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4018. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4019. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4020. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4021. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4022. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4023. a.inLen = RC2_BLOCK_SIZE*2;
  4024. a.outLen = RC2_BLOCK_SIZE*2;
  4025. a.keyLen = 8;
  4026. a.effectiveKeyBits = 63;
  4027. /* key length = 8, effective key bits = 64, all 0xFF */
  4028. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4029. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4030. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4031. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4032. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4033. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4034. b.inLen = RC2_BLOCK_SIZE*2;
  4035. b.outLen = RC2_BLOCK_SIZE*2;
  4036. b.keyLen = 8;
  4037. b.effectiveKeyBits = 64;
  4038. /* key length = 8, effective key bits = 64 */
  4039. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4040. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4041. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4042. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4043. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4044. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4045. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4046. c.inLen = RC2_BLOCK_SIZE*2;
  4047. c.outLen = RC2_BLOCK_SIZE*2;
  4048. c.keyLen = 8;
  4049. c.effectiveKeyBits = 64;
  4050. /* key length = 1, effective key bits = 64 */
  4051. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4052. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4053. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4054. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4055. d.key = "\x88";
  4056. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4057. d.inLen = RC2_BLOCK_SIZE*2;
  4058. d.outLen = RC2_BLOCK_SIZE*2;
  4059. d.keyLen = 1;
  4060. d.effectiveKeyBits = 64;
  4061. /* key length = 7, effective key bits = 64 */
  4062. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4063. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4064. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4065. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4066. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4067. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4068. e.inLen = RC2_BLOCK_SIZE*2;
  4069. e.outLen = RC2_BLOCK_SIZE*2;
  4070. e.keyLen = 7;
  4071. e.effectiveKeyBits = 64;
  4072. /* key length = 16, effective key bits = 64 */
  4073. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4074. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4075. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4076. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4077. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4078. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4079. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4080. f.inLen = RC2_BLOCK_SIZE*2;
  4081. f.outLen = RC2_BLOCK_SIZE*2;
  4082. f.keyLen = 16;
  4083. f.effectiveKeyBits = 64;
  4084. /* key length = 16, effective bits = 128 */
  4085. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4086. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4087. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4088. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4089. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4090. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4091. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4092. g.inLen = RC2_BLOCK_SIZE*2;
  4093. g.outLen = RC2_BLOCK_SIZE*2;
  4094. g.keyLen = 16;
  4095. g.effectiveKeyBits = 128;
  4096. /* key length = 33, effective bits = 129 */
  4097. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4098. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4099. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4100. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4101. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4102. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4103. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4104. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4105. "\x1e";
  4106. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4107. h.inLen = RC2_BLOCK_SIZE*2;
  4108. h.outLen = RC2_BLOCK_SIZE*2;
  4109. h.keyLen = 33;
  4110. h.effectiveKeyBits = 129;
  4111. /* key length = 10, effective bits = 40 */
  4112. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4113. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4114. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4115. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4116. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4117. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4118. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4119. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4120. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4121. "\x3E\x96";
  4122. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4123. i.inLen = RC2_BLOCK_SIZE*4;
  4124. i.outLen = RC2_BLOCK_SIZE*4;
  4125. i.keyLen = 10;
  4126. i.effectiveKeyBits = 40;
  4127. test_rc2[0] = a;
  4128. test_rc2[1] = b;
  4129. test_rc2[2] = c;
  4130. test_rc2[3] = d;
  4131. test_rc2[4] = e;
  4132. test_rc2[5] = f;
  4133. test_rc2[6] = g;
  4134. test_rc2[7] = h;
  4135. test_rc2[8] = i;
  4136. for (j = 0; j < times; ++j) {
  4137. Rc2 rc2;
  4138. XMEMSET(cipher, 0, sizeof(cipher));
  4139. XMEMSET(plain, 0, sizeof(plain));
  4140. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4141. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4142. if (ret != 0) {
  4143. return -4200;
  4144. }
  4145. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4146. test_rc2[j].inLen);
  4147. if (ret != 0) {
  4148. return -4201;
  4149. }
  4150. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4151. return -4202;
  4152. }
  4153. /* reset IV for decrypt, since overriden by encrypt operation */
  4154. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4155. if (ret != 0) {
  4156. return -4203;
  4157. }
  4158. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4159. if (ret != 0) {
  4160. return -4204;
  4161. }
  4162. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4163. return -4205;
  4164. }
  4165. }
  4166. return 0;
  4167. }
  4168. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4169. {
  4170. int ret = 0;
  4171. ret = rc2_ecb_test();
  4172. if (ret != 0) {
  4173. return ret;
  4174. }
  4175. return rc2_cbc_test();
  4176. }
  4177. #endif
  4178. #ifndef NO_RC4
  4179. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4180. {
  4181. byte cipher[16];
  4182. byte plain[16];
  4183. const char* keys[] =
  4184. {
  4185. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4186. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4187. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4188. "\xef\x01\x23\x45"
  4189. };
  4190. testVector a, b, c, d;
  4191. testVector test_arc4[4];
  4192. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4193. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4194. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4195. a.inLen = 8;
  4196. a.outLen = 8;
  4197. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4198. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4199. b.inLen = 8;
  4200. b.outLen = 8;
  4201. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4202. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4203. c.inLen = 8;
  4204. c.outLen = 8;
  4205. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4206. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4207. d.inLen = 10;
  4208. d.outLen = 10;
  4209. test_arc4[0] = a;
  4210. test_arc4[1] = b;
  4211. test_arc4[2] = c;
  4212. test_arc4[3] = d;
  4213. for (i = 0; i < times; ++i) {
  4214. Arc4 enc;
  4215. Arc4 dec;
  4216. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4217. if (i == 3)
  4218. keylen = 4;
  4219. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4220. return -4400;
  4221. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4222. return -4401;
  4223. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4224. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4225. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4226. (word32)test_arc4[i].outLen);
  4227. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4228. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4229. return -4402 - i;
  4230. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4231. return -4412 - i;
  4232. wc_Arc4Free(&enc);
  4233. wc_Arc4Free(&dec);
  4234. }
  4235. return 0;
  4236. }
  4237. #endif
  4238. WOLFSSL_TEST_SUBROUTINE int hc128_test(void)
  4239. {
  4240. #ifdef HAVE_HC128
  4241. byte cipher[16];
  4242. byte plain[16];
  4243. const char* keys[] =
  4244. {
  4245. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4246. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4247. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  4248. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  4249. };
  4250. const char* ivs[] =
  4251. {
  4252. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4253. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4254. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  4255. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  4256. };
  4257. testVector a, b, c, d;
  4258. testVector test_hc128[4];
  4259. int times = sizeof(test_hc128) / sizeof(testVector), i;
  4260. int ret = 0;
  4261. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  4262. HC128 enc[1], dec[1];
  4263. #else
  4264. HC128 *enc = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4265. HC128 *dec = (HC128 *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4266. if ((! enc) || (! dec)) {
  4267. ERROR_OUT(-4500, out);
  4268. }
  4269. #endif
  4270. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4271. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  4272. a.inLen = 8;
  4273. a.outLen = 8;
  4274. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4275. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  4276. b.inLen = 8;
  4277. b.outLen = 8;
  4278. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4279. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  4280. c.inLen = 8;
  4281. c.outLen = 8;
  4282. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4283. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  4284. d.inLen = 15;
  4285. d.outLen = 15;
  4286. test_hc128[0] = a;
  4287. test_hc128[1] = b;
  4288. test_hc128[2] = c;
  4289. test_hc128[3] = d;
  4290. for (i = 0; i < times; ++i) {
  4291. /* align keys/ivs in plain/cipher buffers */
  4292. XMEMCPY(plain, keys[i], 16);
  4293. XMEMCPY(cipher, ivs[i], 16);
  4294. wc_Hc128_SetKey(enc, plain, cipher);
  4295. wc_Hc128_SetKey(dec, plain, cipher);
  4296. /* align input */
  4297. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  4298. if (wc_Hc128_Process(enc, cipher, plain,
  4299. (word32)test_hc128[i].outLen) != 0) {
  4300. ret = -4501;
  4301. goto out;
  4302. }
  4303. if (wc_Hc128_Process(dec, plain, cipher,
  4304. (word32)test_hc128[i].outLen) != 0) {
  4305. ret = -4502;
  4306. goto out;
  4307. }
  4308. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen)) {
  4309. ret = -4503 - i;
  4310. goto out;
  4311. }
  4312. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen)) {
  4313. ret = -4513 - i;
  4314. goto out;
  4315. }
  4316. }
  4317. out:
  4318. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4319. if (enc)
  4320. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4321. if (dec)
  4322. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4323. #endif
  4324. return ret;
  4325. #else
  4326. return 0;
  4327. #endif /* HAVE_HC128 */
  4328. }
  4329. #ifndef NO_RABBIT
  4330. WOLFSSL_TEST_SUBROUTINE int rabbit_test(void)
  4331. {
  4332. byte cipher[16];
  4333. byte plain[16];
  4334. const char* keys[] =
  4335. {
  4336. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4337. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  4338. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  4339. };
  4340. const char* ivs[] =
  4341. {
  4342. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4343. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  4344. 0
  4345. };
  4346. testVector a, b, c;
  4347. testVector test_rabbit[3];
  4348. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  4349. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4350. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  4351. a.inLen = 8;
  4352. a.outLen = 8;
  4353. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4354. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  4355. b.inLen = 8;
  4356. b.outLen = 8;
  4357. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4358. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  4359. c.inLen = 8;
  4360. c.outLen = 8;
  4361. test_rabbit[0] = a;
  4362. test_rabbit[1] = b;
  4363. test_rabbit[2] = c;
  4364. for (i = 0; i < times; ++i) {
  4365. Rabbit enc;
  4366. Rabbit dec;
  4367. byte* iv;
  4368. /* align keys/ivs in plain/cipher buffers */
  4369. XMEMCPY(plain, keys[i], 16);
  4370. if (ivs[i]) {
  4371. XMEMCPY(cipher, ivs[i], 8);
  4372. iv = cipher;
  4373. } else
  4374. iv = NULL;
  4375. wc_RabbitSetKey(&enc, plain, iv);
  4376. wc_RabbitSetKey(&dec, plain, iv);
  4377. /* align input */
  4378. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  4379. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  4380. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  4381. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  4382. return -4600 - i;
  4383. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  4384. return -4610 - i;
  4385. }
  4386. return 0;
  4387. }
  4388. #endif /* NO_RABBIT */
  4389. #ifdef HAVE_CHACHA
  4390. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  4391. {
  4392. ChaCha enc;
  4393. ChaCha dec;
  4394. byte cipher[128];
  4395. byte plain[128];
  4396. byte sliver[64];
  4397. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  4398. word32 keySz = 32;
  4399. int ret = 0;
  4400. int i;
  4401. int times = 4;
  4402. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  4403. {
  4404. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4405. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4406. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4407. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4408. };
  4409. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  4410. {
  4411. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4412. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4413. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4414. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  4415. };
  4416. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  4417. {
  4418. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4420. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4421. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4422. };
  4423. /* 128 bit key */
  4424. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  4425. {
  4426. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4427. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4428. };
  4429. const byte* keys[] = {key1, key2, key3, key4};
  4430. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4431. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4432. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  4433. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4434. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  4435. #ifndef BENCH_EMBEDDED
  4436. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  4437. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  4438. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  4439. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  4440. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  4441. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  4442. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  4443. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  4444. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  4445. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  4446. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  4447. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  4448. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  4449. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  4450. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  4451. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  4452. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  4453. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  4454. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  4455. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  4456. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  4457. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  4458. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  4459. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  4460. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  4461. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  4462. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  4463. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  4464. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  4465. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  4466. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  4467. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  4468. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  4469. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  4470. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  4471. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  4472. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  4473. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  4474. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  4475. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  4476. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  4477. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  4478. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  4479. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  4480. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  4481. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  4482. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  4483. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  4484. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  4485. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  4486. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  4487. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  4488. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  4489. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  4490. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  4491. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  4492. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  4493. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  4494. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  4495. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  4496. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  4497. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  4498. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  4499. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  4500. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  4501. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  4502. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  4503. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  4504. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  4505. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  4506. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  4507. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  4508. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  4509. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  4510. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  4511. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  4512. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  4513. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  4514. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  4515. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  4516. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  4517. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  4518. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  4519. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  4520. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  4521. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  4522. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  4523. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  4524. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  4525. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  4526. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  4527. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  4528. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  4529. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  4530. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  4531. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  4532. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  4533. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  4534. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  4535. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  4536. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  4537. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  4538. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  4539. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  4540. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  4541. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  4542. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  4543. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  4544. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  4545. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  4546. };
  4547. #define CHACHA_BIG_TEST_SIZE 1305
  4548. #ifndef WOLFSSL_SMALL_STACK
  4549. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  4550. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  4551. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  4552. #else
  4553. byte* cipher_big;
  4554. byte* plain_big;
  4555. byte* input_big;
  4556. #endif /* WOLFSSL_SMALL_STACK */
  4557. int block_size;
  4558. #endif /* BENCH_EMBEDDED */
  4559. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  4560. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  4561. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  4562. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  4563. byte* test_chacha[4];
  4564. test_chacha[0] = a;
  4565. test_chacha[1] = b;
  4566. test_chacha[2] = c;
  4567. test_chacha[3] = d;
  4568. #ifndef BENCH_EMBEDDED
  4569. #ifdef WOLFSSL_SMALL_STACK
  4570. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4571. DYNAMIC_TYPE_TMP_BUFFER);
  4572. if (cipher_big == NULL) {
  4573. return MEMORY_E;
  4574. }
  4575. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4576. DYNAMIC_TYPE_TMP_BUFFER);
  4577. if (plain_big == NULL) {
  4578. return MEMORY_E;
  4579. }
  4580. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  4581. DYNAMIC_TYPE_TMP_BUFFER);
  4582. if (input_big == NULL) {
  4583. return MEMORY_E;
  4584. }
  4585. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  4586. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  4587. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  4588. #endif /* WOLFSSL_SMALL_STACK */
  4589. #endif /* BENCH_EMBEDDED */
  4590. for (i = 0; i < times; ++i) {
  4591. if (i < 3) {
  4592. keySz = 32;
  4593. }
  4594. else {
  4595. keySz = 16;
  4596. }
  4597. XMEMCPY(plain, keys[i], keySz);
  4598. XMEMSET(cipher, 0, 32);
  4599. XMEMCPY(cipher + 4, ivs[i], 8);
  4600. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  4601. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  4602. if (ret != 0)
  4603. return ret;
  4604. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4605. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  4606. if (ret != 0)
  4607. return ret;
  4608. XMEMCPY(plain, input, 8);
  4609. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  4610. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  4611. if (ret != 0)
  4612. return ret;
  4613. if (XMEMCMP(test_chacha[i], cipher, 8))
  4614. return -4700 - i;
  4615. if (XMEMCMP(plain, input, 8))
  4616. return -4710 - i;
  4617. }
  4618. /* test of starting at a different counter
  4619. encrypts all of the information and decrypts starting at 2nd chunk */
  4620. XMEMSET(plain, 0, sizeof(plain));
  4621. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  4622. XMEMSET(cipher, 0, sizeof(cipher));
  4623. XMEMCPY(cipher + 4, ivs[0], 8);
  4624. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4625. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4626. if (ret != 0)
  4627. return ret;
  4628. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  4629. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  4630. if (ret != 0)
  4631. return ret;
  4632. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  4633. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  4634. if (ret != 0)
  4635. return ret;
  4636. if (XMEMCMP(plain + 64, sliver, 64))
  4637. return -4720;
  4638. #ifndef BENCH_EMBEDDED
  4639. /* test of encrypting more data */
  4640. keySz = 32;
  4641. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4642. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4643. if (ret != 0)
  4644. return ret;
  4645. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4646. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4647. if (ret != 0)
  4648. return ret;
  4649. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  4650. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  4651. CHACHA_BIG_TEST_SIZE);
  4652. if (ret != 0)
  4653. return ret;
  4654. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4655. return -4721;
  4656. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4657. return -4722;
  4658. for (i = 0; i < 18; ++i) {
  4659. /* this will test all paths */
  4660. // block sizes: 1 2 3 4 7 8 15 16 31 32 63 64 127 128 255 256 511 512
  4661. block_size = (2 << (i%9)) - (i<9?1:0);
  4662. keySz = 32;
  4663. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  4664. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  4665. if (ret != 0)
  4666. return ret;
  4667. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  4668. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  4669. if (ret != 0)
  4670. return ret;
  4671. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, block_size);
  4672. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big, block_size);
  4673. if (ret != 0)
  4674. return ret;
  4675. if (XMEMCMP(plain_big, input_big, block_size))
  4676. return -4723-i;
  4677. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  4678. return -4724-i;
  4679. }
  4680. /* Streaming test */
  4681. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  4682. int j, rem;
  4683. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  4684. if (ret != 0)
  4685. return -4725;
  4686. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  4687. if (ret != 0)
  4688. return -4726;
  4689. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  4690. if (ret != 0)
  4691. return -4727;
  4692. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  4693. if (ret != 0)
  4694. return -4728;
  4695. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  4696. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  4697. if (ret != 0)
  4698. return -4729;
  4699. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  4700. if (ret != 0)
  4701. return -4730;
  4702. }
  4703. rem = CHACHA_BIG_TEST_SIZE - j;
  4704. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  4705. if (ret != 0)
  4706. return -4731;
  4707. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  4708. if (ret != 0)
  4709. return -4732;
  4710. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  4711. return -4733;
  4712. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  4713. return -4734;
  4714. }
  4715. #ifdef WOLFSSL_SMALL_STACK
  4716. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4717. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4718. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4719. #endif /* WOLFSSL_SMALL_STACK */
  4720. #endif /* BENCH_EMBEDDED */
  4721. return 0;
  4722. }
  4723. #endif /* HAVE_CHACHA */
  4724. #ifdef HAVE_POLY1305
  4725. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  4726. {
  4727. int ret = 0;
  4728. int i;
  4729. byte tag[16];
  4730. Poly1305 enc;
  4731. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  4732. {
  4733. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  4734. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  4735. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  4736. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  4737. 0x75,0x70
  4738. };
  4739. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  4740. {
  4741. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  4742. 0x6c,0x64,0x21
  4743. };
  4744. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  4745. {
  4746. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4747. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4748. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4749. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4750. };
  4751. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  4752. {
  4753. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4754. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4755. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4756. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4757. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4758. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4759. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4760. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4761. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4762. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4763. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4764. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4765. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4766. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4767. 0x61,0x16
  4768. };
  4769. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  4770. {
  4771. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4772. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  4773. };
  4774. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  4775. {
  4776. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  4777. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4778. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  4779. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4780. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4781. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  4782. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4783. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  4784. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  4785. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  4786. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4787. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  4788. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  4789. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  4790. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  4791. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  4792. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  4793. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  4794. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  4795. 0x61,0x16
  4796. };
  4797. byte additional[] =
  4798. {
  4799. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  4800. 0xc4,0xc5,0xc6,0xc7
  4801. };
  4802. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  4803. {
  4804. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4805. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4806. };
  4807. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  4808. {
  4809. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  4810. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  4811. };
  4812. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  4813. {
  4814. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  4815. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  4816. };
  4817. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  4818. {
  4819. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  4820. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  4821. };
  4822. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  4823. {
  4824. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  4825. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  4826. };
  4827. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  4828. {
  4829. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4830. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4831. };
  4832. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  4833. {
  4834. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  4835. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  4836. };
  4837. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  4838. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  4839. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  4840. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  4841. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  4842. };
  4843. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4844. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  4845. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  4846. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  4847. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  4848. };
  4849. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  4850. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  4851. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  4852. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  4853. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  4854. };
  4855. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  4856. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4857. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4858. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4859. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4860. };
  4861. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  4862. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  4863. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  4864. const byte* keys[] = {key, key, key2, key2, key5, key};
  4865. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  4866. correct6};
  4867. for (i = 0; i < 6; i++) {
  4868. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  4869. if (ret != 0)
  4870. return -4800 - i;
  4871. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  4872. if (ret != 0)
  4873. return -4810 - i;
  4874. ret = wc_Poly1305Final(&enc, tag);
  4875. if (ret != 0)
  4876. return -4820 - i;
  4877. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  4878. return -4830 - i;
  4879. }
  4880. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  4881. XMEMSET(tag, 0, sizeof(tag));
  4882. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  4883. if (ret != 0)
  4884. return -4840;
  4885. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4886. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4887. if (ret != 0)
  4888. return -4841;
  4889. if (XMEMCMP(tag, correct4, sizeof(tag)))
  4890. return -4842;
  4891. /* Check fail of TLS MAC function if altering additional data */
  4892. XMEMSET(tag, 0, sizeof(tag));
  4893. additional[0]++;
  4894. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  4895. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  4896. if (ret != 0)
  4897. return -4843;
  4898. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  4899. return -4844;
  4900. return 0;
  4901. }
  4902. #endif /* HAVE_POLY1305 */
  4903. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4904. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  4905. {
  4906. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  4907. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4908. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  4909. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  4910. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  4911. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  4912. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  4913. };
  4914. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  4915. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  4916. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  4917. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  4918. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  4919. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  4920. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  4921. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  4922. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  4923. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  4924. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  4925. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  4926. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  4927. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  4928. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  4929. 0x74, 0x2e
  4930. };
  4931. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  4932. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  4933. 0x44, 0x45, 0x46, 0x47
  4934. };
  4935. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  4936. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  4937. 0xc4, 0xc5, 0xc6, 0xc7
  4938. };
  4939. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  4940. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  4941. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  4942. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  4943. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  4944. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  4945. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  4946. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  4947. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  4948. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  4949. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  4950. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  4951. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  4952. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  4953. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  4954. 0x61, 0x16
  4955. };
  4956. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  4957. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  4958. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  4959. };
  4960. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  4961. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  4962. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  4963. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  4964. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  4965. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  4966. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  4967. };
  4968. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  4969. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  4970. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  4971. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  4972. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4973. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  4974. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  4975. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  4976. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  4977. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  4978. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  4979. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  4980. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  4981. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  4982. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  4983. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  4984. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  4985. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  4986. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  4987. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  4988. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  4989. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  4990. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  4991. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  4992. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  4993. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  4994. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  4995. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  4996. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  4997. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  4998. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  4999. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5000. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5001. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5002. 0x9d
  5003. };
  5004. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5005. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5006. 0x05, 0x06, 0x07, 0x08
  5007. };
  5008. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5009. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5010. 0x00, 0x00, 0x4e, 0x91
  5011. };
  5012. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5013. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5014. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5015. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5016. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5017. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5018. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5019. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5020. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5021. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5022. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5023. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5024. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5025. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5026. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5027. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5028. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5029. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5030. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5031. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5032. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5033. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5034. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5035. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5036. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5037. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5038. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5039. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5040. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5041. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5042. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5043. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5044. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5045. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5046. 0x9b
  5047. };
  5048. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5049. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5050. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5051. };
  5052. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5053. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5054. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5055. int err;
  5056. ChaChaPoly_Aead aead;
  5057. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5058. #define TEST_SMALL_CHACHA_CHUNKS 32
  5059. #else
  5060. #define TEST_SMALL_CHACHA_CHUNKS 64
  5061. #endif
  5062. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5063. word32 testLen;
  5064. #endif
  5065. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5066. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5067. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5068. /* Parameter Validation testing */
  5069. /* Encrypt */
  5070. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5071. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5072. if (err != BAD_FUNC_ARG)
  5073. return -4900;
  5074. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5075. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5076. generatedAuthTag);
  5077. if (err != BAD_FUNC_ARG)
  5078. return -4901;
  5079. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5080. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5081. if (err != BAD_FUNC_ARG)
  5082. return -4902;
  5083. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5084. sizeof(plaintext1), NULL, generatedAuthTag);
  5085. if (err != BAD_FUNC_ARG)
  5086. return -4903;
  5087. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5088. sizeof(plaintext1), generatedCiphertext, NULL);
  5089. if (err != BAD_FUNC_ARG)
  5090. return -4904;
  5091. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5092. 0, generatedCiphertext, generatedAuthTag);
  5093. if (err != BAD_FUNC_ARG)
  5094. return -4905;
  5095. /* Decrypt */
  5096. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5097. sizeof(cipher2), authTag2, generatedPlaintext);
  5098. if (err != BAD_FUNC_ARG)
  5099. return -4906;
  5100. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5101. sizeof(cipher2), authTag2, generatedPlaintext);
  5102. if (err != BAD_FUNC_ARG)
  5103. return -4907;
  5104. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5105. sizeof(cipher2), authTag2, generatedPlaintext);
  5106. if (err != BAD_FUNC_ARG)
  5107. return -4908;
  5108. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5109. sizeof(cipher2), NULL, generatedPlaintext);
  5110. if (err != BAD_FUNC_ARG)
  5111. return -4909;
  5112. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5113. sizeof(cipher2), authTag2, NULL);
  5114. if (err != BAD_FUNC_ARG)
  5115. return -4910;
  5116. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5117. 0, authTag2, generatedPlaintext);
  5118. if (err != BAD_FUNC_ARG)
  5119. return -4911;
  5120. /* Test #1 */
  5121. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5122. aad1, sizeof(aad1),
  5123. plaintext1, sizeof(plaintext1),
  5124. generatedCiphertext, generatedAuthTag);
  5125. if (err) {
  5126. return err;
  5127. }
  5128. /* -- Check the ciphertext and authtag */
  5129. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5130. return -4912;
  5131. }
  5132. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5133. return -4913;
  5134. }
  5135. /* -- Verify decryption works */
  5136. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5137. aad1, sizeof(aad1),
  5138. cipher1, sizeof(cipher1),
  5139. authTag1, generatedPlaintext);
  5140. if (err) {
  5141. return err;
  5142. }
  5143. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5144. return -4914;
  5145. }
  5146. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5147. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5148. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5149. /* Test #2 */
  5150. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5151. aad2, sizeof(aad2),
  5152. plaintext2, sizeof(plaintext2),
  5153. generatedCiphertext, generatedAuthTag);
  5154. if (err) {
  5155. return err;
  5156. }
  5157. /* -- Check the ciphertext and authtag */
  5158. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5159. return -4915;
  5160. }
  5161. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5162. return -4916;
  5163. }
  5164. /* -- Verify decryption works */
  5165. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5166. aad2, sizeof(aad2),
  5167. cipher2, sizeof(cipher2),
  5168. authTag2, generatedPlaintext);
  5169. if (err) {
  5170. return err;
  5171. }
  5172. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5173. return -4917;
  5174. }
  5175. /* AEAD init/update/final - bad argument tests */
  5176. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5177. CHACHA20_POLY1305_AEAD_DECRYPT);
  5178. if (err != BAD_FUNC_ARG)
  5179. return -4918;
  5180. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5181. CHACHA20_POLY1305_AEAD_DECRYPT);
  5182. if (err != BAD_FUNC_ARG)
  5183. return -4919;
  5184. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5185. CHACHA20_POLY1305_AEAD_DECRYPT);
  5186. if (err != BAD_FUNC_ARG)
  5187. return -4920;
  5188. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5189. if (err != BAD_FUNC_ARG)
  5190. return -4921;
  5191. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5192. if (err != BAD_FUNC_ARG)
  5193. return -4922;
  5194. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5195. generatedPlaintext, sizeof(plaintext1));
  5196. if (err != BAD_FUNC_ARG)
  5197. return -4923;
  5198. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5199. sizeof(plaintext1));
  5200. if (err != BAD_FUNC_ARG)
  5201. return -4924;
  5202. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5203. sizeof(plaintext1));
  5204. if (err != BAD_FUNC_ARG)
  5205. return -4925;
  5206. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5207. if (err != BAD_FUNC_ARG)
  5208. return -4926;
  5209. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5210. if (err != BAD_FUNC_ARG)
  5211. return -4927;
  5212. /* AEAD init/update/final - bad state tests */
  5213. /* clear struct - make valgrind happy to resolve
  5214. "Conditional jump or move depends on uninitialised value(s)".
  5215. The enum is "int" size and aead.state is "byte" */
  5216. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5217. XMEMSET(&aead, 0, sizeof(aead));
  5218. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5219. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5220. if (err != BAD_STATE_E)
  5221. return -4928;
  5222. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5223. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5224. if (err != BAD_STATE_E)
  5225. return -4929;
  5226. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5227. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5228. generatedPlaintext, sizeof(plaintext1));
  5229. if (err != BAD_STATE_E)
  5230. return -4930;
  5231. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5232. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5233. if (err != BAD_STATE_E)
  5234. return -4931;
  5235. aead.state = CHACHA20_POLY1305_STATE_READY;
  5236. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5237. if (err != BAD_STATE_E)
  5238. return -4932;
  5239. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5240. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5241. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5242. /* Test 1 - Encrypt */
  5243. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5244. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5245. if (err != 0)
  5246. return -4933;
  5247. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5248. if (err != 0)
  5249. return -4934;
  5250. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5251. /* test doing data in smaller chunks */
  5252. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5253. word32 dataLen = sizeof(plaintext1) - testLen;
  5254. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5255. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5256. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5257. &generatedCiphertext[testLen], dataLen);
  5258. if (err != 0)
  5259. return -4935;
  5260. testLen += dataLen;
  5261. }
  5262. #else
  5263. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5264. generatedCiphertext, sizeof(plaintext1));
  5265. #endif
  5266. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5267. if (err != 0)
  5268. return -4936;
  5269. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5270. if (err != 0)
  5271. return -4937;
  5272. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5273. return -4938;
  5274. }
  5275. /* Test 1 - Decrypt */
  5276. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5277. CHACHA20_POLY1305_AEAD_DECRYPT);
  5278. if (err != 0)
  5279. return -4939;
  5280. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5281. if (err != 0)
  5282. return -4940;
  5283. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5284. /* test doing data in smaller chunks */
  5285. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5286. word32 dataLen = sizeof(plaintext1) - testLen;
  5287. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5288. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5289. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5290. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5291. dataLen);
  5292. if (err != 0)
  5293. return -4941;
  5294. testLen += dataLen;
  5295. }
  5296. #else
  5297. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5298. generatedPlaintext, sizeof(cipher1));
  5299. #endif
  5300. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5301. if (err != 0)
  5302. return -4942;
  5303. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5304. if (err != 0)
  5305. return -4943;
  5306. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5307. return -4944;
  5308. }
  5309. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5310. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5311. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5312. /* Test 2 - Encrypt */
  5313. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5314. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5315. if (err != 0)
  5316. return -4945;
  5317. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5318. if (err != 0)
  5319. return -4946;
  5320. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5321. /* test doing data in smaller chunks */
  5322. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5323. word32 dataLen = sizeof(plaintext2) - testLen;
  5324. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5325. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5326. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5327. &generatedCiphertext[testLen], dataLen);
  5328. if (err != 0)
  5329. return -4947;
  5330. testLen += dataLen;
  5331. }
  5332. #else
  5333. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5334. sizeof(plaintext2));
  5335. #endif
  5336. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5337. if (err != 0)
  5338. return -4948;
  5339. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5340. if (err != 0)
  5341. return -4949;
  5342. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5343. return -4950;
  5344. }
  5345. /* Test 2 - Decrypt */
  5346. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5347. CHACHA20_POLY1305_AEAD_DECRYPT);
  5348. if (err != 0)
  5349. return -4951;
  5350. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5351. if (err != 0)
  5352. return -4952;
  5353. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5354. /* test doing data in smaller chunks */
  5355. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5356. word32 dataLen = sizeof(plaintext2) - testLen;
  5357. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5358. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5359. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5360. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5361. dataLen);
  5362. if (err != 0)
  5363. return -4953;
  5364. testLen += dataLen;
  5365. }
  5366. #else
  5367. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5368. generatedPlaintext, sizeof(cipher2));
  5369. #endif
  5370. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5371. if (err != 0)
  5372. return -4954;
  5373. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5374. if (err != 0)
  5375. return -4955;
  5376. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5377. return -4956;
  5378. }
  5379. return err;
  5380. }
  5381. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5382. #ifndef NO_DES3
  5383. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  5384. {
  5385. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  5386. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5387. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5388. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5389. };
  5390. byte plain[24];
  5391. byte cipher[24];
  5392. Des enc;
  5393. Des dec;
  5394. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  5395. {
  5396. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  5397. };
  5398. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  5399. {
  5400. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  5401. };
  5402. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  5403. {
  5404. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  5405. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  5406. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  5407. };
  5408. int ret;
  5409. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  5410. if (ret != 0)
  5411. return -5000;
  5412. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5413. if (ret != 0)
  5414. return -5001;
  5415. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  5416. if (ret != 0)
  5417. return -5002;
  5418. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5419. if (ret != 0)
  5420. return -5003;
  5421. if (XMEMCMP(plain, vector, sizeof(plain)))
  5422. return -5004;
  5423. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  5424. return -5005;
  5425. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  5426. if (ret != 0)
  5427. return -5006;
  5428. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5429. {
  5430. EncryptedInfo info;
  5431. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5432. XMEMCPY(info.iv, iv, sizeof(iv));
  5433. info.ivSz = sizeof(iv);
  5434. info.keySz = sizeof(key);
  5435. info.cipherType = WC_CIPHER_DES;
  5436. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5437. sizeof(key), WC_HASH_TYPE_SHA);
  5438. if (ret != 0)
  5439. return -5007;
  5440. /* Test invalid info ptr */
  5441. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  5442. sizeof(key), WC_HASH_TYPE_SHA);
  5443. if (ret != BAD_FUNC_ARG)
  5444. return -5008;
  5445. #ifndef NO_PWDBASED
  5446. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  5447. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5448. sizeof(key), WC_HASH_TYPE_NONE);
  5449. if (ret == 0)
  5450. return -5009;
  5451. #endif /* !NO_PWDBASED */
  5452. }
  5453. #endif
  5454. return 0;
  5455. }
  5456. #endif /* !NO_DES3 */
  5457. #ifndef NO_DES3
  5458. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  5459. {
  5460. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  5461. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5462. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5463. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5464. };
  5465. byte plain[24];
  5466. byte cipher[24];
  5467. Des3 enc;
  5468. Des3 dec;
  5469. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5470. {
  5471. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  5472. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  5473. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  5474. };
  5475. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5476. {
  5477. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  5478. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  5479. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  5480. };
  5481. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  5482. {
  5483. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  5484. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  5485. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  5486. };
  5487. int ret;
  5488. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  5489. return -5100;
  5490. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  5491. return -5101;
  5492. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  5493. if (ret != 0)
  5494. return -5102;
  5495. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  5496. if (ret != 0)
  5497. return -5103;
  5498. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5499. #if defined(WOLFSSL_ASYNC_CRYPT)
  5500. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5501. #endif
  5502. if (ret != 0)
  5503. return -5104;
  5504. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5505. #if defined(WOLFSSL_ASYNC_CRYPT)
  5506. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5507. #endif
  5508. if (ret != 0)
  5509. return -5105;
  5510. if (XMEMCMP(plain, vector, sizeof(plain)))
  5511. return -5106;
  5512. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5513. return -5107;
  5514. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  5515. /* test the same vectors with using compatibility layer */
  5516. {
  5517. DES_key_schedule ks1;
  5518. DES_key_schedule ks2;
  5519. DES_key_schedule ks3;
  5520. DES_cblock iv4;
  5521. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  5522. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  5523. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  5524. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  5525. XMEMSET(plain, 0, sizeof(plain));
  5526. XMEMSET(cipher, 0, sizeof(cipher));
  5527. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  5528. &iv4, DES_ENCRYPT);
  5529. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  5530. &iv4, DES_DECRYPT);
  5531. if (XMEMCMP(plain, vector, sizeof(plain)))
  5532. return -5108;
  5533. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  5534. return -5109;
  5535. }
  5536. #endif /* OPENSSL_EXTRA */
  5537. wc_Des3Free(&enc);
  5538. wc_Des3Free(&dec);
  5539. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5540. {
  5541. EncryptedInfo info;
  5542. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5543. XMEMCPY(info.iv, iv3, sizeof(iv3));
  5544. info.ivSz = sizeof(iv3);
  5545. info.keySz = sizeof(key3);
  5546. info.cipherType = WC_CIPHER_DES3;
  5547. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  5548. sizeof(key3), WC_HASH_TYPE_SHA);
  5549. if (ret != 0)
  5550. return -5110;
  5551. }
  5552. #endif
  5553. return 0;
  5554. }
  5555. #endif /* NO_DES3 */
  5556. #ifndef NO_AES
  5557. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  5558. defined(WOLFSSL_AES_XTS)
  5559. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5560. /* pass in the function, key, iv, plain text and expected and this function
  5561. * tests that the encryption and decryption is successful */
  5562. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  5563. const byte* iv, const byte* plain, int plainSz,
  5564. const byte* expected, int expectedSz)
  5565. {
  5566. #ifdef WOLFSSL_SMALL_STACK
  5567. EVP_CIPHER_CTX *ctx = NULL;
  5568. #else
  5569. EVP_CIPHER_CTX ctx[1];
  5570. #endif
  5571. int idx, ret = 0, cipherSz;
  5572. byte* cipher;
  5573. #ifdef WOLFSSL_SMALL_STACK
  5574. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  5575. return MEMORY_E;
  5576. #endif
  5577. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5578. if (cipher == NULL) {
  5579. ret = -5120;
  5580. goto EVP_TEST_END;
  5581. }
  5582. /* test encrypt */
  5583. EVP_CIPHER_CTX_init(ctx);
  5584. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  5585. ret = -5121;
  5586. goto EVP_TEST_END;
  5587. }
  5588. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  5589. ret = -5122;
  5590. goto EVP_TEST_END;
  5591. }
  5592. cipherSz = idx;
  5593. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5594. ret = -5123;
  5595. goto EVP_TEST_END;
  5596. }
  5597. cipherSz += idx;
  5598. if (XMEMCMP(cipher, expected, plainSz)) {
  5599. ret = -5124;
  5600. goto EVP_TEST_END;
  5601. }
  5602. /* test decrypt */
  5603. EVP_CIPHER_CTX_init(ctx);
  5604. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  5605. ret = -5125;
  5606. goto EVP_TEST_END;
  5607. }
  5608. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  5609. ret = -5126;
  5610. goto EVP_TEST_END;
  5611. }
  5612. cipherSz = idx;
  5613. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  5614. ret = -5127;
  5615. goto EVP_TEST_END;
  5616. }
  5617. cipherSz += idx;
  5618. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  5619. ret = -5128;
  5620. goto EVP_TEST_END;
  5621. }
  5622. EVP_TEST_END:
  5623. if (cipher)
  5624. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5625. (void)cipherSz;
  5626. #ifdef WOLFSSL_SMALL_STACK
  5627. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  5628. #endif
  5629. return ret;
  5630. }
  5631. #endif /* OPENSSL_EXTRA */
  5632. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  5633. #ifdef WOLFSSL_AES_OFB
  5634. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  5635. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  5636. {
  5637. #ifdef WOLFSSL_AES_256
  5638. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5639. {
  5640. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  5641. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  5642. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  5643. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  5644. };
  5645. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  5646. {
  5647. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  5648. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  5649. };
  5650. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  5651. {
  5652. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  5653. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  5654. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  5655. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  5656. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  5657. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  5658. };
  5659. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5660. {
  5661. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  5662. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  5663. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  5664. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  5665. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  5666. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  5667. };
  5668. #endif /* WOLFSSL_AES_256 */
  5669. #ifdef WOLFSSL_AES_128
  5670. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5671. {
  5672. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  5673. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  5674. };
  5675. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  5676. {
  5677. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5678. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5679. };
  5680. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  5681. {
  5682. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5683. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5684. };
  5685. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  5686. {
  5687. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  5688. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  5689. };
  5690. #endif /* WOLFSSL_AES_128 */
  5691. #ifdef WOLFSSL_AES_192
  5692. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  5693. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  5694. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  5695. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  5696. };
  5697. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  5698. {
  5699. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5700. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5701. };
  5702. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  5703. {
  5704. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  5705. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  5706. };
  5707. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  5708. {
  5709. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5710. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5711. };
  5712. #endif /* WOLFSSL_AES_192 */
  5713. #ifdef WOLFSSL_SMALL_STACK
  5714. Aes *enc = NULL;
  5715. #else
  5716. Aes enc[1];
  5717. #endif
  5718. byte cipher[AES_BLOCK_SIZE * 4];
  5719. #ifdef HAVE_AES_DECRYPT
  5720. #ifdef WOLFSSL_SMALL_STACK
  5721. Aes *dec = NULL;
  5722. #else
  5723. Aes dec[1];
  5724. #endif
  5725. byte plain [AES_BLOCK_SIZE * 4];
  5726. #endif
  5727. int ret = 0;
  5728. #ifdef WOLFSSL_SMALL_STACK
  5729. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5730. ERROR_OUT(-1, out);
  5731. #ifdef HAVE_AES_DECRYPT
  5732. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  5733. ERROR_OUT(-1, out);
  5734. #endif
  5735. #endif
  5736. XMEMSET(enc, 0, sizeof *enc);
  5737. #ifdef HAVE_AES_DECRYPT
  5738. XMEMSET(dec, 0, sizeof *dec);
  5739. #endif
  5740. #ifdef WOLFSSL_AES_128
  5741. /* 128 key size test */
  5742. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5743. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  5744. cipher2, sizeof(cipher2));
  5745. if (ret != 0) {
  5746. goto out;
  5747. }
  5748. #endif
  5749. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5750. if (ret != 0)
  5751. ERROR_OUT(-5129, out);
  5752. #ifdef HAVE_AES_DECRYPT
  5753. /* decrypt uses AES_ENCRYPTION */
  5754. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5755. if (ret != 0)
  5756. ERROR_OUT(-5130, out);
  5757. #endif
  5758. XMEMSET(cipher, 0, sizeof(cipher));
  5759. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  5760. if (ret != 0)
  5761. ERROR_OUT(-5131, out);
  5762. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  5763. ERROR_OUT(-5132, out);
  5764. #ifdef HAVE_AES_DECRYPT
  5765. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  5766. if (ret != 0)
  5767. ERROR_OUT(-5133, out);
  5768. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  5769. ERROR_OUT(-5134, out);
  5770. #endif /* HAVE_AES_DECRYPT */
  5771. #endif /* WOLFSSL_AES_128 */
  5772. #ifdef WOLFSSL_AES_192
  5773. /* 192 key size test */
  5774. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5775. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  5776. cipher3, sizeof(cipher3));
  5777. if (ret != 0) {
  5778. goto out;
  5779. }
  5780. #endif
  5781. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5782. if (ret != 0)
  5783. ERROR_OUT(-5135, out);
  5784. #ifdef HAVE_AES_DECRYPT
  5785. /* decrypt uses AES_ENCRYPTION */
  5786. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  5787. if (ret != 0)
  5788. ERROR_OUT(-5136, out);
  5789. #endif
  5790. XMEMSET(cipher, 0, sizeof(cipher));
  5791. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  5792. if (ret != 0)
  5793. ERROR_OUT(-5137, out);
  5794. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  5795. ERROR_OUT(-5138, out);
  5796. #ifdef HAVE_AES_DECRYPT
  5797. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  5798. if (ret != 0)
  5799. ERROR_OUT(-5139, out);
  5800. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  5801. ERROR_OUT(-5140, out);
  5802. #endif /* HAVE_AES_DECRYPT */
  5803. #endif /* WOLFSSL_AES_192 */
  5804. #ifdef WOLFSSL_AES_256
  5805. /* 256 key size test */
  5806. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  5807. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  5808. cipher1, sizeof(cipher1));
  5809. if (ret != 0) {
  5810. goto out;
  5811. }
  5812. #endif
  5813. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5814. if (ret != 0)
  5815. ERROR_OUT(-5141, out);
  5816. #ifdef HAVE_AES_DECRYPT
  5817. /* decrypt uses AES_ENCRYPTION */
  5818. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5819. if (ret != 0)
  5820. ERROR_OUT(-5142, out);
  5821. #endif
  5822. XMEMSET(cipher, 0, sizeof(cipher));
  5823. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  5824. if (ret != 0)
  5825. ERROR_OUT(-5143, out);
  5826. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  5827. ERROR_OUT(-5144, out);
  5828. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  5829. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5830. if (ret != 0)
  5831. ERROR_OUT(-5145, out);
  5832. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  5833. AES_BLOCK_SIZE))
  5834. ERROR_OUT(-5146, out);
  5835. #ifdef HAVE_AES_DECRYPT
  5836. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  5837. if (ret != 0)
  5838. ERROR_OUT(-5147, out);
  5839. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  5840. ERROR_OUT(-5148, out);
  5841. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  5842. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5843. if (ret != 0)
  5844. ERROR_OUT(-5149, out);
  5845. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  5846. AES_BLOCK_SIZE))
  5847. ERROR_OUT(-5150, out);
  5848. #endif /* HAVE_AES_DECRYPT */
  5849. /* multiple blocks at once */
  5850. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5851. if (ret != 0)
  5852. ERROR_OUT(-5151, out);
  5853. #ifdef HAVE_AES_DECRYPT
  5854. /* decrypt uses AES_ENCRYPTION */
  5855. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5856. if (ret != 0)
  5857. ERROR_OUT(-5152, out);
  5858. #endif
  5859. XMEMSET(cipher, 0, sizeof(cipher));
  5860. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  5861. if (ret != 0)
  5862. ERROR_OUT(-5153, out);
  5863. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  5864. ERROR_OUT(-5154, out);
  5865. #ifdef HAVE_AES_DECRYPT
  5866. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  5867. if (ret != 0)
  5868. ERROR_OUT(-5155, out);
  5869. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  5870. ERROR_OUT(-5156, out);
  5871. #endif /* HAVE_AES_DECRYPT */
  5872. /* inline decrypt/encrypt*/
  5873. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5874. if (ret != 0)
  5875. ERROR_OUT(-5157, out);
  5876. #ifdef HAVE_AES_DECRYPT
  5877. /* decrypt uses AES_ENCRYPTION */
  5878. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5879. if (ret != 0)
  5880. ERROR_OUT(-5158, out);
  5881. #endif
  5882. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  5883. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  5884. if (ret != 0)
  5885. ERROR_OUT(-5159, out);
  5886. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  5887. ERROR_OUT(-5160, out);
  5888. #ifdef HAVE_AES_DECRYPT
  5889. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  5890. if (ret != 0)
  5891. ERROR_OUT(-5161, out);
  5892. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  5893. ERROR_OUT(-5162, out);
  5894. #endif /* HAVE_AES_DECRYPT */
  5895. /* 256 key size test leftover support */
  5896. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5897. if (ret != 0)
  5898. ERROR_OUT(-5163, out);
  5899. #ifdef HAVE_AES_DECRYPT
  5900. /* decrypt uses AES_ENCRYPTION */
  5901. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  5902. if (ret != 0)
  5903. ERROR_OUT(-5164, out);
  5904. #endif
  5905. XMEMSET(cipher, 0, sizeof(cipher));
  5906. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  5907. if (ret != 0)
  5908. ERROR_OUT(-5165, out);
  5909. if (XMEMCMP(cipher, cipher1, 3))
  5910. ERROR_OUT(-5166, out);
  5911. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  5912. if (ret != 0)
  5913. ERROR_OUT(-5167, out);
  5914. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  5915. ERROR_OUT(-5168, out);
  5916. #ifdef HAVE_AES_DECRYPT
  5917. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  5918. if (ret != 0)
  5919. ERROR_OUT(-5169, out);
  5920. if (XMEMCMP(plain, plain1, 6))
  5921. ERROR_OUT(-5170, out);
  5922. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  5923. if (ret != 0)
  5924. ERROR_OUT(-5171, out);
  5925. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  5926. ERROR_OUT(-5172, out);
  5927. #endif /* HAVE_AES_DECRYPT */
  5928. out:
  5929. #ifdef WOLFSSL_SMALL_STACK
  5930. if (enc)
  5931. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  5932. #ifdef HAVE_AES_DECRYPT
  5933. if (dec)
  5934. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  5935. #endif
  5936. #endif
  5937. #endif /* WOLFSSL_AES_256 */
  5938. return ret;
  5939. }
  5940. #endif /* WOLFSSL_AES_OFB */
  5941. #if defined(WOLFSSL_AES_CFB)
  5942. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  5943. static int aescfb_test(void)
  5944. {
  5945. #ifdef WOLFSSL_SMALL_STACK
  5946. Aes *enc = NULL;
  5947. #else
  5948. Aes enc[1];
  5949. #endif
  5950. int enc_inited = 0;
  5951. byte cipher[AES_BLOCK_SIZE * 4];
  5952. #ifdef HAVE_AES_DECRYPT
  5953. #ifdef WOLFSSL_SMALL_STACK
  5954. Aes *dec = NULL;
  5955. #else
  5956. Aes dec[1];
  5957. #endif
  5958. int dec_inited = 0;
  5959. byte plain [AES_BLOCK_SIZE * 4];
  5960. #endif
  5961. int ret = 0;
  5962. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  5963. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  5964. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  5965. };
  5966. #ifdef WOLFSSL_AES_128
  5967. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5968. {
  5969. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  5970. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  5971. };
  5972. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  5973. {
  5974. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  5975. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  5976. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  5977. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  5978. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  5979. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  5980. };
  5981. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5982. {
  5983. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5984. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5985. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5986. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5987. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5988. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  5989. };
  5990. #endif /* WOLFSSL_AES_128 */
  5991. #ifdef WOLFSSL_AES_192
  5992. /* 192 size key test */
  5993. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5994. {
  5995. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  5996. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  5997. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  5998. };
  5999. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6000. {
  6001. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6002. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6003. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6004. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6005. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6006. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6007. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6008. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6009. };
  6010. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6011. {
  6012. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6013. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6014. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6015. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6016. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6017. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6018. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6019. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6020. };
  6021. #endif /* WOLFSSL_AES_192 */
  6022. #ifdef WOLFSSL_AES_256
  6023. /* 256 size key simple test */
  6024. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6025. {
  6026. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6027. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6028. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6029. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6030. };
  6031. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6032. {
  6033. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6034. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6035. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6036. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6037. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6038. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6039. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6040. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6041. };
  6042. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6043. {
  6044. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6045. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6046. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6047. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6048. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6049. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6050. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6051. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6052. };
  6053. #endif /* WOLFSSL_AES_256 */
  6054. #ifdef WOLFSSL_SMALL_STACK
  6055. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6056. ERROR_OUT(-1, out);
  6057. #ifdef HAVE_AES_DECRYPT
  6058. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6059. ERROR_OUT(-1, out);
  6060. #endif
  6061. #endif
  6062. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6063. ERROR_OUT(-5173, out);
  6064. else
  6065. enc_inited = 1;
  6066. #ifdef HAVE_AES_DECRYPT
  6067. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6068. ERROR_OUT(-5174, out);
  6069. else
  6070. dec_inited = 1;
  6071. #endif
  6072. #ifdef WOLFSSL_AES_128
  6073. /* 128 key tests */
  6074. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6075. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6076. cipher1, sizeof(cipher1));
  6077. if (ret != 0) {
  6078. return ret;
  6079. }
  6080. #endif
  6081. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6082. if (ret != 0)
  6083. ERROR_OUT(-5175, out);
  6084. #ifdef HAVE_AES_DECRYPT
  6085. /* decrypt uses AES_ENCRYPTION */
  6086. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6087. if (ret != 0)
  6088. ERROR_OUT(-5176, out);
  6089. #endif
  6090. XMEMSET(cipher, 0, sizeof(cipher));
  6091. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6092. if (ret != 0)
  6093. ERROR_OUT(-5177, out);
  6094. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6095. ERROR_OUT(-5178, out);
  6096. /* test restarting encryption process */
  6097. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6098. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6099. if (ret != 0)
  6100. ERROR_OUT(-5179, out);
  6101. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6102. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6103. ERROR_OUT(-5180, out);
  6104. #ifdef HAVE_AES_DECRYPT
  6105. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6106. if (ret != 0)
  6107. ERROR_OUT(-5181, out);
  6108. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6109. ERROR_OUT(-5182, out);
  6110. #endif /* HAVE_AES_DECRYPT */
  6111. #endif /* WOLFSSL_AES_128 */
  6112. #ifdef WOLFSSL_AES_192
  6113. /* 192 key size test */
  6114. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6115. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6116. cipher2, sizeof(cipher2));
  6117. if (ret != 0) {
  6118. return ret;
  6119. }
  6120. #endif
  6121. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6122. if (ret != 0)
  6123. ERROR_OUT(-5183, out);
  6124. #ifdef HAVE_AES_DECRYPT
  6125. /* decrypt uses AES_ENCRYPTION */
  6126. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6127. if (ret != 0)
  6128. ERROR_OUT(-5184, out);
  6129. #endif
  6130. XMEMSET(cipher, 0, sizeof(cipher));
  6131. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6132. if (ret != 0)
  6133. ERROR_OUT(-5185, out);
  6134. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6135. ERROR_OUT(-5186, out);
  6136. #ifdef HAVE_AES_DECRYPT
  6137. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6138. if (ret != 0)
  6139. ERROR_OUT(-5187, out);
  6140. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6141. ERROR_OUT(-5188, out);
  6142. #endif /* HAVE_AES_DECRYPT */
  6143. #endif /* WOLFSSL_AES_192 */
  6144. #ifdef WOLFSSL_AES_256
  6145. /* 256 key size test */
  6146. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6147. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6148. cipher3, sizeof(cipher3));
  6149. if (ret != 0) {
  6150. return ret;
  6151. }
  6152. #endif
  6153. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6154. if (ret != 0)
  6155. ERROR_OUT(-5189, out);
  6156. #ifdef HAVE_AES_DECRYPT
  6157. /* decrypt uses AES_ENCRYPTION */
  6158. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6159. if (ret != 0)
  6160. ERROR_OUT(-5190, out);
  6161. #endif
  6162. /* test with data left overs, magic lengths are checking near edges */
  6163. XMEMSET(cipher, 0, sizeof(cipher));
  6164. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6165. if (ret != 0)
  6166. ERROR_OUT(-5191, out);
  6167. if (XMEMCMP(cipher, cipher3, 4))
  6168. ERROR_OUT(-5192, out);
  6169. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6170. if (ret != 0)
  6171. ERROR_OUT(-5193, out);
  6172. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6173. ERROR_OUT(-5194, out);
  6174. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6175. (AES_BLOCK_SIZE * 4) - 31);
  6176. if (ret != 0)
  6177. ERROR_OUT(-5195, out);
  6178. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6179. ERROR_OUT(-5196, out);
  6180. #ifdef HAVE_AES_DECRYPT
  6181. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6182. if (ret != 0)
  6183. ERROR_OUT(-5197, out);
  6184. if (XMEMCMP(plain, msg3, 4))
  6185. ERROR_OUT(-5198, out);
  6186. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6187. if (ret != 0)
  6188. ERROR_OUT(-5199, out);
  6189. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6190. if (ret != 0)
  6191. ERROR_OUT(-5200, out);
  6192. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6193. ERROR_OUT(-5201, out);
  6194. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6195. (AES_BLOCK_SIZE * 4) - 31);
  6196. if (ret != 0)
  6197. ERROR_OUT(-5202, out);
  6198. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6199. ERROR_OUT(-5203, out);
  6200. #endif /* HAVE_AES_DECRYPT */
  6201. #endif /* WOLFSSL_AES_256 */
  6202. out:
  6203. if (enc_inited)
  6204. wc_AesFree(enc);
  6205. if (dec_inited)
  6206. wc_AesFree(dec);
  6207. #ifdef WOLFSSL_SMALL_STACK
  6208. if (enc)
  6209. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6210. #ifdef HAVE_AES_DECRYPT
  6211. if (dec)
  6212. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6213. #endif
  6214. #endif
  6215. return ret;
  6216. }
  6217. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6218. static int aescfb1_test(void)
  6219. {
  6220. #ifdef WOLFSSL_SMALL_STACK
  6221. Aes *enc = NULL;
  6222. #else
  6223. Aes enc[1];
  6224. #endif
  6225. int enc_inited = 0;
  6226. byte cipher[AES_BLOCK_SIZE];
  6227. #ifdef HAVE_AES_DECRYPT
  6228. #ifdef WOLFSSL_SMALL_STACK
  6229. Aes *dec = NULL;
  6230. #else
  6231. Aes dec[1];
  6232. #endif
  6233. int dec_inited = 0;
  6234. byte plain [AES_BLOCK_SIZE];
  6235. #endif
  6236. int ret = 0;
  6237. #ifdef WOLFSSL_AES_128
  6238. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6239. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6240. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6241. };
  6242. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6243. {
  6244. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6245. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6246. };
  6247. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6248. {
  6249. 0x00
  6250. };
  6251. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6252. {
  6253. 0xC0
  6254. };
  6255. #endif /* WOLFSSL_AES_128 */
  6256. #ifdef WOLFSSL_AES_192
  6257. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6258. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6259. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6260. };
  6261. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6262. {
  6263. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6264. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6265. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6266. };
  6267. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6268. {
  6269. 0x30
  6270. };
  6271. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6272. {
  6273. 0x80
  6274. };
  6275. #endif /* WOLFSSL_AES_192 */
  6276. #ifdef WOLFSSL_AES_256
  6277. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6278. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6279. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6280. };
  6281. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6282. {
  6283. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6284. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6285. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6286. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6287. };
  6288. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6289. {
  6290. 0xF7, 0x00
  6291. };
  6292. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6293. {
  6294. 0x41, 0xC0
  6295. };
  6296. #endif /* WOLFSSL_AES_256 */
  6297. #ifdef WOLFSSL_SMALL_STACK
  6298. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6299. ERROR_OUT(-1, out);
  6300. #ifdef HAVE_AES_DECRYPT
  6301. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6302. ERROR_OUT(-1, out);
  6303. #endif
  6304. #endif
  6305. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6306. ERROR_OUT(-5204, out);
  6307. else
  6308. enc_inited = 1;
  6309. #ifdef HAVE_AES_DECRYPT
  6310. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6311. ERROR_OUT(-5205, out);
  6312. else
  6313. dec_inited = 1;
  6314. #endif
  6315. #ifdef WOLFSSL_AES_128
  6316. /* 128 key tests */
  6317. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6318. if (ret != 0)
  6319. ERROR_OUT(-5206, out);
  6320. #ifdef HAVE_AES_DECRYPT
  6321. /* decrypt uses AES_ENCRYPTION */
  6322. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6323. if (ret != 0)
  6324. ERROR_OUT(-5207, out);
  6325. #endif
  6326. XMEMSET(cipher, 0, sizeof(cipher));
  6327. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6328. if (ret != 0)
  6329. ERROR_OUT(-5208, out);
  6330. if (cipher[0] != cipher1[0])
  6331. ERROR_OUT(-5209, out);
  6332. #ifdef HAVE_AES_DECRYPT
  6333. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6334. if (ret != 0)
  6335. ERROR_OUT(-5210, out);
  6336. if (plain[0] != msg1[0])
  6337. ERROR_OUT(-5211, out);
  6338. #endif /* HAVE_AES_DECRYPT */
  6339. #ifdef OPENSSL_EXTRA
  6340. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6341. if (ret != 0)
  6342. ERROR_OUT(-5212, out);
  6343. XMEMSET(cipher, 0, sizeof(cipher));
  6344. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6345. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6346. if (ret != 0)
  6347. ERROR_OUT(-5213, out);
  6348. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6349. cipher, sizeof(msg1));
  6350. if (ret != 0) {
  6351. goto out;
  6352. }
  6353. #endif
  6354. #endif /* WOLFSSL_AES_128 */
  6355. #ifdef WOLFSSL_AES_192
  6356. /* 192 key tests */
  6357. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6358. if (ret != 0)
  6359. ERROR_OUT(-5214, out);
  6360. XMEMSET(cipher, 0, sizeof(cipher));
  6361. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6362. if (ret != 0)
  6363. ERROR_OUT(-5215, out);
  6364. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6365. ERROR_OUT(-5216, out);
  6366. #ifdef OPENSSL_EXTRA
  6367. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6368. if (ret != 0)
  6369. ERROR_OUT(-5217, out);
  6370. XMEMSET(cipher, 0, sizeof(cipher));
  6371. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  6372. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  6373. if (ret != 0)
  6374. ERROR_OUT(-5218, out);
  6375. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  6376. cipher, sizeof(msg2));
  6377. if (ret != 0) {
  6378. goto out;
  6379. }
  6380. #endif
  6381. #endif /* WOLFSSL_AES_192 */
  6382. #ifdef WOLFSSL_AES_256
  6383. /* 256 key tests */
  6384. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6385. if (ret != 0)
  6386. ERROR_OUT(-5219, out);
  6387. XMEMSET(cipher, 0, sizeof(cipher));
  6388. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  6389. if (ret != 0)
  6390. ERROR_OUT(-5220, out);
  6391. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6392. ERROR_OUT(-5221, out);
  6393. #ifdef OPENSSL_EXTRA
  6394. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6395. if (ret != 0)
  6396. ERROR_OUT(-5222, out);
  6397. XMEMSET(cipher, 0, sizeof(cipher));
  6398. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  6399. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  6400. if (ret != 0)
  6401. ERROR_OUT(-5223, out);
  6402. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  6403. cipher, sizeof(msg3));
  6404. if (ret != 0) {
  6405. goto out;
  6406. }
  6407. #endif
  6408. out:
  6409. if (enc_inited)
  6410. wc_AesFree(enc);
  6411. #ifdef HAVE_AES_DECRYPT
  6412. if (dec_inited)
  6413. wc_AesFree(dec);
  6414. #endif
  6415. #ifdef WOLFSSL_SMALL_STACK
  6416. if (enc)
  6417. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6418. #ifdef HAVE_AES_DECRYPT
  6419. if (dec)
  6420. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6421. #endif
  6422. #endif
  6423. #endif /* WOLFSSL_AES_256 */
  6424. return ret;
  6425. }
  6426. static int aescfb8_test(void)
  6427. {
  6428. #ifdef WOLFSSL_SMALL_STACK
  6429. Aes *enc = NULL;
  6430. #else
  6431. Aes enc[1];
  6432. #endif
  6433. int enc_inited = 0;
  6434. byte cipher[AES_BLOCK_SIZE];
  6435. #ifdef HAVE_AES_DECRYPT
  6436. #ifdef WOLFSSL_SMALL_STACK
  6437. Aes *dec = NULL;
  6438. #else
  6439. Aes dec[1];
  6440. #endif
  6441. int dec_inited = 0;
  6442. byte plain [AES_BLOCK_SIZE];
  6443. #endif
  6444. int ret = 0;
  6445. #ifdef WOLFSSL_AES_128
  6446. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6447. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  6448. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  6449. };
  6450. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6451. {
  6452. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  6453. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  6454. };
  6455. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6456. {
  6457. 0xd2,0x76,0x91
  6458. };
  6459. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6460. {
  6461. 0xc9,0x06,0x35
  6462. };
  6463. #endif /* WOLFSSL_AES_128 */
  6464. #ifdef WOLFSSL_AES_192
  6465. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6466. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  6467. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  6468. };
  6469. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6470. {
  6471. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  6472. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  6473. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  6474. };
  6475. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6476. {
  6477. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  6478. 0xb5,0xe8
  6479. };
  6480. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6481. {
  6482. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  6483. 0x3a,0x60
  6484. };
  6485. #endif
  6486. #ifdef WOLFSSL_AES_256
  6487. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6488. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  6489. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  6490. };
  6491. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6492. {
  6493. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  6494. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  6495. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  6496. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  6497. };
  6498. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6499. {
  6500. 0x1c,0xff,0x95
  6501. };
  6502. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6503. {
  6504. 0xb9,0x74,0xfa
  6505. };
  6506. #endif
  6507. #ifdef WOLFSSL_SMALL_STACK
  6508. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6509. ERROR_OUT(-5238, out);
  6510. #ifdef HAVE_AES_DECRYPT
  6511. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6512. ERROR_OUT(-5239, out);
  6513. #endif
  6514. #endif
  6515. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6516. ERROR_OUT(-5224, out);
  6517. else
  6518. enc_inited = 1;
  6519. #ifdef HAVE_AES_DECRYPT
  6520. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6521. ERROR_OUT(-5225, out);
  6522. else
  6523. dec_inited = 1;
  6524. #endif
  6525. #ifdef WOLFSSL_AES_128
  6526. /* 128 key tests */
  6527. #ifdef OPENSSL_EXTRA
  6528. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  6529. cipher1, sizeof(cipher1));
  6530. if (ret != 0) {
  6531. return ret;
  6532. }
  6533. #endif
  6534. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6535. if (ret != 0)
  6536. ERROR_OUT(-5226, out);
  6537. #ifdef HAVE_AES_DECRYPT
  6538. /* decrypt uses AES_ENCRYPTION */
  6539. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6540. if (ret != 0)
  6541. ERROR_OUT(-5227, out);
  6542. #endif
  6543. XMEMSET(cipher, 0, sizeof(cipher));
  6544. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  6545. if (ret != 0)
  6546. ERROR_OUT(-5228, out);
  6547. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  6548. ERROR_OUT(-5229, out);
  6549. #ifdef HAVE_AES_DECRYPT
  6550. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  6551. if (ret != 0)
  6552. ERROR_OUT(-5230, out);
  6553. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  6554. ERROR_OUT(-5231, out);
  6555. #endif /* HAVE_AES_DECRYPT */
  6556. #endif /* WOLFSSL_AES_128 */
  6557. #ifdef WOLFSSL_AES_192
  6558. /* 192 key tests */
  6559. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6560. if (ret != 0)
  6561. ERROR_OUT(-5232, out);
  6562. XMEMSET(cipher, 0, sizeof(cipher));
  6563. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  6564. if (ret != 0)
  6565. ERROR_OUT(-5233, out);
  6566. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  6567. ERROR_OUT(-5234, out);
  6568. #ifdef OPENSSL_EXTRA
  6569. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  6570. cipher2, sizeof(msg2));
  6571. if (ret != 0) {
  6572. return ret;
  6573. }
  6574. #endif
  6575. #endif /* WOLFSSL_AES_192 */
  6576. #ifdef WOLFSSL_AES_256
  6577. /* 256 key tests */
  6578. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6579. if (ret != 0)
  6580. ERROR_OUT(-5235, out);
  6581. XMEMSET(cipher, 0, sizeof(cipher));
  6582. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  6583. if (ret != 0)
  6584. ERROR_OUT(-5236, out);
  6585. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6586. ERROR_OUT(-5237, out);
  6587. #ifdef OPENSSL_EXTRA
  6588. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  6589. cipher3, sizeof(msg3));
  6590. if (ret != 0) {
  6591. goto out;
  6592. }
  6593. #endif
  6594. out:
  6595. if (enc_inited)
  6596. wc_AesFree(enc);
  6597. #ifdef HAVE_AES_DECRYPT
  6598. if (dec_inited)
  6599. wc_AesFree(dec);
  6600. #endif
  6601. #ifdef WOLFSSL_SMALL_STACK
  6602. if (enc)
  6603. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6604. #ifdef HAVE_AES_DECRYPT
  6605. if (dec)
  6606. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6607. #endif
  6608. #endif
  6609. #endif /* WOLFSSL_AES_256 */
  6610. return ret;
  6611. }
  6612. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  6613. #endif /* WOLFSSL_AES_CFB */
  6614. static int aes_key_size_test(void)
  6615. {
  6616. int ret;
  6617. #ifdef WOLFSSL_SMALL_STACK
  6618. Aes *aes;
  6619. #else
  6620. Aes aes[1];
  6621. #endif
  6622. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6623. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6624. #ifndef WOLFSSL_CRYPTOCELL
  6625. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6626. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6627. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  6628. #endif
  6629. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6630. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  6631. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  6632. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  6633. byte iv[] = "1234567890abcdef";
  6634. #ifndef HAVE_FIPS
  6635. word32 keySize;
  6636. #endif
  6637. #ifdef WOLFSSL_SMALL_STACK
  6638. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6639. return -5315;
  6640. #endif
  6641. #if !defined(HAVE_FIPS) || \
  6642. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  6643. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  6644. * supported with that FIPS version */
  6645. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  6646. if (ret != BAD_FUNC_ARG)
  6647. ERROR_OUT(-5300, out);
  6648. #endif
  6649. ret = wc_AesInit(aes, HEAP_HINT, devId);
  6650. /* 0 check OK for FIPSv1 */
  6651. if (ret != 0)
  6652. ERROR_OUT(-5301, out);
  6653. #ifndef HAVE_FIPS
  6654. /* Parameter Validation testing. */
  6655. ret = wc_AesGetKeySize(NULL, NULL);
  6656. if (ret != BAD_FUNC_ARG)
  6657. ERROR_OUT(-5302, out);
  6658. ret = wc_AesGetKeySize(aes, NULL);
  6659. if (ret != BAD_FUNC_ARG)
  6660. ERROR_OUT(-5303, out);
  6661. ret = wc_AesGetKeySize(NULL, &keySize);
  6662. if (ret != BAD_FUNC_ARG)
  6663. ERROR_OUT(-5304, out);
  6664. /* Crashes in FIPS */
  6665. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6666. if (ret != BAD_FUNC_ARG)
  6667. ERROR_OUT(-5305, out);
  6668. #endif
  6669. /* NULL IV indicates to use all zeros IV. */
  6670. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  6671. #ifdef WOLFSSL_AES_128
  6672. if (ret != 0)
  6673. #else
  6674. if (ret != BAD_FUNC_ARG)
  6675. #endif
  6676. ERROR_OUT(-5306, out);
  6677. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  6678. if (ret != BAD_FUNC_ARG)
  6679. ERROR_OUT(-5307, out);
  6680. /* CryptoCell handles rounds internally */
  6681. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  6682. /* Force invalid rounds */
  6683. aes->rounds = 16;
  6684. ret = wc_AesGetKeySize(aes, &keySize);
  6685. if (ret != BAD_FUNC_ARG)
  6686. ERROR_OUT(-5308, out);
  6687. #endif
  6688. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  6689. #ifdef WOLFSSL_AES_128
  6690. if (ret != 0)
  6691. #else
  6692. if (ret != BAD_FUNC_ARG)
  6693. #endif
  6694. ERROR_OUT(-5309, out);
  6695. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  6696. ret = wc_AesGetKeySize(aes, &keySize);
  6697. if (ret != 0 || keySize != sizeof(key16))
  6698. ERROR_OUT(-5310, out);
  6699. #endif
  6700. #ifndef WOLFSSL_CRYPTOCELL
  6701. /* Cryptocell only supports AES-128 key size */
  6702. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  6703. #ifdef WOLFSSL_AES_192
  6704. if (ret != 0)
  6705. #else
  6706. if (ret != BAD_FUNC_ARG)
  6707. #endif
  6708. ERROR_OUT(-5311, out);
  6709. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  6710. ret = wc_AesGetKeySize(aes, &keySize);
  6711. if (ret != 0 || keySize != sizeof(key24))
  6712. ERROR_OUT(-5312, out);
  6713. #endif
  6714. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  6715. #ifdef WOLFSSL_AES_256
  6716. if (ret != 0)
  6717. #else
  6718. if (ret != BAD_FUNC_ARG)
  6719. #endif
  6720. ERROR_OUT(-5313, out);
  6721. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  6722. ret = wc_AesGetKeySize(aes, &keySize);
  6723. if (ret != 0 || keySize != sizeof(key32))
  6724. ERROR_OUT(-5314, out);
  6725. #endif
  6726. #endif /* !WOLFSSL_CRYPTOCELL */
  6727. out:
  6728. #ifdef WOLFSSL_SMALL_STACK
  6729. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6730. #endif
  6731. return ret;
  6732. }
  6733. #if defined(WOLFSSL_AES_XTS)
  6734. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  6735. #ifdef WOLFSSL_AES_128
  6736. static int aes_xts_128_test(void)
  6737. {
  6738. #ifdef WOLFSSL_SMALL_STACK
  6739. XtsAes *aes = NULL;
  6740. #else
  6741. XtsAes aes[1];
  6742. #endif
  6743. int aes_inited = 0;
  6744. int ret = 0;
  6745. unsigned char buf[AES_BLOCK_SIZE * 2];
  6746. unsigned char cipher[AES_BLOCK_SIZE * 2];
  6747. /* 128 key tests */
  6748. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6749. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  6750. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  6751. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  6752. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  6753. };
  6754. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6755. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  6756. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6757. };
  6758. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6759. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6760. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  6761. };
  6762. /* plain text test of partial block is not from NIST test vector list */
  6763. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6764. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6765. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6766. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6767. };
  6768. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6769. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  6770. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  6771. };
  6772. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6773. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  6774. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  6775. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  6776. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  6777. };
  6778. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6779. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  6780. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  6781. };
  6782. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6783. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  6784. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  6785. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  6786. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  6787. };
  6788. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6789. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  6790. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  6791. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  6792. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  6793. };
  6794. #ifdef WOLFSSL_SMALL_STACK
  6795. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6796. ERROR_OUT(-5417, out);
  6797. #endif
  6798. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6799. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6800. if (ret != 0) {
  6801. printf("EVP_aes_128_xts failed!\n");
  6802. goto out;
  6803. }
  6804. #endif
  6805. XMEMSET(buf, 0, sizeof(buf));
  6806. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6807. HEAP_HINT, devId) != 0)
  6808. ERROR_OUT(-5400, out);
  6809. else
  6810. aes_inited = 1;
  6811. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6812. #if defined(WOLFSSL_ASYNC_CRYPT)
  6813. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6814. #endif
  6815. if (ret != 0)
  6816. ERROR_OUT(-5401, out);
  6817. if (XMEMCMP(c2, buf, sizeof(c2)))
  6818. ERROR_OUT(-5402, out);
  6819. XMEMSET(buf, 0, sizeof(buf));
  6820. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6821. HEAP_HINT, devId) != 0)
  6822. ERROR_OUT(-5403, out);
  6823. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  6824. #if defined(WOLFSSL_ASYNC_CRYPT)
  6825. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6826. #endif
  6827. if (ret != 0)
  6828. ERROR_OUT(-5404, out);
  6829. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  6830. ERROR_OUT(-5405, out);
  6831. /* partial block encryption test */
  6832. XMEMSET(cipher, 0, sizeof(cipher));
  6833. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  6834. #if defined(WOLFSSL_ASYNC_CRYPT)
  6835. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6836. #endif
  6837. if (ret != 0)
  6838. ERROR_OUT(-5406, out);
  6839. wc_AesXtsFree(aes);
  6840. /* partial block decrypt test */
  6841. XMEMSET(buf, 0, sizeof(buf));
  6842. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  6843. HEAP_HINT, devId) != 0)
  6844. ERROR_OUT(-5407, out);
  6845. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  6846. #if defined(WOLFSSL_ASYNC_CRYPT)
  6847. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6848. #endif
  6849. if (ret != 0)
  6850. ERROR_OUT(-5408, out);
  6851. if (XMEMCMP(pp, buf, sizeof(pp)))
  6852. ERROR_OUT(-5409, out);
  6853. /* NIST decrypt test vector */
  6854. XMEMSET(buf, 0, sizeof(buf));
  6855. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  6856. #if defined(WOLFSSL_ASYNC_CRYPT)
  6857. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6858. #endif
  6859. if (ret != 0)
  6860. ERROR_OUT(-5410, out);
  6861. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  6862. ERROR_OUT(-5411, out);
  6863. /* fail case with decrypting using wrong key */
  6864. XMEMSET(buf, 0, sizeof(buf));
  6865. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6866. #if defined(WOLFSSL_ASYNC_CRYPT)
  6867. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6868. #endif
  6869. if (ret != 0)
  6870. ERROR_OUT(-5412, out);
  6871. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  6872. ERROR_OUT(-5413, out);
  6873. /* set correct key and retest */
  6874. XMEMSET(buf, 0, sizeof(buf));
  6875. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  6876. HEAP_HINT, devId) != 0)
  6877. ERROR_OUT(-5414, out);
  6878. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  6879. #if defined(WOLFSSL_ASYNC_CRYPT)
  6880. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6881. #endif
  6882. if (ret != 0)
  6883. ERROR_OUT(-5415, out);
  6884. if (XMEMCMP(p2, buf, sizeof(p2)))
  6885. ERROR_OUT(-5416, out);
  6886. out:
  6887. if (aes_inited)
  6888. wc_AesXtsFree(aes);
  6889. #ifdef WOLFSSL_SMALL_STACK
  6890. if (aes)
  6891. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  6892. #endif
  6893. return ret;
  6894. }
  6895. #endif /* WOLFSSL_AES_128 */
  6896. #ifdef WOLFSSL_AES_256
  6897. static int aes_xts_256_test(void)
  6898. {
  6899. #ifdef WOLFSSL_SMALL_STACK
  6900. XtsAes *aes = NULL;
  6901. #else
  6902. XtsAes aes[1];
  6903. #endif
  6904. int aes_inited = 0;
  6905. int ret = 0;
  6906. unsigned char buf[AES_BLOCK_SIZE * 3];
  6907. unsigned char cipher[AES_BLOCK_SIZE * 3];
  6908. /* 256 key tests */
  6909. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  6910. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  6911. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  6912. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  6913. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  6914. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  6915. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  6916. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  6917. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  6918. };
  6919. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  6920. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  6921. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  6922. };
  6923. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  6924. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  6925. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  6926. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  6927. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  6928. };
  6929. /* plain text test of partial block is not from NIST test vector list */
  6930. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  6931. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  6932. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  6933. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  6934. };
  6935. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  6936. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  6937. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  6938. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  6939. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  6940. };
  6941. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  6942. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  6943. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  6944. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  6945. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  6946. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  6947. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  6948. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  6949. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  6950. };
  6951. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  6952. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  6953. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  6954. };
  6955. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  6956. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  6957. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  6958. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  6959. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  6960. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  6961. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  6962. };
  6963. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  6964. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  6965. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  6966. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  6967. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  6968. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  6969. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  6970. };
  6971. #ifdef WOLFSSL_SMALL_STACK
  6972. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6973. ERROR_OUT(-5515, out);
  6974. #endif
  6975. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6976. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  6977. if (ret != 0) {
  6978. printf("EVP_aes_256_xts failed\n");
  6979. goto out;
  6980. }
  6981. #endif
  6982. XMEMSET(buf, 0, sizeof(buf));
  6983. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  6984. HEAP_HINT, devId) != 0)
  6985. ERROR_OUT(-5500, out);
  6986. else
  6987. aes_inited = 1;
  6988. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  6989. #if defined(WOLFSSL_ASYNC_CRYPT)
  6990. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6991. #endif
  6992. if (ret != 0)
  6993. ERROR_OUT(-5501, out);
  6994. if (XMEMCMP(c2, buf, sizeof(c2)))
  6995. ERROR_OUT(-5502, out);
  6996. XMEMSET(buf, 0, sizeof(buf));
  6997. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  6998. HEAP_HINT, devId) != 0)
  6999. ERROR_OUT(-5503, out);
  7000. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7001. #if defined(WOLFSSL_ASYNC_CRYPT)
  7002. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7003. #endif
  7004. if (ret != 0)
  7005. ERROR_OUT(-5504, out);
  7006. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7007. ERROR_OUT(-5505, out);
  7008. /* partial block encryption test */
  7009. XMEMSET(cipher, 0, sizeof(cipher));
  7010. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7011. #if defined(WOLFSSL_ASYNC_CRYPT)
  7012. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7013. #endif
  7014. if (ret != 0)
  7015. ERROR_OUT(-5506, out);
  7016. wc_AesXtsFree(aes);
  7017. /* partial block decrypt test */
  7018. XMEMSET(buf, 0, sizeof(buf));
  7019. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7020. HEAP_HINT, devId) != 0)
  7021. ERROR_OUT(-5507, out);
  7022. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7023. #if defined(WOLFSSL_ASYNC_CRYPT)
  7024. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7025. #endif
  7026. if (ret != 0)
  7027. ERROR_OUT(-5508, out);
  7028. if (XMEMCMP(pp, buf, sizeof(pp)))
  7029. ERROR_OUT(-5509, out);
  7030. /* NIST decrypt test vector */
  7031. XMEMSET(buf, 0, sizeof(buf));
  7032. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7033. #if defined(WOLFSSL_ASYNC_CRYPT)
  7034. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7035. #endif
  7036. if (ret != 0)
  7037. ERROR_OUT(-5510, out);
  7038. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7039. ERROR_OUT(-5511, out);
  7040. XMEMSET(buf, 0, sizeof(buf));
  7041. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7042. HEAP_HINT, devId) != 0)
  7043. ERROR_OUT(-5512, out);
  7044. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7045. #if defined(WOLFSSL_ASYNC_CRYPT)
  7046. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7047. #endif
  7048. if (ret != 0)
  7049. ERROR_OUT(-5513, out);
  7050. if (XMEMCMP(p2, buf, sizeof(p2)))
  7051. ERROR_OUT(-5514, out);
  7052. out:
  7053. if (aes_inited)
  7054. wc_AesXtsFree(aes);
  7055. #ifdef WOLFSSL_SMALL_STACK
  7056. if (aes)
  7057. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7058. #endif
  7059. return ret;
  7060. }
  7061. #endif /* WOLFSSL_AES_256 */
  7062. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7063. /* both 128 and 256 bit key test */
  7064. static int aes_xts_sector_test(void)
  7065. {
  7066. #ifdef WOLFSSL_SMALL_STACK
  7067. XtsAes *aes = NULL;
  7068. #else
  7069. XtsAes aes[1];
  7070. #endif
  7071. int aes_inited = 0;
  7072. int ret = 0;
  7073. unsigned char buf[AES_BLOCK_SIZE * 2];
  7074. /* 128 key tests */
  7075. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7076. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7077. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7078. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7079. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7080. };
  7081. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7082. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7083. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7084. };
  7085. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7086. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7087. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7088. };
  7089. word64 s1 = 141;
  7090. /* 256 key tests */
  7091. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7092. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7093. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7094. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7095. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7096. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7097. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7098. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7099. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7100. };
  7101. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7102. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7103. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7104. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7105. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7106. };
  7107. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7108. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7109. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7110. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7111. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7112. };
  7113. word64 s2 = 187;
  7114. #ifdef WOLFSSL_SMALL_STACK
  7115. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7116. ERROR_OUT(-5612, out);
  7117. #endif
  7118. XMEMSET(buf, 0, sizeof(buf));
  7119. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7120. HEAP_HINT, devId) != 0)
  7121. ERROR_OUT(-5600, out);
  7122. else
  7123. aes_inited = 1;
  7124. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7125. #if defined(WOLFSSL_ASYNC_CRYPT)
  7126. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7127. #endif
  7128. if (ret != 0)
  7129. ERROR_OUT(-5601, out);
  7130. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7131. ERROR_OUT(-5602, out);
  7132. /* decrypt test */
  7133. XMEMSET(buf, 0, sizeof(buf));
  7134. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7135. HEAP_HINT, devId) != 0)
  7136. ERROR_OUT(-5603, out);
  7137. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7138. #if defined(WOLFSSL_ASYNC_CRYPT)
  7139. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7140. #endif
  7141. if (ret != 0)
  7142. ERROR_OUT(-5604, out);
  7143. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7144. ERROR_OUT(-5605, out);
  7145. wc_AesXtsFree(aes);
  7146. /* 256 bit key tests */
  7147. XMEMSET(buf, 0, sizeof(buf));
  7148. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7149. HEAP_HINT, devId) != 0)
  7150. ERROR_OUT(-5606, out);
  7151. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7152. #if defined(WOLFSSL_ASYNC_CRYPT)
  7153. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7154. #endif
  7155. if (ret != 0)
  7156. ERROR_OUT(-5607, out);
  7157. if (XMEMCMP(c2, buf, sizeof(c2)))
  7158. ERROR_OUT(-5608, out);
  7159. /* decrypt test */
  7160. XMEMSET(buf, 0, sizeof(buf));
  7161. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7162. HEAP_HINT, devId) != 0)
  7163. ERROR_OUT(-5609, out);
  7164. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7165. #if defined(WOLFSSL_ASYNC_CRYPT)
  7166. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7167. #endif
  7168. if (ret != 0)
  7169. ERROR_OUT(-5610, out);
  7170. if (XMEMCMP(p2, buf, sizeof(p2)))
  7171. ERROR_OUT(-5611, out);
  7172. out:
  7173. if (aes_inited)
  7174. wc_AesXtsFree(aes);
  7175. #ifdef WOLFSSL_SMALL_STACK
  7176. if (aes)
  7177. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7178. #endif
  7179. return ret;
  7180. }
  7181. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7182. #ifdef WOLFSSL_AES_128
  7183. /* testing of bad arguments */
  7184. static int aes_xts_args_test(void)
  7185. {
  7186. #ifdef WOLFSSL_SMALL_STACK
  7187. XtsAes *aes = NULL;
  7188. #else
  7189. XtsAes aes[1];
  7190. #endif
  7191. int aes_inited = 0;
  7192. int ret;
  7193. unsigned char buf[AES_BLOCK_SIZE * 2];
  7194. /* 128 key tests */
  7195. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7196. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7197. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7198. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7199. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7200. };
  7201. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7202. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7203. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7204. };
  7205. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7206. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7207. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7208. };
  7209. word64 s1 = 141;
  7210. #ifdef WOLFSSL_SMALL_STACK
  7211. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7212. ERROR_OUT(-5708, out);
  7213. #endif
  7214. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7215. HEAP_HINT, devId) == 0)
  7216. ERROR_OUT(-5700, out);
  7217. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7218. HEAP_HINT, devId) == 0)
  7219. ERROR_OUT(-5701, out);
  7220. /* encryption operations */
  7221. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7222. HEAP_HINT, devId) != 0)
  7223. ERROR_OUT(-5702, out);
  7224. else
  7225. aes_inited = 1;
  7226. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7227. #if defined(WOLFSSL_ASYNC_CRYPT)
  7228. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7229. #endif
  7230. if (ret == 0)
  7231. ERROR_OUT(-5703, out);
  7232. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7233. #if defined(WOLFSSL_ASYNC_CRYPT)
  7234. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7235. #endif
  7236. if (ret == 0)
  7237. ERROR_OUT(-5704, out);
  7238. wc_AesXtsFree(aes);
  7239. /* decryption operations */
  7240. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7241. HEAP_HINT, devId) != 0)
  7242. ERROR_OUT(-5705, out);
  7243. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7244. #if defined(WOLFSSL_ASYNC_CRYPT)
  7245. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7246. #endif
  7247. if (ret == 0)
  7248. ERROR_OUT(-5706, out);
  7249. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7250. #if defined(WOLFSSL_ASYNC_CRYPT)
  7251. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7252. #endif
  7253. if (ret == 0)
  7254. ERROR_OUT(-5707, out);
  7255. ret = 0;
  7256. out:
  7257. if (aes_inited)
  7258. wc_AesXtsFree(aes);
  7259. #ifdef WOLFSSL_SMALL_STACK
  7260. if (aes)
  7261. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7262. #endif
  7263. return ret;
  7264. }
  7265. #endif /* WOLFSSL_AES_128 */
  7266. #endif /* WOLFSSL_AES_XTS */
  7267. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7268. static int aes_cbc_test(void)
  7269. {
  7270. byte cipher[AES_BLOCK_SIZE];
  7271. byte plain[AES_BLOCK_SIZE];
  7272. int ret;
  7273. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7274. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7275. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7276. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7277. };
  7278. byte key[] = "0123456789abcdef "; /* align */
  7279. byte iv[] = "1234567890abcdef "; /* align */
  7280. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7281. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7282. /* Parameter Validation testing. */
  7283. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7284. if (ret != BAD_FUNC_ARG)
  7285. return -5800;
  7286. #ifdef HAVE_AES_DECRYPT
  7287. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7288. if (ret != BAD_FUNC_ARG)
  7289. return -5801;
  7290. #endif
  7291. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7292. AES_BLOCK_SIZE, iv);
  7293. if (ret != 0)
  7294. return -5802;
  7295. #ifdef HAVE_AES_DECRYPT
  7296. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7297. AES_BLOCK_SIZE, iv);
  7298. if (ret != 0)
  7299. return -5803;
  7300. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7301. return -5804;
  7302. #endif /* HAVE_AES_DECRYPT */
  7303. (void)plain;
  7304. return 0;
  7305. }
  7306. #endif
  7307. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  7308. {
  7309. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7310. #ifdef WOLFSSL_SMALL_STACK
  7311. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7312. #else
  7313. Aes enc[1];
  7314. #endif
  7315. byte cipher[AES_BLOCK_SIZE * 4];
  7316. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7317. #ifdef WOLFSSL_SMALL_STACK
  7318. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7319. #else
  7320. Aes dec[1];
  7321. #endif
  7322. byte plain [AES_BLOCK_SIZE * 4];
  7323. #endif
  7324. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER */
  7325. int ret = 0;
  7326. #ifdef HAVE_AES_CBC
  7327. #ifdef WOLFSSL_AES_128
  7328. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7329. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7330. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7331. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7332. };
  7333. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7334. {
  7335. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  7336. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  7337. };
  7338. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  7339. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  7340. #ifdef WOLFSSL_SMALL_STACK
  7341. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7342. if (enc == NULL)
  7343. ERROR_OUT(-5948, out);
  7344. #endif
  7345. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  7346. if (dec == NULL)
  7347. ERROR_OUT(-5949, out);
  7348. #endif
  7349. #endif
  7350. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7351. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  7352. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7353. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7354. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  7355. #endif
  7356. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7357. if (ret != 0)
  7358. ERROR_OUT(-5902, out);
  7359. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7360. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  7361. if (ret != 0)
  7362. ERROR_OUT(-5903, out);
  7363. #endif
  7364. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  7365. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  7366. #if defined(WOLFSSL_ASYNC_CRYPT)
  7367. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7368. #endif
  7369. if (ret != 0)
  7370. ERROR_OUT(-5904, out);
  7371. #ifdef HAVE_AES_DECRYPT
  7372. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  7373. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  7374. #if defined(WOLFSSL_ASYNC_CRYPT)
  7375. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7376. #endif
  7377. if (ret != 0)
  7378. ERROR_OUT(-5905, out);
  7379. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  7380. ERROR_OUT(-5906, out);
  7381. #endif /* HAVE_AES_DECRYPT */
  7382. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  7383. ERROR_OUT(-5907, out);
  7384. #endif /* WOLFSSL_AES_128 */
  7385. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  7386. {
  7387. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  7388. /* "All work and no play makes Jack a dull boy. " */
  7389. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7390. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7391. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7392. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7393. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7394. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7395. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7396. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7397. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7398. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7399. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7400. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7401. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7402. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7403. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7404. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7405. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7406. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7407. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7408. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7409. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7410. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7411. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7412. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7413. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7414. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7415. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7416. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7417. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7418. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7419. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7420. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7421. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7422. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7423. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7424. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7425. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  7426. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  7427. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  7428. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  7429. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  7430. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  7431. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  7432. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  7433. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  7434. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  7435. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  7436. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  7437. };
  7438. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  7439. word32 keySz, msgSz;
  7440. #ifdef WOLFSSL_SMALL_STACK
  7441. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7442. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7443. if ((bigCipher == NULL) ||
  7444. (bigPlain == NULL)) {
  7445. if (bigCipher != NULL)
  7446. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7447. ERROR_OUT(-5947, out);
  7448. }
  7449. #else
  7450. byte bigCipher[sizeof(bigMsg)];
  7451. byte bigPlain[sizeof(bigMsg)];
  7452. #endif
  7453. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  7454. * message by AES_BLOCK_SIZE for each size of AES key. */
  7455. for (keySz = 16; keySz <= 32; keySz += 8) {
  7456. for (msgSz = AES_BLOCK_SIZE;
  7457. msgSz <= sizeof(bigMsg);
  7458. msgSz += AES_BLOCK_SIZE) {
  7459. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  7460. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  7461. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  7462. if (ret != 0) {
  7463. ret = -5908;
  7464. break;
  7465. }
  7466. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  7467. if (ret != 0) {
  7468. ret = -5909;
  7469. break;
  7470. }
  7471. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  7472. #if defined(WOLFSSL_ASYNC_CRYPT)
  7473. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7474. #endif
  7475. if (ret != 0) {
  7476. ret = -5910;
  7477. break;
  7478. }
  7479. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  7480. #if defined(WOLFSSL_ASYNC_CRYPT)
  7481. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7482. #endif
  7483. if (ret != 0) {
  7484. ret = -5911;
  7485. break;
  7486. }
  7487. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  7488. ret = -5912;
  7489. break;
  7490. }
  7491. }
  7492. if (ret != 0)
  7493. break;
  7494. }
  7495. #ifdef WOLFSSL_SMALL_STACK
  7496. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7497. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7498. #endif
  7499. if (ret != 0)
  7500. goto out;
  7501. }
  7502. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  7503. /* Test of AES IV state with encrypt/decrypt */
  7504. #ifdef WOLFSSL_AES_128
  7505. {
  7506. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  7507. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  7508. */
  7509. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7510. {
  7511. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  7512. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  7513. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  7514. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  7515. };
  7516. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  7517. {
  7518. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  7519. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  7520. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  7521. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  7522. };
  7523. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  7524. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  7525. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  7526. };
  7527. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7528. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  7529. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  7530. };
  7531. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7532. if (ret != 0)
  7533. ERROR_OUT(-5913, out);
  7534. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  7535. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  7536. #if defined(WOLFSSL_ASYNC_CRYPT)
  7537. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7538. #endif
  7539. if (ret != 0)
  7540. ERROR_OUT(-5914, out);
  7541. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  7542. ERROR_OUT(-5915, out);
  7543. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7544. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7545. #if defined(WOLFSSL_ASYNC_CRYPT)
  7546. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7547. #endif
  7548. if (ret != 0)
  7549. ERROR_OUT(-5916, out);
  7550. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  7551. AES_BLOCK_SIZE))
  7552. ERROR_OUT(-5917, out);
  7553. #if defined(HAVE_AES_DECRYPT)
  7554. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  7555. if (ret != 0)
  7556. ERROR_OUT(-5918, out);
  7557. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  7558. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  7559. #if defined(WOLFSSL_ASYNC_CRYPT)
  7560. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7561. #endif
  7562. if (ret != 0)
  7563. ERROR_OUT(-5919, out);
  7564. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  7565. ERROR_OUT(-5920, out);
  7566. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  7567. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7568. #if defined(WOLFSSL_ASYNC_CRYPT)
  7569. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7570. #endif
  7571. if (ret != 0)
  7572. ERROR_OUT(-5921, out);
  7573. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  7574. AES_BLOCK_SIZE))
  7575. ERROR_OUT(-5922, out);
  7576. #endif /* HAVE_AES_DECRYPT */
  7577. }
  7578. #endif /* WOLFSSL_AES_128 */
  7579. #endif /* HAVE_AES_CBC */
  7580. #ifdef WOLFSSL_AES_COUNTER
  7581. {
  7582. /* test vectors from "Recommendation for Block Cipher Modes of
  7583. * Operation" NIST Special Publication 800-38A */
  7584. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  7585. {
  7586. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  7587. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  7588. };
  7589. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  7590. {
  7591. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7592. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7593. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7594. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7595. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7596. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7597. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7598. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7599. };
  7600. #ifdef WOLFSSL_AES_128
  7601. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  7602. {
  7603. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  7604. 0xc2
  7605. };
  7606. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  7607. {
  7608. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7609. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7610. };
  7611. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  7612. {
  7613. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  7614. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  7615. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  7616. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  7617. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  7618. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  7619. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  7620. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  7621. };
  7622. #endif /* WOLFSSL_AES_128 */
  7623. #ifdef WOLFSSL_AES_192
  7624. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  7625. {
  7626. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7627. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7628. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7629. };
  7630. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  7631. {
  7632. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  7633. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  7634. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  7635. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  7636. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  7637. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  7638. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  7639. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  7640. };
  7641. #endif
  7642. #ifdef WOLFSSL_AES_256
  7643. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  7644. {
  7645. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7646. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7647. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7648. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7649. };
  7650. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  7651. {
  7652. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  7653. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  7654. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  7655. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  7656. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  7657. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  7658. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  7659. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  7660. };
  7661. #endif
  7662. #ifdef WOLFSSL_AES_128
  7663. wc_AesSetKeyDirect(enc, ctr128Key, sizeof(ctr128Key),
  7664. ctrIv, AES_ENCRYPTION);
  7665. /* Ctr only uses encrypt, even on key setup */
  7666. wc_AesSetKeyDirect(dec, ctr128Key, sizeof(ctr128Key),
  7667. ctrIv, AES_ENCRYPTION);
  7668. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(ctrPlain));
  7669. if (ret != 0) {
  7670. ERROR_OUT(-5923, out);
  7671. }
  7672. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(ctrPlain));
  7673. if (ret != 0) {
  7674. ERROR_OUT(-5924, out);
  7675. }
  7676. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7677. ERROR_OUT(-5925, out);
  7678. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  7679. ERROR_OUT(-5926, out);
  7680. /* let's try with just 9 bytes, non block size test */
  7681. wc_AesSetKeyDirect(enc, ctr128Key, AES_BLOCK_SIZE,
  7682. ctrIv, AES_ENCRYPTION);
  7683. /* Ctr only uses encrypt, even on key setup */
  7684. wc_AesSetKeyDirect(dec, ctr128Key, AES_BLOCK_SIZE,
  7685. ctrIv, AES_ENCRYPTION);
  7686. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7687. if (ret != 0) {
  7688. ERROR_OUT(-5927, out);
  7689. }
  7690. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7691. if (ret != 0) {
  7692. ERROR_OUT(-5928, out);
  7693. }
  7694. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7695. ERROR_OUT(-5929, out);
  7696. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  7697. ERROR_OUT(-5930, out);
  7698. /* and an additional 9 bytes to reuse tmp left buffer */
  7699. ret = wc_AesCtrEncrypt(enc, cipher, ctrPlain, sizeof(oddCipher));
  7700. if (ret != 0) {
  7701. ERROR_OUT(-5931, out);
  7702. }
  7703. ret = wc_AesCtrEncrypt(dec, plain, cipher, sizeof(oddCipher));
  7704. if (ret != 0) {
  7705. ERROR_OUT(-5932, out);
  7706. }
  7707. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  7708. ERROR_OUT(-5933, out);
  7709. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  7710. ERROR_OUT(-5934, out);
  7711. #endif /* WOLFSSL_AES_128 */
  7712. #ifdef WOLFSSL_AES_192
  7713. /* 192 bit key */
  7714. wc_AesSetKeyDirect(enc, ctr192Key, sizeof(ctr192Key),
  7715. ctrIv, AES_ENCRYPTION);
  7716. /* Ctr only uses encrypt, even on key setup */
  7717. wc_AesSetKeyDirect(dec, ctr192Key, sizeof(ctr192Key),
  7718. ctrIv, AES_ENCRYPTION);
  7719. XMEMSET(plain, 0, sizeof(plain));
  7720. ret = wc_AesCtrEncrypt(enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  7721. if (ret != 0) {
  7722. ERROR_OUT(-5935, out);
  7723. }
  7724. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  7725. ERROR_OUT(-5936, out);
  7726. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7727. if (ret != 0) {
  7728. ERROR_OUT(-5937, out);
  7729. }
  7730. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  7731. ERROR_OUT(-5938, out);
  7732. #endif /* WOLFSSL_AES_192 */
  7733. #ifdef WOLFSSL_AES_256
  7734. /* 256 bit key */
  7735. wc_AesSetKeyDirect(enc, ctr256Key, sizeof(ctr256Key),
  7736. ctrIv, AES_ENCRYPTION);
  7737. /* Ctr only uses encrypt, even on key setup */
  7738. wc_AesSetKeyDirect(dec, ctr256Key, sizeof(ctr256Key),
  7739. ctrIv, AES_ENCRYPTION);
  7740. XMEMSET(plain, 0, sizeof(plain));
  7741. ret = wc_AesCtrEncrypt(enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  7742. if (ret != 0) {
  7743. ERROR_OUT(-5939, out);
  7744. }
  7745. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  7746. ERROR_OUT(-5940, out);
  7747. ret = wc_AesCtrEncrypt(dec, cipher, ctrPlain, sizeof(ctrPlain));
  7748. if (ret != 0) {
  7749. ERROR_OUT(-5941, out);
  7750. }
  7751. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  7752. ERROR_OUT(-5942, out);
  7753. #endif /* WOLFSSL_AES_256 */
  7754. }
  7755. #endif /* WOLFSSL_AES_COUNTER */
  7756. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  7757. {
  7758. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7759. {
  7760. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7761. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7762. };
  7763. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7764. {
  7765. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7766. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7767. };
  7768. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7769. {
  7770. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7771. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7772. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7773. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7774. };
  7775. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7776. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7777. if (ret != 0)
  7778. ERROR_OUT(-5943, out);
  7779. wc_AesEncryptDirect(enc, cipher, niPlain);
  7780. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  7781. ERROR_OUT(-5944, out);
  7782. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7783. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  7784. if (ret != 0)
  7785. ERROR_OUT(-5945, out);
  7786. wc_AesDecryptDirect(dec, plain, niCipher);
  7787. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  7788. ERROR_OUT(-5946, out);
  7789. }
  7790. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  7791. ret = aes_key_size_test();
  7792. if (ret != 0)
  7793. goto out;
  7794. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7795. ret = aes_cbc_test();
  7796. if (ret != 0)
  7797. goto out;
  7798. #endif
  7799. #if defined(WOLFSSL_AES_XTS)
  7800. #ifdef WOLFSSL_AES_128
  7801. ret = aes_xts_128_test();
  7802. if (ret != 0)
  7803. goto out;
  7804. #endif
  7805. #ifdef WOLFSSL_AES_256
  7806. ret = aes_xts_256_test();
  7807. if (ret != 0)
  7808. goto out;
  7809. #endif
  7810. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7811. ret = aes_xts_sector_test();
  7812. if (ret != 0)
  7813. goto out;
  7814. #endif
  7815. #ifdef WOLFSSL_AES_128
  7816. ret = aes_xts_args_test();
  7817. if (ret != 0)
  7818. goto out;
  7819. #endif
  7820. #endif
  7821. #if defined(WOLFSSL_AES_CFB)
  7822. ret = aescfb_test();
  7823. if (ret != 0)
  7824. goto out;
  7825. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7826. ret = aescfb1_test();
  7827. if (ret != 0)
  7828. goto out;
  7829. ret = aescfb8_test();
  7830. if (ret != 0)
  7831. goto out;
  7832. #endif
  7833. #endif
  7834. out:
  7835. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  7836. #ifdef WOLFSSL_SMALL_STACK
  7837. if (enc) {
  7838. if (ret != -5900) /* note this must match ERRROR_OUT() code
  7839. * for wc_AesInit(enc, ...) failure above.
  7840. */
  7841. wc_AesFree(enc);
  7842. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7843. }
  7844. #else
  7845. if (ret != -5900)
  7846. wc_AesFree(enc);
  7847. #endif
  7848. (void)cipher;
  7849. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  7850. #ifdef WOLFSSL_SMALL_STACK
  7851. if (dec) {
  7852. if ((ret != -5900) && (ret != -5901))
  7853. /* note these codes must match the ERRROR_OUT() codes for
  7854. * wc_AesInit() failures above.
  7855. */
  7856. wc_AesFree(dec);
  7857. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7858. }
  7859. #else
  7860. if ((ret != -5900) && (ret != -5901))
  7861. wc_AesFree(dec);
  7862. #endif
  7863. (void)plain;
  7864. #endif
  7865. #endif
  7866. return ret;
  7867. }
  7868. #ifdef WOLFSSL_AES_192
  7869. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  7870. {
  7871. #ifdef HAVE_AES_CBC
  7872. #ifdef WOLFSSL_SMALL_STACK
  7873. Aes *enc = NULL;
  7874. #else
  7875. Aes enc[1];
  7876. #endif
  7877. byte cipher[AES_BLOCK_SIZE];
  7878. #ifdef HAVE_AES_DECRYPT
  7879. #ifdef WOLFSSL_SMALL_STACK
  7880. Aes *dec = NULL;
  7881. #else
  7882. Aes dec[1];
  7883. #endif
  7884. byte plain[AES_BLOCK_SIZE];
  7885. #endif
  7886. #endif /* HAVE_AES_CBC */
  7887. int ret = 0;
  7888. #ifdef HAVE_AES_CBC
  7889. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  7890. * Appendix F.2.3 */
  7891. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  7892. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7893. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7894. };
  7895. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7896. {
  7897. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  7898. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  7899. };
  7900. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  7901. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7902. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7903. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7904. };
  7905. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  7906. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7907. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  7908. };
  7909. #ifdef WOLFSSL_SMALL_STACK
  7910. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7911. ERROR_OUT(-6008, out);
  7912. #ifdef HAVE_AES_DECRYPT
  7913. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7914. ERROR_OUT(-6009, out);
  7915. #endif
  7916. #endif
  7917. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7918. ERROR_OUT(-6000, out);
  7919. #ifdef HAVE_AES_DECRYPT
  7920. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7921. ERROR_OUT(-6001, out);
  7922. #endif
  7923. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  7924. if (ret != 0)
  7925. ERROR_OUT(-6002, out);
  7926. #ifdef HAVE_AES_DECRYPT
  7927. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  7928. if (ret != 0)
  7929. ERROR_OUT(-6003, out);
  7930. #endif
  7931. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7932. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  7933. #if defined(WOLFSSL_ASYNC_CRYPT)
  7934. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  7935. #endif
  7936. if (ret != 0)
  7937. ERROR_OUT(-6004, out);
  7938. #ifdef HAVE_AES_DECRYPT
  7939. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7940. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  7941. #if defined(WOLFSSL_ASYNC_CRYPT)
  7942. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  7943. #endif
  7944. if (ret != 0)
  7945. ERROR_OUT(-6005, out);
  7946. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  7947. ERROR_OUT(-6006, out);
  7948. }
  7949. #endif
  7950. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  7951. ERROR_OUT(-6007, out);
  7952. wc_AesFree(enc);
  7953. #ifdef HAVE_AES_DECRYPT
  7954. wc_AesFree(dec);
  7955. #endif
  7956. out:
  7957. #ifdef WOLFSSL_SMALL_STACK
  7958. if (enc)
  7959. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7960. #ifdef HAVE_AES_DECRYPT
  7961. if (dec)
  7962. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7963. #endif
  7964. #endif
  7965. #endif /* HAVE_AES_CBC */
  7966. return ret;
  7967. }
  7968. #endif /* WOLFSSL_AES_192 */
  7969. #ifdef WOLFSSL_AES_256
  7970. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  7971. {
  7972. #ifdef HAVE_AES_CBC
  7973. #ifdef WOLFSSL_SMALL_STACK
  7974. Aes *enc = NULL;
  7975. #else
  7976. Aes enc[1];
  7977. #endif
  7978. byte cipher[AES_BLOCK_SIZE];
  7979. #ifdef HAVE_AES_DECRYPT
  7980. #ifdef WOLFSSL_SMALL_STACK
  7981. Aes *dec = NULL;
  7982. #else
  7983. Aes dec[1];
  7984. #endif
  7985. byte plain[AES_BLOCK_SIZE];
  7986. #endif
  7987. #endif /* HAVE_AES_CBC */
  7988. int ret = 0;
  7989. #ifdef HAVE_AES_CBC
  7990. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  7991. * Appendix F.2.5 */
  7992. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  7993. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7994. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7995. };
  7996. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  7997. {
  7998. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  7999. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  8000. };
  8001. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  8002. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8003. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8004. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8005. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8006. };
  8007. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  8008. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8009. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  8010. };
  8011. #ifdef WOLFSSL_SMALL_STACK
  8012. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8013. ERROR_OUT(-6108, out);
  8014. #ifdef HAVE_AES_DECRYPT
  8015. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8016. ERROR_OUT(-6109, out);
  8017. #endif
  8018. #endif
  8019. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8020. ERROR_OUT(-6100, out);
  8021. #ifdef HAVE_AES_DECRYPT
  8022. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8023. ERROR_OUT(-6101, out);
  8024. #endif
  8025. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  8026. if (ret != 0)
  8027. ERROR_OUT(-6102, out);
  8028. #ifdef HAVE_AES_DECRYPT
  8029. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  8030. if (ret != 0)
  8031. ERROR_OUT(-6103, out);
  8032. #endif
  8033. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8034. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  8035. #if defined(WOLFSSL_ASYNC_CRYPT)
  8036. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8037. #endif
  8038. if (ret != 0)
  8039. ERROR_OUT(-6104, out);
  8040. #ifdef HAVE_AES_DECRYPT
  8041. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8042. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  8043. #if defined(WOLFSSL_ASYNC_CRYPT)
  8044. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8045. #endif
  8046. if (ret != 0)
  8047. ERROR_OUT(-6105, out);
  8048. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  8049. ERROR_OUT(-6106, out);
  8050. }
  8051. #endif
  8052. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  8053. ERROR_OUT(-6107, out);
  8054. wc_AesFree(enc);
  8055. #ifdef HAVE_AES_DECRYPT
  8056. wc_AesFree(dec);
  8057. #endif
  8058. out:
  8059. #ifdef WOLFSSL_SMALL_STACK
  8060. if (enc)
  8061. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8062. #ifdef HAVE_AES_DECRYPT
  8063. if (dec)
  8064. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8065. #endif
  8066. #endif
  8067. #endif /* HAVE_AES_CBC */
  8068. return ret;
  8069. }
  8070. #endif /* WOLFSSL_AES_256 */
  8071. #ifdef HAVE_AESGCM
  8072. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  8073. byte* plain, int plainSz, byte* cipher, int cipherSz,
  8074. byte* aad, int aadSz, byte* tag, int tagSz)
  8075. {
  8076. int ret, enc_inited = 0, dec_inited = 0;
  8077. #ifdef WOLFSSL_SMALL_STACK
  8078. Aes *enc = NULL;
  8079. Aes *dec = NULL;
  8080. #else
  8081. Aes enc[1];
  8082. Aes dec[1];
  8083. #endif
  8084. byte resultT[AES_BLOCK_SIZE];
  8085. byte resultP[AES_BLOCK_SIZE * 3];
  8086. byte resultC[AES_BLOCK_SIZE * 3];
  8087. int result;
  8088. #ifdef WOLFSSL_SMALL_STACK
  8089. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8090. ERROR_OUT(-6118, out);
  8091. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8092. ERROR_OUT(-6119, out);
  8093. #endif
  8094. XMEMSET(resultT, 0, sizeof(resultT));
  8095. XMEMSET(resultC, 0, sizeof(resultC));
  8096. XMEMSET(resultP, 0, sizeof(resultP));
  8097. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8098. ERROR_OUT(-6110, out);
  8099. else
  8100. enc_inited = 1;
  8101. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8102. ERROR_OUT(-6111, out);
  8103. else
  8104. dec_inited = 1;
  8105. result = wc_AesGcmSetKey(enc, key, keySz);
  8106. if (result != 0)
  8107. ERROR_OUT(-6112, out);
  8108. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8109. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  8110. resultT, tagSz, aad, aadSz);
  8111. #if defined(WOLFSSL_ASYNC_CRYPT)
  8112. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8113. #endif
  8114. if (result != 0)
  8115. ERROR_OUT(-6113, out);
  8116. if (cipher != NULL) {
  8117. if (XMEMCMP(cipher, resultC, cipherSz))
  8118. ERROR_OUT(-6114, out);
  8119. }
  8120. if (XMEMCMP(tag, resultT, tagSz))
  8121. ERROR_OUT(-6115, out);
  8122. #ifdef HAVE_AES_DECRYPT
  8123. result = wc_AesGcmSetKey(dec, key, keySz);
  8124. if (result != 0)
  8125. ERROR_OUT(-6116, out);
  8126. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  8127. iv, ivSz, resultT, tagSz, aad, aadSz);
  8128. #if defined(WOLFSSL_ASYNC_CRYPT)
  8129. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8130. #endif
  8131. if (result != 0)
  8132. ERROR_OUT(-6117, out);
  8133. if (plain != NULL) {
  8134. if (XMEMCMP(plain, resultP, plainSz))
  8135. ERROR_OUT(-6118, out);
  8136. }
  8137. #endif /* HAVE_AES_DECRYPT */
  8138. ret = 0;
  8139. out:
  8140. if (enc_inited)
  8141. wc_AesFree(enc);
  8142. if (dec_inited)
  8143. wc_AesFree(dec);
  8144. #ifdef WOLFSSL_SMALL_STACK
  8145. if (enc)
  8146. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8147. if (dec)
  8148. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8149. #endif
  8150. return ret;
  8151. }
  8152. /* tests that only use 12 byte IV and 16 or less byte AAD
  8153. * test vectors are from NIST SP 800-38D
  8154. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  8155. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  8156. {
  8157. byte key1[] = {
  8158. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  8159. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  8160. };
  8161. byte iv1[] = {
  8162. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  8163. 0xe4, 0xed, 0x2f, 0x6d
  8164. };
  8165. ALIGN64 byte plain1[] = {
  8166. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  8167. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  8168. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  8169. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  8170. };
  8171. byte aad1[] = {
  8172. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  8173. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  8174. };
  8175. ALIGN64 byte cipher1[] = {
  8176. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  8177. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  8178. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  8179. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  8180. };
  8181. byte tag1[] = {
  8182. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  8183. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  8184. };
  8185. byte key2[] = {
  8186. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  8187. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  8188. };
  8189. byte iv2[] = {
  8190. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  8191. 0xc9, 0x8a, 0xff, 0xe3
  8192. };
  8193. ALIGN64 byte plain2[] = {
  8194. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  8195. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  8196. };
  8197. ALIGN64 byte cipher2[] = {
  8198. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  8199. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  8200. };
  8201. byte tag2[] = {
  8202. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  8203. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  8204. };
  8205. byte key3[] = {
  8206. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  8207. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  8208. };
  8209. byte iv3[] = {
  8210. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  8211. 0x6a, 0x4b, 0xd5, 0xe1
  8212. };
  8213. byte tag3[] = {
  8214. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  8215. 0x11, 0x64, 0xb2, 0xff
  8216. };
  8217. int ret;
  8218. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  8219. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  8220. aad1, sizeof(aad1), tag1, sizeof(tag1));
  8221. if (ret != 0) {
  8222. return ret;
  8223. }
  8224. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  8225. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  8226. NULL, 0, tag2, sizeof(tag2));
  8227. if (ret != 0) {
  8228. return ret;
  8229. }
  8230. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  8231. NULL, 0, NULL, 0,
  8232. NULL, 0, tag3, sizeof(tag3));
  8233. if (ret != 0) {
  8234. return ret;
  8235. }
  8236. return 0;
  8237. }
  8238. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  8239. {
  8240. #ifdef WOLFSSL_SMALL_STACK
  8241. Aes *enc = NULL;
  8242. Aes *dec = NULL;
  8243. #else
  8244. Aes enc[1];
  8245. Aes dec[1];
  8246. #endif
  8247. /*
  8248. * This is Test Case 16 from the document Galois/
  8249. * Counter Mode of Operation (GCM) by McGrew and
  8250. * Viega.
  8251. */
  8252. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8253. {
  8254. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  8255. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  8256. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  8257. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  8258. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  8259. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  8260. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  8261. 0xba, 0x63, 0x7b, 0x39
  8262. };
  8263. #if defined(WOLFSSL_AES_256)
  8264. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8265. {
  8266. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8267. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  8268. 0xab, 0xad, 0xda, 0xd2
  8269. };
  8270. #endif
  8271. #ifdef WOLFSSL_AES_256
  8272. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8273. {
  8274. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8275. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8276. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8277. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  8278. };
  8279. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8280. {
  8281. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  8282. 0xde, 0xca, 0xf8, 0x88
  8283. };
  8284. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  8285. {
  8286. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  8287. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  8288. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  8289. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  8290. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  8291. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  8292. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  8293. 0xbc, 0xc9, 0xf6, 0x62
  8294. };
  8295. #endif /* WOLFSSL_AES_256 */
  8296. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8297. {
  8298. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  8299. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  8300. };
  8301. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  8302. #if !defined(HAVE_FIPS) && \
  8303. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  8304. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  8305. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8306. !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  8307. !(defined(WOLF_CRYPTO_CB) && \
  8308. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  8309. #define ENABLE_NON_12BYTE_IV_TEST
  8310. #ifdef WOLFSSL_AES_192
  8311. /* Test Case 12, uses same plaintext and AAD data. */
  8312. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8313. {
  8314. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  8315. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  8316. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  8317. };
  8318. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8319. {
  8320. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  8321. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  8322. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  8323. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  8324. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  8325. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  8326. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  8327. 0xa6, 0x37, 0xb3, 0x9b
  8328. };
  8329. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  8330. {
  8331. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  8332. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  8333. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  8334. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  8335. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  8336. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  8337. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  8338. 0xe9, 0xb7, 0x37, 0x3b
  8339. };
  8340. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8341. {
  8342. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  8343. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  8344. };
  8345. #endif /* WOLFSSL_AES_192 */
  8346. #ifdef WOLFSSL_AES_128
  8347. /* The following is an interesting test case from the example
  8348. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  8349. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  8350. {
  8351. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  8352. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  8353. };
  8354. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  8355. {
  8356. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  8357. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  8358. };
  8359. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  8360. {
  8361. 0xca
  8362. };
  8363. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  8364. {
  8365. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  8366. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  8367. };
  8368. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  8369. {
  8370. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  8371. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  8372. };
  8373. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  8374. {
  8375. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  8376. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  8377. };
  8378. #endif /* WOLFSSL_AES_128 */
  8379. #ifdef WOLFSSL_AES_256
  8380. int ivlen;
  8381. #endif
  8382. #endif
  8383. byte resultT[sizeof(t1)];
  8384. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  8385. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  8386. int result = 0;
  8387. int ret;
  8388. #ifdef WOLFSSL_AES_256
  8389. int alen;
  8390. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8391. int plen;
  8392. #endif
  8393. #endif
  8394. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8395. #if !defined(BENCH_AESGCM_LARGE)
  8396. #define BENCH_AESGCM_LARGE 1024
  8397. #endif
  8398. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8399. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8400. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8401. if ((! large_input) || (! large_output) || (! large_outdec))
  8402. ERROR_OUT(MEMORY_E, out);
  8403. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  8404. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  8405. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  8406. #endif
  8407. #ifdef WOLFSSL_SMALL_STACK
  8408. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8409. ERROR_OUT(-6342, out);
  8410. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8411. ERROR_OUT(-6343, out);
  8412. #endif
  8413. (void)result;
  8414. XMEMSET(resultT, 0, sizeof(resultT));
  8415. XMEMSET(resultC, 0, sizeof(resultC));
  8416. XMEMSET(resultP, 0, sizeof(resultP));
  8417. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  8418. ERROR_OUT(-6300, out);
  8419. }
  8420. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  8421. ERROR_OUT(-6301, out);
  8422. }
  8423. #ifdef WOLFSSL_AES_256
  8424. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8425. if (result != 0)
  8426. ERROR_OUT(-6302, out);
  8427. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8428. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8429. resultT, sizeof(resultT), a, sizeof(a));
  8430. #if defined(WOLFSSL_ASYNC_CRYPT)
  8431. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8432. #endif
  8433. if (result != 0)
  8434. ERROR_OUT(-6303, out);
  8435. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8436. ERROR_OUT(-6304, out);
  8437. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  8438. ERROR_OUT(-6305, out);
  8439. #ifdef HAVE_AES_DECRYPT
  8440. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8441. if (result != 0)
  8442. ERROR_OUT(-6306, out);
  8443. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  8444. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8445. #if defined(WOLFSSL_ASYNC_CRYPT)
  8446. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8447. #endif
  8448. if (result != 0)
  8449. ERROR_OUT(-6307, out);
  8450. if (XMEMCMP(p, resultP, sizeof(p)))
  8451. ERROR_OUT(-6308, out);
  8452. #endif /* HAVE_AES_DECRYPT */
  8453. /* Large buffer test */
  8454. #ifdef BENCH_AESGCM_LARGE
  8455. /* setup test buffer */
  8456. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  8457. large_input[alen] = (byte)alen;
  8458. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8459. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8460. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  8461. resultT, sizeof(resultT), a, sizeof(a));
  8462. #if defined(WOLFSSL_ASYNC_CRYPT)
  8463. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8464. #endif
  8465. if (result != 0)
  8466. ERROR_OUT(-6309, out);
  8467. #ifdef HAVE_AES_DECRYPT
  8468. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8469. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  8470. sizeof(resultT), a, sizeof(a));
  8471. #if defined(WOLFSSL_ASYNC_CRYPT)
  8472. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8473. #endif
  8474. if (result != 0)
  8475. ERROR_OUT(-6310, out);
  8476. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  8477. ERROR_OUT(-6311, out);
  8478. #endif /* HAVE_AES_DECRYPT */
  8479. #endif /* BENCH_AESGCM_LARGE */
  8480. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  8481. /* Variable IV length test */
  8482. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  8483. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8484. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  8485. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8486. #if defined(WOLFSSL_ASYNC_CRYPT)
  8487. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8488. #endif
  8489. if (result != 0)
  8490. ERROR_OUT(-6312, out);
  8491. #ifdef HAVE_AES_DECRYPT
  8492. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  8493. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  8494. #if defined(WOLFSSL_ASYNC_CRYPT)
  8495. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8496. #endif
  8497. if (result != 0)
  8498. ERROR_OUT(-6313, out);
  8499. #endif /* HAVE_AES_DECRYPT */
  8500. }
  8501. #endif
  8502. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  8503. /* Variable authenticated data length test */
  8504. for (alen=0; alen<(int)sizeof(p); alen++) {
  8505. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8506. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  8507. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8508. #if defined(WOLFSSL_ASYNC_CRYPT)
  8509. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8510. #endif
  8511. if (result != 0)
  8512. ERROR_OUT(-6314, out);
  8513. #ifdef HAVE_AES_DECRYPT
  8514. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  8515. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  8516. #if defined(WOLFSSL_ASYNC_CRYPT)
  8517. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8518. #endif
  8519. if (result != 0)
  8520. ERROR_OUT(-6315, out);
  8521. #endif /* HAVE_AES_DECRYPT */
  8522. }
  8523. #endif
  8524. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  8525. #ifdef BENCH_AESGCM_LARGE
  8526. /* Variable plain text length test */
  8527. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  8528. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8529. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  8530. plen, iv1, sizeof(iv1), resultT,
  8531. sizeof(resultT), a, sizeof(a));
  8532. #if defined(WOLFSSL_ASYNC_CRYPT)
  8533. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8534. #endif
  8535. if (result != 0)
  8536. ERROR_OUT(-6316, out);
  8537. #ifdef HAVE_AES_DECRYPT
  8538. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  8539. plen, iv1, sizeof(iv1), resultT,
  8540. sizeof(resultT), a, sizeof(a));
  8541. #if defined(WOLFSSL_ASYNC_CRYPT)
  8542. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8543. #endif
  8544. if (result != 0)
  8545. ERROR_OUT(-6317, out);
  8546. #endif /* HAVE_AES_DECRYPT */
  8547. }
  8548. #else /* BENCH_AESGCM_LARGE */
  8549. /* Variable plain text length test */
  8550. for (plen=1; plen<(int)sizeof(p); plen++) {
  8551. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8552. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  8553. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8554. #if defined(WOLFSSL_ASYNC_CRYPT)
  8555. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8556. #endif
  8557. if (result != 0)
  8558. ERROR_OUT(-6318, out);
  8559. #ifdef HAVE_AES_DECRYPT
  8560. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  8561. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  8562. #if defined(WOLFSSL_ASYNC_CRYPT)
  8563. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8564. #endif
  8565. if (result != 0)
  8566. ERROR_OUT(-6319, out);
  8567. #endif /* HAVE_AES_DECRYPT */
  8568. }
  8569. #endif /* BENCH_AESGCM_LARGE */
  8570. #endif
  8571. #endif /* WOLFSSL_AES_256 */
  8572. /* test with IV != 12 bytes */
  8573. #ifdef ENABLE_NON_12BYTE_IV_TEST
  8574. XMEMSET(resultT, 0, sizeof(resultT));
  8575. XMEMSET(resultC, 0, sizeof(resultC));
  8576. XMEMSET(resultP, 0, sizeof(resultP));
  8577. #ifdef WOLFSSL_AES_192
  8578. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  8579. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8580. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  8581. resultT, sizeof(resultT), a, sizeof(a));
  8582. #if defined(WOLFSSL_ASYNC_CRYPT)
  8583. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8584. #endif
  8585. if (result != 0)
  8586. ERROR_OUT(-6320, out);
  8587. if (XMEMCMP(c2, resultC, sizeof(c2)))
  8588. ERROR_OUT(-6321, out);
  8589. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  8590. ERROR_OUT(-6322, out);
  8591. #ifdef HAVE_AES_DECRYPT
  8592. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  8593. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  8594. #if defined(WOLFSSL_ASYNC_CRYPT)
  8595. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8596. #endif
  8597. if (result != 0)
  8598. ERROR_OUT(-6323, out);
  8599. if (XMEMCMP(p, resultP, sizeof(p)))
  8600. ERROR_OUT(-6324, out);
  8601. #endif /* HAVE_AES_DECRYPT */
  8602. XMEMSET(resultT, 0, sizeof(resultT));
  8603. XMEMSET(resultC, 0, sizeof(resultC));
  8604. XMEMSET(resultP, 0, sizeof(resultP));
  8605. #endif /* WOLFSSL_AES_192 */
  8606. #ifdef WOLFSSL_AES_128
  8607. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  8608. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8609. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  8610. resultT, sizeof(t3), a3, sizeof(a3));
  8611. #if defined(WOLFSSL_ASYNC_CRYPT)
  8612. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8613. #endif
  8614. if (result != 0)
  8615. ERROR_OUT(-6325, out);
  8616. if (XMEMCMP(c3, resultC, sizeof(c3)))
  8617. ERROR_OUT(-6326, out);
  8618. if (XMEMCMP(t3, resultT, sizeof(t3)))
  8619. ERROR_OUT(-6327, out);
  8620. #ifdef HAVE_AES_DECRYPT
  8621. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  8622. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  8623. #if defined(WOLFSSL_ASYNC_CRYPT)
  8624. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8625. #endif
  8626. if (result != 0)
  8627. ERROR_OUT(-6328, out);
  8628. if (XMEMCMP(p3, resultP, sizeof(p3)))
  8629. ERROR_OUT(-6329, out);
  8630. #endif /* HAVE_AES_DECRYPT */
  8631. #endif /* WOLFSSL_AES_128 */
  8632. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  8633. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  8634. !defined(WOLFSSL_XILINX_CRYPT) && \
  8635. !(defined(WOLF_CRYPTO_CB) && \
  8636. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  8637. XMEMSET(resultT, 0, sizeof(resultT));
  8638. XMEMSET(resultC, 0, sizeof(resultC));
  8639. XMEMSET(resultP, 0, sizeof(resultP));
  8640. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8641. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  8642. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  8643. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8644. #if defined(WOLFSSL_ASYNC_CRYPT)
  8645. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8646. #endif
  8647. if (result != 0)
  8648. ERROR_OUT(-6330, out);
  8649. if (XMEMCMP(c1, resultC, sizeof(c1)))
  8650. ERROR_OUT(-6331, out);
  8651. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  8652. ERROR_OUT(-6332, out);
  8653. #ifdef HAVE_AES_DECRYPT
  8654. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  8655. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  8656. #if defined(WOLFSSL_ASYNC_CRYPT)
  8657. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8658. #endif
  8659. if (result != 0)
  8660. ERROR_OUT(-6333, out);
  8661. if (XMEMCMP(p, resultP, sizeof(p)))
  8662. ERROR_OUT(-6334, out);
  8663. #endif /* HAVE_AES_DECRYPT */
  8664. #endif /* WOLFSSL_AES_256 */
  8665. #if !defined(HAVE_FIPS) || \
  8666. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  8667. /* Test encrypt with internally generated IV */
  8668. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  8669. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  8670. {
  8671. WC_RNG rng;
  8672. byte randIV[12];
  8673. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  8674. if (result != 0)
  8675. ERROR_OUT(-6335, out);
  8676. XMEMSET(randIV, 0, sizeof(randIV));
  8677. XMEMSET(resultT, 0, sizeof(resultT));
  8678. XMEMSET(resultC, 0, sizeof(resultC));
  8679. XMEMSET(resultP, 0, sizeof(resultP));
  8680. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  8681. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  8682. if (result != 0)
  8683. ERROR_OUT(-6336, out);
  8684. result = wc_AesGcmEncrypt_ex(enc,
  8685. resultC, p, sizeof(p),
  8686. randIV, sizeof(randIV),
  8687. resultT, sizeof(resultT),
  8688. a, sizeof(a));
  8689. #if defined(WOLFSSL_ASYNC_CRYPT)
  8690. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8691. #endif
  8692. if (result != 0)
  8693. ERROR_OUT(-6337, out);
  8694. /* Check the IV has been set. */
  8695. {
  8696. word32 i, ivSum = 0;
  8697. for (i = 0; i < sizeof(randIV); i++)
  8698. ivSum += randIV[i];
  8699. if (ivSum == 0)
  8700. ERROR_OUT(-6338, out);
  8701. }
  8702. #ifdef HAVE_AES_DECRYPT
  8703. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  8704. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  8705. if (result != 0)
  8706. ERROR_OUT(-6339, out);
  8707. result = wc_AesGcmDecrypt(dec,
  8708. resultP, resultC, sizeof(c1),
  8709. randIV, sizeof(randIV),
  8710. resultT, sizeof(resultT),
  8711. a, sizeof(a));
  8712. #if defined(WOLFSSL_ASYNC_CRYPT)
  8713. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8714. #endif
  8715. if (result != 0)
  8716. ERROR_OUT(-6340, out);
  8717. if (XMEMCMP(p, resultP, sizeof(p)))
  8718. ERROR_OUT(-6341, out);
  8719. #endif /* HAVE_AES_DECRYPT */
  8720. wc_FreeRng(&rng);
  8721. }
  8722. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  8723. #endif /* HAVE_FIPS_VERSION >= 2 */
  8724. wc_AesFree(enc);
  8725. wc_AesFree(dec);
  8726. ret = 0;
  8727. out:
  8728. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  8729. if (large_input)
  8730. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8731. if (large_output)
  8732. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8733. if (large_outdec)
  8734. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8735. #endif
  8736. #ifdef WOLFSSL_SMALL_STACK
  8737. if (enc)
  8738. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8739. if (dec)
  8740. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8741. #endif
  8742. return ret;
  8743. }
  8744. #ifdef WOLFSSL_AES_128
  8745. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  8746. {
  8747. int ret;
  8748. #ifdef WOLFSSL_SMALL_STACK
  8749. Gmac *gmac;
  8750. #else
  8751. Gmac gmac[1];
  8752. #endif
  8753. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  8754. {
  8755. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  8756. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  8757. };
  8758. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  8759. {
  8760. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  8761. 0xe2, 0x8c, 0x8f, 0x16
  8762. };
  8763. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  8764. {
  8765. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  8766. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  8767. };
  8768. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  8769. {
  8770. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  8771. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8772. };
  8773. #if (!defined(HAVE_FIPS) || \
  8774. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  8775. /* FIPS builds only allow 16-byte auth tags. */
  8776. /* This sample uses a 15-byte auth tag. */
  8777. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  8778. {
  8779. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  8780. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  8781. };
  8782. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  8783. {
  8784. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  8785. 0x1a, 0x60, 0x24, 0xa7
  8786. };
  8787. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  8788. {
  8789. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  8790. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  8791. };
  8792. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  8793. {
  8794. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  8795. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  8796. };
  8797. #endif
  8798. byte tag[16];
  8799. #ifdef WOLFSSL_SMALL_STACK
  8800. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8801. return -6409;
  8802. #endif
  8803. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  8804. (void)wc_AesInit((Aes*)gmac, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  8805. XMEMSET(tag, 0, sizeof(tag));
  8806. wc_GmacSetKey(gmac, k1, sizeof(k1));
  8807. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  8808. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  8809. ERROR_OUT(-6400, out);
  8810. #if (!defined(HAVE_FIPS) || \
  8811. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  8812. XMEMSET(tag, 0, sizeof(tag));
  8813. wc_GmacSetKey(gmac, k2, sizeof(k2));
  8814. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  8815. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  8816. ERROR_OUT(-6401, out);
  8817. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  8818. {
  8819. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  8820. {
  8821. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  8822. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  8823. };
  8824. WC_RNG rng;
  8825. byte iv[12];
  8826. #ifndef HAVE_FIPS
  8827. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  8828. ERROR_OUT(-6402, out);
  8829. #else
  8830. if (wc_InitRng(&rng) != 0)
  8831. ERROR_OUT(-6403, out);
  8832. #endif
  8833. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  8834. t1, sizeof(t1)) != 0)
  8835. ERROR_OUT(-6404, out);
  8836. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  8837. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  8838. ERROR_OUT(-6405, out);
  8839. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  8840. t2, sizeof(t2)) != 0)
  8841. ERROR_OUT(-6406, out);
  8842. XMEMSET(tag, 0, sizeof(tag));
  8843. XMEMSET(iv, 0, sizeof(iv));
  8844. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  8845. tag, sizeof(tag), &rng) != 0)
  8846. ERROR_OUT(-6407, out);
  8847. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  8848. tag, sizeof(tag)) != 0)
  8849. ERROR_OUT(-6408, out);
  8850. wc_FreeRng(&rng);
  8851. }
  8852. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  8853. #endif /* HAVE_FIPS */
  8854. ret = 0;
  8855. out:
  8856. #ifdef WOLFSSL_SMALL_STACK
  8857. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  8858. #endif
  8859. return ret;
  8860. }
  8861. #endif /* WOLFSSL_AES_128 */
  8862. #endif /* HAVE_AESGCM */
  8863. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  8864. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  8865. {
  8866. int ret;
  8867. #ifdef WOLFSSL_SMALL_STACK
  8868. Aes *enc;
  8869. #else
  8870. Aes enc[1];
  8871. #endif
  8872. /* key */
  8873. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  8874. {
  8875. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  8876. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  8877. };
  8878. /* nonce */
  8879. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  8880. {
  8881. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  8882. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  8883. };
  8884. /* plaintext */
  8885. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  8886. {
  8887. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  8888. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8889. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  8890. };
  8891. /* plaintext - long */
  8892. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  8893. {
  8894. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  8895. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  8896. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  8897. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  8898. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  8899. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  8900. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  8901. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  8902. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  8903. 0x50
  8904. };
  8905. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  8906. {
  8907. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  8908. };
  8909. /* ciphertext */
  8910. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  8911. {
  8912. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  8913. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  8914. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  8915. };
  8916. /* tag - authentication */
  8917. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  8918. {
  8919. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  8920. };
  8921. /* ciphertext - long */
  8922. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  8923. {
  8924. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  8925. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  8926. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  8927. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  8928. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  8929. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  8930. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  8931. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  8932. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  8933. 0x0b
  8934. };
  8935. /* tag - authentication - long */
  8936. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  8937. {
  8938. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  8939. };
  8940. /* tag - authentication - empty plaintext */
  8941. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  8942. {
  8943. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  8944. };
  8945. byte t2[sizeof(t)];
  8946. byte p2[sizeof(p)];
  8947. byte c2[sizeof(c)];
  8948. byte iv2[sizeof(iv)];
  8949. byte pl2[sizeof(pl)];
  8950. byte cl2[sizeof(cl)];
  8951. byte tl2[sizeof(tl)];
  8952. byte t_empty2[sizeof(t_empty)];
  8953. int result;
  8954. #ifdef WOLFSSL_SMALL_STACK
  8955. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8956. return -6521;
  8957. #endif
  8958. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  8959. XMEMSET(t2, 0, sizeof(t2));
  8960. XMEMSET(c2, 0, sizeof(c2));
  8961. XMEMSET(p2, 0, sizeof(p2));
  8962. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  8963. if (result != 0)
  8964. ERROR_OUT(-6500, out);
  8965. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  8966. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  8967. t2, sizeof(t2), a, sizeof(a));
  8968. if (result != 0)
  8969. ERROR_OUT(-6501, out);
  8970. if (XMEMCMP(c, c2, sizeof(c2)))
  8971. ERROR_OUT(-6502, out);
  8972. if (XMEMCMP(t, t2, sizeof(t2)))
  8973. ERROR_OUT(-6503, out);
  8974. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  8975. t2, sizeof(t2), a, sizeof(a));
  8976. if (result != 0)
  8977. ERROR_OUT(-6504, out);
  8978. if (XMEMCMP(p, p2, sizeof(p2)))
  8979. ERROR_OUT(-6505, out);
  8980. /* Test the authentication failure */
  8981. t2[0]++; /* Corrupt the authentication tag. */
  8982. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  8983. t2, sizeof(t2), a, sizeof(a));
  8984. if (result == 0)
  8985. ERROR_OUT(-6506, out);
  8986. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  8987. * authentication fail. */
  8988. XMEMSET(c2, 0, sizeof(c2));
  8989. if (XMEMCMP(p2, c2, sizeof(p2)))
  8990. ERROR_OUT(-6507, out);
  8991. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  8992. XMEMSET(t2, 0, sizeof(t2));
  8993. XMEMSET(c2, 0, sizeof(c2));
  8994. XMEMSET(p2, 0, sizeof(p2));
  8995. XMEMSET(iv2, 0, sizeof(iv2));
  8996. #ifndef HAVE_SELFTEST
  8997. /* selftest build does not have wc_AesCcmSetNonce() or
  8998. * wc_AesCcmEncrypt_ex() */
  8999. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  9000. ERROR_OUT(-6508, out);
  9001. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  9002. ERROR_OUT(-6509, out);
  9003. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  9004. t2, sizeof(t2), a, sizeof(a)) != 0)
  9005. ERROR_OUT(-6510, out);
  9006. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  9007. ERROR_OUT(-6511, out);
  9008. if (XMEMCMP(c, c2, sizeof(c2)))
  9009. ERROR_OUT(-6512, out);
  9010. if (XMEMCMP(t, t2, sizeof(t2)))
  9011. ERROR_OUT(-6513, out);
  9012. #endif
  9013. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9014. /* test fail on invalid IV sizes */
  9015. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  9016. if (result != 0)
  9017. ERROR_OUT(-6514, out);
  9018. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  9019. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  9020. t2, 1, a, sizeof(a));
  9021. if (result == 0) {
  9022. ERROR_OUT(-6515, out);
  9023. }
  9024. #endif
  9025. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  9026. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  9027. tl2, sizeof(tl2), a, sizeof(a));
  9028. if (result != 0)
  9029. ERROR_OUT(-6516, out);
  9030. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  9031. ERROR_OUT(-6517, out);
  9032. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  9033. ERROR_OUT(-6518, out);
  9034. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  9035. tl2, sizeof(tl2), a, sizeof(a));
  9036. if (result != 0)
  9037. ERROR_OUT(-6519, out);
  9038. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  9039. ERROR_OUT(-6520, out);
  9040. /* test empty message as null input or output with nonzero inSz. */
  9041. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  9042. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  9043. a, sizeof(a));
  9044. if (result != BAD_FUNC_ARG)
  9045. ERROR_OUT(-6527, out);
  9046. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  9047. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  9048. a, sizeof(a));
  9049. if (result != BAD_FUNC_ARG)
  9050. ERROR_OUT(-6528, out);
  9051. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  9052. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9053. sizeof(a));
  9054. if (result != BAD_FUNC_ARG)
  9055. ERROR_OUT(-6529, out);
  9056. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  9057. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9058. sizeof(a));
  9059. if (result != BAD_FUNC_ARG)
  9060. ERROR_OUT(-6530, out);
  9061. /* test empty message as null input and output with zero inSz --
  9062. * must either succeed, or fail early with BAD_FUNC_ARG.
  9063. */
  9064. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  9065. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  9066. a, sizeof(a));
  9067. if (result != BAD_FUNC_ARG) {
  9068. if (result != 0)
  9069. ERROR_OUT(-6521, out);
  9070. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  9071. ERROR_OUT(-6522, out);
  9072. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  9073. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  9074. sizeof(t_empty2), a, sizeof(a));
  9075. if (result != 0)
  9076. ERROR_OUT(-6523, out);
  9077. }
  9078. /* test empty message as zero-length string -- must work. */
  9079. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  9080. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9081. sizeof(a));
  9082. if (result != 0)
  9083. ERROR_OUT(-6524, out);
  9084. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  9085. ERROR_OUT(-6525, out);
  9086. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  9087. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  9088. sizeof(a));
  9089. if (result != 0)
  9090. ERROR_OUT(-6526, out);
  9091. ret = 0;
  9092. out:
  9093. #ifdef WOLFSSL_SMALL_STACK
  9094. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9095. #endif
  9096. return ret;
  9097. }
  9098. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  9099. #ifdef HAVE_AES_KEYWRAP
  9100. #define MAX_KEYWRAP_TEST_OUTLEN 40
  9101. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  9102. typedef struct keywrapVector {
  9103. const byte* kek;
  9104. const byte* data;
  9105. const byte* verify;
  9106. word32 kekLen;
  9107. word32 dataLen;
  9108. word32 verifyLen;
  9109. } keywrapVector;
  9110. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  9111. {
  9112. int wrapSz, plainSz, testSz, i;
  9113. /* test vectors from RFC 3394 (kek, data, verify) */
  9114. #ifdef WOLFSSL_AES_128
  9115. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  9116. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  9117. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9118. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9119. };
  9120. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  9121. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9122. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9123. };
  9124. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  9125. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  9126. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  9127. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  9128. };
  9129. #endif /* WOLFSSL_AES_128 */
  9130. #ifdef WOLFSSL_AES_192
  9131. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  9132. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  9133. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9134. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9135. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9136. };
  9137. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  9138. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9139. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9140. };
  9141. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  9142. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  9143. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  9144. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  9145. };
  9146. #endif
  9147. #ifdef WOLFSSL_AES_256
  9148. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  9149. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  9150. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9151. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9152. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9153. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9154. };
  9155. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  9156. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9157. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  9158. };
  9159. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  9160. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  9161. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  9162. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  9163. };
  9164. #endif
  9165. #ifdef WOLFSSL_AES_192
  9166. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  9167. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  9168. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9169. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9170. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  9171. };
  9172. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  9173. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9174. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9175. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9176. };
  9177. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  9178. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  9179. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  9180. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  9181. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  9182. };
  9183. #endif
  9184. #ifdef WOLFSSL_AES_256
  9185. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  9186. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  9187. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9188. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9189. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9190. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9191. };
  9192. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  9193. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9194. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9195. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  9196. };
  9197. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  9198. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  9199. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  9200. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  9201. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  9202. };
  9203. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  9204. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  9205. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9206. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  9207. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  9208. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  9209. };
  9210. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  9211. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9212. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  9213. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9214. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9215. };
  9216. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  9217. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  9218. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  9219. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  9220. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  9221. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  9222. };
  9223. #endif /* WOLFSSL_AES_256 */
  9224. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  9225. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  9226. const keywrapVector test_wrap[] =
  9227. {
  9228. #ifdef WOLFSSL_AES_128
  9229. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  9230. #endif
  9231. #ifdef WOLFSSL_AES_192
  9232. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  9233. #endif
  9234. #ifdef WOLFSSL_AES_256
  9235. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  9236. #endif
  9237. #ifdef WOLFSSL_AES_192
  9238. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  9239. #endif
  9240. #ifdef WOLFSSL_AES_256
  9241. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  9242. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  9243. #endif
  9244. };
  9245. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  9246. XMEMSET(output, 0, sizeof(output));
  9247. XMEMSET(plain, 0, sizeof(plain));
  9248. for (i = 0; i < testSz; i++) {
  9249. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  9250. test_wrap[i].data, test_wrap[i].dataLen,
  9251. output, sizeof(output), NULL);
  9252. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  9253. return -6600;
  9254. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  9255. return -6601;
  9256. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  9257. output, wrapSz,
  9258. plain, sizeof(plain), NULL);
  9259. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  9260. return -6602;
  9261. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  9262. return -6603 - i;
  9263. }
  9264. return 0;
  9265. }
  9266. #endif /* HAVE_AES_KEYWRAP */
  9267. #endif /* NO_AES */
  9268. #ifdef HAVE_CAMELLIA
  9269. enum {
  9270. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  9271. };
  9272. typedef struct {
  9273. int type;
  9274. const byte* plaintext;
  9275. const byte* iv;
  9276. const byte* ciphertext;
  9277. const byte* key;
  9278. word32 keySz;
  9279. int errorCode;
  9280. } test_vector_t;
  9281. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  9282. {
  9283. /* Camellia ECB Test Plaintext */
  9284. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  9285. {
  9286. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9287. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9288. };
  9289. /* Camellia ECB Test Initialization Vector */
  9290. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  9291. /* Test 1: Camellia ECB 128-bit key */
  9292. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9293. {
  9294. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9295. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  9296. };
  9297. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9298. {
  9299. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  9300. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  9301. };
  9302. /* Test 2: Camellia ECB 192-bit key */
  9303. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9304. {
  9305. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9306. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9307. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  9308. };
  9309. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9310. {
  9311. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  9312. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  9313. };
  9314. /* Test 3: Camellia ECB 256-bit key */
  9315. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9316. {
  9317. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  9318. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  9319. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  9320. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  9321. };
  9322. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9323. {
  9324. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  9325. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  9326. };
  9327. /* Camellia CBC Test Plaintext */
  9328. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  9329. {
  9330. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  9331. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  9332. };
  9333. /* Camellia CBC Test Initialization Vector */
  9334. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  9335. {
  9336. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9337. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  9338. };
  9339. /* Test 4: Camellia-CBC 128-bit key */
  9340. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  9341. {
  9342. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  9343. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  9344. };
  9345. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  9346. {
  9347. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  9348. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  9349. };
  9350. /* Test 5: Camellia-CBC 192-bit key */
  9351. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  9352. {
  9353. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  9354. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  9355. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  9356. };
  9357. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  9358. {
  9359. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  9360. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  9361. };
  9362. /* Test 6: CBC 256-bit key */
  9363. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  9364. {
  9365. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  9366. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  9367. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  9368. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  9369. };
  9370. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  9371. {
  9372. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  9373. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  9374. };
  9375. byte out[CAMELLIA_BLOCK_SIZE];
  9376. Camellia cam;
  9377. int i, testsSz, ret;
  9378. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  9379. {
  9380. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  9381. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  9382. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  9383. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  9384. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  9385. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  9386. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  9387. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  9388. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  9389. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  9390. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  9391. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  9392. };
  9393. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  9394. for (i = 0; i < testsSz; i++) {
  9395. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  9396. testVectors[i].iv) != 0)
  9397. return testVectors[i].errorCode;
  9398. switch (testVectors[i].type) {
  9399. case CAM_ECB_ENC:
  9400. ret = wc_CamelliaEncryptDirect(&cam, out,
  9401. testVectors[i].plaintext);
  9402. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9403. CAMELLIA_BLOCK_SIZE))
  9404. return testVectors[i].errorCode;
  9405. break;
  9406. case CAM_ECB_DEC:
  9407. ret = wc_CamelliaDecryptDirect(&cam, out,
  9408. testVectors[i].ciphertext);
  9409. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9410. CAMELLIA_BLOCK_SIZE))
  9411. return testVectors[i].errorCode;
  9412. break;
  9413. case CAM_CBC_ENC:
  9414. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  9415. CAMELLIA_BLOCK_SIZE);
  9416. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  9417. CAMELLIA_BLOCK_SIZE))
  9418. return testVectors[i].errorCode;
  9419. break;
  9420. case CAM_CBC_DEC:
  9421. ret = wc_CamelliaCbcDecrypt(&cam, out,
  9422. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  9423. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  9424. CAMELLIA_BLOCK_SIZE))
  9425. return testVectors[i].errorCode;
  9426. break;
  9427. default:
  9428. break;
  9429. }
  9430. }
  9431. /* Setting the IV and checking it was actually set. */
  9432. ret = wc_CamelliaSetIV(&cam, ivc);
  9433. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  9434. return -6700;
  9435. /* Setting the IV to NULL should be same as all zeros IV */
  9436. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  9437. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  9438. return -6701;
  9439. /* First parameter should never be null */
  9440. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  9441. return -6702;
  9442. /* First parameter should never be null, check it fails */
  9443. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  9444. return -6703;
  9445. /* Key should have a size of 16, 24, or 32 */
  9446. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  9447. return -6704;
  9448. return 0;
  9449. }
  9450. #endif /* HAVE_CAMELLIA */
  9451. #ifdef HAVE_IDEA
  9452. WOLFSSL_TEST_SUBROUTINE int idea_test(void)
  9453. {
  9454. int ret;
  9455. word16 i, j;
  9456. Idea idea;
  9457. byte data[IDEA_BLOCK_SIZE];
  9458. /* Project NESSIE test vectors */
  9459. #define IDEA_NB_TESTS 6
  9460. #define IDEA_NB_TESTS_EXTRA 4
  9461. WOLFSSL_SMALL_STACK_STATIC const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  9462. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  9463. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9464. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  9465. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9466. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9467. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9468. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9469. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9470. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9471. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  9472. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  9473. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  9474. };
  9475. WOLFSSL_SMALL_STACK_STATIC const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9476. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  9477. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  9478. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9479. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9480. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  9481. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  9482. };
  9483. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  9484. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  9485. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  9486. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  9487. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  9488. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  9489. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  9490. };
  9491. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9492. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  9493. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  9494. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  9495. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  9496. };
  9497. WOLFSSL_SMALL_STACK_STATIC const byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  9498. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  9499. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  9500. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  9501. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  9502. };
  9503. /* CBC test */
  9504. const char *message = "International Data Encryption Algorithm";
  9505. byte msg_enc[40], msg_dec[40];
  9506. for (i = 0; i < IDEA_NB_TESTS; i++) {
  9507. /* Set encryption key */
  9508. XMEMSET(&idea, 0, sizeof(Idea));
  9509. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9510. NULL, IDEA_ENCRYPTION);
  9511. if (ret != 0) {
  9512. printf("wc_IdeaSetKey (enc) failed\n");
  9513. return -6800;
  9514. }
  9515. /* Data encryption */
  9516. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  9517. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  9518. printf("Bad encryption\n");
  9519. return -6801;
  9520. }
  9521. /* Set decryption key */
  9522. XMEMSET(&idea, 0, sizeof(Idea));
  9523. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9524. NULL, IDEA_DECRYPTION);
  9525. if (ret != 0) {
  9526. printf("wc_IdeaSetKey (dec) failed\n");
  9527. return -6802;
  9528. }
  9529. /* Data decryption */
  9530. ret = wc_IdeaCipher(&idea, data, data);
  9531. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  9532. printf("Bad decryption\n");
  9533. return -6803;
  9534. }
  9535. /* Set encryption key */
  9536. XMEMSET(&idea, 0, sizeof(Idea));
  9537. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9538. v_key[i], IDEA_ENCRYPTION);
  9539. if (ret != 0) {
  9540. printf("wc_IdeaSetKey (enc) failed\n");
  9541. return -6804;
  9542. }
  9543. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  9544. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  9545. (word32)XSTRLEN(message)+1);
  9546. if (ret != 0) {
  9547. printf("wc_IdeaCbcEncrypt failed\n");
  9548. return -6805;
  9549. }
  9550. /* Set decryption key */
  9551. XMEMSET(&idea, 0, sizeof(Idea));
  9552. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9553. v_key[i], IDEA_DECRYPTION);
  9554. if (ret != 0) {
  9555. printf("wc_IdeaSetKey (dec) failed\n");
  9556. return -6806;
  9557. }
  9558. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  9559. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  9560. (word32)XSTRLEN(message)+1);
  9561. if (ret != 0) {
  9562. printf("wc_IdeaCbcDecrypt failed\n");
  9563. return -6807;
  9564. }
  9565. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  9566. printf("Bad CBC decryption\n");
  9567. return -6808;
  9568. }
  9569. }
  9570. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  9571. /* Set encryption key */
  9572. XMEMSET(&idea, 0, sizeof(Idea));
  9573. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  9574. NULL, IDEA_ENCRYPTION);
  9575. if (ret != 0) {
  9576. printf("wc_IdeaSetKey (enc) failed\n");
  9577. return -6809;
  9578. }
  9579. /* 100 times data encryption */
  9580. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9581. for (j = 0; j < 100; j++) {
  9582. ret = wc_IdeaCipher(&idea, data, data);
  9583. if (ret != 0) {
  9584. return -6810;
  9585. }
  9586. }
  9587. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  9588. printf("Bad encryption (100 times)\n");
  9589. return -6811;
  9590. }
  9591. /* 1000 times data encryption */
  9592. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  9593. for (j = 0; j < 1000; j++) {
  9594. ret = wc_IdeaCipher(&idea, data, data);
  9595. if (ret != 0) {
  9596. return -6812;
  9597. }
  9598. }
  9599. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  9600. printf("Bad encryption (100 times)\n");
  9601. return -6813;
  9602. }
  9603. }
  9604. #ifndef WC_NO_RNG
  9605. /* random test for CBC */
  9606. {
  9607. WC_RNG rng;
  9608. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  9609. rnd[1000], enc[1000], dec[1000];
  9610. /* random values */
  9611. #ifndef HAVE_FIPS
  9612. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9613. #else
  9614. ret = wc_InitRng(&rng);
  9615. #endif
  9616. if (ret != 0)
  9617. return -6814;
  9618. for (i = 0; i < 1000; i++) {
  9619. /* random key */
  9620. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  9621. if (ret != 0)
  9622. return -6815;
  9623. /* random iv */
  9624. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  9625. if (ret != 0)
  9626. return -6816;
  9627. /* random data */
  9628. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  9629. if (ret != 0)
  9630. return -6817;
  9631. /* Set encryption key */
  9632. XMEMSET(&idea, 0, sizeof(Idea));
  9633. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  9634. if (ret != 0) {
  9635. printf("wc_IdeaSetKey (enc) failed\n");
  9636. return -6818;
  9637. }
  9638. /* Data encryption */
  9639. XMEMSET(enc, 0, sizeof(enc));
  9640. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  9641. if (ret != 0) {
  9642. printf("wc_IdeaCbcEncrypt failed\n");
  9643. return -6819;
  9644. }
  9645. /* Set decryption key */
  9646. XMEMSET(&idea, 0, sizeof(Idea));
  9647. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  9648. if (ret != 0) {
  9649. printf("wc_IdeaSetKey (enc) failed\n");
  9650. return -6820;
  9651. }
  9652. /* Data decryption */
  9653. XMEMSET(dec, 0, sizeof(dec));
  9654. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  9655. if (ret != 0) {
  9656. printf("wc_IdeaCbcDecrypt failed\n");
  9657. return -6821;
  9658. }
  9659. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  9660. printf("Bad CBC decryption\n");
  9661. return -6822;
  9662. }
  9663. }
  9664. wc_FreeRng(&rng);
  9665. }
  9666. #endif /* WC_NO_RNG */
  9667. return 0;
  9668. }
  9669. #endif /* HAVE_IDEA */
  9670. #ifdef HAVE_XCHACHA
  9671. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  9672. int ret = -6830;
  9673. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9674. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  9675. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  9676. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  9677. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  9678. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  9679. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  9680. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  9681. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  9682. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  9683. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  9684. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  9685. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  9686. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  9687. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  9688. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  9689. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  9690. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  9691. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  9692. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  9693. };
  9694. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9695. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9696. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9697. };
  9698. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9699. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9700. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  9701. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9702. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  9703. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  9704. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  9705. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  9706. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  9707. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  9708. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  9709. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  9710. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  9711. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  9712. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  9713. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  9714. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  9715. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  9716. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  9717. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  9718. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  9719. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  9720. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  9721. };
  9722. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9723. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9724. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9725. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9726. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  9727. ERROR_OUT(MEMORY_E, out);
  9728. #else
  9729. struct ChaCha chacha[1];
  9730. byte buf1[sizeof Plaintext];
  9731. byte buf2[sizeof Plaintext];
  9732. #endif
  9733. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9734. if (ret < 0)
  9735. ERROR_OUT(-6831, out);
  9736. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  9737. if (ret < 0)
  9738. ERROR_OUT(-6832, out);
  9739. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9740. ERROR_OUT(-6833, out);
  9741. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  9742. if (ret < 0)
  9743. ERROR_OUT(-6834, out);
  9744. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  9745. if (ret < 0)
  9746. ERROR_OUT(-6835, out);
  9747. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9748. ERROR_OUT(-6836, out);
  9749. out:
  9750. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9751. if (chacha)
  9752. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  9753. if (buf1)
  9754. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9755. if (buf2)
  9756. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9757. #endif
  9758. return ret;
  9759. }
  9760. #endif /* HAVE_XCHACHA */
  9761. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  9762. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  9763. int ret;
  9764. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  9765. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  9766. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  9767. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  9768. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  9769. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  9770. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  9771. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  9772. 0x74, 0x2e }; /* t. */
  9773. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  9774. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  9775. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  9776. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  9777. };
  9778. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  9779. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  9780. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  9781. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  9782. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  9783. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  9784. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  9785. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  9786. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  9787. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  9788. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  9789. 0xb5, 0x2e };
  9790. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  9791. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  9792. };
  9793. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9794. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9795. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9796. #else
  9797. byte buf1[sizeof Ciphertext + sizeof Tag];
  9798. byte buf2[sizeof Plaintext];
  9799. #endif
  9800. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  9801. Plaintext, sizeof Plaintext,
  9802. AAD, sizeof AAD,
  9803. IV, sizeof IV,
  9804. Key, sizeof Key);
  9805. if (ret < 0)
  9806. ERROR_OUT(-6840, out);
  9807. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  9808. ERROR_OUT(-6841, out);
  9809. if (XMEMCMP(buf1 + sizeof Plaintext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  9810. ERROR_OUT(-6842, out);
  9811. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  9812. buf1, sizeof Plaintext + sizeof Tag,
  9813. AAD, sizeof AAD,
  9814. IV, sizeof IV,
  9815. Key, sizeof Key);
  9816. if (ret < 0)
  9817. ERROR_OUT(-6843, out);
  9818. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  9819. ERROR_OUT(-6844, out);
  9820. out:
  9821. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9822. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9823. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9824. #endif
  9825. return ret;
  9826. }
  9827. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  9828. #ifndef WC_NO_RNG
  9829. static int _rng_test(WC_RNG* rng, int errorOffset)
  9830. {
  9831. byte block[32];
  9832. int ret, i;
  9833. XMEMSET(block, 0, sizeof(block));
  9834. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  9835. if (ret != 0) {
  9836. ret = -6850;
  9837. goto exit;
  9838. }
  9839. /* Check for 0's */
  9840. for (i=0; i<(int)sizeof(block); i++) {
  9841. if (block[i] == 0) {
  9842. ret++;
  9843. }
  9844. }
  9845. /* All zeros count check */
  9846. if (ret >= (int)sizeof(block)) {
  9847. ret = -6851;
  9848. goto exit;
  9849. }
  9850. ret = wc_RNG_GenerateByte(rng, block);
  9851. if (ret != 0) {
  9852. ret = -6852;
  9853. goto exit;
  9854. }
  9855. /* Parameter validation testing. */
  9856. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  9857. if (ret != BAD_FUNC_ARG) {
  9858. ret = -6853;
  9859. goto exit;
  9860. }
  9861. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  9862. if (ret != BAD_FUNC_ARG) {
  9863. ret = -6854;
  9864. goto exit;
  9865. }
  9866. ret = wc_RNG_GenerateByte(NULL, block);
  9867. if (ret != BAD_FUNC_ARG) {
  9868. ret = -6855;
  9869. goto exit;
  9870. }
  9871. ret = wc_RNG_GenerateByte(rng, NULL);
  9872. if (ret != BAD_FUNC_ARG) {
  9873. ret = -6856;
  9874. goto exit;
  9875. }
  9876. ret = 0;
  9877. exit:
  9878. if (ret != 0)
  9879. ret += errorOffset;
  9880. return ret;
  9881. }
  9882. static int random_rng_test(void)
  9883. {
  9884. WC_RNG localRng;
  9885. WC_RNG* rng;
  9886. int ret;
  9887. rng = &localRng;
  9888. /* Test stack based RNG. */
  9889. #ifndef HAVE_FIPS
  9890. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  9891. #else
  9892. ret = wc_InitRng(rng);
  9893. #endif
  9894. if (ret != 0) return -6900;
  9895. ret = _rng_test(rng, -6300);
  9896. /* Make sure and free RNG */
  9897. wc_FreeRng(rng);
  9898. if (ret != 0) return ret;
  9899. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  9900. {
  9901. byte nonce[8] = { 0 };
  9902. /* Test dynamic RNG. */
  9903. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  9904. if (rng == NULL) return -6901;
  9905. ret = _rng_test(rng, -6310);
  9906. wc_rng_free(rng);
  9907. }
  9908. #endif
  9909. return ret;
  9910. }
  9911. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  9912. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  9913. {
  9914. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  9915. {
  9916. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  9917. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  9918. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  9919. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  9920. };
  9921. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  9922. {
  9923. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  9924. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  9925. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  9926. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  9927. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  9928. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  9929. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  9930. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  9931. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  9932. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  9933. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  9934. };
  9935. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  9936. {
  9937. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  9938. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  9939. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  9940. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  9941. };
  9942. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  9943. {
  9944. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  9945. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  9946. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  9947. };
  9948. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  9949. {
  9950. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  9951. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  9952. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  9953. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  9954. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  9955. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  9956. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  9957. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  9958. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  9959. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  9960. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  9961. };
  9962. byte output[WC_SHA256_DIGEST_SIZE * 4];
  9963. int ret;
  9964. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  9965. output, sizeof(output));
  9966. if (ret != 0)
  9967. return -7000;
  9968. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  9969. return -7001;
  9970. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  9971. test2EntropyB, sizeof(test2EntropyB),
  9972. output, sizeof(output));
  9973. if (ret != 0)
  9974. return -7002;
  9975. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  9976. return -7003;
  9977. /* Basic RNG generate block test */
  9978. if ((ret = random_rng_test()) != 0)
  9979. return ret;
  9980. /* Test the seed check function. */
  9981. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  9982. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9983. {
  9984. word32 i, outputSz;
  9985. /* Repeat the same byte over and over. Should fail. */
  9986. outputSz = sizeof(output);
  9987. XMEMSET(output, 1, outputSz);
  9988. ret = wc_RNG_TestSeed(output, outputSz);
  9989. if (ret == 0)
  9990. return -7004;
  9991. /* Every byte of the entropy scratch is different,
  9992. * entropy is a single byte that shouldn't match. */
  9993. outputSz = (sizeof(word32) * 2) + 1;
  9994. for (i = 0; i < outputSz; i++)
  9995. output[i] = (byte)i;
  9996. ret = wc_RNG_TestSeed(output, outputSz);
  9997. if (ret != 0)
  9998. return -7005;
  9999. outputSz = sizeof(output);
  10000. for (i = 0; i < outputSz; i++)
  10001. output[i] = (byte)i;
  10002. ret = wc_RNG_TestSeed(output, outputSz);
  10003. if (ret != 0)
  10004. return -7006;
  10005. }
  10006. #endif
  10007. return 0;
  10008. }
  10009. #else
  10010. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  10011. {
  10012. /* Basic RNG generate block test */
  10013. return random_rng_test();
  10014. }
  10015. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  10016. #endif /* WC_NO_RNG */
  10017. #ifndef MEM_TEST_SZ
  10018. #define MEM_TEST_SZ 1024
  10019. #endif
  10020. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  10021. static int simple_mem_test(int sz)
  10022. {
  10023. int ret = 0;
  10024. byte* b;
  10025. int i;
  10026. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10027. if (b == NULL) {
  10028. return -7110;
  10029. }
  10030. /* utilize memory */
  10031. for (i = 0; i < sz; i++) {
  10032. b[i] = (byte)i;
  10033. }
  10034. /* read back and verify */
  10035. for (i = 0; i < sz; i++) {
  10036. if (b[i] != (byte)i) {
  10037. ret = -7111;
  10038. break;
  10039. }
  10040. }
  10041. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10042. return ret;
  10043. }
  10044. #endif
  10045. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  10046. {
  10047. int ret = 0;
  10048. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  10049. byte* b = NULL;
  10050. #endif
  10051. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  10052. int i;
  10053. #endif
  10054. #ifdef WOLFSSL_STATIC_MEMORY
  10055. word32 size[] = { WOLFMEM_BUCKETS };
  10056. word32 dist[] = { WOLFMEM_DIST };
  10057. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  10058. int pad = -(int)((wolfssl_word)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  10059. /* pad to account for if head of buffer is not at set memory
  10060. * alignment when tests are ran */
  10061. #endif
  10062. #ifdef WOLFSSL_STATIC_MEMORY
  10063. /* check macro settings */
  10064. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  10065. return -7200;
  10066. }
  10067. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  10068. return -7201;
  10069. }
  10070. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  10071. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  10072. /* each element in array should be divisible by alignment size */
  10073. return -7202;
  10074. }
  10075. }
  10076. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  10077. if (size[i - 1] >= size[i]) {
  10078. return -7203; /* sizes should be in increasing order */
  10079. }
  10080. }
  10081. /* check that padding size returned is possible */
  10082. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  10083. return -7204; /* no room for wc_Memory struct */
  10084. }
  10085. if (wolfSSL_MemoryPaddingSz() < 0) {
  10086. return -7205;
  10087. }
  10088. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  10089. return -7206; /* not aligned! */
  10090. }
  10091. /* check function to return optimum buffer size (rounded down) */
  10092. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  10093. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  10094. return -7207; /* not aligned! */
  10095. }
  10096. if (ret < 0) {
  10097. return -7208;
  10098. }
  10099. if ((unsigned int)ret > sizeof(buffer)) {
  10100. return -7209; /* did not round down as expected */
  10101. }
  10102. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  10103. return -7210; /* return value changed when using suggested value */
  10104. }
  10105. ret = wolfSSL_MemoryPaddingSz();
  10106. ret += pad; /* add space that is going to be needed if buffer not aligned */
  10107. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  10108. (ret + (int)size[0])) {
  10109. return -7211; /* did not round down to nearest bucket value */
  10110. }
  10111. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  10112. if ((ret - pad) < 0) {
  10113. return -7212;
  10114. }
  10115. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  10116. return -7213; /* not even chunks of memory for IO size */
  10117. }
  10118. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  10119. return -7214; /* memory not aligned */
  10120. }
  10121. /* check for passing bad or unknown arguments to functions */
  10122. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  10123. return -7215;
  10124. }
  10125. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  10126. return -7216; /* should round to 0 since struct + bucket will not fit */
  10127. }
  10128. (void)dist; /* avoid static analysis warning of variable not used */
  10129. #endif
  10130. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  10131. /* simple test */
  10132. ret = simple_mem_test(MEM_TEST_SZ);
  10133. if (ret != 0)
  10134. return ret;
  10135. #endif
  10136. #ifdef COMPLEX_MEM_TEST
  10137. /* test various size blocks */
  10138. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  10139. ret = simple_mem_test(i);
  10140. if (ret != 0)
  10141. return ret;
  10142. }
  10143. #endif
  10144. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  10145. /* realloc test */
  10146. b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10147. if (b) {
  10148. b = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  10149. DYNAMIC_TYPE_TMP_BUFFER);
  10150. }
  10151. if (b == NULL) {
  10152. return -7217;
  10153. }
  10154. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10155. #endif
  10156. return ret;
  10157. }
  10158. #ifdef HAVE_NTRU
  10159. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  10160. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  10161. {
  10162. static WC_RNG rng;
  10163. if (cmd == INIT)
  10164. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  10165. if (out == NULL)
  10166. return 0;
  10167. if (cmd == GET_BYTE_OF_ENTROPY)
  10168. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  10169. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  10170. *out = 1;
  10171. return 1;
  10172. }
  10173. return 0;
  10174. }
  10175. #endif /* HAVE_NTRU */
  10176. #ifndef NO_FILESYSTEM
  10177. /* Cert Paths */
  10178. #ifdef FREESCALE_MQX
  10179. #define CERT_PREFIX "a:\\"
  10180. #define CERT_PATH_SEP "\\"
  10181. #elif defined(WOLFSSL_uTKERNEL2)
  10182. #define CERT_PREFIX "/uda/"
  10183. #define CERT_PATH_SEP "/"
  10184. #elif defined(_WIN32_WCE)
  10185. #define CERT_PREFIX "\\windows\\"
  10186. #define CERT_PATH_SEP "\\"
  10187. #endif
  10188. #ifndef CERT_PREFIX
  10189. #define CERT_PREFIX "./"
  10190. #endif
  10191. #ifndef CERT_PATH_SEP
  10192. #define CERT_PATH_SEP "/"
  10193. #endif
  10194. #ifndef CERT_WRITE_TEMP_DIR
  10195. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  10196. #endif
  10197. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  10198. /* Generated Test Certs */
  10199. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10200. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  10201. #if !defined(NO_RSA) && !defined(NO_ASN)
  10202. static const char* clientKey = CERT_ROOT "client-key.der";
  10203. static const char* clientCert = CERT_ROOT "client-cert.der";
  10204. #ifdef WOLFSSL_CERT_EXT
  10205. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  10206. #endif
  10207. #endif /* !NO_RSA && !NO_ASN */
  10208. #endif
  10209. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  10210. #if !defined(NO_RSA) && !defined(NO_ASN)
  10211. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  10212. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  10213. #ifdef WOLFSSL_CERT_GEN
  10214. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  10215. #endif
  10216. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  10217. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  10218. #endif
  10219. #ifdef HAVE_PKCS7
  10220. static const char* rsaServerCertDerFile =
  10221. CERT_ROOT "server-cert.der";
  10222. static const char* rsaServerKeyDerFile =
  10223. CERT_ROOT "server-key.der";
  10224. #endif
  10225. #endif
  10226. #endif /* !NO_RSA && !NO_ASN */
  10227. #endif /* !USE_CERT_BUFFER_* */
  10228. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  10229. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  10230. !defined(NO_ASN)
  10231. #ifndef NO_DH
  10232. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  10233. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  10234. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  10235. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  10236. #endif
  10237. #endif
  10238. #ifndef NO_DSA
  10239. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  10240. #endif
  10241. #endif /* !USE_CERT_BUFFER_* */
  10242. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  10243. #ifdef HAVE_ECC
  10244. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  10245. #ifdef HAVE_ECC_KEY_IMPORT
  10246. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  10247. #endif
  10248. #endif
  10249. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  10250. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  10251. #ifndef NO_RSA
  10252. /* eccKeyPubFile is used in a test that requires RSA. */
  10253. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  10254. #endif
  10255. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  10256. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  10257. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10258. static const char* eccCaKey384File =
  10259. CERT_ROOT "ca-ecc384-key.der";
  10260. static const char* eccCaCert384File =
  10261. CERT_ROOT "ca-ecc384-cert.pem";
  10262. #endif
  10263. #endif
  10264. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  10265. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  10266. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  10267. #endif
  10268. #endif /* HAVE_ECC */
  10269. #ifdef HAVE_ED25519
  10270. #ifdef WOLFSSL_TEST_CERT
  10271. static const char* serverEd25519Cert =
  10272. CERT_ROOT "ed25519/server-ed25519.der";
  10273. static const char* caEd25519Cert =
  10274. CERT_ROOT "ed25519/ca-ed25519.der";
  10275. #endif
  10276. #endif
  10277. #ifdef HAVE_ED448
  10278. #ifdef WOLFSSL_TEST_CERT
  10279. static const char* serverEd448Cert =
  10280. CERT_ROOT "ed448/server-ed448.der";
  10281. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  10282. #endif
  10283. #endif
  10284. #endif /* !USE_CERT_BUFFER_* */
  10285. #ifndef NO_WRITE_TEMP_FILES
  10286. #ifdef HAVE_ECC
  10287. #ifdef WOLFSSL_CERT_GEN
  10288. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  10289. #endif
  10290. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  10291. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  10292. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  10293. #endif
  10294. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  10295. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  10296. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  10297. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  10298. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG)
  10299. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  10300. #endif
  10301. #endif /* HAVE_ECC_KEY_EXPORT */
  10302. #if defined(WOLFSSL_CERT_GEN) || \
  10303. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10304. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  10305. #endif
  10306. #endif /* HAVE_ECC */
  10307. #ifndef NO_RSA
  10308. #if defined(WOLFSSL_CERT_GEN) || \
  10309. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  10310. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  10311. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  10312. #endif
  10313. #ifdef WOLFSSL_CERT_GEN
  10314. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  10315. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  10316. #endif
  10317. #ifdef WOLFSSL_CERT_REQ
  10318. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  10319. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  10320. #endif
  10321. #endif /* !NO_RSA */
  10322. #if !defined(NO_RSA) || !defined(NO_DSA)
  10323. #ifdef WOLFSSL_KEY_GEN
  10324. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  10325. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  10326. #endif
  10327. #endif
  10328. #endif /* !NO_WRITE_TEMP_FILES */
  10329. #endif /* !NO_FILESYSTEM */
  10330. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  10331. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  10332. #ifdef WOLFSSL_MULTI_ATTRIB
  10333. static CertName certDefaultName;
  10334. static void initDefaultName(void)
  10335. {
  10336. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  10337. certDefaultName.countryEnc = CTC_PRINTABLE;
  10338. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  10339. certDefaultName.stateEnc = CTC_UTF8;
  10340. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  10341. certDefaultName.localityEnc = CTC_UTF8;
  10342. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  10343. certDefaultName.surEnc = CTC_UTF8;
  10344. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  10345. certDefaultName.orgEnc = CTC_UTF8;
  10346. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  10347. certDefaultName.unitEnc = CTC_UTF8;
  10348. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  10349. certDefaultName.commonNameEnc = CTC_UTF8;
  10350. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  10351. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  10352. #ifdef WOLFSSL_CERT_EXT
  10353. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  10354. certDefaultName.busCatEnc = CTC_UTF8;
  10355. #endif
  10356. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  10357. #ifdef WOLFSSL_TEST_CERT
  10358. {
  10359. NameAttrib* n;
  10360. /* test having additional OUs and setting DC */
  10361. n = &certDefaultName.name[0];
  10362. n->id = ASN_ORGUNIT_NAME;
  10363. n->type = CTC_UTF8;
  10364. n->sz = sizeof("Development-2");
  10365. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  10366. #if CTC_MAX_ATTRIB > 3
  10367. n = &certDefaultName.name[1];
  10368. n->id = ASN_DOMAIN_COMPONENT;
  10369. n->type = CTC_UTF8;
  10370. n->sz = sizeof("com");
  10371. XMEMCPY(n->value, "com", sizeof("com"));
  10372. n = &certDefaultName.name[2];
  10373. n->id = ASN_DOMAIN_COMPONENT;
  10374. n->type = CTC_UTF8;
  10375. n->sz = sizeof("wolfssl");
  10376. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  10377. #endif
  10378. }
  10379. #endif /* WOLFSSL_TEST_CERT */
  10380. }
  10381. #else
  10382. static const CertName certDefaultName = {
  10383. "US", CTC_PRINTABLE, /* country */
  10384. "Oregon", CTC_UTF8, /* state */
  10385. "Portland", CTC_UTF8, /* locality */
  10386. "Test", CTC_UTF8, /* sur */
  10387. "wolfSSL", CTC_UTF8, /* org */
  10388. "Development", CTC_UTF8, /* unit */
  10389. "www.wolfssl.com", CTC_UTF8, /* commonName */
  10390. "wolfSSL12345", CTC_PRINTABLE, /* serial number of device */
  10391. #ifdef WOLFSSL_CERT_EXT
  10392. "Private Organization", CTC_UTF8, /* businessCategory */
  10393. "US", CTC_PRINTABLE, /* jurisdiction country */
  10394. "Oregon", CTC_PRINTABLE, /* jurisdiction state */
  10395. #endif
  10396. "info@wolfssl.com" /* email */
  10397. };
  10398. #endif /* WOLFSSL_MULTI_ATTRIB */
  10399. #ifdef WOLFSSL_CERT_EXT
  10400. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  10401. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  10402. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  10403. "digitalSignature,nonRepudiation";
  10404. #endif
  10405. #if (defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)) && !defined(NO_RSA)
  10406. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  10407. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  10408. #endif
  10409. #endif /* WOLFSSL_CERT_EXT */
  10410. #endif /* WOLFSSL_CERT_GEN */
  10411. #ifndef NO_RSA
  10412. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  10413. !defined(NO_FILESYSTEM)
  10414. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  10415. static byte minName[] = { 0x30, 0x00 };
  10416. static byte nameBad[] = {
  10417. 0x30, 0x08,
  10418. 0x31, 0x06,
  10419. 0x30, 0x04,
  10420. 0x06, 0x02,
  10421. 0x55, 0x04,
  10422. };
  10423. static byte minDates[] = {
  10424. 0x30, 0x1e,
  10425. 0x17, 0x0d,
  10426. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  10427. 0x32, 0x33, 0x31, 0x30, 0x5a,
  10428. 0x17, 0x0d,
  10429. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  10430. 0x32, 0x33, 0x31, 0x30, 0x5a
  10431. };
  10432. static byte minPubKey[] = {
  10433. 0x30, 0x1b,
  10434. 0x30, 0x0d,
  10435. 0x06, 0x09,
  10436. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10437. 0x01,
  10438. 0x05, 0x00,
  10439. 0x03, 0x0b,
  10440. 0x00, 0x30, 0x08,
  10441. 0x02, 0x01,
  10442. 0x03,
  10443. 0x02, 0x03,
  10444. 0x01, 0x00, 0x01
  10445. };
  10446. static byte minSigAlg[] = {
  10447. 0x30, 0x0d,
  10448. 0x06, 0x09,
  10449. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  10450. 0x0b,
  10451. 0x05, 0x00
  10452. };
  10453. static byte minSig[] = {
  10454. 0x03, 0x01,
  10455. 0x00
  10456. };
  10457. static int add_seq(byte* certData, int offset, byte* data, byte length)
  10458. {
  10459. XMEMMOVE(certData + offset + 2, data, length);
  10460. certData[offset++] = 0x30;
  10461. certData[offset++] = length;
  10462. return offset + length;
  10463. }
  10464. static int add_data(byte* certData, int offset, byte* data, byte length)
  10465. {
  10466. XMEMCPY(certData + offset, data, length);
  10467. return offset + length;
  10468. }
  10469. static int cert_asn1_test(void)
  10470. {
  10471. int ret;
  10472. int len[3];
  10473. DecodedCert cert;
  10474. byte certData[106];
  10475. byte* badCert = NULL;
  10476. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10477. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10478. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10479. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  10480. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  10481. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  10482. len[1] = add_seq(certData, 0, certData, len[2]);
  10483. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  10484. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  10485. len[0] = add_seq(certData, 0, certData, len[1]);
  10486. /* Minimal good certificate */
  10487. InitDecodedCert(&cert, certData, len[0], 0);
  10488. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10489. FreeDecodedCert(&cert);
  10490. if (ret != 0) {
  10491. ERROR_OUT(-7300, done);
  10492. }
  10493. /* Bad issuer name */
  10494. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  10495. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  10496. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  10497. len[1] = add_seq(certData, 0, certData, len[2]);
  10498. len[0] = add_seq(certData, 0, certData, len[1]);
  10499. /* Put data into allocated buffer to allow access error checking. */
  10500. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10501. XMEMCPY(badCert, certData, len[0]);
  10502. InitDecodedCert(&cert, badCert, len[0], 0);
  10503. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10504. FreeDecodedCert(&cert);
  10505. if (ret != ASN_PARSE_E) {
  10506. ERROR_OUT(-7301, done);
  10507. }
  10508. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10509. badCert = NULL;
  10510. ret = 0;
  10511. done:
  10512. if (badCert != NULL)
  10513. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10514. return ret;
  10515. }
  10516. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  10517. {
  10518. #if !defined(NO_FILESYSTEM)
  10519. DecodedCert cert;
  10520. byte* tmp;
  10521. size_t bytes;
  10522. XFILE file;
  10523. int ret;
  10524. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10525. if (tmp == NULL)
  10526. return -7400;
  10527. /* Certificate with Name Constraints extension. */
  10528. #ifdef FREESCALE_MQX
  10529. file = XFOPEN(".\\certs\\test\\cert-ext-nc.der", "rb");
  10530. #else
  10531. file = XFOPEN("./certs/test/cert-ext-nc.der", "rb");
  10532. #endif
  10533. if (!file) {
  10534. ERROR_OUT(-7401, done);
  10535. }
  10536. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10537. XFCLOSE(file);
  10538. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10539. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10540. if (ret != 0) {
  10541. ERROR_OUT(-7402, done);
  10542. }
  10543. FreeDecodedCert(&cert);
  10544. /* Certificate with Inhibit Any Policy extension. */
  10545. #ifdef FREESCALE_MQX
  10546. file = XFOPEN(".\\certs\\test\\cert-ext-ia.der", "rb");
  10547. #else
  10548. file = XFOPEN("./certs/test/cert-ext-ia.der", "rb");
  10549. #endif
  10550. if (!file) {
  10551. ERROR_OUT(-7403, done);
  10552. }
  10553. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10554. XFCLOSE(file);
  10555. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10556. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10557. if (ret != 0) {
  10558. ERROR_OUT(-7404, done);
  10559. }
  10560. FreeDecodedCert(&cert);
  10561. /* Certificate with Netscape Certificate Type extension. */
  10562. #ifdef FREESCALE_MQX
  10563. file = XFOPEN(".\\certs\\test\\cert-ext-nct.der", "rb");
  10564. #else
  10565. file = XFOPEN("./certs/test/cert-ext-nct.der", "rb");
  10566. #endif
  10567. if (!file) {
  10568. ERROR_OUT(-7405, done);
  10569. }
  10570. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10571. XFCLOSE(file);
  10572. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10573. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  10574. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  10575. if (ret != 0) {
  10576. ERROR_OUT(-7406, done);
  10577. }
  10578. #else
  10579. if (ret != ASN_CRIT_EXT_E) {
  10580. ERROR_OUT(-7407, done);
  10581. }
  10582. ret = 0;
  10583. #endif
  10584. done:
  10585. FreeDecodedCert(&cert);
  10586. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10587. #endif /* !NO_FILESYSTEM */
  10588. if (ret == 0)
  10589. ret = cert_asn1_test();
  10590. return ret;
  10591. }
  10592. #endif /* WOLFSSL_TEST_CERT */
  10593. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  10594. !defined(NO_FILESYSTEM)
  10595. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  10596. {
  10597. DecodedCert cert;
  10598. byte* tmp;
  10599. size_t bytes;
  10600. XFILE file;
  10601. int ret;
  10602. /* created from rsa_test : othercert.der */
  10603. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10604. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10605. /* created from rsa_test : othercert.der */
  10606. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  10607. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  10608. #ifdef HAVE_ECC
  10609. /* created from ecc_test_cert_gen : certecc.der */
  10610. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10611. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  10612. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  10613. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  10614. #else
  10615. /* Authority key id from ./certs/ca-ecc-cert.pem */
  10616. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  10617. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  10618. #endif
  10619. #endif /* HAVE_ECC */
  10620. /* created from rsa_test : cert.der */
  10621. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  10622. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  10623. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10624. if (tmp == NULL)
  10625. return -7500;
  10626. /* load othercert.der (Cert signed by an authority) */
  10627. file = XFOPEN(otherCertDerFile, "rb");
  10628. if (!file) {
  10629. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10630. return -7501;
  10631. }
  10632. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10633. XFCLOSE(file);
  10634. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10635. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10636. if (ret != 0)
  10637. return -7502;
  10638. /* check the SKID from a RSA certificate */
  10639. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10640. return -7503;
  10641. /* check the AKID from an RSA certificate */
  10642. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10643. return -7504;
  10644. /* check the Key Usage from an RSA certificate */
  10645. if (!cert.extKeyUsageSet)
  10646. return -7505;
  10647. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  10648. return -7506;
  10649. /* check the CA Basic Constraints from an RSA certificate */
  10650. if (cert.isCA)
  10651. return -7507;
  10652. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10653. /* check the Certificate Policies Id */
  10654. if (cert.extCertPoliciesNb != 1)
  10655. return -7508;
  10656. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10657. return -7509;
  10658. #endif
  10659. FreeDecodedCert(&cert);
  10660. #ifdef HAVE_ECC
  10661. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  10662. file = XFOPEN(certEccDerFile, "rb");
  10663. if (!file) {
  10664. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10665. return -7510;
  10666. }
  10667. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10668. XFCLOSE(file);
  10669. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10670. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10671. if (ret != 0)
  10672. return -7511;
  10673. /* check the SKID from a ECC certificate - generated dynamically */
  10674. /* check the AKID from an ECC certificate */
  10675. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10676. return -7512;
  10677. /* check the Key Usage from an ECC certificate */
  10678. if (!cert.extKeyUsageSet)
  10679. return -7513;
  10680. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  10681. return -7514;
  10682. /* check the CA Basic Constraints from an ECC certificate */
  10683. if (cert.isCA)
  10684. return -7515;
  10685. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10686. /* check the Certificate Policies Id */
  10687. if (cert.extCertPoliciesNb != 2)
  10688. return -7516;
  10689. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  10690. return -7517;
  10691. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  10692. return -7518;
  10693. #endif
  10694. FreeDecodedCert(&cert);
  10695. #endif /* HAVE_ECC */
  10696. /* load cert.der (self signed certificate) */
  10697. file = XFOPEN(certDerFile, "rb");
  10698. if (!file) {
  10699. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10700. return -7519;
  10701. }
  10702. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  10703. XFCLOSE(file);
  10704. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  10705. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  10706. if (ret != 0)
  10707. return -7520;
  10708. /* check the SKID from a CA certificate */
  10709. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  10710. return -7521;
  10711. /* check the AKID from an CA certificate */
  10712. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  10713. return -7522;
  10714. /* check the Key Usage from CA certificate */
  10715. if (!cert.extKeyUsageSet)
  10716. return -7523;
  10717. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  10718. return -7524;
  10719. /* check the CA Basic Constraints CA certificate */
  10720. if (!cert.isCA)
  10721. return -7525;
  10722. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  10723. /* check the Certificate Policies Id */
  10724. if (cert.extCertPoliciesNb != 2)
  10725. return -7526;
  10726. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  10727. return -7527;
  10728. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  10729. return -7528;
  10730. #endif
  10731. FreeDecodedCert(&cert);
  10732. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10733. return 0;
  10734. }
  10735. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  10736. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  10737. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  10738. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  10739. {
  10740. int ret = 0;
  10741. Cert cert;
  10742. FILE* file;
  10743. byte* der;
  10744. word32 derSz;
  10745. derSz = FOURK_BUF;
  10746. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10747. if (der == NULL)
  10748. ret = -7600;
  10749. if (ret == 0) {
  10750. /* load cert.der */
  10751. file = XFOPEN(certDerFile, "rb");
  10752. if (file != NULL) {
  10753. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  10754. XFCLOSE(file);
  10755. }
  10756. else
  10757. ret = -7601;
  10758. }
  10759. if (ret == 0) {
  10760. if (wc_InitCert(&cert)) {
  10761. ret = -7602;
  10762. }
  10763. }
  10764. if (ret == 0) {
  10765. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  10766. }
  10767. if (ret == 0) {
  10768. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10769. ret = -7603;
  10770. }
  10771. if (ret == 0) {
  10772. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  10773. ret = -7604;
  10774. }
  10775. if (ret == 0) {
  10776. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  10777. ret = -7605;
  10778. }
  10779. if (ret == 0) {
  10780. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  10781. ret = -7606;
  10782. }
  10783. if (ret == 0) {
  10784. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10785. ret = -7607;
  10786. }
  10787. if (ret == 0) {
  10788. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  10789. ret = -7608;
  10790. }
  10791. if (ret == 0) {
  10792. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  10793. ret = -7609;
  10794. }
  10795. #ifdef WOLFSSL_ALT_NAMES
  10796. if (ret == 0) {
  10797. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  10798. ret = -7610;
  10799. }
  10800. if (ret == 0) {
  10801. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10802. ret = -7611;
  10803. }
  10804. if (ret == 0) {
  10805. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  10806. ret = -7612;
  10807. }
  10808. if (ret == 0) {
  10809. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  10810. ret = -7613;
  10811. }
  10812. #endif
  10813. if (ret == 0) {
  10814. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  10815. ret = -7614;
  10816. }
  10817. if (ret == 0) {
  10818. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  10819. ret = -7615;
  10820. }
  10821. wc_SetCert_Free(&cert);
  10822. if (ret == 0) {
  10823. if(cert.decodedCert != NULL)
  10824. ret = -7616;
  10825. }
  10826. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  10827. return ret;
  10828. }
  10829. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  10830. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  10831. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  10832. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  10833. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  10834. static int rsa_flatten_test(RsaKey* key)
  10835. {
  10836. int ret;
  10837. byte e[RSA_TEST_BYTES];
  10838. byte n[RSA_TEST_BYTES];
  10839. word32 eSz = sizeof(e);
  10840. word32 nSz = sizeof(n);
  10841. /* Parameter Validation testing. */
  10842. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  10843. #ifdef HAVE_USER_RSA
  10844. /* Implementation using IPP Libraries returns:
  10845. * -101 = USER_CRYPTO_ERROR
  10846. */
  10847. if (ret == 0)
  10848. #else
  10849. if (ret != BAD_FUNC_ARG)
  10850. #endif
  10851. return -7620;
  10852. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  10853. #ifdef HAVE_USER_RSA
  10854. /* Implementation using IPP Libraries returns:
  10855. * -101 = USER_CRYPTO_ERROR
  10856. */
  10857. if (ret == 0)
  10858. #else
  10859. if (ret != BAD_FUNC_ARG)
  10860. #endif
  10861. return -7621;
  10862. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  10863. #ifdef HAVE_USER_RSA
  10864. /* Implementation using IPP Libraries returns:
  10865. * -101 = USER_CRYPTO_ERROR
  10866. */
  10867. if (ret == 0)
  10868. #else
  10869. if (ret != BAD_FUNC_ARG)
  10870. #endif
  10871. return -7622;
  10872. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  10873. #ifdef HAVE_USER_RSA
  10874. /* Implementation using IPP Libraries returns:
  10875. * -101 = USER_CRYPTO_ERROR
  10876. */
  10877. if (ret == 0)
  10878. #else
  10879. if (ret != BAD_FUNC_ARG)
  10880. #endif
  10881. return -7623;
  10882. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  10883. #ifdef HAVE_USER_RSA
  10884. /* Implementation using IPP Libraries returns:
  10885. * -101 = USER_CRYPTO_ERROR
  10886. */
  10887. if (ret == 0)
  10888. #else
  10889. if (ret != BAD_FUNC_ARG)
  10890. #endif
  10891. return -7624;
  10892. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10893. if (ret != 0)
  10894. return -7625;
  10895. eSz = 0;
  10896. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10897. #ifdef HAVE_USER_RSA
  10898. /* Implementation using IPP Libraries returns:
  10899. * -101 = USER_CRYPTO_ERROR
  10900. */
  10901. if (ret == 0)
  10902. #elif defined(HAVE_FIPS) && \
  10903. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  10904. if (ret != 0)
  10905. #else
  10906. if (ret != RSA_BUFFER_E)
  10907. #endif
  10908. return -7626;
  10909. eSz = sizeof(e);
  10910. nSz = 0;
  10911. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  10912. #ifdef HAVE_USER_RSA
  10913. /* Implementation using IPP Libraries returns:
  10914. * -101 = USER_CRYPTO_ERROR
  10915. */
  10916. if (ret == 0)
  10917. #else
  10918. if (ret != RSA_BUFFER_E)
  10919. #endif
  10920. return -7627;
  10921. return 0;
  10922. }
  10923. #endif /* NO_ASN */
  10924. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  10925. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  10926. static int rsa_export_key_test(RsaKey* key)
  10927. {
  10928. int ret;
  10929. byte e[3];
  10930. word32 eSz = sizeof(e);
  10931. byte n[RSA_TEST_BYTES];
  10932. word32 nSz = sizeof(n);
  10933. byte d[RSA_TEST_BYTES];
  10934. word32 dSz = sizeof(d);
  10935. byte p[RSA_TEST_BYTES/2];
  10936. word32 pSz = sizeof(p);
  10937. byte q[RSA_TEST_BYTES/2];
  10938. word32 qSz = sizeof(q);
  10939. word32 zero = 0;
  10940. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10941. if (ret != BAD_FUNC_ARG)
  10942. return -7630;
  10943. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10944. if (ret != BAD_FUNC_ARG)
  10945. return -7631;
  10946. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10947. if (ret != BAD_FUNC_ARG)
  10948. return -7632;
  10949. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10950. if (ret != BAD_FUNC_ARG)
  10951. return -7633;
  10952. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  10953. if (ret != BAD_FUNC_ARG)
  10954. return -7634;
  10955. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  10956. if (ret != BAD_FUNC_ARG)
  10957. return -7635;
  10958. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  10959. if (ret != BAD_FUNC_ARG)
  10960. return -7636;
  10961. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  10962. if (ret != BAD_FUNC_ARG)
  10963. return -7637;
  10964. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  10965. if (ret != BAD_FUNC_ARG)
  10966. return -7638;
  10967. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  10968. if (ret != BAD_FUNC_ARG)
  10969. return -7639;
  10970. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  10971. if (ret != BAD_FUNC_ARG)
  10972. return -7640;
  10973. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10974. if (ret != RSA_BUFFER_E)
  10975. return -7641;
  10976. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  10977. if (ret != RSA_BUFFER_E)
  10978. return -7642;
  10979. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  10980. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  10981. if (ret != RSA_BUFFER_E)
  10982. return -7643;
  10983. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  10984. if (ret != RSA_BUFFER_E)
  10985. return -7644;
  10986. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  10987. if (ret != RSA_BUFFER_E)
  10988. return -7645;
  10989. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  10990. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  10991. if (ret != 0)
  10992. return -7646;
  10993. return 0;
  10994. }
  10995. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  10996. #ifndef NO_SIG_WRAPPER
  10997. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  10998. {
  10999. int ret;
  11000. word32 sigSz;
  11001. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  11002. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  11003. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  11004. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  11005. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  11006. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  11007. };
  11008. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  11009. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  11010. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  11011. 0x00, 0x04, 0x20,
  11012. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  11013. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  11014. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  11015. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  11016. };
  11017. word32 inLen = (word32)XSTRLEN((char*)in);
  11018. byte out[RSA_TEST_BYTES];
  11019. /* Parameter Validation testing. */
  11020. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  11021. if (ret != BAD_FUNC_ARG)
  11022. return -7650;
  11023. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  11024. if (ret != BAD_FUNC_ARG)
  11025. return -7651;
  11026. sigSz = (word32)modLen;
  11027. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  11028. inLen, out, &sigSz, key, keyLen, rng);
  11029. if (ret != BAD_FUNC_ARG)
  11030. return -7652;
  11031. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11032. 0, out, &sigSz, key, keyLen, rng);
  11033. if (ret != BAD_FUNC_ARG)
  11034. return -7653;
  11035. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11036. inLen, NULL, &sigSz, key, keyLen, rng);
  11037. if (ret != BAD_FUNC_ARG)
  11038. return -7654;
  11039. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11040. inLen, out, NULL, key, keyLen, rng);
  11041. if (ret != BAD_FUNC_ARG)
  11042. return -7655;
  11043. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11044. inLen, out, &sigSz, NULL, keyLen, rng);
  11045. if (ret != BAD_FUNC_ARG)
  11046. return -7656;
  11047. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11048. inLen, out, &sigSz, key, 0, rng);
  11049. if (ret != BAD_FUNC_ARG)
  11050. return -7657;
  11051. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11052. inLen, out, &sigSz, key, keyLen, NULL);
  11053. #ifdef HAVE_USER_RSA
  11054. /* Implementation using IPP Libraries returns:
  11055. * -101 = USER_CRYPTO_ERROR
  11056. */
  11057. if (ret == 0)
  11058. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  11059. /* blinding / rng handled with hardware acceleration */
  11060. if (ret != 0)
  11061. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  11062. /* async may not require RNG */
  11063. if (ret != 0 && ret != MISSING_RNG_E)
  11064. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  11065. /* FIPS140 implementation does not do blinding */
  11066. if (ret != 0)
  11067. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  11068. if (ret != SIG_TYPE_E)
  11069. #elif defined(WOLFSSL_CRYPTOCELL)
  11070. /* RNG is handled with the cryptocell */
  11071. if (ret != 0)
  11072. #else
  11073. if (ret != MISSING_RNG_E)
  11074. #endif
  11075. return -7658;
  11076. sigSz = 0;
  11077. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11078. inLen, out, &sigSz, key, keyLen, rng);
  11079. if (ret != BAD_FUNC_ARG)
  11080. return -7659;
  11081. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  11082. inLen, out, (word32)modLen, key, keyLen);
  11083. if (ret != BAD_FUNC_ARG)
  11084. return -7660;
  11085. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11086. 0, out, (word32)modLen, key, keyLen);
  11087. if (ret != BAD_FUNC_ARG)
  11088. return -7661;
  11089. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11090. inLen, NULL, (word32)modLen, key, keyLen);
  11091. if (ret != BAD_FUNC_ARG)
  11092. return -7662;
  11093. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11094. inLen, out, 0, key, keyLen);
  11095. if (ret != BAD_FUNC_ARG)
  11096. return -7663;
  11097. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11098. inLen, out, (word32)modLen, NULL, keyLen);
  11099. if (ret != BAD_FUNC_ARG)
  11100. return -7664;
  11101. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11102. inLen, out, (word32)modLen, key, 0);
  11103. if (ret != BAD_FUNC_ARG)
  11104. return -7665;
  11105. #ifndef HAVE_ECC
  11106. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  11107. if (ret != SIG_TYPE_E)
  11108. return -7666;
  11109. #endif
  11110. /* Use APIs. */
  11111. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  11112. if (ret != modLen)
  11113. return -7667;
  11114. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  11115. if (ret != modLen)
  11116. return -7668;
  11117. sigSz = (word32)ret;
  11118. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11119. XMEMSET(out, 0, sizeof(out));
  11120. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11121. inLen, out, &sigSz, key, keyLen, rng);
  11122. if (ret != 0)
  11123. return -7669;
  11124. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11125. inLen, out, (word32)modLen, key, keyLen);
  11126. if (ret != 0)
  11127. return -7670;
  11128. sigSz = (word32)sizeof(out);
  11129. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11130. in, inLen, out, &sigSz, key, keyLen, rng);
  11131. if (ret != 0)
  11132. return -7671;
  11133. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11134. in, inLen, out, (word32)modLen, key, keyLen);
  11135. if (ret != 0)
  11136. return -7672;
  11137. /* Wrong signature type. */
  11138. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  11139. inLen, out, (word32)modLen, key, keyLen);
  11140. if (ret == 0)
  11141. return -7673;
  11142. /* check hash functions */
  11143. sigSz = (word32)sizeof(out);
  11144. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11145. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  11146. if (ret != 0)
  11147. return -7674;
  11148. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  11149. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  11150. if (ret != 0)
  11151. return -7675;
  11152. sigSz = (word32)sizeof(out);
  11153. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11154. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  11155. if (ret != 0)
  11156. return -7676;
  11157. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  11158. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  11159. if (ret != 0)
  11160. return -7677;
  11161. #else
  11162. (void)hash;
  11163. (void)hashEnc;
  11164. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  11165. return 0;
  11166. }
  11167. #endif /* !NO_SIG_WRAPPER */
  11168. #ifdef WC_RSA_NONBLOCK
  11169. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  11170. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  11171. {
  11172. int ret = 0, count;
  11173. int signSz = 0;
  11174. RsaNb nb;
  11175. byte* inlinePlain = NULL;
  11176. /* Enable non-blocking RSA mode - provide context */
  11177. ret = wc_RsaSetNonBlock(key, &nb);
  11178. if (ret != 0)
  11179. return ret;
  11180. #ifdef WC_RSA_NONBLOCK_TIME
  11181. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  11182. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  11183. if (ret != 0)
  11184. return ret;
  11185. #endif
  11186. count = 0;
  11187. do {
  11188. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  11189. count++; /* track number of would blocks */
  11190. if (ret == FP_WOULDBLOCK) {
  11191. /* do "other" work here */
  11192. }
  11193. } while (ret == FP_WOULDBLOCK);
  11194. if (ret < 0) {
  11195. return ret;
  11196. }
  11197. #ifdef DEBUG_WOLFSSL
  11198. printf("RSA non-block sign: %d times\n", count);
  11199. #endif
  11200. signSz = ret;
  11201. /* Test non-blocking verify */
  11202. XMEMSET(plain, 0, plainSz);
  11203. count = 0;
  11204. do {
  11205. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  11206. count++; /* track number of would blocks */
  11207. if (ret == FP_WOULDBLOCK) {
  11208. /* do "other" work here */
  11209. }
  11210. } while (ret == FP_WOULDBLOCK);
  11211. if (ret < 0) {
  11212. return ret;
  11213. }
  11214. #ifdef DEBUG_WOLFSSL
  11215. printf("RSA non-block verify: %d times\n", count);
  11216. #endif
  11217. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  11218. return SIG_VERIFY_E;
  11219. }
  11220. /* Test inline non-blocking verify */
  11221. count = 0;
  11222. do {
  11223. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  11224. count++; /* track number of would blocks */
  11225. if (ret == FP_WOULDBLOCK) {
  11226. /* do "other" work here */
  11227. }
  11228. } while (ret == FP_WOULDBLOCK);
  11229. if (ret < 0) {
  11230. return ret;
  11231. }
  11232. #ifdef DEBUG_WOLFSSL
  11233. printf("RSA non-block inline verify: %d times\n", count);
  11234. #endif
  11235. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  11236. return SIG_VERIFY_E;
  11237. }
  11238. /* Disabling non-block RSA mode */
  11239. ret = wc_RsaSetNonBlock(key, NULL);
  11240. (void)count;
  11241. return 0;
  11242. }
  11243. #endif
  11244. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  11245. static int rsa_decode_test(RsaKey* keyPub)
  11246. {
  11247. int ret;
  11248. word32 inSz;
  11249. word32 inOutIdx;
  11250. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  11251. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  11252. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  11253. 0x03 };
  11254. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11255. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11256. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00,
  11257. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11258. 0x02, 0x1, 0x03 };
  11259. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00,
  11260. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  11261. 0x02, 0x1, 0x03 };
  11262. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11263. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11264. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  11265. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11266. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03,
  11267. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11268. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  11269. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  11270. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11271. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1,
  11272. 0x03 };
  11273. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1,
  11274. 0x03 };
  11275. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1,
  11276. 0x03 };
  11277. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00,
  11278. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  11279. ret = wc_InitRsaKey(keyPub, NULL);
  11280. if (ret != 0)
  11281. return -7690;
  11282. /* Parameter Validation testing. */
  11283. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  11284. if (ret != BAD_FUNC_ARG) {
  11285. ret = -7691;
  11286. goto done;
  11287. }
  11288. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  11289. if (ret != BAD_FUNC_ARG) {
  11290. ret = -7692;
  11291. goto done;
  11292. }
  11293. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  11294. if (ret != BAD_FUNC_ARG) {
  11295. ret = -7693;
  11296. goto done;
  11297. }
  11298. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  11299. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11300. if (ret != 0) {
  11301. #else
  11302. if (ret != ASN_GETINT_E) {
  11303. #endif
  11304. ret = -7694;
  11305. goto done;
  11306. }
  11307. wc_FreeRsaKey(keyPub);
  11308. ret = wc_InitRsaKey(keyPub, NULL);
  11309. if (ret != 0)
  11310. return -7695;
  11311. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  11312. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  11313. if (ret != 0) {
  11314. #else
  11315. if (ret != ASN_GETINT_E) {
  11316. #endif
  11317. ret = -7696;
  11318. goto done;
  11319. }
  11320. wc_FreeRsaKey(keyPub);
  11321. ret = wc_InitRsaKey(keyPub, NULL);
  11322. if (ret != 0)
  11323. return -7697;
  11324. /* Use API. */
  11325. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  11326. if (ret != 0) {
  11327. ret = -7698;
  11328. goto done;
  11329. }
  11330. wc_FreeRsaKey(keyPub);
  11331. ret = wc_InitRsaKey(keyPub, NULL);
  11332. if (ret != 0)
  11333. return -7699;
  11334. /* Parameter Validation testing. */
  11335. inSz = sizeof(good);
  11336. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  11337. if (ret != BAD_FUNC_ARG) {
  11338. ret = -7700;
  11339. goto done;
  11340. }
  11341. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  11342. if (ret != BAD_FUNC_ARG) {
  11343. ret = -7701;
  11344. goto done;
  11345. }
  11346. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  11347. if (ret != BAD_FUNC_ARG) {
  11348. ret = -7702;
  11349. goto done;
  11350. }
  11351. /* Use good data and offset to bad data. */
  11352. inOutIdx = 2;
  11353. inSz = sizeof(good) - inOutIdx;
  11354. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11355. if (ret != ASN_PARSE_E) {
  11356. ret = -7703;
  11357. goto done;
  11358. }
  11359. inOutIdx = 2;
  11360. inSz = sizeof(goodAlgId) - inOutIdx;
  11361. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11362. if (ret != ASN_PARSE_E) {
  11363. ret = -7704;
  11364. goto done;
  11365. }
  11366. inOutIdx = 2;
  11367. inSz = sizeof(goodAlgId);
  11368. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11369. #ifndef WOLFSSL_NO_DECODE_EXTRA
  11370. if (ret != ASN_PARSE_E)
  11371. #else
  11372. if (ret != ASN_RSA_KEY_E)
  11373. #endif
  11374. {
  11375. ret = -7705;
  11376. goto done;
  11377. }
  11378. /* Try different bad data. */
  11379. inSz = sizeof(badAlgIdNull);
  11380. inOutIdx = 0;
  11381. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  11382. if (ret != ASN_EXPECT_0_E) {
  11383. ret = -7706;
  11384. goto done;
  11385. }
  11386. inSz = sizeof(badNotBitString);
  11387. inOutIdx = 0;
  11388. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  11389. if (ret != ASN_BITSTR_E) {
  11390. ret = -7707;
  11391. goto done;
  11392. }
  11393. inSz = sizeof(badBitStringLen);
  11394. inOutIdx = 0;
  11395. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  11396. if (ret != ASN_PARSE_E) {
  11397. ret = -7708;
  11398. goto done;
  11399. }
  11400. inSz = sizeof(badNoSeq);
  11401. inOutIdx = 0;
  11402. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  11403. if (ret != ASN_PARSE_E) {
  11404. ret = -7709;
  11405. goto done;
  11406. }
  11407. inSz = sizeof(badNoObj);
  11408. inOutIdx = 0;
  11409. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  11410. if (ret != ASN_PARSE_E) {
  11411. ret = -7710;
  11412. goto done;
  11413. }
  11414. inSz = sizeof(badIntN);
  11415. inOutIdx = 0;
  11416. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  11417. if (ret != ASN_RSA_KEY_E) {
  11418. ret = -7711;
  11419. goto done;
  11420. }
  11421. inSz = sizeof(badNotIntE);
  11422. inOutIdx = 0;
  11423. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  11424. if (ret != ASN_RSA_KEY_E) {
  11425. ret = -7712;
  11426. goto done;
  11427. }
  11428. /* TODO: Shouldn't pass as the sequence length is too small. */
  11429. inSz = sizeof(badLength);
  11430. inOutIdx = 0;
  11431. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  11432. if (ret != 0) {
  11433. ret = -7713;
  11434. goto done;
  11435. }
  11436. /* TODO: Shouldn't ignore object id's data. */
  11437. wc_FreeRsaKey(keyPub);
  11438. ret = wc_InitRsaKey(keyPub, NULL);
  11439. if (ret != 0)
  11440. return -7714;
  11441. inSz = sizeof(badBitStrNoZero);
  11442. inOutIdx = 0;
  11443. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  11444. if (ret != ASN_EXPECT_0_E) {
  11445. ret = -7715;
  11446. goto done;
  11447. }
  11448. wc_FreeRsaKey(keyPub);
  11449. ret = wc_InitRsaKey(keyPub, NULL);
  11450. if (ret != 0)
  11451. return -7716;
  11452. /* Valid data cases. */
  11453. inSz = sizeof(good);
  11454. inOutIdx = 0;
  11455. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  11456. if (ret != 0) {
  11457. ret = -7717;
  11458. goto done;
  11459. }
  11460. if (inOutIdx != inSz) {
  11461. ret = -7718;
  11462. goto done;
  11463. }
  11464. wc_FreeRsaKey(keyPub);
  11465. ret = wc_InitRsaKey(keyPub, NULL);
  11466. if (ret != 0)
  11467. return -7719;
  11468. inSz = sizeof(goodAlgId);
  11469. inOutIdx = 0;
  11470. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  11471. if (ret != 0) {
  11472. ret = -7720;
  11473. goto done;
  11474. }
  11475. if (inOutIdx != inSz) {
  11476. ret = -7721;
  11477. goto done;
  11478. }
  11479. wc_FreeRsaKey(keyPub);
  11480. ret = wc_InitRsaKey(keyPub, NULL);
  11481. if (ret != 0)
  11482. return -7722;
  11483. inSz = sizeof(goodAlgIdNull);
  11484. inOutIdx = 0;
  11485. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  11486. if (ret != 0) {
  11487. ret = -7723;
  11488. goto done;
  11489. }
  11490. if (inOutIdx != inSz) {
  11491. ret = -7724;
  11492. goto done;
  11493. }
  11494. done:
  11495. wc_FreeRsaKey(keyPub);
  11496. return ret;
  11497. }
  11498. #endif
  11499. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  11500. /* Need to create known good signatures to test with this. */
  11501. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  11502. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  11503. {
  11504. byte digest[WC_MAX_DIGEST_SIZE];
  11505. int ret = 0;
  11506. const char inStr[] = TEST_STRING;
  11507. word32 inLen = (word32)TEST_STRING_SZ;
  11508. word32 outSz;
  11509. word32 plainSz;
  11510. word32 digestSz;
  11511. int i, j;
  11512. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11513. int k, l;
  11514. #endif
  11515. int len;
  11516. byte* plain;
  11517. int mgf[] = {
  11518. #ifndef NO_SHA
  11519. WC_MGF1SHA1,
  11520. #endif
  11521. #ifdef WOLFSSL_SHA224
  11522. WC_MGF1SHA224,
  11523. #endif
  11524. WC_MGF1SHA256,
  11525. #ifdef WOLFSSL_SHA384
  11526. WC_MGF1SHA384,
  11527. #endif
  11528. #ifdef WOLFSSL_SHA512
  11529. WC_MGF1SHA512
  11530. #endif
  11531. };
  11532. enum wc_HashType hash[] = {
  11533. #ifndef NO_SHA
  11534. WC_HASH_TYPE_SHA,
  11535. #endif
  11536. #ifdef WOLFSSL_SHA224
  11537. WC_HASH_TYPE_SHA224,
  11538. #endif
  11539. WC_HASH_TYPE_SHA256,
  11540. #ifdef WOLFSSL_SHA384
  11541. WC_HASH_TYPE_SHA384,
  11542. #endif
  11543. #ifdef WOLFSSL_SHA512
  11544. WC_HASH_TYPE_SHA512,
  11545. #endif
  11546. };
  11547. DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  11548. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11549. DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  11550. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11551. if (in == NULL || out == NULL || sig == NULL)
  11552. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  11553. #endif
  11554. XMEMCPY(in, inStr, inLen);
  11555. /* Test all combinations of hash and MGF. */
  11556. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  11557. /* Calculate hash of message. */
  11558. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  11559. if (ret != 0)
  11560. ERROR_OUT(-7730, exit_rsa_pss);
  11561. digestSz = wc_HashGetDigestSize(hash[j]);
  11562. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  11563. outSz = RSA_TEST_BYTES;
  11564. do {
  11565. #if defined(WOLFSSL_ASYNC_CRYPT)
  11566. ret = wc_AsyncWait(ret, &key->asyncDev,
  11567. WC_ASYNC_FLAG_CALL_AGAIN);
  11568. #endif
  11569. if (ret >= 0) {
  11570. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  11571. hash[j], mgf[i], -1, key, rng);
  11572. }
  11573. } while (ret == WC_PENDING_E);
  11574. if (ret <= 0)
  11575. ERROR_OUT(-7731, exit_rsa_pss);
  11576. outSz = ret;
  11577. XMEMCPY(sig, out, outSz);
  11578. plain = NULL;
  11579. TEST_SLEEP();
  11580. do {
  11581. #if defined(WOLFSSL_ASYNC_CRYPT)
  11582. ret = wc_AsyncWait(ret, &key->asyncDev,
  11583. WC_ASYNC_FLAG_CALL_AGAIN);
  11584. #endif
  11585. if (ret >= 0) {
  11586. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  11587. mgf[i], -1, key);
  11588. }
  11589. } while (ret == WC_PENDING_E);
  11590. if (ret <= 0)
  11591. ERROR_OUT(-7732, exit_rsa_pss);
  11592. plainSz = ret;
  11593. TEST_SLEEP();
  11594. #if defined(HAVE_SELFTEST) && \
  11595. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11596. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11597. hash[j], -1);
  11598. #else
  11599. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  11600. hash[j], -1, wc_RsaEncryptSize(key)*8);
  11601. #endif
  11602. if (ret != 0)
  11603. ERROR_OUT(-7733, exit_rsa_pss);
  11604. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  11605. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  11606. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  11607. if (i == k && j == l)
  11608. continue;
  11609. XMEMCPY(sig, out, outSz);
  11610. do {
  11611. #if defined(WOLFSSL_ASYNC_CRYPT)
  11612. ret = wc_AsyncWait(ret, &key->asyncDev,
  11613. WC_ASYNC_FLAG_CALL_AGAIN);
  11614. #endif
  11615. if (ret >= 0) {
  11616. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  11617. (byte**)&plain, hash[l], mgf[k], -1, key);
  11618. }
  11619. } while (ret == WC_PENDING_E);
  11620. if (ret >= 0)
  11621. ERROR_OUT(-7734, exit_rsa_pss);
  11622. }
  11623. }
  11624. #endif
  11625. }
  11626. }
  11627. /* Test that a salt length of zero works. */
  11628. digestSz = wc_HashGetDigestSize(hash[0]);
  11629. outSz = RSA_TEST_BYTES;
  11630. do {
  11631. #if defined(WOLFSSL_ASYNC_CRYPT)
  11632. ret = wc_AsyncWait(ret, &key->asyncDev,
  11633. WC_ASYNC_FLAG_CALL_AGAIN);
  11634. #endif
  11635. if (ret >= 0) {
  11636. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11637. mgf[0], 0, key, rng);
  11638. }
  11639. } while (ret == WC_PENDING_E);
  11640. if (ret <= 0)
  11641. ERROR_OUT(-7735, exit_rsa_pss);
  11642. outSz = ret;
  11643. TEST_SLEEP();
  11644. do {
  11645. #if defined(WOLFSSL_ASYNC_CRYPT)
  11646. ret = wc_AsyncWait(ret, &key->asyncDev,
  11647. WC_ASYNC_FLAG_CALL_AGAIN);
  11648. #endif
  11649. if (ret >= 0) {
  11650. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  11651. 0, key);
  11652. }
  11653. } while (ret == WC_PENDING_E);
  11654. if (ret <= 0)
  11655. ERROR_OUT(-7736, exit_rsa_pss);
  11656. plainSz = ret;
  11657. TEST_SLEEP();
  11658. do {
  11659. #if defined(WOLFSSL_ASYNC_CRYPT)
  11660. ret = wc_AsyncWait(ret, &key->asyncDev,
  11661. WC_ASYNC_FLAG_CALL_AGAIN);
  11662. #endif
  11663. if (ret >= 0) {
  11664. #if defined(HAVE_SELFTEST) && \
  11665. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11666. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11667. hash[0], 0);
  11668. #else
  11669. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  11670. hash[0], 0, 0);
  11671. #endif
  11672. }
  11673. } while (ret == WC_PENDING_E);
  11674. if (ret != 0)
  11675. ERROR_OUT(-7737, exit_rsa_pss);
  11676. XMEMCPY(sig, out, outSz);
  11677. plain = NULL;
  11678. do {
  11679. #if defined(WOLFSSL_ASYNC_CRYPT)
  11680. ret = wc_AsyncWait(ret, &key->asyncDev,
  11681. WC_ASYNC_FLAG_CALL_AGAIN);
  11682. #endif
  11683. if (ret >= 0) {
  11684. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11685. 0, key);
  11686. }
  11687. } while (ret == WC_PENDING_E);
  11688. if (ret <= 0)
  11689. ERROR_OUT(-7738, exit_rsa_pss);
  11690. plainSz = ret;
  11691. TEST_SLEEP();
  11692. #if defined(HAVE_SELFTEST) && \
  11693. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11694. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11695. 0);
  11696. #else
  11697. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11698. 0, 0);
  11699. #endif
  11700. if (ret != 0)
  11701. ERROR_OUT(-7739, exit_rsa_pss);
  11702. /* Test bad salt lengths in various APIs. */
  11703. digestSz = wc_HashGetDigestSize(hash[0]);
  11704. outSz = RSA_TEST_BYTES;
  11705. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11706. len = -2;
  11707. #else
  11708. len = -3;
  11709. #endif
  11710. do {
  11711. #if defined(WOLFSSL_ASYNC_CRYPT)
  11712. ret = wc_AsyncWait(ret, &key->asyncDev,
  11713. WC_ASYNC_FLAG_CALL_AGAIN);
  11714. #endif
  11715. if (ret >= 0) {
  11716. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11717. mgf[0], len, key, rng);
  11718. }
  11719. } while (ret == WC_PENDING_E);
  11720. if (ret != PSS_SALTLEN_E)
  11721. ERROR_OUT(-7740, exit_rsa_pss);
  11722. do {
  11723. #if defined(WOLFSSL_ASYNC_CRYPT)
  11724. ret = wc_AsyncWait(ret, &key->asyncDev,
  11725. WC_ASYNC_FLAG_CALL_AGAIN);
  11726. #endif
  11727. if (ret >= 0) {
  11728. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  11729. mgf[0], digestSz + 1, key, rng);
  11730. }
  11731. } while (ret == WC_PENDING_E);
  11732. if (ret != PSS_SALTLEN_E)
  11733. ERROR_OUT(-7741, exit_rsa_pss);
  11734. TEST_SLEEP();
  11735. do {
  11736. #if defined(WOLFSSL_ASYNC_CRYPT)
  11737. ret = wc_AsyncWait(ret, &key->asyncDev,
  11738. WC_ASYNC_FLAG_CALL_AGAIN);
  11739. #endif
  11740. if (ret >= 0) {
  11741. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  11742. mgf[0], -2, key);
  11743. }
  11744. } while (ret == WC_PENDING_E);
  11745. if (ret != PSS_SALTLEN_E)
  11746. ERROR_OUT(-7742, exit_rsa_pss);
  11747. TEST_SLEEP();
  11748. do {
  11749. #if defined(WOLFSSL_ASYNC_CRYPT)
  11750. ret = wc_AsyncWait(ret, &key->asyncDev,
  11751. WC_ASYNC_FLAG_CALL_AGAIN);
  11752. #endif
  11753. if (ret >= 0) {
  11754. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  11755. digestSz + 1, key);
  11756. }
  11757. } while (ret == WC_PENDING_E);
  11758. if (ret != PSS_SALTLEN_E)
  11759. ERROR_OUT(-7743, exit_rsa_pss);
  11760. TEST_SLEEP();
  11761. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  11762. len = -2;
  11763. #else
  11764. len = -3;
  11765. #endif
  11766. #if defined(HAVE_SELFTEST) && \
  11767. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11768. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11769. len);
  11770. #else
  11771. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11772. len, 0);
  11773. #endif
  11774. if (ret != PSS_SALTLEN_E)
  11775. ERROR_OUT(-7744, exit_rsa_pss);
  11776. #ifndef WOLFSSL_PSS_LONG_SALT
  11777. len = digestSz + 1;
  11778. #else
  11779. len = plainSz - digestSz - 1;
  11780. #endif
  11781. #if defined(HAVE_SELFTEST) && \
  11782. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  11783. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11784. len);
  11785. #else
  11786. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  11787. len, 0);
  11788. #endif
  11789. if (ret != PSS_SALTLEN_E)
  11790. ERROR_OUT(-7745, exit_rsa_pss);
  11791. ret = 0;
  11792. exit_rsa_pss:
  11793. FREE_VAR(sig, HEAP_HINT);
  11794. FREE_VAR(in, HEAP_HINT);
  11795. FREE_VAR(out, HEAP_HINT);
  11796. return ret;
  11797. }
  11798. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  11799. #endif
  11800. #ifdef WC_RSA_NO_PADDING
  11801. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  11802. {
  11803. WC_RNG rng;
  11804. byte* tmp = NULL;
  11805. size_t bytes;
  11806. int ret;
  11807. word32 inLen = 0;
  11808. word32 idx = 0;
  11809. word32 outSz = RSA_TEST_BYTES;
  11810. word32 plainSz = RSA_TEST_BYTES;
  11811. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11812. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  11813. !defined(NO_FILESYSTEM)
  11814. XFILE file;
  11815. #endif
  11816. DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  11817. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  11818. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  11819. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  11820. if (key == NULL || out == NULL || plain == NULL)
  11821. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  11822. #endif
  11823. /* initialize stack structures */
  11824. XMEMSET(&rng, 0, sizeof(rng));
  11825. XMEMSET(key, 0, sizeof(RsaKey));
  11826. #ifdef USE_CERT_BUFFERS_1024
  11827. bytes = (size_t)sizeof_client_key_der_1024;
  11828. if (bytes < (size_t)sizeof_client_cert_der_1024)
  11829. bytes = (size_t)sizeof_client_cert_der_1024;
  11830. #elif defined(USE_CERT_BUFFERS_2048)
  11831. bytes = (size_t)sizeof_client_key_der_2048;
  11832. if (bytes < (size_t)sizeof_client_cert_der_2048)
  11833. bytes = (size_t)sizeof_client_cert_der_2048;
  11834. #else
  11835. bytes = FOURK_BUF;
  11836. #endif
  11837. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11838. if (tmp == NULL
  11839. #ifdef WOLFSSL_ASYNC_CRYPT
  11840. || out == NULL || plain == NULL
  11841. #endif
  11842. ) {
  11843. ERROR_OUT(-7800, exit_rsa_nopadding);
  11844. }
  11845. #ifdef USE_CERT_BUFFERS_1024
  11846. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  11847. #elif defined(USE_CERT_BUFFERS_2048)
  11848. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  11849. #elif defined(USE_CERT_BUFFERS_3072)
  11850. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  11851. #elif defined(USE_CERT_BUFFERS_4096)
  11852. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  11853. #elif !defined(NO_FILESYSTEM)
  11854. file = XFOPEN(clientKey, "rb");
  11855. if (!file) {
  11856. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  11857. ERROR_OUT(-7801, exit_rsa_nopadding);
  11858. }
  11859. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11860. XFCLOSE(file);
  11861. #else
  11862. /* No key to use. */
  11863. ERROR_OUT(-7802, exit_rsa_nopadding);
  11864. #endif /* USE_CERT_BUFFERS */
  11865. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  11866. if (ret != 0) {
  11867. ERROR_OUT(-7803, exit_rsa_nopadding);
  11868. }
  11869. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  11870. if (ret != 0) {
  11871. ERROR_OUT(-7804, exit_rsa_nopadding);
  11872. }
  11873. /* after loading in key use tmp as the test buffer */
  11874. #ifndef HAVE_FIPS
  11875. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11876. #else
  11877. ret = wc_InitRng(&rng);
  11878. #endif
  11879. if (ret != 0) {
  11880. ERROR_OUT(-7805, exit_rsa_nopadding);
  11881. }
  11882. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11883. inLen = wc_RsaEncryptSize(key);
  11884. outSz = inLen;
  11885. plainSz = inLen;
  11886. XMEMSET(tmp, 7, inLen);
  11887. do {
  11888. #if defined(WOLFSSL_ASYNC_CRYPT)
  11889. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11890. #endif
  11891. if (ret >= 0) {
  11892. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  11893. RSA_PRIVATE_ENCRYPT, &rng);
  11894. }
  11895. } while (ret == WC_PENDING_E);
  11896. if (ret <= 0) {
  11897. ERROR_OUT(-7806, exit_rsa_nopadding);
  11898. }
  11899. /* encrypted result should not be the same as input */
  11900. if (XMEMCMP(out, tmp, inLen) == 0) {
  11901. ERROR_OUT(-7807, exit_rsa_nopadding);
  11902. }
  11903. TEST_SLEEP();
  11904. /* decrypt with public key and compare result */
  11905. do {
  11906. #if defined(WOLFSSL_ASYNC_CRYPT)
  11907. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11908. #endif
  11909. if (ret >= 0) {
  11910. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  11911. RSA_PUBLIC_DECRYPT, &rng);
  11912. }
  11913. } while (ret == WC_PENDING_E);
  11914. if (ret <= 0) {
  11915. ERROR_OUT(-7808, exit_rsa_nopadding);
  11916. }
  11917. if (XMEMCMP(plain, tmp, inLen) != 0) {
  11918. ERROR_OUT(-7809, exit_rsa_nopadding);
  11919. }
  11920. TEST_SLEEP();
  11921. #endif
  11922. #ifdef WC_RSA_BLINDING
  11923. ret = wc_RsaSetRNG(NULL, &rng);
  11924. if (ret != BAD_FUNC_ARG) {
  11925. ERROR_OUT(-7810, exit_rsa_nopadding);
  11926. }
  11927. ret = wc_RsaSetRNG(key, &rng);
  11928. if (ret < 0) {
  11929. ERROR_OUT(-7811, exit_rsa_nopadding);
  11930. }
  11931. #endif
  11932. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  11933. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  11934. do {
  11935. #if defined(WOLFSSL_ASYNC_CRYPT)
  11936. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11937. #endif
  11938. if (ret >= 0) {
  11939. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  11940. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  11941. }
  11942. } while (ret == WC_PENDING_E);
  11943. if (ret < 0) {
  11944. ERROR_OUT(-7812, exit_rsa_nopadding);
  11945. }
  11946. TEST_SLEEP();
  11947. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  11948. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11949. do {
  11950. #if defined(WOLFSSL_ASYNC_CRYPT)
  11951. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  11952. #endif
  11953. if (ret >= 0) {
  11954. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  11955. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  11956. }
  11957. } while (ret == WC_PENDING_E);
  11958. if (ret < 0) {
  11959. ERROR_OUT(-7813, exit_rsa_nopadding);
  11960. }
  11961. if (XMEMCMP(plain, tmp, inLen) != 0) {
  11962. ERROR_OUT(-7814, exit_rsa_nopadding);
  11963. }
  11964. TEST_SLEEP();
  11965. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  11966. /* test some bad arguments */
  11967. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  11968. &rng);
  11969. if (ret != BAD_FUNC_ARG) {
  11970. ERROR_OUT(-7815, exit_rsa_nopadding);
  11971. }
  11972. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  11973. &rng);
  11974. if (ret != BAD_FUNC_ARG) {
  11975. ERROR_OUT(-7816, exit_rsa_nopadding);
  11976. }
  11977. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  11978. &rng);
  11979. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  11980. ERROR_OUT(-7817, exit_rsa_nopadding);
  11981. }
  11982. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  11983. RSA_PUBLIC_DECRYPT, &rng);
  11984. if (ret != BAD_FUNC_ARG) {
  11985. ERROR_OUT(-7818, exit_rsa_nopadding);
  11986. }
  11987. /* if making it to this point of code without hitting an ERROR_OUT then
  11988. * all tests have passed */
  11989. ret = 0;
  11990. exit_rsa_nopadding:
  11991. wc_FreeRsaKey(key);
  11992. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11993. FREE_VAR(key, HEAP_HINT);
  11994. FREE_VAR(out, HEAP_HINT);
  11995. FREE_VAR(plain, HEAP_HINT);
  11996. wc_FreeRng(&rng);
  11997. return ret;
  11998. }
  11999. #endif /* WC_RSA_NO_PADDING */
  12000. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  12001. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  12002. {
  12003. byte* tmp = NULL;
  12004. size_t bytes;
  12005. int ret;
  12006. word32 inLen = 0;
  12007. #ifndef NO_ASN
  12008. word32 idx = 0;
  12009. #endif
  12010. word32 outSz = RSA_TEST_BYTES;
  12011. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12012. word32 plainSz = RSA_TEST_BYTES;
  12013. #endif
  12014. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  12015. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  12016. XFILE file;
  12017. #endif
  12018. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12019. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12020. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12021. #endif
  12022. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  12023. if (out == NULL
  12024. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12025. || plain == NULL
  12026. #endif
  12027. ) {
  12028. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  12029. }
  12030. #endif
  12031. #if defined(USE_CERT_BUFFERS_2048)
  12032. bytes = (size_t)sizeof_client_key_der_2048;
  12033. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12034. bytes = (size_t)sizeof_client_cert_der_2048;
  12035. #else
  12036. bytes = FOURK_BUF;
  12037. #endif
  12038. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12039. if (tmp == NULL
  12040. #ifdef WOLFSSL_ASYNC_CRYPT
  12041. || out == NULL || plain == NULL
  12042. #endif
  12043. ) {
  12044. ERROR_OUT(-7800, exit_rsa_even_mod);
  12045. }
  12046. #if defined(USE_CERT_BUFFERS_2048)
  12047. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12048. #elif defined(USE_CERT_BUFFERS_3072)
  12049. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12050. #elif defined(USE_CERT_BUFFERS_4096)
  12051. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12052. #elif !defined(NO_FILESYSTEM)
  12053. file = XFOPEN(clientKey, "rb");
  12054. if (!file) {
  12055. err_sys("can't open ./certs/client-key.der, "
  12056. "Please run from wolfSSL home dir", -40);
  12057. ERROR_OUT(-7801, exit_rsa_even_mod);
  12058. }
  12059. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12060. XFCLOSE(file);
  12061. #else
  12062. /* No key to use. */
  12063. ERROR_OUT(-7802, exit_rsa_even_mod);
  12064. #endif /* USE_CERT_BUFFERS */
  12065. #ifndef NO_ASN
  12066. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12067. if (ret != 0) {
  12068. ERROR_OUT(-7804, exit_rsa_even_mod);
  12069. }
  12070. #else
  12071. #ifdef USE_CERT_BUFFERS_2048
  12072. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  12073. if (ret != 0) {
  12074. ERROR_OUT(-7804, exit_rsa_even_mod);
  12075. }
  12076. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  12077. if (ret != 0) {
  12078. ERROR_OUT(-7804, exit_rsa_even_mod);
  12079. }
  12080. #ifndef NO_SIG_WRAPPER
  12081. modLen = 2048;
  12082. #endif
  12083. #else
  12084. #error Not supported yet!
  12085. #endif
  12086. #endif
  12087. key->n.dp[0] &= (mp_digit)-2;
  12088. if (ret != 0) {
  12089. ERROR_OUT(-7804, exit_rsa_even_mod);
  12090. }
  12091. /* after loading in key use tmp as the test buffer */
  12092. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  12093. inLen = 32;
  12094. outSz = wc_RsaEncryptSize(key);
  12095. XMEMSET(tmp, 7, plainSz);
  12096. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  12097. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12098. ERROR_OUT(-7806, exit_rsa_even_mod);
  12099. }
  12100. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  12101. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12102. ERROR_OUT(-7808, exit_rsa_even_mod);
  12103. }
  12104. #endif
  12105. #ifdef WC_RSA_BLINDING
  12106. ret = wc_RsaSetRNG(key, rng);
  12107. if (ret < 0) {
  12108. ERROR_OUT(-7811, exit_rsa_even_mod);
  12109. }
  12110. #endif
  12111. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  12112. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  12113. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  12114. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  12115. ERROR_OUT(-7812, exit_rsa_even_mod);
  12116. }
  12117. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  12118. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12119. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  12120. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  12121. ERROR_OUT(-7813, exit_rsa_even_mod);
  12122. }
  12123. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12124. /* if making it to this point of code without hitting an ERROR_OUT then
  12125. * all tests have passed */
  12126. ret = 0;
  12127. exit_rsa_even_mod:
  12128. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12129. FREE_VAR(out, HEAP_HINT);
  12130. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12131. FREE_VAR(plain, HEAP_HINT);
  12132. #endif
  12133. (void)out;
  12134. (void)outSz;
  12135. (void)plain;
  12136. (void)plainSz;
  12137. (void)inLen;
  12138. (void)rng;
  12139. return ret;
  12140. }
  12141. #endif /* WOLFSSL_HAVE_SP_RSA */
  12142. #ifdef WOLFSSL_CERT_GEN
  12143. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  12144. {
  12145. #ifdef WOLFSSL_SMALL_STACK
  12146. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12147. #ifdef WOLFSSL_TEST_CERT
  12148. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12149. #endif
  12150. #else
  12151. RsaKey caKey[1];
  12152. #ifdef WOLFSSL_TEST_CERT
  12153. DecodedCert decode[1];
  12154. #endif
  12155. #endif
  12156. byte* der = NULL;
  12157. int ret;
  12158. Cert* myCert = NULL;
  12159. int certSz;
  12160. size_t bytes3;
  12161. word32 idx3 = 0;
  12162. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12163. XFILE file3;
  12164. #endif
  12165. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  12166. struct tm beforeTime;
  12167. struct tm afterTime;
  12168. #endif
  12169. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  12170. (void)keypub;
  12171. #ifdef WOLFSSL_SMALL_STACK
  12172. if (caKey == NULL)
  12173. ERROR_OUT(MEMORY_E, exit_rsa);
  12174. #ifdef WOLFSSL_TEST_CERT
  12175. if (decode == NULL)
  12176. ERROR_OUT(MEMORY_E, exit_rsa);
  12177. #endif
  12178. #endif
  12179. XMEMSET(caKey, 0, sizeof *caKey);
  12180. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12181. if (der == NULL) {
  12182. ERROR_OUT(-7820, exit_rsa);
  12183. }
  12184. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12185. if (myCert == NULL) {
  12186. ERROR_OUT(-7821, exit_rsa);
  12187. }
  12188. /* self signed */
  12189. if (wc_InitCert(myCert)) {
  12190. ERROR_OUT(-7822, exit_rsa);
  12191. }
  12192. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12193. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  12194. myCert->serialSz = (int)sizeof(mySerial);
  12195. myCert->isCA = 1;
  12196. #ifndef NO_SHA256
  12197. myCert->sigType = CTC_SHA256wRSA;
  12198. #else
  12199. myCert->sigType = CTC_SHAwRSA;
  12200. #endif
  12201. #ifdef WOLFSSL_CERT_EXT
  12202. /* add Policies */
  12203. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12204. CTC_MAX_CERTPOL_SZ);
  12205. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  12206. CTC_MAX_CERTPOL_SZ);
  12207. myCert->certPoliciesNb = 2;
  12208. /* add SKID from the Public Key */
  12209. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12210. ERROR_OUT(-7823, exit_rsa);
  12211. }
  12212. /* add AKID from the Public Key */
  12213. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  12214. ERROR_OUT(-7824, exit_rsa);
  12215. }
  12216. /* add Key Usage */
  12217. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  12218. ERROR_OUT(-7825, exit_rsa);
  12219. }
  12220. #ifdef WOLFSSL_EKU_OID
  12221. {
  12222. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  12223. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  12224. HEAP_HINT) != 0) {
  12225. ERROR_OUT(-7826, exit_rsa);
  12226. }
  12227. }
  12228. #endif /* WOLFSSL_EKU_OID */
  12229. #endif /* WOLFSSL_CERT_EXT */
  12230. ret = 0;
  12231. do {
  12232. #if defined(WOLFSSL_ASYNC_CRYPT)
  12233. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12234. #endif
  12235. if (ret >= 0) {
  12236. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  12237. }
  12238. } while (ret == WC_PENDING_E);
  12239. if (ret < 0) {
  12240. ERROR_OUT(-7827, exit_rsa);
  12241. }
  12242. certSz = ret;
  12243. #ifdef WOLFSSL_TEST_CERT
  12244. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12245. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12246. if (ret != 0) {
  12247. FreeDecodedCert(decode);
  12248. ERROR_OUT(-7828, exit_rsa);
  12249. }
  12250. FreeDecodedCert(decode);
  12251. #endif
  12252. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  12253. CERT_TYPE, -5578);
  12254. if (ret != 0) {
  12255. goto exit_rsa;
  12256. }
  12257. /* Setup Certificate */
  12258. if (wc_InitCert(myCert)) {
  12259. ERROR_OUT(-7829, exit_rsa);
  12260. }
  12261. #ifdef WOLFSSL_ALT_NAMES
  12262. /* Get CA Cert for testing */
  12263. #ifdef USE_CERT_BUFFERS_1024
  12264. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  12265. bytes3 = sizeof_ca_cert_der_1024;
  12266. #elif defined(USE_CERT_BUFFERS_2048)
  12267. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  12268. bytes3 = sizeof_ca_cert_der_2048;
  12269. #else
  12270. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  12271. if (!file3) {
  12272. ERROR_OUT(-7830, exit_rsa);
  12273. }
  12274. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12275. XFCLOSE(file3);
  12276. #endif /* USE_CERT_BUFFERS */
  12277. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  12278. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  12279. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  12280. if (ret != 0) {
  12281. ERROR_OUT(-7831, exit_rsa);
  12282. }
  12283. #endif
  12284. /* get alt names from der */
  12285. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  12286. if (ret != 0) {
  12287. ERROR_OUT(-7832, exit_rsa);
  12288. }
  12289. /* get dates from der */
  12290. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  12291. if (ret != 0) {
  12292. ERROR_OUT(-7833, exit_rsa);
  12293. }
  12294. #ifndef NO_ASN_TIME
  12295. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  12296. if (ret < 0) {
  12297. ERROR_OUT(-7834, exit_rsa);
  12298. }
  12299. #endif
  12300. #endif /* WOLFSSL_ALT_NAMES */
  12301. /* Get CA Key */
  12302. #ifdef USE_CERT_BUFFERS_1024
  12303. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12304. bytes3 = sizeof_ca_key_der_1024;
  12305. #elif defined(USE_CERT_BUFFERS_2048)
  12306. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12307. bytes3 = sizeof_ca_key_der_2048;
  12308. #else
  12309. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12310. if (!file3) {
  12311. ERROR_OUT(-7835, exit_rsa);
  12312. }
  12313. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12314. XFCLOSE(file3);
  12315. #endif /* USE_CERT_BUFFERS */
  12316. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12317. if (ret != 0) {
  12318. ERROR_OUT(-7836, exit_rsa);
  12319. }
  12320. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12321. if (ret != 0) {
  12322. ERROR_OUT(-7837, exit_rsa);
  12323. }
  12324. #ifndef NO_SHA256
  12325. myCert->sigType = CTC_SHA256wRSA;
  12326. #else
  12327. myCert->sigType = CTC_SHAwRSA;
  12328. #endif
  12329. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12330. #ifdef WOLFSSL_CERT_EXT
  12331. /* add Policies */
  12332. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  12333. CTC_MAX_CERTPOL_SZ);
  12334. myCert->certPoliciesNb =1;
  12335. /* add SKID from the Public Key */
  12336. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  12337. ERROR_OUT(-7838, exit_rsa);
  12338. }
  12339. /* add AKID from the CA certificate */
  12340. #if defined(USE_CERT_BUFFERS_2048)
  12341. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12342. sizeof_ca_cert_der_2048);
  12343. #elif defined(USE_CERT_BUFFERS_1024)
  12344. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12345. sizeof_ca_cert_der_1024);
  12346. #else
  12347. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12348. #endif
  12349. if (ret != 0) {
  12350. ERROR_OUT(-7839, exit_rsa);
  12351. }
  12352. /* add Key Usage */
  12353. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  12354. ERROR_OUT(-7840, exit_rsa);
  12355. }
  12356. #endif /* WOLFSSL_CERT_EXT */
  12357. #if defined(USE_CERT_BUFFERS_2048)
  12358. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12359. sizeof_ca_cert_der_2048);
  12360. #elif defined(USE_CERT_BUFFERS_1024)
  12361. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12362. sizeof_ca_cert_der_1024);
  12363. #else
  12364. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12365. #endif
  12366. if (ret < 0) {
  12367. ERROR_OUT(-7841, exit_rsa);
  12368. }
  12369. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  12370. if (certSz < 0) {
  12371. ERROR_OUT(-7842, exit_rsa);
  12372. }
  12373. ret = 0;
  12374. do {
  12375. #if defined(WOLFSSL_ASYNC_CRYPT)
  12376. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12377. #endif
  12378. if (ret >= 0) {
  12379. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  12380. caKey, NULL, rng);
  12381. }
  12382. } while (ret == WC_PENDING_E);
  12383. if (ret < 0) {
  12384. ERROR_OUT(-7843, exit_rsa);
  12385. }
  12386. certSz = ret;
  12387. #ifdef WOLFSSL_TEST_CERT
  12388. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  12389. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12390. if (ret != 0) {
  12391. FreeDecodedCert(decode);
  12392. ERROR_OUT(-7844, exit_rsa);
  12393. }
  12394. FreeDecodedCert(decode);
  12395. #endif
  12396. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  12397. CERT_TYPE, -5598);
  12398. if (ret != 0) {
  12399. goto exit_rsa;
  12400. }
  12401. exit_rsa:
  12402. #ifdef WOLFSSL_SMALL_STACK
  12403. if (caKey != NULL) {
  12404. wc_FreeRsaKey(caKey);
  12405. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12406. }
  12407. #ifdef WOLFSSL_TEST_CERT
  12408. if (decode != NULL)
  12409. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12410. #endif
  12411. #else
  12412. wc_FreeRsaKey(caKey);
  12413. #endif
  12414. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12415. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12416. return ret;
  12417. }
  12418. #endif
  12419. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  12420. /* Make Cert / Sign example for ECC cert and RSA CA */
  12421. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  12422. {
  12423. #ifdef WOLFSSL_SMALL_STACK
  12424. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12425. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12426. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12427. #ifdef WOLFSSL_TEST_CERT
  12428. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12429. #endif
  12430. #else
  12431. RsaKey caKey[1];
  12432. ecc_key caEccKey[1];
  12433. ecc_key caEccKeyPub[1];
  12434. #ifdef WOLFSSL_TEST_CERT
  12435. DecodedCert decode[1];
  12436. #endif
  12437. #endif
  12438. byte* der = NULL;
  12439. Cert* myCert = NULL;
  12440. int certSz;
  12441. size_t bytes3;
  12442. word32 idx3 = 0;
  12443. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  12444. || !defined(USE_CERT_BUFFERS_256)
  12445. XFILE file3;
  12446. #endif
  12447. int ret;
  12448. #ifdef WOLFSSL_SMALL_STACK
  12449. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  12450. #ifdef WOLFSSL_TEST_CERT
  12451. || (decode == NULL)
  12452. #endif
  12453. )
  12454. ERROR_OUT(MEMORY_E, exit_rsa);
  12455. #endif
  12456. XMEMSET(caKey, 0, sizeof *caKey);
  12457. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  12458. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  12459. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12460. if (der == NULL) {
  12461. ERROR_OUT(-7850, exit_rsa);
  12462. }
  12463. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12464. if (myCert == NULL) {
  12465. ERROR_OUT(-7851, exit_rsa);
  12466. }
  12467. /* Get CA Key */
  12468. #ifdef USE_CERT_BUFFERS_1024
  12469. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  12470. bytes3 = sizeof_ca_key_der_1024;
  12471. #elif defined(USE_CERT_BUFFERS_2048)
  12472. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  12473. bytes3 = sizeof_ca_key_der_2048;
  12474. #else
  12475. file3 = XFOPEN(rsaCaKeyFile, "rb");
  12476. if (!file3) {
  12477. ERROR_OUT(-7852, exit_rsa);
  12478. }
  12479. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12480. XFCLOSE(file3);
  12481. #endif /* USE_CERT_BUFFERS */
  12482. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  12483. if (ret != 0) {
  12484. ERROR_OUT(-7853, exit_rsa);
  12485. }
  12486. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  12487. if (ret != 0) {
  12488. ERROR_OUT(-7854, exit_rsa);
  12489. }
  12490. /* Get Cert Key */
  12491. #ifdef USE_CERT_BUFFERS_256
  12492. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  12493. bytes3 = sizeof_ecc_key_pub_der_256;
  12494. #else
  12495. file3 = XFOPEN(eccKeyPubFile, "rb");
  12496. if (!file3) {
  12497. ERROR_OUT(-7855, exit_rsa);
  12498. }
  12499. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  12500. XFCLOSE(file3);
  12501. #endif
  12502. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  12503. if (ret != 0) {
  12504. ERROR_OUT(-7856, exit_rsa);
  12505. }
  12506. idx3 = 0;
  12507. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  12508. if (ret != 0) {
  12509. ERROR_OUT(-7857, exit_rsa);
  12510. }
  12511. /* Setup Certificate */
  12512. if (wc_InitCert(myCert)) {
  12513. ERROR_OUT(-7858, exit_rsa);
  12514. }
  12515. #ifndef NO_SHA256
  12516. myCert->sigType = CTC_SHA256wRSA;
  12517. #else
  12518. myCert->sigType = CTC_SHAwRSA;
  12519. #endif
  12520. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  12521. #ifdef WOLFSSL_CERT_EXT
  12522. /* add Policies */
  12523. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  12524. CTC_MAX_CERTPOL_SZ);
  12525. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  12526. CTC_MAX_CERTPOL_SZ);
  12527. myCert->certPoliciesNb = 2;
  12528. /* add SKID from the Public Key */
  12529. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  12530. ERROR_OUT(-7859, exit_rsa);
  12531. }
  12532. /* add AKID from the CA certificate */
  12533. #if defined(USE_CERT_BUFFERS_2048)
  12534. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  12535. sizeof_ca_cert_der_2048);
  12536. #elif defined(USE_CERT_BUFFERS_1024)
  12537. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  12538. sizeof_ca_cert_der_1024);
  12539. #else
  12540. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  12541. #endif
  12542. if (ret != 0) {
  12543. ERROR_OUT(-7860, exit_rsa);
  12544. }
  12545. /* add Key Usage */
  12546. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  12547. ERROR_OUT(-7861, exit_rsa);
  12548. }
  12549. #endif /* WOLFSSL_CERT_EXT */
  12550. #if defined(USE_CERT_BUFFERS_2048)
  12551. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  12552. sizeof_ca_cert_der_2048);
  12553. #elif defined(USE_CERT_BUFFERS_1024)
  12554. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  12555. sizeof_ca_cert_der_1024);
  12556. #else
  12557. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  12558. #endif
  12559. if (ret < 0) {
  12560. ERROR_OUT(-7862, exit_rsa);
  12561. }
  12562. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  12563. if (certSz < 0) {
  12564. ERROR_OUT(-7863, exit_rsa);
  12565. }
  12566. ret = 0;
  12567. do {
  12568. #if defined(WOLFSSL_ASYNC_CRYPT)
  12569. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12570. #endif
  12571. if (ret >= 0) {
  12572. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  12573. FOURK_BUF, caKey, NULL, rng);
  12574. }
  12575. } while (ret == WC_PENDING_E);
  12576. if (ret < 0) {
  12577. ERROR_OUT(-7864, exit_rsa);
  12578. }
  12579. certSz = ret;
  12580. #ifdef WOLFSSL_TEST_CERT
  12581. InitDecodedCert(decode, der, certSz, 0);
  12582. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  12583. if (ret != 0) {
  12584. FreeDecodedCert(decode);
  12585. ERROR_OUT(-7865, exit_rsa);
  12586. }
  12587. FreeDecodedCert(decode);
  12588. #endif
  12589. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  12590. CERT_TYPE, -5616);
  12591. if (ret != 0) {
  12592. goto exit_rsa;
  12593. }
  12594. exit_rsa:
  12595. #ifdef WOLFSSL_SMALL_STACK
  12596. if (caKey != NULL) {
  12597. wc_FreeRsaKey(caKey);
  12598. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12599. }
  12600. if (caEccKey != NULL) {
  12601. wc_ecc_free(caEccKey);
  12602. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12603. }
  12604. if (caEccKeyPub != NULL) {
  12605. wc_ecc_free(caEccKeyPub);
  12606. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12607. }
  12608. #ifdef WOLFSSL_TEST_CERT
  12609. if (decode != NULL)
  12610. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12611. #endif
  12612. #else
  12613. wc_FreeRsaKey(caKey);
  12614. wc_ecc_free(caEccKey);
  12615. wc_ecc_free(caEccKeyPub);
  12616. #endif
  12617. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12618. myCert = NULL;
  12619. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12620. der = NULL;
  12621. if (ret >= 0)
  12622. ret = 0;
  12623. return ret;
  12624. }
  12625. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  12626. #ifdef WOLFSSL_KEY_GEN
  12627. static int rsa_keygen_test(WC_RNG* rng)
  12628. {
  12629. #ifdef WOLFSSL_SMALL_STACK
  12630. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12631. #else
  12632. RsaKey genKey[1];
  12633. #endif
  12634. int ret;
  12635. byte* der = NULL;
  12636. #ifndef WOLFSSL_CRYPTOCELL
  12637. word32 idx = 0;
  12638. #endif
  12639. int derSz = 0;
  12640. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  12641. int keySz = 1024;
  12642. #else
  12643. int keySz = 2048;
  12644. #endif
  12645. #ifdef WOLFSSL_SMALL_STACK
  12646. if (! genKey)
  12647. ERROR_OUT(MEMORY_E, exit_rsa);
  12648. #endif
  12649. XMEMSET(genKey, 0, sizeof *genKey);
  12650. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  12651. if (ret != 0) {
  12652. ERROR_OUT(-7870, exit_rsa);
  12653. }
  12654. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  12655. #if defined(WOLFSSL_ASYNC_CRYPT)
  12656. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  12657. #endif
  12658. if (ret != 0) {
  12659. ERROR_OUT(-7871, exit_rsa);
  12660. }
  12661. TEST_SLEEP();
  12662. /* If not using old FIPS, or not using FAST or USER RSA... */
  12663. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  12664. (!defined(HAVE_FIPS) || \
  12665. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  12666. !defined(HAVE_SELFTEST) && !defined(HAVE_INTEL_QA)
  12667. ret = wc_CheckRsaKey(genKey);
  12668. if (ret != 0) {
  12669. ERROR_OUT(-7872, exit_rsa);
  12670. }
  12671. #endif
  12672. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12673. if (der == NULL) {
  12674. ERROR_OUT(-7873, exit_rsa);
  12675. }
  12676. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  12677. if (derSz < 0) {
  12678. ERROR_OUT(-7874, exit_rsa);
  12679. }
  12680. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  12681. PRIVATEKEY_TYPE, -5555);
  12682. if (ret != 0) {
  12683. goto exit_rsa;
  12684. }
  12685. wc_FreeRsaKey(genKey);
  12686. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  12687. if (ret != 0) {
  12688. ERROR_OUT(-7875, exit_rsa);
  12689. }
  12690. #ifndef WOLFSSL_CRYPTOCELL
  12691. idx = 0;
  12692. /* The private key part of the key gen pairs from cryptocell can't be exported */
  12693. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  12694. if (ret != 0) {
  12695. ERROR_OUT(-7876, exit_rsa);
  12696. }
  12697. #endif /* WOLFSSL_CRYPTOCELL */
  12698. exit_rsa:
  12699. #ifdef WOLFSSL_SMALL_STACK
  12700. if (genKey) {
  12701. wc_FreeRsaKey(genKey);
  12702. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12703. }
  12704. #else
  12705. wc_FreeRsaKey(genKey);
  12706. #endif
  12707. if (der != NULL) {
  12708. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12709. der = NULL;
  12710. }
  12711. return ret;
  12712. }
  12713. #endif
  12714. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  12715. {
  12716. int ret;
  12717. byte* tmp = NULL;
  12718. byte* der = NULL;
  12719. size_t bytes;
  12720. WC_RNG rng;
  12721. #ifdef WOLFSSL_SMALL_STACK
  12722. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12723. #else
  12724. RsaKey key[1];
  12725. #endif
  12726. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12727. #ifdef WOLFSSL_SMALL_STACK
  12728. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12729. #else
  12730. RsaKey keypub[1];
  12731. #endif
  12732. #endif
  12733. #if defined(HAVE_NTRU)
  12734. #ifdef WOLFSSL_SMALL_STACK
  12735. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12736. #else
  12737. RsaKey caKey[1];
  12738. #endif
  12739. #endif
  12740. word32 idx = 0;
  12741. const char inStr[] = TEST_STRING;
  12742. const word32 inLen = (word32)TEST_STRING_SZ;
  12743. const word32 outSz = RSA_TEST_BYTES;
  12744. const word32 plainSz = RSA_TEST_BYTES;
  12745. byte* res;
  12746. #ifndef NO_SIG_WRAPPER
  12747. int modLen;
  12748. #endif
  12749. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12750. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  12751. !defined(NO_FILESYSTEM)
  12752. XFILE file;
  12753. XFILE file2;
  12754. #endif
  12755. #ifdef WOLFSSL_TEST_CERT
  12756. #ifdef WOLFSSL_SMALL_STACK
  12757. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12758. #else
  12759. DecodedCert cert[1];
  12760. #endif
  12761. #endif
  12762. DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  12763. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12764. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12765. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  12766. if (in == NULL || out == NULL || plain == NULL)
  12767. ERROR_OUT(MEMORY_E, exit_rsa);
  12768. #endif
  12769. XMEMCPY(in, inStr, inLen);
  12770. #ifdef WOLFSSL_SMALL_STACK
  12771. if (key == NULL)
  12772. ERROR_OUT(MEMORY_E, exit_rsa);
  12773. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12774. if (keypub == NULL)
  12775. ERROR_OUT(MEMORY_E, exit_rsa);
  12776. #endif
  12777. #if defined(HAVE_NTRU)
  12778. if (caKey == NULL)
  12779. ERROR_OUT(MEMORY_E, exit_rsa);
  12780. #endif
  12781. #ifdef WOLFSSL_TEST_CERT
  12782. if (cert == NULL)
  12783. ERROR_OUT(MEMORY_E, exit_rsa);
  12784. #endif
  12785. #endif /* WOLFSSL_SMALL_STACK */
  12786. /* initialize stack structures */
  12787. XMEMSET(&rng, 0, sizeof(rng));
  12788. XMEMSET(key, 0, sizeof *key);
  12789. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  12790. XMEMSET(keypub, 0, sizeof *keypub);
  12791. #endif
  12792. #if defined(HAVE_NTRU)
  12793. XMEMSET(caKey, 0, sizeof *caKey);
  12794. #endif
  12795. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12796. ret = rsa_decode_test(key);
  12797. if (ret != 0)
  12798. ERROR_OUT(ret, exit_rsa);
  12799. #endif
  12800. #ifdef USE_CERT_BUFFERS_1024
  12801. bytes = (size_t)sizeof_client_key_der_1024;
  12802. if (bytes < (size_t)sizeof_client_cert_der_1024)
  12803. bytes = (size_t)sizeof_client_cert_der_1024;
  12804. #elif defined(USE_CERT_BUFFERS_2048)
  12805. bytes = (size_t)sizeof_client_key_der_2048;
  12806. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12807. bytes = (size_t)sizeof_client_cert_der_2048;
  12808. #elif defined(USE_CERT_BUFFERS_3072)
  12809. bytes = (size_t)sizeof_client_key_der_3072;
  12810. if (bytes < (size_t)sizeof_client_cert_der_3072)
  12811. bytes = (size_t)sizeof_client_cert_der_3072;
  12812. #elif defined(USE_CERT_BUFFERS_4096)
  12813. bytes = (size_t)sizeof_client_key_der_4096;
  12814. if (bytes < (size_t)sizeof_client_cert_der_4096)
  12815. bytes = (size_t)sizeof_client_cert_der_4096;
  12816. #else
  12817. bytes = FOURK_BUF;
  12818. #endif
  12819. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12820. if (tmp == NULL)
  12821. ERROR_OUT(-7900, exit_rsa);
  12822. #ifdef USE_CERT_BUFFERS_1024
  12823. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  12824. #elif defined(USE_CERT_BUFFERS_2048)
  12825. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12826. #elif defined(USE_CERT_BUFFERS_3072)
  12827. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12828. #elif defined(USE_CERT_BUFFERS_4096)
  12829. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12830. #elif !defined(NO_FILESYSTEM)
  12831. file = XFOPEN(clientKey, "rb");
  12832. if (!file) {
  12833. err_sys("can't open ./certs/client-key.der, "
  12834. "Please run from wolfSSL home dir", -40);
  12835. ERROR_OUT(-7901, exit_rsa);
  12836. }
  12837. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12838. XFCLOSE(file);
  12839. #else
  12840. /* No key to use. */
  12841. ERROR_OUT(-7902, exit_rsa);
  12842. #endif /* USE_CERT_BUFFERS */
  12843. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  12844. if (ret != 0) {
  12845. ERROR_OUT(-7903, exit_rsa);
  12846. }
  12847. #ifndef NO_ASN
  12848. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12849. if (ret != 0) {
  12850. ERROR_OUT(-7904, exit_rsa);
  12851. }
  12852. #ifndef NO_SIG_WRAPPER
  12853. modLen = wc_RsaEncryptSize(key);
  12854. #endif
  12855. #else
  12856. #ifdef USE_CERT_BUFFERS_2048
  12857. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  12858. if (ret != 0) {
  12859. ERROR_OUT(-7905, exit_rsa);
  12860. }
  12861. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  12862. if (ret != 0) {
  12863. ERROR_OUT(-7906, exit_rsa);
  12864. }
  12865. #ifndef NO_SIG_WRAPPER
  12866. modLen = 2048;
  12867. #endif
  12868. #else
  12869. #error Not supported yet!
  12870. #endif
  12871. #endif
  12872. #ifndef WC_NO_RNG
  12873. #ifndef HAVE_FIPS
  12874. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12875. #else
  12876. ret = wc_InitRng(&rng);
  12877. #endif
  12878. if (ret != 0) {
  12879. ERROR_OUT(-7907, exit_rsa);
  12880. }
  12881. #endif
  12882. #ifndef NO_SIG_WRAPPER
  12883. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  12884. if (ret != 0)
  12885. goto exit_rsa;
  12886. #endif
  12887. #ifdef WC_RSA_NONBLOCK
  12888. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  12889. if (ret != 0)
  12890. goto exit_rsa;
  12891. #endif
  12892. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12893. !defined(WC_NO_RNG)
  12894. do {
  12895. #if defined(WOLFSSL_ASYNC_CRYPT)
  12896. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12897. #endif
  12898. if (ret >= 0) {
  12899. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  12900. }
  12901. } while (ret == WC_PENDING_E);
  12902. if (ret < 0) {
  12903. ERROR_OUT(-7908, exit_rsa);
  12904. }
  12905. TEST_SLEEP();
  12906. #ifdef WC_RSA_BLINDING
  12907. {
  12908. int tmpret = ret;
  12909. ret = wc_RsaSetRNG(key, &rng);
  12910. if (ret < 0) {
  12911. ERROR_OUT(-7909, exit_rsa);
  12912. }
  12913. ret = tmpret;
  12914. }
  12915. #endif
  12916. idx = (word32)ret; /* save off encrypted length */
  12917. do {
  12918. #if defined(WOLFSSL_ASYNC_CRYPT)
  12919. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12920. #endif
  12921. if (ret >= 0) {
  12922. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  12923. }
  12924. } while (ret == WC_PENDING_E);
  12925. if (ret < 0) {
  12926. ERROR_OUT(-7910, exit_rsa);
  12927. }
  12928. if (XMEMCMP(plain, in, inLen)) {
  12929. ERROR_OUT(-7911, exit_rsa);
  12930. }
  12931. TEST_SLEEP();
  12932. do {
  12933. #if defined(WOLFSSL_ASYNC_CRYPT)
  12934. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12935. #endif
  12936. if (ret >= 0) {
  12937. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  12938. }
  12939. } while (ret == WC_PENDING_E);
  12940. if (ret < 0) {
  12941. ERROR_OUT(-7912, exit_rsa);
  12942. }
  12943. if (ret != (int)inLen) {
  12944. ERROR_OUT(-7913, exit_rsa);
  12945. }
  12946. if (XMEMCMP(res, in, inLen)) {
  12947. ERROR_OUT(-7914, exit_rsa);
  12948. }
  12949. TEST_SLEEP();
  12950. do {
  12951. #if defined(WOLFSSL_ASYNC_CRYPT)
  12952. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12953. #endif
  12954. if (ret >= 0) {
  12955. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  12956. }
  12957. } while (ret == WC_PENDING_E);
  12958. if (ret < 0) {
  12959. ERROR_OUT(-7915, exit_rsa);
  12960. }
  12961. TEST_SLEEP();
  12962. #elif defined(WOLFSSL_PUBLIC_MP)
  12963. {
  12964. static byte signature_2048[] = {
  12965. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  12966. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  12967. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  12968. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  12969. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  12970. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  12971. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  12972. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  12973. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  12974. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  12975. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  12976. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  12977. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  12978. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  12979. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  12980. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  12981. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  12982. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  12983. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  12984. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  12985. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  12986. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  12987. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  12988. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  12989. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  12990. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  12991. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  12992. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  12993. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  12994. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  12995. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  12996. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  12997. };
  12998. ret = sizeof(signature_2048);
  12999. XMEMCPY(out, signature_2048, ret);
  13000. }
  13001. #endif
  13002. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  13003. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  13004. defined(WOLFSSL_PUBLIC_MP))
  13005. idx = (word32)ret;
  13006. XMEMSET(plain, 0, plainSz);
  13007. do {
  13008. #if defined(WOLFSSL_ASYNC_CRYPT)
  13009. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13010. #endif
  13011. if (ret >= 0) {
  13012. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  13013. #if defined(WOLFSSL_CRYPTOCELL)
  13014. /*
  13015. Cryptocell requires the input data and signature byte array to verify.
  13016. first argument must be the input data
  13017. second argument must be the length of input data
  13018. third argument must be the signature byte array or the output from
  13019. wc_RsaSSL_Sign()
  13020. fourth argument must be the length of the signature byte array
  13021. */
  13022. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  13023. #else
  13024. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  13025. #endif /* WOLFSSL_CRYPTOCELL */
  13026. #else
  13027. byte* dec = NULL;
  13028. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  13029. if (ret > 0) {
  13030. XMEMCPY(plain, dec, ret);
  13031. }
  13032. #endif
  13033. }
  13034. } while (ret == WC_PENDING_E);
  13035. if (ret < 0) {
  13036. ERROR_OUT(-7916, exit_rsa);
  13037. }
  13038. if (XMEMCMP(plain, in, (size_t)ret)) {
  13039. ERROR_OUT(-7917, exit_rsa);
  13040. }
  13041. TEST_SLEEP();
  13042. #endif
  13043. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13044. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  13045. /* OAEP padding testing */
  13046. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  13047. (!defined(HAVE_FIPS) || \
  13048. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  13049. #ifndef NO_SHA
  13050. XMEMSET(plain, 0, plainSz);
  13051. do {
  13052. #if defined(WOLFSSL_ASYNC_CRYPT)
  13053. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13054. #endif
  13055. if (ret >= 0) {
  13056. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13057. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13058. }
  13059. } while (ret == WC_PENDING_E);
  13060. if (ret < 0) {
  13061. ERROR_OUT(-7918, exit_rsa);
  13062. }
  13063. TEST_SLEEP();
  13064. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13065. idx = (word32)ret;
  13066. do {
  13067. #if defined(WOLFSSL_ASYNC_CRYPT)
  13068. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13069. #endif
  13070. if (ret >= 0) {
  13071. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13072. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13073. }
  13074. } while (ret == WC_PENDING_E);
  13075. if (ret < 0) {
  13076. ERROR_OUT(-7919, exit_rsa);
  13077. }
  13078. if (XMEMCMP(plain, in, inLen)) {
  13079. ERROR_OUT(-7920, exit_rsa);
  13080. }
  13081. TEST_SLEEP();
  13082. #endif /* NO_SHA */
  13083. #endif
  13084. #ifndef NO_SHA256
  13085. XMEMSET(plain, 0, plainSz);
  13086. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13087. do {
  13088. #if defined(WOLFSSL_ASYNC_CRYPT)
  13089. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13090. #endif
  13091. if (ret >= 0) {
  13092. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13093. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13094. }
  13095. } while (ret == WC_PENDING_E);
  13096. if (ret < 0) {
  13097. ERROR_OUT(-7921, exit_rsa);
  13098. }
  13099. TEST_SLEEP();
  13100. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13101. idx = (word32)ret;
  13102. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13103. do {
  13104. #if defined(WOLFSSL_ASYNC_CRYPT)
  13105. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13106. #endif
  13107. if (ret >= 0) {
  13108. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13109. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13110. }
  13111. } while (ret == WC_PENDING_E);
  13112. if (ret < 0) {
  13113. ERROR_OUT(-7922, exit_rsa);
  13114. }
  13115. if (XMEMCMP(plain, in, inLen)) {
  13116. ERROR_OUT(-7923, exit_rsa);
  13117. }
  13118. TEST_SLEEP();
  13119. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13120. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13121. do {
  13122. #if defined(WOLFSSL_ASYNC_CRYPT)
  13123. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13124. #endif
  13125. if (ret >= 0) {
  13126. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  13127. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13128. }
  13129. } while (ret == WC_PENDING_E);
  13130. if (ret < 0) {
  13131. ERROR_OUT(-7924, exit_rsa);
  13132. }
  13133. if (ret != (int)inLen) {
  13134. ERROR_OUT(-7925, exit_rsa);
  13135. }
  13136. if (XMEMCMP(res, in, inLen)) {
  13137. ERROR_OUT(-7926, exit_rsa);
  13138. }
  13139. TEST_SLEEP();
  13140. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13141. /* check fails if not using the same optional label */
  13142. XMEMSET(plain, 0, plainSz);
  13143. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13144. do {
  13145. #if defined(WOLFSSL_ASYNC_CRYPT)
  13146. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13147. #endif
  13148. if (ret >= 0) {
  13149. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13150. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13151. }
  13152. } while (ret == WC_PENDING_E);
  13153. if (ret < 0) {
  13154. ERROR_OUT(-7927, exit_rsa);
  13155. }
  13156. TEST_SLEEP();
  13157. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13158. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13159. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13160. !defined(WOLFSSL_CRYPTOCELL)
  13161. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  13162. idx = (word32)ret;
  13163. do {
  13164. #if defined(WOLFSSL_ASYNC_CRYPT)
  13165. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13166. #endif
  13167. if (ret >= 0) {
  13168. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13169. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13170. }
  13171. } while (ret == WC_PENDING_E);
  13172. if (ret > 0) { /* in this case decrypt should fail */
  13173. ERROR_OUT(-7928, exit_rsa);
  13174. }
  13175. ret = 0;
  13176. TEST_SLEEP();
  13177. #endif /* !HAVE_CAVIUM */
  13178. /* check using optional label with encrypt/decrypt */
  13179. XMEMSET(plain, 0, plainSz);
  13180. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13181. do {
  13182. #if defined(WOLFSSL_ASYNC_CRYPT)
  13183. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13184. #endif
  13185. if (ret >= 0) {
  13186. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13187. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13188. }
  13189. } while (ret == WC_PENDING_E);
  13190. if (ret < 0) {
  13191. ERROR_OUT(-7929, exit_rsa);
  13192. }
  13193. TEST_SLEEP();
  13194. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13195. idx = (word32)ret;
  13196. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13197. do {
  13198. #if defined(WOLFSSL_ASYNC_CRYPT)
  13199. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13200. #endif
  13201. if (ret >= 0) {
  13202. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13203. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13204. }
  13205. } while (ret == WC_PENDING_E);
  13206. if (ret < 0) {
  13207. ERROR_OUT(-7930, exit_rsa);
  13208. }
  13209. if (XMEMCMP(plain, in, inLen)) {
  13210. ERROR_OUT(-7931, exit_rsa);
  13211. }
  13212. TEST_SLEEP();
  13213. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13214. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13215. #ifndef NO_SHA
  13216. /* check fail using mismatch hash algorithms */
  13217. XMEMSET(plain, 0, plainSz);
  13218. do {
  13219. #if defined(WOLFSSL_ASYNC_CRYPT)
  13220. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13221. #endif
  13222. if (ret >= 0) {
  13223. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13224. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  13225. }
  13226. } while (ret == WC_PENDING_E);
  13227. if (ret < 0) {
  13228. ERROR_OUT(-7932, exit_rsa);
  13229. }
  13230. TEST_SLEEP();
  13231. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13232. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13233. !defined(WOLFSSL_CRYPTOCELL)
  13234. idx = (word32)ret;
  13235. do {
  13236. #if defined(WOLFSSL_ASYNC_CRYPT)
  13237. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13238. #endif
  13239. if (ret >= 0) {
  13240. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13241. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  13242. in, inLen);
  13243. }
  13244. } while (ret == WC_PENDING_E);
  13245. if (ret > 0) { /* should fail */
  13246. ERROR_OUT(-7933, exit_rsa);
  13247. }
  13248. ret = 0;
  13249. TEST_SLEEP();
  13250. #endif /* !HAVE_CAVIUM */
  13251. #endif /* NO_SHA */
  13252. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13253. #endif /* NO_SHA256 */
  13254. #ifdef WOLFSSL_SHA512
  13255. /* Check valid RSA key size is used while using hash length of SHA512
  13256. If key size is less than (hash length * 2) + 2 then is invalid use
  13257. and test, since OAEP padding requires this.
  13258. BAD_FUNC_ARG is returned when this case is not met */
  13259. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  13260. XMEMSET(plain, 0, plainSz);
  13261. do {
  13262. #if defined(WOLFSSL_ASYNC_CRYPT)
  13263. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13264. #endif
  13265. if (ret >= 0) {
  13266. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13267. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13268. }
  13269. } while (ret == WC_PENDING_E);
  13270. if (ret < 0) {
  13271. ERROR_OUT(-7934, exit_rsa);
  13272. }
  13273. TEST_SLEEP();
  13274. idx = ret;
  13275. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13276. do {
  13277. #if defined(WOLFSSL_ASYNC_CRYPT)
  13278. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13279. #endif
  13280. if (ret >= 0) {
  13281. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13282. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  13283. }
  13284. } while (ret == WC_PENDING_E);
  13285. if (ret < 0) {
  13286. ERROR_OUT(-7935, exit_rsa);
  13287. }
  13288. if (XMEMCMP(plain, in, inLen)) {
  13289. ERROR_OUT(-7936, exit_rsa);
  13290. }
  13291. TEST_SLEEP();
  13292. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13293. }
  13294. #endif /* WOLFSSL_SHA512 */
  13295. /* check using pkcsv15 padding with _ex API */
  13296. XMEMSET(plain, 0, plainSz);
  13297. do {
  13298. #if defined(WOLFSSL_ASYNC_CRYPT)
  13299. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13300. #endif
  13301. if (ret >= 0) {
  13302. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, &rng,
  13303. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13304. }
  13305. } while (ret == WC_PENDING_E);
  13306. if (ret < 0) {
  13307. ERROR_OUT(-7937, exit_rsa);
  13308. }
  13309. TEST_SLEEP();
  13310. idx = (word32)ret;
  13311. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13312. do {
  13313. #if defined(WOLFSSL_ASYNC_CRYPT)
  13314. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13315. #endif
  13316. if (ret >= 0) {
  13317. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13318. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  13319. }
  13320. } while (ret == WC_PENDING_E);
  13321. if (ret < 0) {
  13322. ERROR_OUT(-7938, exit_rsa);
  13323. }
  13324. if (XMEMCMP(plain, in, inLen)) {
  13325. ERROR_OUT(-7939, exit_rsa);
  13326. }
  13327. TEST_SLEEP();
  13328. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13329. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  13330. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  13331. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13332. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  13333. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13334. ret = rsa_export_key_test(key);
  13335. if (ret != 0)
  13336. return ret;
  13337. #endif
  13338. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13339. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13340. ret = rsa_flatten_test(key);
  13341. if (ret != 0)
  13342. return ret;
  13343. #endif
  13344. #if defined(WOLFSSL_MDK_ARM)
  13345. #define sizeof(s) XSTRLEN((char *)(s))
  13346. #endif
  13347. #ifdef USE_CERT_BUFFERS_1024
  13348. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  13349. bytes = (size_t)sizeof_client_cert_der_1024;
  13350. #elif defined(USE_CERT_BUFFERS_2048)
  13351. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  13352. bytes = (size_t)sizeof_client_cert_der_2048;
  13353. #elif defined(USE_CERT_BUFFERS_3072)
  13354. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  13355. bytes = (size_t)sizeof_client_cert_der_3072;
  13356. #elif defined(USE_CERT_BUFFERS_4096)
  13357. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  13358. bytes = (size_t)sizeof_client_cert_der_4096;
  13359. #elif !defined(NO_FILESYSTEM)
  13360. file2 = XFOPEN(clientCert, "rb");
  13361. if (!file2) {
  13362. ERROR_OUT(-7940, exit_rsa);
  13363. }
  13364. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  13365. XFCLOSE(file2);
  13366. #else
  13367. /* No certificate to use. */
  13368. ERROR_OUT(-7941, exit_rsa);
  13369. #endif
  13370. #ifdef sizeof
  13371. #undef sizeof
  13372. #endif
  13373. #ifdef WOLFSSL_TEST_CERT
  13374. InitDecodedCert(cert, tmp, (word32)bytes, 0);
  13375. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, 0);
  13376. if (ret != 0) {
  13377. FreeDecodedCert(cert);
  13378. ERROR_OUT(-7942, exit_rsa);
  13379. }
  13380. FreeDecodedCert(cert);
  13381. #endif
  13382. #ifdef WOLFSSL_CERT_EXT
  13383. #ifdef USE_CERT_BUFFERS_1024
  13384. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  13385. bytes = sizeof_client_keypub_der_1024;
  13386. #elif defined(USE_CERT_BUFFERS_2048)
  13387. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  13388. bytes = sizeof_client_keypub_der_2048;
  13389. #elif defined(USE_CERT_BUFFERS_3072)
  13390. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  13391. bytes = sizeof_client_keypub_der_3072;
  13392. #elif defined(USE_CERT_BUFFERS_4096)
  13393. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  13394. bytes = sizeof_client_keypub_der_4096;
  13395. #else
  13396. file = XFOPEN(clientKeyPub, "rb");
  13397. if (!file) {
  13398. err_sys("can't open ./certs/client-keyPub.der, "
  13399. "Please run from wolfSSL home dir", -40);
  13400. ERROR_OUT(-7943, exit_rsa);
  13401. }
  13402. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13403. XFCLOSE(file);
  13404. #endif /* USE_CERT_BUFFERS */
  13405. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  13406. if (ret != 0) {
  13407. ERROR_OUT(-7944, exit_rsa);
  13408. }
  13409. idx = 0;
  13410. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  13411. if (ret != 0) {
  13412. ERROR_OUT(-7945, exit_rsa);
  13413. }
  13414. #endif /* WOLFSSL_CERT_EXT */
  13415. #ifdef WOLFSSL_KEY_GEN
  13416. ret = rsa_keygen_test(&rng);
  13417. if (ret != 0)
  13418. goto exit_rsa;
  13419. #endif
  13420. #ifdef WOLFSSL_CERT_GEN
  13421. /* Make Cert / Sign example for RSA cert and RSA CA */
  13422. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  13423. if (ret != 0)
  13424. goto exit_rsa;
  13425. #if !defined(NO_RSA) && defined(HAVE_ECC)
  13426. ret = rsa_ecc_certgen_test(&rng, tmp);
  13427. if (ret != 0)
  13428. goto exit_rsa;
  13429. #endif
  13430. #ifdef HAVE_NTRU
  13431. {
  13432. Cert myCert;
  13433. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13434. XFILE caFile;
  13435. #endif
  13436. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13437. XFILE ntruPrivFile;
  13438. #endif
  13439. int certSz;
  13440. word32 idx3 = 0;
  13441. #ifdef WOLFSSL_TEST_CERT
  13442. DecodedCert decode;
  13443. #endif
  13444. byte public_key[557]; /* sized for EES401EP2 */
  13445. word16 public_key_len; /* no. of octets in public key */
  13446. byte private_key[607]; /* sized for EES401EP2 */
  13447. word16 private_key_len; /* no. of octets in private key */
  13448. DRBG_HANDLE drbg;
  13449. static uint8_t const pers_str[] = {
  13450. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  13451. };
  13452. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  13453. sizeof(pers_str), GetEntropy, &drbg);
  13454. if (rc != DRBG_OK) {
  13455. ERROR_OUT(-7946, exit_rsa);
  13456. }
  13457. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13458. &public_key_len, NULL,
  13459. &private_key_len, NULL);
  13460. if (rc != NTRU_OK) {
  13461. ERROR_OUT(-7947, exit_rsa);
  13462. }
  13463. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  13464. &public_key_len, public_key,
  13465. &private_key_len, private_key);
  13466. if (rc != NTRU_OK) {
  13467. ERROR_OUT(-7948, exit_rsa);
  13468. }
  13469. rc = ntru_crypto_drbg_uninstantiate(drbg);
  13470. if (rc != NTRU_OK) {
  13471. ERROR_OUT(-7949, exit_rsa);
  13472. }
  13473. #ifdef USE_CERT_BUFFERS_1024
  13474. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13475. bytes = sizeof_ca_key_der_1024;
  13476. #elif defined(USE_CERT_BUFFERS_2048)
  13477. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13478. bytes = sizeof_ca_key_der_2048;
  13479. #else
  13480. caFile = XFOPEN(rsaCaKeyFile, "rb");
  13481. if (!caFile) {
  13482. ERROR_OUT(-7950, exit_rsa);
  13483. }
  13484. bytes = XFREAD(tmp, 1, FOURK_BUF, caFile);
  13485. XFCLOSE(caFile);
  13486. #endif /* USE_CERT_BUFFERS */
  13487. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13488. if (ret != 0) {
  13489. ERROR_OUT(-7951, exit_rsa);
  13490. }
  13491. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes);
  13492. if (ret != 0) {
  13493. ERROR_OUT(-7952, exit_rsa);
  13494. }
  13495. if (wc_InitCert(&myCert)) {
  13496. ERROR_OUT(-7953, exit_rsa);
  13497. }
  13498. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  13499. myCert.daysValid = 1000;
  13500. #ifdef WOLFSSL_CERT_EXT
  13501. /* add SKID from the Public Key */
  13502. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  13503. public_key_len) != 0) {
  13504. ERROR_OUT(-7954, exit_rsa);
  13505. }
  13506. /* add AKID from the CA certificate */
  13507. #if defined(USE_CERT_BUFFERS_2048)
  13508. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  13509. sizeof_ca_cert_der_2048);
  13510. #elif defined(USE_CERT_BUFFERS_1024)
  13511. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  13512. sizeof_ca_cert_der_1024);
  13513. #else
  13514. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  13515. #endif
  13516. if (ret != 0) {
  13517. ERROR_OUT(-7955, exit_rsa);
  13518. }
  13519. /* add Key Usage */
  13520. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  13521. ERROR_OUT(-7956, exit_rsa);
  13522. }
  13523. #endif /* WOLFSSL_CERT_EXT */
  13524. #if defined(USE_CERT_BUFFERS_2048)
  13525. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  13526. sizeof_ca_cert_der_2048);
  13527. #elif defined(USE_CERT_BUFFERS_1024)
  13528. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  13529. sizeof_ca_cert_der_1024);
  13530. #else
  13531. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  13532. #endif
  13533. if (ret < 0) {
  13534. ERROR_OUT(-7957, exit_rsa);
  13535. }
  13536. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13537. if (der == NULL) {
  13538. ERROR_OUT(-7958, exit_rsa);
  13539. }
  13540. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  13541. public_key_len, &rng);
  13542. if (certSz < 0) {
  13543. ERROR_OUT(-7959, exit_rsa);
  13544. }
  13545. ret = 0;
  13546. do {
  13547. #if defined(WOLFSSL_ASYNC_CRYPT)
  13548. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13549. #endif
  13550. if (ret >= 0) {
  13551. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  13552. caKey, NULL, &rng);
  13553. }
  13554. } while (ret == WC_PENDING_E);
  13555. wc_FreeRsaKey(caKey);
  13556. if (ret < 0) {
  13557. ERROR_OUT(-7960, exit_rsa);
  13558. }
  13559. certSz = ret;
  13560. #ifdef WOLFSSL_TEST_CERT
  13561. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  13562. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  13563. if (ret != 0) {
  13564. FreeDecodedCert(&decode);
  13565. ERROR_OUT(-7961, exit_rsa);
  13566. }
  13567. FreeDecodedCert(&decode);
  13568. #endif
  13569. ret = SaveDerAndPem(der, certSz, "./ntru-cert.der", "./ntru-cert.pem",
  13570. CERT_TYPE, -5637);
  13571. if (ret != 0) {
  13572. goto exit_rsa;
  13573. }
  13574. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  13575. ntruPrivFile = XFOPEN("./ntru-key.raw", "wb");
  13576. if (!ntruPrivFile) {
  13577. ERROR_OUT(-7962, exit_rsa);
  13578. }
  13579. ret = (int)XFWRITE(private_key, 1, private_key_len, ntruPrivFile);
  13580. XFCLOSE(ntruPrivFile);
  13581. if (ret != private_key_len) {
  13582. ERROR_OUT(-7963, exit_rsa);
  13583. }
  13584. #endif
  13585. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13586. der = NULL;
  13587. }
  13588. #endif /* HAVE_NTRU */
  13589. #ifdef WOLFSSL_CERT_REQ
  13590. {
  13591. Cert *req;
  13592. int derSz;
  13593. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13594. if (! req)
  13595. ERROR_OUT(MEMORY_E, exit_rsa);
  13596. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  13597. if (der == NULL) {
  13598. ERROR_OUT(-7964, exit_rsa);
  13599. }
  13600. if (wc_InitCert(req)) {
  13601. ERROR_OUT(-7965, exit_rsa);
  13602. }
  13603. req->version = 0;
  13604. req->isCA = 1;
  13605. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  13606. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  13607. #ifndef NO_SHA256
  13608. req->sigType = CTC_SHA256wRSA;
  13609. #else
  13610. req->sigType = CTC_SHAwRSA;
  13611. #endif
  13612. #ifdef WOLFSSL_CERT_EXT
  13613. /* add SKID from the Public Key */
  13614. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  13615. ERROR_OUT(-7966, exit_rsa);
  13616. }
  13617. /* add Key Usage */
  13618. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  13619. ERROR_OUT(-7967, exit_rsa);
  13620. }
  13621. /* add Extended Key Usage */
  13622. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  13623. "emailProtection,timeStamping,OCSPSigning") != 0) {
  13624. ERROR_OUT(-7968, exit_rsa);
  13625. }
  13626. #ifdef WOLFSSL_EKU_OID
  13627. {
  13628. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13629. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  13630. HEAP_HINT) != 0) {
  13631. ERROR_OUT(-7969, exit_rsa);
  13632. }
  13633. }
  13634. #endif /* WOLFSSL_EKU_OID */
  13635. #endif /* WOLFSSL_CERT_EXT */
  13636. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13637. if (derSz < 0) {
  13638. ERROR_OUT(-7970, exit_rsa);
  13639. }
  13640. #ifdef WOLFSSL_CERT_EXT
  13641. /* Try again with "any" flag set, will override all others */
  13642. if (wc_SetExtKeyUsage(req, "any") != 0) {
  13643. ERROR_OUT(-7971, exit_rsa);
  13644. }
  13645. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  13646. if (derSz < 0) {
  13647. ERROR_OUT(-7972, exit_rsa);
  13648. }
  13649. #endif /* WOLFSSL_CERT_EXT */
  13650. ret = 0;
  13651. do {
  13652. #if defined(WOLFSSL_ASYNC_CRYPT)
  13653. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13654. #endif
  13655. if (ret >= 0) {
  13656. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  13657. key, NULL, &rng);
  13658. }
  13659. } while (ret == WC_PENDING_E);
  13660. if (ret < 0) {
  13661. ERROR_OUT(-7973, exit_rsa);
  13662. }
  13663. derSz = ret;
  13664. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  13665. CERTREQ_TYPE, -5650);
  13666. if (ret != 0) {
  13667. goto exit_rsa;
  13668. }
  13669. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  13670. if (derSz < 0) {
  13671. ERROR_OUT(-7974, exit_rsa);
  13672. }
  13673. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13674. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13675. der = NULL;
  13676. }
  13677. #endif /* WOLFSSL_CERT_REQ */
  13678. #endif /* WOLFSSL_CERT_GEN */
  13679. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  13680. /* Need to create known good signatures to test with this. */
  13681. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13682. ret = rsa_pss_test(&rng, key);
  13683. if (ret != 0)
  13684. goto exit_rsa;
  13685. #endif
  13686. #endif
  13687. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13688. #ifdef WOLFSSL_SMALL_STACK
  13689. /* New key to be loaded in rsa_even_mod_test(). */
  13690. if (key != NULL)
  13691. #endif
  13692. wc_FreeRsaKey(key);
  13693. /* New key to be loaded in rsa_even_mod_test(). */
  13694. ret = rsa_even_mod_test(&rng, key);
  13695. #endif
  13696. exit_rsa:
  13697. #ifdef WOLFSSL_SMALL_STACK
  13698. if (key != NULL) {
  13699. wc_FreeRsaKey(key);
  13700. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13701. }
  13702. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13703. if (keypub != NULL) {
  13704. wc_FreeRsaKey(keypub);
  13705. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13706. }
  13707. #endif
  13708. #if defined(HAVE_NTRU)
  13709. if (caKey != NULL) {
  13710. wc_FreeRsaKey(caKey);
  13711. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13712. }
  13713. #endif
  13714. #ifdef WOLFSSL_TEST_CERT
  13715. if (cert != NULL)
  13716. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13717. #endif
  13718. #else
  13719. wc_FreeRsaKey(key);
  13720. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  13721. wc_FreeRsaKey(keypub);
  13722. #endif
  13723. #if defined(HAVE_NTRU)
  13724. wc_FreeRsaKey(caKey);
  13725. #endif
  13726. #endif /* WOLFSSL_SMALL_STACK */
  13727. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13728. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13729. wc_FreeRng(&rng);
  13730. FREE_VAR(in, HEAP_HINT);
  13731. FREE_VAR(out, HEAP_HINT);
  13732. FREE_VAR(plain, HEAP_HINT);
  13733. (void)res;
  13734. (void)bytes;
  13735. (void)in;
  13736. (void)out;
  13737. (void)plain;
  13738. (void)inLen;
  13739. (void)outSz;
  13740. (void)plainSz;
  13741. /* ret can be greater then 0 with certgen but all negative values should
  13742. * be returned and treated as an error */
  13743. if (ret >= 0) {
  13744. return 0;
  13745. }
  13746. else {
  13747. return ret;
  13748. }
  13749. }
  13750. #endif /* !NO_RSA */
  13751. #ifndef NO_DH
  13752. static int dh_fips_generate_test(WC_RNG *rng)
  13753. {
  13754. int ret = 0;
  13755. #ifdef WOLFSSL_SMALL_STACK
  13756. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  13757. #else
  13758. DhKey key[1];
  13759. #endif
  13760. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  13761. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  13762. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  13763. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  13764. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  13765. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  13766. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  13767. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  13768. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  13769. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  13770. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  13771. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  13772. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  13773. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  13774. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  13775. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  13776. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  13777. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  13778. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  13779. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  13780. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  13781. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  13782. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  13783. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  13784. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  13785. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  13786. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  13787. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  13788. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  13789. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  13790. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  13791. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  13792. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  13793. };
  13794. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  13795. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  13796. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  13797. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  13798. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  13799. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  13800. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  13801. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  13802. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  13803. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  13804. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  13805. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  13806. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  13807. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  13808. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  13809. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  13810. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  13811. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  13812. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  13813. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  13814. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  13815. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  13816. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  13817. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  13818. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  13819. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  13820. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  13821. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  13822. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  13823. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  13824. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  13825. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  13826. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  13827. };
  13828. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  13829. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  13830. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  13831. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  13832. 0x40, 0x52, 0xed, 0x41
  13833. };
  13834. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  13835. 0x00,
  13836. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  13837. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  13838. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  13839. 0x40, 0x52, 0xed, 0x41
  13840. };
  13841. byte priv[256];
  13842. byte pub[256];
  13843. word32 privSz = sizeof(priv);
  13844. word32 pubSz = sizeof(pub);
  13845. #ifdef WOLFSSL_SMALL_STACK
  13846. if (key == NULL)
  13847. ERROR_OUT(MEMORY_E, exit_gen_test);
  13848. #endif
  13849. /* Parameter Validation testing. */
  13850. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  13851. if (ret != BAD_FUNC_ARG)
  13852. ERROR_OUT(-7980, exit_gen_test);
  13853. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  13854. if (ret != BAD_FUNC_ARG)
  13855. ERROR_OUT(-7981, exit_gen_test);
  13856. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  13857. if (ret != BAD_FUNC_ARG)
  13858. ERROR_OUT(-7982, exit_gen_test);
  13859. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  13860. if (ret != BAD_FUNC_ARG)
  13861. ERROR_OUT(-7983, exit_gen_test);
  13862. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  13863. if (ret != BAD_FUNC_ARG)
  13864. ERROR_OUT(-7984, exit_gen_test);
  13865. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  13866. if (ret != BAD_FUNC_ARG)
  13867. ERROR_OUT(-7985, exit_gen_test);
  13868. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13869. if (ret != 0)
  13870. ERROR_OUT(-7986, exit_gen_test);
  13871. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  13872. if (ret != 0) {
  13873. ERROR_OUT(-7987, exit_gen_test);
  13874. }
  13875. wc_FreeDhKey(key);
  13876. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13877. if (ret != 0)
  13878. ERROR_OUT(-7988, exit_gen_test);
  13879. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  13880. if (ret != 0) {
  13881. ERROR_OUT(-7989, exit_gen_test);
  13882. }
  13883. /* Use API. */
  13884. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  13885. #if defined(WOLFSSL_ASYNC_CRYPT)
  13886. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  13887. #endif
  13888. if (ret != 0) {
  13889. ERROR_OUT(-7990, exit_gen_test);
  13890. }
  13891. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  13892. if (ret != 0) {
  13893. ERROR_OUT(-7991, exit_gen_test);
  13894. }
  13895. wc_FreeDhKey(key);
  13896. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13897. if (ret != 0)
  13898. ERROR_OUT(-7992, exit_gen_test);
  13899. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  13900. if (ret != 0) {
  13901. ERROR_OUT(-7993, exit_gen_test);
  13902. }
  13903. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  13904. if (ret != 0) {
  13905. ERROR_OUT(-7994, exit_gen_test);
  13906. }
  13907. #ifndef HAVE_SELFTEST
  13908. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  13909. if (ret != 0) {
  13910. ERROR_OUT(-7995, exit_gen_test);
  13911. }
  13912. /* Taint the public key so the check fails. */
  13913. pub[0]++;
  13914. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  13915. if (ret != MP_CMP_E) {
  13916. ERROR_OUT(-7996, exit_gen_test);
  13917. }
  13918. #ifdef WOLFSSL_KEY_GEN
  13919. wc_FreeDhKey(key);
  13920. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  13921. if (ret != 0)
  13922. ERROR_OUT(-7997, exit_gen_test);
  13923. ret = wc_DhGenerateParams(rng, 2048, key);
  13924. if (ret != 0) {
  13925. ERROR_OUT(-7998, exit_gen_test);
  13926. }
  13927. privSz = sizeof(priv);
  13928. pubSz = sizeof(pub);
  13929. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  13930. #if defined(WOLFSSL_ASYNC_CRYPT)
  13931. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  13932. #endif
  13933. if (ret != 0) {
  13934. ERROR_OUT(-7999, exit_gen_test);
  13935. }
  13936. #endif /* WOLFSSL_KEY_GEN */
  13937. #endif /* HAVE_SELFTEST */
  13938. ret = 0;
  13939. exit_gen_test:
  13940. #ifdef WOLFSSL_SMALL_STACK
  13941. if (key) {
  13942. wc_FreeDhKey(key);
  13943. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13944. }
  13945. #else
  13946. wc_FreeDhKey(key);
  13947. #endif
  13948. return ret;
  13949. }
  13950. static int dh_generate_test(WC_RNG *rng)
  13951. {
  13952. int ret = 0;
  13953. DhKey smallKey;
  13954. byte p[2] = { 0, 5 };
  13955. byte g[2] = { 0, 2 };
  13956. #if !defined(WOLFSSL_SP_MATH)
  13957. #ifdef WOLFSSL_DH_CONST
  13958. /* the table for constant DH lookup will round to the lowest byte size 21 */
  13959. byte priv[21];
  13960. byte pub[21];
  13961. #else
  13962. byte priv[2];
  13963. byte pub[2];
  13964. #endif
  13965. word32 privSz = sizeof(priv);
  13966. word32 pubSz = sizeof(pub);
  13967. #endif
  13968. ret = wc_InitDhKey_ex(&smallKey, HEAP_HINT, devId);
  13969. if (ret != 0)
  13970. return -8010;
  13971. /* Parameter Validation testing. */
  13972. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  13973. if (ret != BAD_FUNC_ARG)
  13974. return -8011;
  13975. wc_FreeDhKey(NULL);
  13976. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  13977. if (ret != BAD_FUNC_ARG) {
  13978. ERROR_OUT(-8012, exit_gen_test);
  13979. }
  13980. ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g));
  13981. if (ret != BAD_FUNC_ARG) {
  13982. ERROR_OUT(-8013, exit_gen_test);
  13983. }
  13984. ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g));
  13985. if (ret != BAD_FUNC_ARG) {
  13986. ERROR_OUT(-8014, exit_gen_test);
  13987. }
  13988. ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g));
  13989. if (ret != BAD_FUNC_ARG) {
  13990. ERROR_OUT(-8015, exit_gen_test);
  13991. }
  13992. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0);
  13993. if (ret != BAD_FUNC_ARG) {
  13994. ERROR_OUT(-8016, exit_gen_test);
  13995. }
  13996. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
  13997. if (ret != 0) {
  13998. ERROR_OUT(-8017, exit_gen_test);
  13999. }
  14000. #if !defined(WOLFSSL_SP_MATH)
  14001. /* Use API. */
  14002. ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz);
  14003. #if defined(WOLFSSL_ASYNC_CRYPT)
  14004. ret = wc_AsyncWait(ret, &smallKey.asyncDev, WC_ASYNC_FLAG_NONE);
  14005. #endif
  14006. if (ret != 0) {
  14007. ret = -8018;
  14008. }
  14009. #else
  14010. (void)rng;
  14011. ret = 0;
  14012. #endif
  14013. exit_gen_test:
  14014. wc_FreeDhKey(&smallKey);
  14015. return ret;
  14016. }
  14017. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14018. typedef struct dh_pubvalue_test {
  14019. const byte* data;
  14020. word32 len;
  14021. } dh_pubvalue_test;
  14022. static int dh_test_check_pubvalue(void)
  14023. {
  14024. int ret;
  14025. word32 i;
  14026. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  14027. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  14028. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  14029. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  14030. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  14031. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  14032. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  14033. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  14034. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  14035. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  14036. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  14037. const dh_pubvalue_test dh_pubval_fail[] = {
  14038. { prime, sizeof(prime) },
  14039. { pubValZero, sizeof(pubValZero) },
  14040. { pubValZeroLong, sizeof(pubValZeroLong) },
  14041. { pubValOne, sizeof(pubValOne) },
  14042. { pubValOneLong, sizeof(pubValOneLong) },
  14043. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  14044. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  14045. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  14046. { pubValTooBig0, sizeof(pubValTooBig0) },
  14047. { pubValTooBig1, sizeof(pubValTooBig1) },
  14048. { pubValTooLong, sizeof(pubValTooLong) },
  14049. };
  14050. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  14051. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  14052. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  14053. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  14054. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  14055. const dh_pubvalue_test dh_pubval_pass[] = {
  14056. { pubValTwo, sizeof(pubValTwo) },
  14057. { pubValTwoLong, sizeof(pubValTwoLong) },
  14058. { pubValGood, sizeof(pubValGood) },
  14059. { pubValGoodLen, sizeof(pubValGoodLen) },
  14060. { pubValGoodLong, sizeof(pubValGoodLong) },
  14061. };
  14062. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  14063. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  14064. dh_pubval_fail[i].len);
  14065. if (ret != MP_VAL)
  14066. return -8020 - (int)i;
  14067. }
  14068. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  14069. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  14070. dh_pubval_pass[i].len);
  14071. if (ret != 0)
  14072. return -8030 - (int)i;
  14073. }
  14074. return 0;
  14075. }
  14076. #endif
  14077. #if defined(HAVE_FFDHE)
  14078. #ifdef HAVE_FFDHE_3072
  14079. #define FFDHE_KEY_SIZE (3072/8)
  14080. #else
  14081. #define FFDHE_KEY_SIZE (2048/8)
  14082. #endif
  14083. #ifndef WC_NO_RNG
  14084. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  14085. {
  14086. int ret;
  14087. word32 privSz, pubSz, privSz2, pubSz2;
  14088. #ifdef WOLFSSL_SMALL_STACK
  14089. byte *priv = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14090. byte *pub = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14091. byte *priv2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14092. byte *pub2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14093. byte *agree = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14094. byte *agree2 = (byte *)XMALLOC(FFDHE_KEY_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14095. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14096. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14097. #else
  14098. byte priv[FFDHE_KEY_SIZE];
  14099. byte pub[FFDHE_KEY_SIZE];
  14100. byte priv2[FFDHE_KEY_SIZE];
  14101. byte pub2[FFDHE_KEY_SIZE];
  14102. byte agree[FFDHE_KEY_SIZE];
  14103. byte agree2[FFDHE_KEY_SIZE];
  14104. DhKey key[1];
  14105. DhKey key2[1];
  14106. #endif
  14107. word32 agreeSz = FFDHE_KEY_SIZE;
  14108. word32 agreeSz2 = FFDHE_KEY_SIZE;
  14109. #ifdef WOLFSSL_SMALL_STACK
  14110. if ((priv == NULL) ||
  14111. (pub == NULL) ||
  14112. (priv2 == NULL) ||
  14113. (pub2 == NULL) ||
  14114. (agree == NULL) ||
  14115. (agree2 == NULL) ||
  14116. (key == NULL) ||
  14117. (key2 == NULL))
  14118. ERROR_OUT(-8050, done);
  14119. #endif
  14120. pubSz = FFDHE_KEY_SIZE;
  14121. pubSz2 = FFDHE_KEY_SIZE;
  14122. privSz = FFDHE_KEY_SIZE;
  14123. privSz2 = FFDHE_KEY_SIZE;
  14124. XMEMSET(key, 0, sizeof *key);
  14125. XMEMSET(key2, 0, sizeof *key2);
  14126. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14127. if (ret != 0) {
  14128. ERROR_OUT(-8051, done);
  14129. }
  14130. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14131. if (ret != 0) {
  14132. ERROR_OUT(-8052, done);
  14133. }
  14134. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  14135. if (ret != 0) {
  14136. ERROR_OUT(-8053, done);
  14137. }
  14138. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  14139. params->g_len);
  14140. if (ret != 0) {
  14141. ERROR_OUT(-8054, done);
  14142. }
  14143. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14144. #if defined(WOLFSSL_ASYNC_CRYPT)
  14145. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14146. #endif
  14147. if (ret != 0) {
  14148. ERROR_OUT(-8055, done);
  14149. }
  14150. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  14151. #if defined(WOLFSSL_ASYNC_CRYPT)
  14152. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14153. #endif
  14154. if (ret != 0) {
  14155. ERROR_OUT(-8056, done);
  14156. }
  14157. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14158. #if defined(WOLFSSL_ASYNC_CRYPT)
  14159. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14160. #endif
  14161. if (ret != 0) {
  14162. ERROR_OUT(-8057, done);
  14163. }
  14164. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14165. #if defined(WOLFSSL_ASYNC_CRYPT)
  14166. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14167. #endif
  14168. if (ret != 0) {
  14169. ERROR_OUT(-8058, done);
  14170. }
  14171. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14172. ERROR_OUT(-8059, done);
  14173. }
  14174. #if defined(WOLFSSL_HAVE_SP_DH) && defined(USE_FAST_MATH)
  14175. /* Make p even */
  14176. key->p.dp[0] &= (mp_digit)-2;
  14177. if (ret != 0) {
  14178. ERROR_OUT(-8058, done);
  14179. }
  14180. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14181. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14182. ERROR_OUT(-8058, done);
  14183. }
  14184. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14185. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14186. ERROR_OUT(-8057, done);
  14187. }
  14188. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14189. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14190. ERROR_OUT(-8057, done);
  14191. }
  14192. /* Getting here means success - set ret to 0. */
  14193. ret = 0;
  14194. #endif
  14195. done:
  14196. #ifdef WOLFSSL_SMALL_STACK
  14197. if (priv)
  14198. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14199. if (pub)
  14200. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14201. if (priv2)
  14202. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14203. if (pub2)
  14204. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14205. if (agree)
  14206. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14207. if (agree2)
  14208. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14209. if (key) {
  14210. wc_FreeDhKey(key);
  14211. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14212. }
  14213. if (key2) {
  14214. wc_FreeDhKey(key2);
  14215. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14216. }
  14217. #else
  14218. wc_FreeDhKey(key);
  14219. wc_FreeDhKey(key2);
  14220. #endif
  14221. return ret;
  14222. }
  14223. #endif /* !WC_NO_RNG */
  14224. #endif /* HAVE_FFDHE */
  14225. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  14226. {
  14227. int ret;
  14228. word32 bytes;
  14229. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  14230. #ifndef WC_NO_RNG
  14231. WC_RNG rng;
  14232. #endif
  14233. int keyInit = 0;
  14234. #define DH_TEST_TMP_SIZE 1024
  14235. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14236. #define DH_TEST_BUF_SIZE 256
  14237. #else
  14238. #define DH_TEST_BUF_SIZE 512
  14239. #endif
  14240. #ifndef WC_NO_RNG
  14241. word32 agreeSz = DH_TEST_BUF_SIZE;
  14242. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  14243. #endif
  14244. #ifdef WOLFSSL_SMALL_STACK
  14245. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14246. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14247. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14248. #else
  14249. DhKey key[1];
  14250. DhKey key2[1];
  14251. byte tmp[DH_TEST_TMP_SIZE];
  14252. #endif
  14253. #ifndef WC_NO_RNG
  14254. #ifdef WOLFSSL_SMALL_STACK
  14255. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14256. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14257. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14258. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14259. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14260. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14261. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  14262. agree == NULL || agree2 == NULL) {
  14263. ERROR_OUT(-8100, done);
  14264. }
  14265. #else
  14266. byte priv[DH_TEST_BUF_SIZE];
  14267. byte pub[DH_TEST_BUF_SIZE];
  14268. byte priv2[DH_TEST_BUF_SIZE];
  14269. byte pub2[DH_TEST_BUF_SIZE];
  14270. byte agree[DH_TEST_BUF_SIZE];
  14271. byte agree2[DH_TEST_BUF_SIZE];
  14272. #endif
  14273. #endif /* !WC_NO_RNG */
  14274. #ifdef WOLFSSL_SMALL_STACK
  14275. if (key == NULL || key2 == NULL || tmp == NULL) {
  14276. ERROR_OUT(-8100, done);
  14277. }
  14278. #endif
  14279. #ifdef USE_CERT_BUFFERS_1024
  14280. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  14281. bytes = (size_t)sizeof_dh_key_der_1024;
  14282. #elif defined(USE_CERT_BUFFERS_2048)
  14283. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  14284. bytes = (size_t)sizeof_dh_key_der_2048;
  14285. #elif defined(USE_CERT_BUFFERS_3072)
  14286. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  14287. bytes = (size_t)sizeof_dh_key_der_3072;
  14288. #elif defined(USE_CERT_BUFFERS_4096)
  14289. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  14290. bytes = (size_t)sizeof_dh_key_der_4096;
  14291. #elif defined(NO_ASN)
  14292. /* don't use file, no DER parsing */
  14293. #elif !defined(NO_FILESYSTEM)
  14294. {
  14295. XFILE file = XFOPEN(dhParamsFile, "rb");
  14296. if (! file)
  14297. ERROR_OUT(-8101, done);
  14298. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14299. XFCLOSE(file);
  14300. }
  14301. #else
  14302. /* No DH key to use. */
  14303. ERROR_OUT(-8102, done);
  14304. #endif /* USE_CERT_BUFFERS */
  14305. (void)idx;
  14306. (void)tmp;
  14307. (void)bytes;
  14308. pubSz = DH_TEST_BUF_SIZE;
  14309. pubSz2 = DH_TEST_BUF_SIZE;
  14310. privSz = DH_TEST_BUF_SIZE;
  14311. privSz2 = DH_TEST_BUF_SIZE;
  14312. #ifndef WC_NO_RNG
  14313. XMEMSET(&rng, 0, sizeof(rng));
  14314. #endif
  14315. /* Use API for coverage. */
  14316. ret = wc_InitDhKey(key);
  14317. if (ret != 0) {
  14318. ERROR_OUT(-8103, done);
  14319. }
  14320. wc_FreeDhKey(key);
  14321. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14322. if (ret != 0) {
  14323. ERROR_OUT(-8104, done);
  14324. }
  14325. keyInit = 1;
  14326. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  14327. if (ret != 0) {
  14328. ERROR_OUT(-8105, done);
  14329. }
  14330. #ifdef NO_ASN
  14331. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14332. if (ret != 0) {
  14333. ERROR_OUT(-8106, done);
  14334. }
  14335. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14336. if (ret != 0) {
  14337. ERROR_OUT(-8107, done);
  14338. }
  14339. #else
  14340. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14341. if (ret != 0) {
  14342. ERROR_OUT(-8108, done);
  14343. }
  14344. idx = 0;
  14345. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  14346. if (ret != 0) {
  14347. ERROR_OUT(-8109, done);
  14348. }
  14349. #endif
  14350. #ifndef WC_NO_RNG
  14351. #ifndef HAVE_FIPS
  14352. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14353. #else
  14354. ret = wc_InitRng(&rng);
  14355. #endif
  14356. if (ret != 0) {
  14357. ERROR_OUT(-8110, done);
  14358. }
  14359. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  14360. #if defined(WOLFSSL_ASYNC_CRYPT)
  14361. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14362. #endif
  14363. if (ret != 0) {
  14364. ERROR_OUT(-8111, done);
  14365. }
  14366. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  14367. #if defined(WOLFSSL_ASYNC_CRYPT)
  14368. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14369. #endif
  14370. if (ret != 0) {
  14371. ERROR_OUT(-8112, done);
  14372. }
  14373. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  14374. #if defined(WOLFSSL_ASYNC_CRYPT)
  14375. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14376. #endif
  14377. if (ret != 0) {
  14378. ERROR_OUT(-8113, done);
  14379. }
  14380. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  14381. #if defined(WOLFSSL_ASYNC_CRYPT)
  14382. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  14383. #endif
  14384. if (ret != 0) {
  14385. ERROR_OUT(-8114, done);
  14386. }
  14387. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  14388. ERROR_OUT(-8115, done);
  14389. }
  14390. #endif /* !WC_NO_RNG */
  14391. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14392. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  14393. ERROR_OUT(-8116, done);
  14394. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  14395. ERROR_OUT(-8117, done);
  14396. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  14397. ERROR_OUT(-8118, done);
  14398. {
  14399. word32 pSz, qSz, gSz;
  14400. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  14401. ERROR_OUT(-8119, done);
  14402. }
  14403. #endif
  14404. /* Test DH key import / export */
  14405. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  14406. (!defined(HAVE_FIPS) || \
  14407. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  14408. wc_FreeDhKey(key);
  14409. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14410. if (ret != 0) {
  14411. ERROR_OUT(-8120, done);
  14412. }
  14413. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM)
  14414. {
  14415. XFILE file = XFOPEN(dhKeyFile, "rb");
  14416. if (!file)
  14417. ERROR_OUT(-8121, done);
  14418. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  14419. XFCLOSE(file);
  14420. }
  14421. idx = 0;
  14422. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  14423. if (ret != 0) {
  14424. ERROR_OUT(-8122, done);
  14425. }
  14426. #else
  14427. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  14428. if (ret != 0) {
  14429. ERROR_OUT(-8123, done);
  14430. }
  14431. #endif
  14432. privSz = DH_TEST_BUF_SIZE;
  14433. pubSz = DH_TEST_BUF_SIZE;
  14434. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  14435. if (ret != 0) {
  14436. ERROR_OUT(-8124, done);
  14437. }
  14438. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  14439. if (ret != 0) {
  14440. ERROR_OUT(-8125, done);
  14441. }
  14442. #endif /* WOLFSSL_DH_EXTRA */
  14443. #ifndef WC_NO_RNG
  14444. ret = dh_generate_test(&rng);
  14445. if (ret != 0)
  14446. ERROR_OUT(-8126, done);
  14447. ret = dh_fips_generate_test(&rng);
  14448. if (ret != 0)
  14449. ERROR_OUT(-8127, done);
  14450. #endif /* !WC_NO_RNG */
  14451. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  14452. ret = dh_test_check_pubvalue();
  14453. if (ret != 0)
  14454. ERROR_OUT(-8128, done);
  14455. #endif
  14456. #ifndef WC_NO_RNG
  14457. /* Specialized code for key gen when using FFDHE-2048 and FFDHE-3072. */
  14458. #ifdef HAVE_FFDHE_2048
  14459. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  14460. if (ret != 0)
  14461. ERROR_OUT(-8129, done);
  14462. #endif
  14463. #ifdef HAVE_FFDHE_3072
  14464. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  14465. if (ret != 0)
  14466. ERROR_OUT(-8130, done);
  14467. #endif
  14468. #endif /* !WC_NO_RNG */
  14469. wc_FreeDhKey(key);
  14470. keyInit = 0;
  14471. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  14472. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  14473. /* Test Check Key */
  14474. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  14475. NULL, 0, 0, &rng);
  14476. if (ret != 0)
  14477. ERROR_OUT(-8131, done);
  14478. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  14479. #endif
  14480. done:
  14481. #ifndef WC_NO_RNG
  14482. wc_FreeRng(&rng);
  14483. #endif
  14484. #ifdef WOLFSSL_SMALL_STACK
  14485. if (key) {
  14486. if (keyInit)
  14487. wc_FreeDhKey(key);
  14488. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14489. }
  14490. if (key2) {
  14491. wc_FreeDhKey(key2);
  14492. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14493. }
  14494. if (tmp)
  14495. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14496. if (priv)
  14497. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14498. if (pub)
  14499. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14500. if (priv2)
  14501. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14502. if (pub2)
  14503. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14504. if (agree)
  14505. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14506. if (agree2)
  14507. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14508. #else
  14509. if (keyInit)
  14510. wc_FreeDhKey(key);
  14511. wc_FreeDhKey(key2);
  14512. #endif
  14513. (void)privSz;
  14514. (void)pubSz;
  14515. (void)pubSz2;
  14516. (void)privSz2;
  14517. return ret;
  14518. #undef DH_TEST_BUF_SIZE
  14519. #undef DH_TEST_TMP_SIZE
  14520. }
  14521. #endif /* NO_DH */
  14522. #ifndef NO_DSA
  14523. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  14524. {
  14525. int ret = 0, answer;
  14526. word32 bytes;
  14527. word32 idx = 0;
  14528. WC_RNG rng;
  14529. wc_Sha sha;
  14530. byte hash[WC_SHA_DIGEST_SIZE];
  14531. byte signature[40];
  14532. #ifdef WOLFSSL_KEY_GEN
  14533. byte* der = 0;
  14534. #endif
  14535. #define DSA_TEST_TMP_SIZE 1024
  14536. #ifdef WOLFSSL_SMALL_STACK
  14537. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14538. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14539. #ifdef WOLFSSL_KEY_GEN
  14540. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14541. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14542. #endif
  14543. if ((tmp == NULL) ||
  14544. (key == NULL)
  14545. #ifdef WOLFSSL_KEY_GEN
  14546. || (derIn == NULL)
  14547. || (genKey == NULL)
  14548. #endif
  14549. ) {
  14550. ret = -8216;
  14551. goto out;
  14552. }
  14553. #else
  14554. byte tmp[1024];
  14555. DsaKey key[1];
  14556. #ifdef WOLFSSL_KEY_GEN
  14557. DsaKey derIn[1];
  14558. DsaKey genKey[1];
  14559. #endif
  14560. #endif
  14561. #ifdef USE_CERT_BUFFERS_1024
  14562. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  14563. bytes = sizeof_dsa_key_der_1024;
  14564. #elif defined(USE_CERT_BUFFERS_2048)
  14565. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  14566. bytes = sizeof_dsa_key_der_2048;
  14567. #else
  14568. {
  14569. XFILE file = XFOPEN(dsaKey, "rb");
  14570. if (!file)
  14571. ERROR_OUT(-8200, out);
  14572. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  14573. XFCLOSE(file);
  14574. }
  14575. #endif /* USE_CERT_BUFFERS */
  14576. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  14577. if (ret != 0)
  14578. ERROR_OUT(-8201, out);
  14579. wc_ShaUpdate(&sha, tmp, bytes);
  14580. wc_ShaFinal(&sha, hash);
  14581. wc_ShaFree(&sha);
  14582. ret = wc_InitDsaKey(key);
  14583. if (ret != 0)
  14584. ERROR_OUT(-8202, out);
  14585. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  14586. if (ret != 0)
  14587. ERROR_OUT(-8203, out);
  14588. #ifndef HAVE_FIPS
  14589. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14590. #else
  14591. ret = wc_InitRng(&rng);
  14592. #endif
  14593. if (ret != 0)
  14594. ERROR_OUT(-8204, out);
  14595. ret = wc_DsaSign(hash, signature, key, &rng);
  14596. if (ret != 0)
  14597. ERROR_OUT(-8205, out);
  14598. ret = wc_DsaVerify(hash, signature, key, &answer);
  14599. if (ret != 0)
  14600. ERROR_OUT(-8206, out);
  14601. if (answer != 1)
  14602. ERROR_OUT(-8207, out);
  14603. wc_FreeDsaKey(key);
  14604. #ifdef WOLFSSL_KEY_GEN
  14605. {
  14606. int derSz = 0;
  14607. ret = wc_InitDsaKey(genKey);
  14608. if (ret != 0)
  14609. ERROR_OUT(-8208, out);
  14610. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  14611. if (ret != 0) {
  14612. wc_FreeDsaKey(genKey);
  14613. ERROR_OUT(-8209, out);
  14614. }
  14615. ret = wc_MakeDsaKey(&rng, genKey);
  14616. if (ret != 0) {
  14617. wc_FreeDsaKey(genKey);
  14618. ERROR_OUT(-8210, out);
  14619. }
  14620. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14621. if (der == NULL) {
  14622. wc_FreeDsaKey(genKey);
  14623. ERROR_OUT(-8211, out);
  14624. }
  14625. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  14626. if (derSz < 0) {
  14627. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14628. ERROR_OUT(-8212, out);
  14629. }
  14630. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14631. DSA_PRIVATEKEY_TYPE, -5814);
  14632. if (ret != 0) {
  14633. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14634. wc_FreeDsaKey(genKey);
  14635. goto out;
  14636. }
  14637. ret = wc_InitDsaKey(derIn);
  14638. if (ret != 0) {
  14639. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14640. wc_FreeDsaKey(genKey);
  14641. ERROR_OUT(-8213, out);
  14642. }
  14643. idx = 0;
  14644. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  14645. if (ret != 0) {
  14646. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14647. wc_FreeDsaKey(derIn);
  14648. wc_FreeDsaKey(genKey);
  14649. ERROR_OUT(-8214, out);
  14650. }
  14651. }
  14652. #endif /* WOLFSSL_KEY_GEN */
  14653. out:
  14654. #ifdef WOLFSSL_SMALL_STACK
  14655. if (key) {
  14656. #endif
  14657. if (wc_InitDsaKey_h(key, NULL) != 0)
  14658. ret = -8215;
  14659. #ifdef WOLFSSL_SMALL_STACK
  14660. }
  14661. #endif
  14662. #ifdef WOLFSSL_KEY_GEN
  14663. if (der)
  14664. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14665. #endif
  14666. #ifdef WOLFSSL_SMALL_STACK
  14667. if (tmp)
  14668. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14669. if (key)
  14670. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14671. #ifdef WOLFSSL_KEY_GEN
  14672. if (derIn) {
  14673. wc_FreeDsaKey(derIn);
  14674. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14675. }
  14676. if (genKey) {
  14677. wc_FreeDsaKey(genKey);
  14678. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14679. }
  14680. #endif
  14681. #else /* !WOLFSSL_SMALL_STACK */
  14682. #ifdef WOLFSSL_KEY_GEN
  14683. wc_FreeDsaKey(derIn);
  14684. wc_FreeDsaKey(genKey);
  14685. #endif
  14686. #endif
  14687. wc_FreeRng(&rng);
  14688. return ret;
  14689. }
  14690. #endif /* NO_DSA */
  14691. #ifdef WOLFCRYPT_HAVE_SRP
  14692. static int generate_random_salt(byte *buf, word32 size)
  14693. {
  14694. int ret = -8220;
  14695. WC_RNG rng;
  14696. if(NULL == buf || !size)
  14697. return -8221;
  14698. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  14699. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  14700. wc_FreeRng(&rng);
  14701. }
  14702. return ret;
  14703. }
  14704. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  14705. {
  14706. int r;
  14707. byte clientPubKey[80]; /* A */
  14708. byte serverPubKey[80]; /* B */
  14709. word32 clientPubKeySz = 80;
  14710. word32 serverPubKeySz = 80;
  14711. byte username[] = "user";
  14712. word32 usernameSz = 4;
  14713. byte password[] = "password";
  14714. word32 passwordSz = 8;
  14715. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  14716. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  14717. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  14718. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  14719. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  14720. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  14721. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  14722. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  14723. };
  14724. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  14725. 0x02
  14726. };
  14727. byte salt[10];
  14728. byte verifier[80];
  14729. word32 v_size = sizeof(verifier);
  14730. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  14731. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  14732. #ifdef WOLFSSL_SMALL_STACK
  14733. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14734. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14735. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  14736. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  14737. if ((cli == NULL) ||
  14738. (srv == NULL) ||
  14739. (clientProof == NULL) ||
  14740. (serverProof == NULL)) {
  14741. r = -8222;
  14742. goto out;
  14743. }
  14744. #else
  14745. Srp cli[1], srv[1];
  14746. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  14747. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  14748. #endif
  14749. /* set as 0's so if second init on srv not called SrpTerm is not on
  14750. * garbage values */
  14751. XMEMSET(srv, 0, sizeof *srv);
  14752. XMEMSET(cli, 0, sizeof *cli);
  14753. /* generating random salt */
  14754. r = generate_random_salt(salt, sizeof(salt));
  14755. /* client knows username and password. */
  14756. /* server knows N, g, salt and verifier. */
  14757. if (!r) r = wc_SrpInit(cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  14758. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  14759. /* loading N, g and salt in advance to generate the verifier. */
  14760. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  14761. g, sizeof(g),
  14762. salt, sizeof(salt));
  14763. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  14764. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  14765. /* client sends username to server */
  14766. if (!r) r = wc_SrpInit(srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  14767. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  14768. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  14769. g, sizeof(g),
  14770. salt, sizeof(salt));
  14771. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  14772. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  14773. /* server sends N, g, salt and B to client */
  14774. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  14775. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  14776. serverPubKey, serverPubKeySz);
  14777. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  14778. /* client sends A and M1 to server */
  14779. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  14780. serverPubKey, serverPubKeySz);
  14781. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  14782. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  14783. /* server sends M2 to client */
  14784. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  14785. wc_SrpTerm(cli);
  14786. wc_SrpTerm(srv);
  14787. #ifdef WOLFSSL_SMALL_STACK
  14788. out:
  14789. if (cli)
  14790. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14791. if (srv)
  14792. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14793. if (clientProof)
  14794. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14795. if (serverProof)
  14796. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14797. #endif
  14798. return r;
  14799. }
  14800. #endif /* WOLFCRYPT_HAVE_SRP */
  14801. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  14802. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  14803. static int openssl_aes_test(void)
  14804. {
  14805. #ifdef HAVE_AES_CBC
  14806. #ifdef WOLFSSL_AES_128
  14807. {
  14808. /* EVP_CipherUpdate test */
  14809. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  14810. {
  14811. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  14812. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  14813. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  14814. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  14815. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  14816. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  14817. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  14818. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  14819. };
  14820. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  14821. "0123456789abcdef "; /* align */
  14822. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  14823. "1234567890abcdef "; /* align */
  14824. byte cipher[AES_BLOCK_SIZE * 4];
  14825. byte plain [AES_BLOCK_SIZE * 4];
  14826. #ifdef WOLFSSL_SMALL_STACK
  14827. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  14828. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  14829. #else
  14830. EVP_CIPHER_CTX en[1];
  14831. EVP_CIPHER_CTX de[1];
  14832. #endif
  14833. int outlen ;
  14834. int total = 0;
  14835. int i;
  14836. #ifdef WOLFSSL_SMALL_STACK
  14837. if ((en == NULL) || (de == NULL))
  14838. return MEMORY_E;
  14839. #endif
  14840. EVP_CIPHER_CTX_init(en);
  14841. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14842. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14843. return -8400;
  14844. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14845. (byte*)cbcPlain, 9) == 0)
  14846. return -8401;
  14847. if (outlen != 0)
  14848. return -8402;
  14849. total += outlen;
  14850. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  14851. (byte*)&cbcPlain[9] , 9) == 0)
  14852. return -8403;
  14853. if (outlen != 16)
  14854. return -8404;
  14855. total += outlen;
  14856. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14857. return -8405;
  14858. if (outlen != 16)
  14859. return -8406;
  14860. total += outlen;
  14861. if (total != 32)
  14862. return 3408;
  14863. total = 0;
  14864. EVP_CIPHER_CTX_init(de);
  14865. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14866. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14867. return -8407;
  14868. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  14869. return -8408;
  14870. if (outlen != 0)
  14871. return -8409;
  14872. total += outlen;
  14873. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14874. (byte*)&cipher[6], 12) == 0)
  14875. return -8410;
  14876. if (outlen != 0)
  14877. total += outlen;
  14878. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14879. (byte*)&cipher[6+12], 14) == 0)
  14880. return -8411;
  14881. if (outlen != 16)
  14882. return -8412;
  14883. total += outlen;
  14884. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14885. return -8413;
  14886. if (outlen != 2)
  14887. return -8414;
  14888. total += outlen;
  14889. if (total != 18)
  14890. return 3427;
  14891. if (XMEMCMP(plain, cbcPlain, 18))
  14892. return -8415;
  14893. /* test with encrypting/decrypting more than 16 bytes at once */
  14894. total = 0;
  14895. EVP_CIPHER_CTX_init(en);
  14896. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14897. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14898. return -8416;
  14899. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14900. (byte*)cbcPlain, 17) == 0)
  14901. return -8417;
  14902. if (outlen != 16)
  14903. return -8418;
  14904. total += outlen;
  14905. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  14906. (byte*)&cbcPlain[17] , 1) == 0)
  14907. return -8419;
  14908. if (outlen != 0)
  14909. return -8420;
  14910. total += outlen;
  14911. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14912. return -8421;
  14913. if (outlen != 16)
  14914. return -8422;
  14915. total += outlen;
  14916. if (total != 32)
  14917. return -8423;
  14918. total = 0;
  14919. EVP_CIPHER_CTX_init(de);
  14920. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14921. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14922. return -8424;
  14923. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  14924. return -8425;
  14925. if (outlen != 16)
  14926. return -8426;
  14927. total += outlen;
  14928. /* final call on non block size should fail */
  14929. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  14930. return -8427;
  14931. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14932. (byte*)&cipher[17], 1) == 0)
  14933. return -8428;
  14934. if (outlen != 0)
  14935. total += outlen;
  14936. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  14937. (byte*)&cipher[17+1], 14) == 0)
  14938. return -8429;
  14939. if (outlen != 0)
  14940. return -8430;
  14941. total += outlen;
  14942. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14943. return -8431;
  14944. if (outlen != 2)
  14945. return -8432;
  14946. total += outlen;
  14947. if (total != 18)
  14948. return -8433;
  14949. if (XMEMCMP(plain, cbcPlain, 18))
  14950. return -8434;
  14951. /* test byte by byte decrypt */
  14952. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  14953. plain[i] = i;
  14954. }
  14955. total = 0;
  14956. EVP_CIPHER_CTX_init(en);
  14957. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  14958. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  14959. return -8435;
  14960. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  14961. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  14962. return -8436;
  14963. if (outlen != AES_BLOCK_SIZE * 3)
  14964. return -8437;
  14965. total += outlen;
  14966. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  14967. return -8438;
  14968. if (outlen != AES_BLOCK_SIZE)
  14969. return -8439;
  14970. total += outlen;
  14971. if (total != sizeof(plain))
  14972. return -8440;
  14973. total = 0;
  14974. EVP_CIPHER_CTX_init(de);
  14975. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  14976. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  14977. return -8441;
  14978. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  14979. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  14980. (byte*)cipher + i, 1) == 0)
  14981. return -8442;
  14982. if (outlen > 0) {
  14983. int j;
  14984. total += outlen;
  14985. for (j = 0; j < total; j++) {
  14986. if (plain[j] != j) {
  14987. return -8443;
  14988. }
  14989. }
  14990. }
  14991. }
  14992. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  14993. return -8444;
  14994. total += outlen;
  14995. if (total != AES_BLOCK_SIZE * 3) {
  14996. return -8445;
  14997. }
  14998. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  14999. if (plain[i] != i) {
  15000. return -8446;
  15001. }
  15002. }
  15003. #ifdef WOLFSSL_SMALL_STACK
  15004. wolfSSL_EVP_CIPHER_CTX_free(en);
  15005. wolfSSL_EVP_CIPHER_CTX_free(de);
  15006. #endif
  15007. }
  15008. /* set buffers to be exact size to catch potential over read/write */
  15009. {
  15010. /* EVP_CipherUpdate test */
  15011. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  15012. {
  15013. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15014. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15015. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15016. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15017. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15018. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15019. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15020. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15021. };
  15022. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15023. "0123456789abcdef "; /* align */
  15024. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15025. "1234567890abcdef "; /* align */
  15026. #define EVP_TEST_BUF_SZ 18
  15027. #define EVP_TEST_BUF_PAD 32
  15028. byte cipher[EVP_TEST_BUF_SZ];
  15029. byte plain [EVP_TEST_BUF_SZ];
  15030. byte padded[EVP_TEST_BUF_PAD];
  15031. #ifdef WOLFSSL_SMALL_STACK
  15032. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15033. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15034. #else
  15035. EVP_CIPHER_CTX en[1];
  15036. EVP_CIPHER_CTX de[1];
  15037. #endif
  15038. int outlen ;
  15039. int total = 0;
  15040. #ifdef WOLFSSL_SMALL_STACK
  15041. if ((en == NULL) || (de == NULL))
  15042. return MEMORY_E;
  15043. #endif
  15044. EVP_CIPHER_CTX_init(en);
  15045. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15046. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15047. return -8447;
  15048. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  15049. return -8448;
  15050. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  15051. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  15052. return -8449;
  15053. if (outlen != 16)
  15054. return -8450;
  15055. total += outlen;
  15056. /* should fail here */
  15057. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  15058. return -8451;
  15059. /* turn padding back on and do successful encrypt */
  15060. total = 0;
  15061. EVP_CIPHER_CTX_init(en);
  15062. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  15063. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  15064. return -8452;
  15065. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  15066. return -8453;
  15067. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  15068. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  15069. return -8454;
  15070. if (outlen != 16)
  15071. return -8455;
  15072. total += outlen;
  15073. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  15074. return -8456;
  15075. total += outlen;
  15076. if (total != 32)
  15077. return -8457;
  15078. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  15079. /* test out of bounds read on buffers w/o padding during decryption */
  15080. total = 0;
  15081. EVP_CIPHER_CTX_init(de);
  15082. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15083. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15084. return -8458;
  15085. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  15086. return -8459;
  15087. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  15088. EVP_TEST_BUF_SZ) == 0)
  15089. return -8460;
  15090. if (outlen != 16)
  15091. return -8461;
  15092. total += outlen;
  15093. /* should fail since not using padding */
  15094. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  15095. return -8462;
  15096. total = 0;
  15097. EVP_CIPHER_CTX_init(de);
  15098. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  15099. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  15100. return -8463;
  15101. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  15102. return -8464;
  15103. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  15104. EVP_TEST_BUF_PAD) == 0)
  15105. return -8465;
  15106. if (outlen != 16)
  15107. return -8466;
  15108. total += outlen;
  15109. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  15110. return -8467;
  15111. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  15112. return -8468;
  15113. #ifdef WOLFSSL_SMALL_STACK
  15114. wolfSSL_EVP_CIPHER_CTX_free(en);
  15115. wolfSSL_EVP_CIPHER_CTX_free(de);
  15116. #endif
  15117. }
  15118. { /* evp_cipher test: EVP_aes_128_cbc */
  15119. #ifdef WOLFSSL_SMALL_STACK
  15120. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15121. #else
  15122. EVP_CIPHER_CTX ctx[1];
  15123. #endif
  15124. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  15125. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15126. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15127. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15128. };
  15129. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15130. {
  15131. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15132. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  15133. };
  15134. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15135. "0123456789abcdef "; /* align */
  15136. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15137. "1234567890abcdef "; /* align */
  15138. byte cipher[AES_BLOCK_SIZE * 4];
  15139. byte plain [AES_BLOCK_SIZE * 4];
  15140. #ifdef WOLFSSL_SMALL_STACK
  15141. if (ctx == NULL)
  15142. return MEMORY_E;
  15143. #endif
  15144. EVP_CIPHER_CTX_init(ctx);
  15145. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  15146. return -8469;
  15147. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15148. return -8470;
  15149. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15150. return -8471;
  15151. EVP_CIPHER_CTX_init(ctx);
  15152. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  15153. return -8472;
  15154. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15155. return -8473;
  15156. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15157. return -8474;
  15158. #ifdef WOLFSSL_SMALL_STACK
  15159. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15160. #endif
  15161. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15162. #endif /* WOLFSSL_AES_128 */
  15163. #endif /* HAVE_AES_CBC */
  15164. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15165. { /* evp_cipher test: EVP_aes_256_ecb*/
  15166. #ifdef WOLFSSL_SMALL_STACK
  15167. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15168. #else
  15169. EVP_CIPHER_CTX ctx[1];
  15170. #endif
  15171. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15172. {
  15173. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15174. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15175. };
  15176. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15177. {
  15178. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15179. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15180. };
  15181. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15182. {
  15183. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15184. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15185. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15186. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15187. };
  15188. byte cipher[AES_BLOCK_SIZE * 4];
  15189. byte plain [AES_BLOCK_SIZE * 4];
  15190. #ifdef WOLFSSL_SMALL_STACK
  15191. if (ctx == NULL)
  15192. return MEMORY_E;
  15193. #endif
  15194. EVP_CIPHER_CTX_init(ctx);
  15195. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  15196. return -8475;
  15197. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  15198. return -8476;
  15199. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15200. return -8477;
  15201. EVP_CIPHER_CTX_init(ctx);
  15202. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  15203. return -8478;
  15204. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  15205. return -8479;
  15206. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15207. return -8480;
  15208. #ifdef WOLFSSL_SMALL_STACK
  15209. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15210. #endif
  15211. } /* end evp_cipher test */
  15212. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  15213. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  15214. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  15215. {
  15216. /* Test: AES_encrypt/decrypt/set Key */
  15217. #ifdef WOLFSSL_SMALL_STACK
  15218. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15219. #ifdef HAVE_AES_DECRYPT
  15220. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15221. #endif
  15222. #else
  15223. AES_KEY enc[1];
  15224. #ifdef HAVE_AES_DECRYPT
  15225. AES_KEY dec[1];
  15226. #endif
  15227. #endif
  15228. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15229. {
  15230. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15231. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15232. };
  15233. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  15234. {
  15235. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15236. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15237. };
  15238. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15239. {
  15240. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15241. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15242. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15243. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15244. };
  15245. byte plain[sizeof(msg)];
  15246. byte cipher[sizeof(msg)];
  15247. #ifdef WOLFSSL_SMALL_STACK
  15248. if (enc == NULL)
  15249. return MEMORY_E;
  15250. #ifdef HAVE_AES_DECRYPT
  15251. if (dec == NULL)
  15252. return MEMORY_E;
  15253. #endif
  15254. #endif
  15255. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  15256. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  15257. AES_encrypt(msg, cipher, enc);
  15258. #ifdef HAVE_AES_DECRYPT
  15259. AES_decrypt(cipher, plain, dec);
  15260. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  15261. return -8481;
  15262. #endif /* HAVE_AES_DECRYPT */
  15263. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  15264. return -8482;
  15265. #ifdef WOLFSSL_SMALL_STACK
  15266. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15267. #ifdef HAVE_AES_DECRYPT
  15268. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15269. #endif
  15270. #endif
  15271. }
  15272. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  15273. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  15274. #ifdef WOLFSSL_AES_COUNTER
  15275. {
  15276. byte plainBuff [64];
  15277. byte cipherBuff[64];
  15278. #ifdef WOLFSSL_AES_128
  15279. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  15280. {
  15281. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15282. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15283. };
  15284. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  15285. {
  15286. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15287. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15288. };
  15289. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  15290. {
  15291. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15292. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15293. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15294. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15295. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15296. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15297. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15298. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15299. };
  15300. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  15301. {
  15302. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  15303. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  15304. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  15305. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  15306. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  15307. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  15308. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  15309. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  15310. };
  15311. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  15312. {
  15313. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  15314. 0xc2
  15315. };
  15316. #endif
  15317. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15318. * NIST Special Publication 800-38A */
  15319. #ifdef WOLFSSL_AES_192
  15320. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  15321. {
  15322. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  15323. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  15324. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  15325. };
  15326. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  15327. {
  15328. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15329. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15330. };
  15331. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  15332. {
  15333. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15334. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15335. };
  15336. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  15337. {
  15338. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  15339. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  15340. };
  15341. #endif /* WOLFSSL_AES_192 */
  15342. #ifdef WOLFSSL_AES_256
  15343. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  15344. * NIST Special Publication 800-38A */
  15345. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  15346. {
  15347. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15348. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15349. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15350. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15351. };
  15352. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  15353. {
  15354. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  15355. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  15356. };
  15357. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  15358. {
  15359. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15360. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15361. };
  15362. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  15363. {
  15364. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  15365. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  15366. };
  15367. #endif /* WOLFSSL_AES_256 */
  15368. #ifdef WOLFSSL_SMALL_STACK
  15369. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15370. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15371. #else
  15372. EVP_CIPHER_CTX en[1];
  15373. EVP_CIPHER_CTX de[1];
  15374. #endif
  15375. #ifdef WOLFSSL_AES_128
  15376. #ifndef WOLFSSL_SMALL_STACK
  15377. EVP_CIPHER_CTX *p_en;
  15378. EVP_CIPHER_CTX *p_de;
  15379. #endif
  15380. #ifdef WOLFSSL_SMALL_STACK
  15381. if ((en == NULL) || (de == NULL))
  15382. return MEMORY_E;
  15383. #endif
  15384. EVP_CIPHER_CTX_init(en);
  15385. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15386. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15387. return -8483;
  15388. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  15389. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15390. return -8484;
  15391. EVP_CIPHER_CTX_init(de);
  15392. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15393. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15394. return -8485;
  15395. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15396. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15397. return -8486;
  15398. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15399. return -8487;
  15400. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15401. return -8488;
  15402. #ifndef WOLFSSL_SMALL_STACK
  15403. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  15404. if (p_en == NULL)
  15405. return -8489;
  15406. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  15407. if (p_de == NULL)
  15408. return -8490;
  15409. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  15410. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15411. return -8491;
  15412. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  15413. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15414. return -8492;
  15415. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  15416. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15417. return -8493;
  15418. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  15419. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  15420. return -8494;
  15421. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  15422. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  15423. #endif /* !WOLFSSL_SMALL_STACK */
  15424. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  15425. return -8495;
  15426. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  15427. return -8496;
  15428. EVP_CIPHER_CTX_init(en);
  15429. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  15430. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15431. return -8497;
  15432. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15433. return -8498;
  15434. EVP_CIPHER_CTX_init(de);
  15435. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  15436. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  15437. return -8499;
  15438. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15439. return -8500;
  15440. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15441. return -8501;
  15442. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  15443. return -8502;
  15444. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  15445. return -8503;
  15446. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  15447. return -8504;
  15448. if (XMEMCMP(plainBuff, ctrPlain, 9))
  15449. return -8505;
  15450. if (XMEMCMP(cipherBuff, oddCipher, 9))
  15451. return -8506;
  15452. #endif /* WOLFSSL_AES_128 */
  15453. #ifdef WOLFSSL_AES_192
  15454. EVP_CIPHER_CTX_init(en);
  15455. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  15456. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15457. return -8507;
  15458. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  15459. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15460. return -8508;
  15461. EVP_CIPHER_CTX_init(de);
  15462. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  15463. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  15464. return -8509;
  15465. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15466. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15467. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15468. return -8510;
  15469. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  15470. return -8511;
  15471. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  15472. return -8512;
  15473. #endif /* WOLFSSL_AES_192 */
  15474. #ifdef WOLFSSL_AES_256
  15475. EVP_CIPHER_CTX_init(en);
  15476. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  15477. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15478. return -8513;
  15479. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  15480. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15481. return -8514;
  15482. EVP_CIPHER_CTX_init(de);
  15483. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  15484. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  15485. return -8515;
  15486. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  15487. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  15488. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  15489. return -8516;
  15490. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  15491. return -8517;
  15492. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  15493. return -8518;
  15494. #ifdef WOLFSSL_SMALL_STACK
  15495. wolfSSL_EVP_CIPHER_CTX_free(en);
  15496. wolfSSL_EVP_CIPHER_CTX_free(de);
  15497. #endif
  15498. #endif /* WOLFSSL_AES_256 */
  15499. }
  15500. #endif /* HAVE_AES_COUNTER */
  15501. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  15502. {
  15503. #ifdef WOLFSSL_SMALL_STACK
  15504. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15505. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15506. #else
  15507. AES_KEY enc[1];
  15508. AES_KEY dec[1];
  15509. #endif
  15510. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  15511. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  15512. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  15513. };
  15514. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15515. {
  15516. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  15517. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  15518. };
  15519. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  15520. {
  15521. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  15522. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  15523. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  15524. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  15525. };
  15526. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  15527. {
  15528. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15529. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15530. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15531. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  15532. };
  15533. byte cipher[AES_BLOCK_SIZE * 2];
  15534. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  15535. int num = 0;
  15536. #ifdef WOLFSSL_SMALL_STACK
  15537. if ((enc == NULL) || (dec == NULL))
  15538. return MEMORY_E;
  15539. #endif
  15540. XMEMCPY(iv, setIv, sizeof(setIv));
  15541. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  15542. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  15543. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  15544. &num, AES_ENCRYPT);
  15545. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  15546. return -8519;
  15547. if (num != 15) /* should have used 15 of the 16 bytes */
  15548. return -8520;
  15549. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  15550. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  15551. &num, AES_ENCRYPT);
  15552. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  15553. return -8521;
  15554. if (num != 0)
  15555. return -8522;
  15556. #ifdef WOLFSSL_SMALL_STACK
  15557. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  15558. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  15559. #endif
  15560. }
  15561. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  15562. return 0;
  15563. }
  15564. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  15565. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  15566. {
  15567. int ret;
  15568. EVP_MD_CTX md_ctx;
  15569. testVector a, b, c, d, e, f;
  15570. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  15571. a.inLen = 0;
  15572. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  15573. (void)a;
  15574. (void)b;
  15575. (void)c;
  15576. (void)d;
  15577. (void)e;
  15578. (void)f;
  15579. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  15580. {
  15581. byte* p;
  15582. p = (byte*)CRYPTO_malloc(10);
  15583. if (p == NULL) {
  15584. return -8600;
  15585. }
  15586. XMEMSET(p, 0, 10);
  15587. CRYPTO_free(p);
  15588. }
  15589. #ifndef NO_MD5
  15590. a.input = "1234567890123456789012345678901234567890123456789012345678"
  15591. "9012345678901234567890";
  15592. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  15593. "\x7a";
  15594. a.inLen = XSTRLEN(a.input);
  15595. a.outLen = WC_MD5_DIGEST_SIZE;
  15596. EVP_MD_CTX_init(&md_ctx);
  15597. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  15598. if (ret == WOLFSSL_SUCCESS) {
  15599. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  15600. if (ret == WOLFSSL_SUCCESS)
  15601. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15602. }
  15603. EVP_MD_CTX_cleanup(&md_ctx);
  15604. if (ret != WOLFSSL_SUCCESS ||
  15605. XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  15606. return -8601;
  15607. }
  15608. #endif /* NO_MD5 */
  15609. #ifndef NO_SHA
  15610. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  15611. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  15612. "aaaaaaaaaa";
  15613. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  15614. "\x53\x99\x5E\x26\xA0";
  15615. b.inLen = XSTRLEN(b.input);
  15616. b.outLen = WC_SHA_DIGEST_SIZE;
  15617. EVP_MD_CTX_init(&md_ctx);
  15618. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  15619. if (ret == WOLFSSL_SUCCESS) {
  15620. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  15621. if (ret == WOLFSSL_SUCCESS)
  15622. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15623. }
  15624. EVP_MD_CTX_cleanup(&md_ctx);
  15625. if (ret != WOLFSSL_SUCCESS ||
  15626. XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0) {
  15627. return -8602;
  15628. }
  15629. #endif /* NO_SHA */
  15630. #ifdef WOLFSSL_SHA224
  15631. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15632. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15633. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  15634. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  15635. e.inLen = XSTRLEN(e.input);
  15636. e.outLen = WC_SHA224_DIGEST_SIZE;
  15637. EVP_MD_CTX_init(&md_ctx);
  15638. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  15639. if (ret == WOLFSSL_SUCCESS) {
  15640. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15641. if (ret == WOLFSSL_SUCCESS)
  15642. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15643. }
  15644. EVP_MD_CTX_cleanup(&md_ctx);
  15645. if (ret != WOLFSSL_SUCCESS ||
  15646. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  15647. return -8603;
  15648. }
  15649. #endif /* WOLFSSL_SHA224 */
  15650. #ifndef NO_SHA256
  15651. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  15652. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  15653. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  15654. "\x06\xC1";
  15655. d.inLen = XSTRLEN(d.input);
  15656. d.outLen = WC_SHA256_DIGEST_SIZE;
  15657. EVP_MD_CTX_init(&md_ctx);
  15658. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  15659. if (ret == WOLFSSL_SUCCESS) {
  15660. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  15661. if (ret == WOLFSSL_SUCCESS)
  15662. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15663. }
  15664. EVP_MD_CTX_cleanup(&md_ctx);
  15665. if (ret != WOLFSSL_SUCCESS ||
  15666. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  15667. return -8604;
  15668. }
  15669. #endif /* !NO_SHA256 */
  15670. #ifdef WOLFSSL_SHA384
  15671. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15672. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15673. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  15674. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  15675. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  15676. "\x74\x60\x39";
  15677. e.inLen = XSTRLEN(e.input);
  15678. e.outLen = WC_SHA384_DIGEST_SIZE;
  15679. EVP_MD_CTX_init(&md_ctx);
  15680. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  15681. if (ret == WOLFSSL_SUCCESS) {
  15682. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15683. if (ret == WOLFSSL_SUCCESS)
  15684. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15685. }
  15686. EVP_MD_CTX_cleanup(&md_ctx);
  15687. if (ret != WOLFSSL_SUCCESS ||
  15688. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  15689. return -8605;
  15690. }
  15691. #endif /* WOLFSSL_SHA384 */
  15692. #ifdef WOLFSSL_SHA512
  15693. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15694. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15695. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  15696. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  15697. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  15698. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  15699. "\x87\x4b\xe9\x09";
  15700. f.inLen = XSTRLEN(f.input);
  15701. f.outLen = WC_SHA512_DIGEST_SIZE;
  15702. EVP_MD_CTX_init(&md_ctx);
  15703. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  15704. if (ret == WOLFSSL_SUCCESS) {
  15705. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  15706. if (ret == WOLFSSL_SUCCESS)
  15707. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15708. }
  15709. EVP_MD_CTX_cleanup(&md_ctx);
  15710. if (ret != WOLFSSL_SUCCESS ||
  15711. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  15712. return -8606;
  15713. }
  15714. #endif /* WOLFSSL_SHA512 */
  15715. #ifdef WOLFSSL_SHA3
  15716. #ifndef WOLFSSL_NOSHA3_224
  15717. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15718. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15719. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  15720. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  15721. e.inLen = XSTRLEN(e.input);
  15722. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  15723. EVP_MD_CTX_init(&md_ctx);
  15724. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  15725. if (ret == WOLFSSL_SUCCESS) {
  15726. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15727. if (ret == WOLFSSL_SUCCESS)
  15728. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15729. }
  15730. EVP_MD_CTX_cleanup(&md_ctx);
  15731. if (ret != WOLFSSL_SUCCESS ||
  15732. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  15733. return -8607;
  15734. }
  15735. #endif /* WOLFSSL_NOSHA3_224 */
  15736. #ifndef WOLFSSL_NOSHA3_256
  15737. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15738. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15739. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  15740. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  15741. "\x1d\x18";
  15742. d.inLen = XSTRLEN(d.input);
  15743. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  15744. EVP_MD_CTX_init(&md_ctx);
  15745. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  15746. if (ret == WOLFSSL_SUCCESS) {
  15747. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  15748. if (ret == WOLFSSL_SUCCESS)
  15749. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15750. }
  15751. EVP_MD_CTX_cleanup(&md_ctx);
  15752. if (ret != WOLFSSL_SUCCESS ||
  15753. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  15754. return -8608;
  15755. }
  15756. #endif /* WOLFSSL_NOSHA3_256 */
  15757. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15758. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15759. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  15760. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  15761. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  15762. "\xaa\x7f\xc7";
  15763. e.inLen = XSTRLEN(e.input);
  15764. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  15765. EVP_MD_CTX_init(&md_ctx);
  15766. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  15767. if (ret == WOLFSSL_SUCCESS) {
  15768. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  15769. if (ret == WOLFSSL_SUCCESS)
  15770. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15771. }
  15772. EVP_MD_CTX_cleanup(&md_ctx);
  15773. if (ret != WOLFSSL_SUCCESS ||
  15774. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  15775. return -8609;
  15776. }
  15777. #ifndef WOLFSSL_NOSHA3_512
  15778. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  15779. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  15780. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  15781. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  15782. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  15783. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  15784. "\xf2\x1d\xd1\x85";
  15785. f.inLen = XSTRLEN(f.input);
  15786. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  15787. EVP_MD_CTX_init(&md_ctx);
  15788. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  15789. if (ret == WOLFSSL_SUCCESS) {
  15790. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  15791. if (ret == WOLFSSL_SUCCESS)
  15792. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  15793. }
  15794. EVP_MD_CTX_cleanup(&md_ctx);
  15795. if (ret != WOLFSSL_SUCCESS ||
  15796. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  15797. return -8610;
  15798. }
  15799. #endif /* WOLFSSL_NOSHA3_512 */
  15800. #endif /* WOLFSSL_SHA3 */
  15801. #ifndef WC_NO_RNG
  15802. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  15803. return -8611;
  15804. #endif
  15805. #ifndef NO_MD5
  15806. c.input = "what do ya want for nothing?";
  15807. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  15808. "\x76";
  15809. c.inLen = XSTRLEN(c.input);
  15810. c.outLen = WC_MD5_DIGEST_SIZE;
  15811. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  15812. hash, 0) == NULL ||
  15813. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  15814. {
  15815. return -8612;
  15816. }
  15817. #endif /* NO_MD5 */
  15818. #ifndef NO_DES3
  15819. { /* des test */
  15820. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  15821. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15822. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15823. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15824. };
  15825. byte plain[24];
  15826. byte cipher[24];
  15827. const_DES_cblock key = {
  15828. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  15829. };
  15830. DES_cblock iv = {
  15831. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  15832. };
  15833. DES_key_schedule sched;
  15834. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15835. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  15836. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  15837. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  15838. };
  15839. DES_key_sched(&key, &sched);
  15840. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  15841. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  15842. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  15843. return -8613;
  15844. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  15845. return -8614;
  15846. /* test changing iv */
  15847. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  15848. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  15849. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  15850. return -8615;
  15851. } /* end des test */
  15852. #endif /* NO_DES3 */
  15853. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  15854. if (openssl_aes_test() != 0) {
  15855. return -8616;
  15856. }
  15857. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  15858. { /* evp_cipher test: EVP_aes_128_cbc */
  15859. #ifdef WOLFSSL_SMALL_STACK
  15860. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15861. #else
  15862. EVP_CIPHER_CTX ctx[1];
  15863. #endif
  15864. int idx, cipherSz, plainSz;
  15865. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  15866. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15867. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15868. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15869. };
  15870. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15871. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15872. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  15873. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  15874. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  15875. };
  15876. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  15877. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  15878. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  15879. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  15880. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  15881. };
  15882. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15883. "0123456789abcdef "; /* align */
  15884. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15885. "1234567890abcdef "; /* align */
  15886. byte cipher[AES_BLOCK_SIZE * 4];
  15887. byte plain [AES_BLOCK_SIZE * 4];
  15888. #ifdef WOLFSSL_SMALL_STACK
  15889. if (ctx == NULL)
  15890. return MEMORY_E;
  15891. #endif
  15892. cipherSz = 0;
  15893. EVP_CIPHER_CTX_init(ctx);
  15894. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  15895. if (ret == WOLFSSL_SUCCESS) {
  15896. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  15897. if (ret == WOLFSSL_SUCCESS)
  15898. cipherSz += idx;
  15899. }
  15900. if (ret == WOLFSSL_SUCCESS) {
  15901. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  15902. if (ret == WOLFSSL_SUCCESS)
  15903. cipherSz += idx;
  15904. }
  15905. EVP_CIPHER_CTX_cleanup(ctx);
  15906. if (ret != WOLFSSL_SUCCESS)
  15907. return -8617;
  15908. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  15909. return -8618;
  15910. /* check partial decrypt (not enough padding for full block) */
  15911. plainSz = 0;
  15912. EVP_CIPHER_CTX_init(ctx);
  15913. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  15914. if (ret == WOLFSSL_SUCCESS) {
  15915. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  15916. if (ret == WOLFSSL_SUCCESS)
  15917. plainSz += idx;
  15918. }
  15919. if (ret == WOLFSSL_SUCCESS) {
  15920. /* this test should fail... not enough padding for full block */
  15921. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  15922. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  15923. ret = WOLFSSL_SUCCESS;
  15924. else
  15925. ret = -8619;
  15926. }
  15927. else
  15928. ret = -8620;
  15929. EVP_CIPHER_CTX_cleanup(ctx);
  15930. if (ret != WOLFSSL_SUCCESS)
  15931. return ret;
  15932. plainSz = 0;
  15933. EVP_CIPHER_CTX_init(ctx);
  15934. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  15935. if (ret == WOLFSSL_SUCCESS) {
  15936. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  15937. if (ret == WOLFSSL_SUCCESS)
  15938. plainSz += idx;
  15939. }
  15940. if (ret == WOLFSSL_SUCCESS) {
  15941. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  15942. if (ret == WOLFSSL_SUCCESS)
  15943. plainSz += idx;
  15944. }
  15945. EVP_CIPHER_CTX_cleanup(ctx);
  15946. if (ret != WOLFSSL_SUCCESS)
  15947. return -8621;
  15948. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  15949. return -8622;
  15950. cipherSz = 0;
  15951. EVP_CIPHER_CTX_init(ctx);
  15952. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  15953. if (ret == WOLFSSL_SUCCESS) {
  15954. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  15955. if (ret == WOLFSSL_SUCCESS)
  15956. cipherSz += idx;
  15957. }
  15958. if (ret == WOLFSSL_SUCCESS) {
  15959. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  15960. if (ret == WOLFSSL_SUCCESS)
  15961. cipherSz += idx;
  15962. }
  15963. EVP_CIPHER_CTX_cleanup(ctx);
  15964. if (ret != WOLFSSL_SUCCESS)
  15965. return -8623;
  15966. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  15967. return -8624;
  15968. #ifdef WOLFSSL_SMALL_STACK
  15969. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  15970. #endif
  15971. } /* end evp_cipher test: EVP_aes_128_cbc*/
  15972. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  15973. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  15974. { /* evp_cipher test: EVP_aes_256_ecb*/
  15975. #ifdef WOLFSSL_SMALL_STACK
  15976. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  15977. #else
  15978. EVP_CIPHER_CTX ctx[1];
  15979. #endif
  15980. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  15981. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15982. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  15983. };
  15984. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  15985. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  15986. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  15987. };
  15988. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  15989. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  15990. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  15991. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  15992. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  15993. };
  15994. byte cipher[AES_BLOCK_SIZE * 4];
  15995. byte plain [AES_BLOCK_SIZE * 4];
  15996. #ifdef WOLFSSL_SMALL_STACK
  15997. if (ctx == NULL)
  15998. return MEMORY_E;
  15999. #endif
  16000. EVP_CIPHER_CTX_init(ctx);
  16001. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  16002. if (ret == WOLFSSL_SUCCESS)
  16003. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  16004. EVP_CIPHER_CTX_cleanup(ctx);
  16005. if (ret != 16)
  16006. return -8625;
  16007. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16008. return -8626;
  16009. EVP_CIPHER_CTX_init(ctx);
  16010. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  16011. if (ret == WOLFSSL_SUCCESS)
  16012. ret = EVP_Cipher(ctx, plain, cipher, 16);
  16013. EVP_CIPHER_CTX_cleanup(ctx);
  16014. if (ret != 16)
  16015. return -8627;
  16016. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16017. return -8628;
  16018. #ifdef WOLFSSL_SMALL_STACK
  16019. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16020. #endif
  16021. } /* end evp_cipher test */
  16022. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  16023. #define OPENSSL_TEST_ERROR (-10000)
  16024. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16025. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16026. {
  16027. /* Test: AES_encrypt/decrypt/set Key */
  16028. #ifdef WOLFSSL_SMALL_STACK
  16029. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16030. #ifdef HAVE_AES_DECRYPT
  16031. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16032. #endif
  16033. #else
  16034. AES_KEY enc[1];
  16035. #ifdef HAVE_AES_DECRYPT
  16036. AES_KEY dec[1];
  16037. #endif
  16038. #endif
  16039. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16040. {
  16041. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16042. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16043. };
  16044. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16045. {
  16046. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16047. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16048. };
  16049. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16050. {
  16051. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16052. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16053. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16054. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16055. };
  16056. byte plain[sizeof(msg)];
  16057. byte cipher[sizeof(msg)];
  16058. printf("openSSL extra test\n") ;
  16059. #ifdef WOLFSSL_SMALL_STACK
  16060. if (enc == NULL)
  16061. return MEMORY_E;
  16062. #ifdef HAVE_AES_DECRYPT
  16063. if (dec == NULL)
  16064. return MEMORY_E;
  16065. #endif
  16066. #endif
  16067. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16068. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16069. AES_encrypt(msg, cipher, enc);
  16070. #ifdef HAVE_AES_DECRYPT
  16071. AES_decrypt(cipher, plain, dec);
  16072. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16073. return OPENSSL_TEST_ERROR-60;
  16074. #endif /* HAVE_AES_DECRYPT */
  16075. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16076. return OPENSSL_TEST_ERROR-61;
  16077. #ifdef WOLFSSL_SMALL_STACK
  16078. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16079. #ifdef HAVE_AES_DECRYPT
  16080. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16081. #endif
  16082. #endif
  16083. }
  16084. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16085. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16086. #ifdef WOLFSSL_AES_COUNTER
  16087. {
  16088. byte plainBuff [64];
  16089. byte cipherBuff[64];
  16090. #ifdef WOLFSSL_AES_128
  16091. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16092. {
  16093. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16094. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16095. };
  16096. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16097. {
  16098. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16099. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16100. };
  16101. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16102. {
  16103. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16104. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16105. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16106. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16107. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16108. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16109. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16110. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16111. };
  16112. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16113. {
  16114. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16115. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16116. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16117. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16118. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16119. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16120. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16121. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16122. };
  16123. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16124. {
  16125. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16126. 0xc2
  16127. };
  16128. #endif /* WOLFSSL_AES_128 */
  16129. #ifdef WOLFSSL_AES_192
  16130. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16131. * NIST Special Publication 800-38A */
  16132. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16133. {
  16134. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16135. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16136. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16137. };
  16138. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16139. {
  16140. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16141. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16142. };
  16143. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16144. {
  16145. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16146. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16147. };
  16148. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16149. {
  16150. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16151. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16152. };
  16153. #endif /* WOLFSSL_AES_192 */
  16154. #ifdef WOLFSSL_AES_256
  16155. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16156. * NIST Special Publication 800-38A */
  16157. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16158. {
  16159. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16160. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16161. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16162. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16163. };
  16164. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16165. {
  16166. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16167. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16168. };
  16169. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16170. {
  16171. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16172. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16173. };
  16174. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16175. {
  16176. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16177. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16178. };
  16179. #endif /* WOLFSSL_AES_256 */
  16180. #ifdef WOLFSSL_SMALL_STACK
  16181. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16182. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16183. if ((en == NULL) || (de == NULL))
  16184. return MEMORY_E;
  16185. #else
  16186. EVP_CIPHER_CTX en[1];
  16187. EVP_CIPHER_CTX de[1];
  16188. #endif
  16189. #ifdef WOLFSSL_AES_128
  16190. #ifndef WOLFSSL_SMALL_STACK
  16191. EVP_CIPHER_CTX *p_en;
  16192. EVP_CIPHER_CTX *p_de;
  16193. #endif
  16194. EVP_CIPHER_CTX_init(en);
  16195. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16196. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16197. return -8629;
  16198. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16199. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16200. return -8630;
  16201. EVP_CIPHER_CTX_init(de);
  16202. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16203. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16204. return -8631;
  16205. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16206. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16207. return -8632;
  16208. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16209. return -8633;
  16210. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16211. return -8634;
  16212. #ifndef WOLFSSL_SMALL_STACK
  16213. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16214. if(p_en == NULL)return -8635;
  16215. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16216. if(p_de == NULL)return -8636;
  16217. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16218. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16219. return -8637;
  16220. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16221. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16222. return -8638;
  16223. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16224. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16225. return -8639;
  16226. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16227. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16228. return -8640;
  16229. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16230. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16231. #endif /* !WOLFSSL_SMALL_STACK */
  16232. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16233. return -8641;
  16234. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16235. return -8642;
  16236. EVP_CIPHER_CTX_init(en);
  16237. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16238. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16239. return -8643;
  16240. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16241. return -8644;
  16242. EVP_CIPHER_CTX_init(de);
  16243. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16244. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16245. return -8645;
  16246. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16247. return -8646;
  16248. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16249. return -8647;
  16250. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16251. return -8648;
  16252. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16253. return -8649;
  16254. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16255. return -8650;
  16256. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16257. return -8651;
  16258. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16259. return -8652;
  16260. #endif /* WOLFSSL_AES_128 */
  16261. #ifdef WOLFSSL_AES_192
  16262. EVP_CIPHER_CTX_init(en);
  16263. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16264. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16265. return -8653;
  16266. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16267. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16268. return -8654;
  16269. EVP_CIPHER_CTX_init(de);
  16270. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16271. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16272. return -8655;
  16273. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16274. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16275. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16276. return -8656;
  16277. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16278. return -8657;
  16279. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16280. return -8658;
  16281. #endif /* WOLFSSL_AES_192 */
  16282. #ifdef WOLFSSL_AES_256
  16283. EVP_CIPHER_CTX_init(en);
  16284. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16285. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16286. return -8659;
  16287. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16288. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16289. return -8660;
  16290. EVP_CIPHER_CTX_init(de);
  16291. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16292. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16293. return -8661;
  16294. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16295. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16296. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16297. return -8662;
  16298. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16299. return -8663;
  16300. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16301. return -8664;
  16302. #endif /* WOLFSSL_AES_256 */
  16303. #ifdef WOLFSSL_SMALL_STACK
  16304. wolfSSL_EVP_CIPHER_CTX_free(en);
  16305. wolfSSL_EVP_CIPHER_CTX_free(de);
  16306. #endif
  16307. }
  16308. #endif /* HAVE_AES_COUNTER */
  16309. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  16310. {
  16311. /* EVP_CipherUpdate test */
  16312. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16313. {
  16314. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16315. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16316. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16317. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16318. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16319. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16320. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16321. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16322. };
  16323. byte key[] = "0123456789abcdef "; /* align */
  16324. byte iv[] = "1234567890abcdef "; /* align */
  16325. byte cipher[AES_BLOCK_SIZE * 4];
  16326. byte plain [AES_BLOCK_SIZE * 4];
  16327. #ifdef WOLFSSL_SMALL_STACK
  16328. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16329. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16330. #else
  16331. EVP_CIPHER_CTX en[1];
  16332. EVP_CIPHER_CTX de[1];
  16333. #endif
  16334. int outlen ;
  16335. int total = 0;
  16336. #ifdef WOLFSSL_SMALL_STACK
  16337. if ((en == NULL) || (de == NULL))
  16338. return MEMORY_E;
  16339. #endif
  16340. EVP_CIPHER_CTX_init(en);
  16341. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16342. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16343. return -8665;
  16344. /* openSSL compatibility, if(inlen == 0)return 1; */
  16345. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16346. (byte*)cbcPlain, 0) != 1)
  16347. return -8666;
  16348. EVP_CIPHER_CTX_init(en);
  16349. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16350. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16351. return -8667;
  16352. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16353. (byte*)cbcPlain, 9) == 0)
  16354. return -8668;
  16355. if(outlen != 0)
  16356. return -8669;
  16357. total += outlen;
  16358. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16359. (byte*)&cbcPlain[9] , 9) == 0)
  16360. return -8670;
  16361. if(outlen != 16)
  16362. return -8671;
  16363. total += outlen;
  16364. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16365. return -8672;
  16366. if(outlen != 16)
  16367. return -8673;
  16368. total += outlen;
  16369. if(total != 32)
  16370. return -8674;
  16371. total = 0;
  16372. EVP_CIPHER_CTX_init(de);
  16373. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16374. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16375. return -8675;
  16376. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16377. return -8676;
  16378. if(outlen != 0)
  16379. return -8677;
  16380. total += outlen;
  16381. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16382. (byte*)&cipher[6], 12) == 0)
  16383. return -8678;
  16384. if(outlen != 0)
  16385. total += outlen;
  16386. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16387. (byte*)&cipher[6+12], 14) == 0)
  16388. return -8679;
  16389. if(outlen != 16)
  16390. return -8680;
  16391. total += outlen;
  16392. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16393. return -8681;
  16394. if(outlen != 2)
  16395. return -8682;
  16396. total += outlen;
  16397. if(total != 18)
  16398. return -8683;
  16399. if (XMEMCMP(plain, cbcPlain, 18))
  16400. return -8684;
  16401. total = 0;
  16402. EVP_CIPHER_CTX_init(en);
  16403. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  16404. (unsigned char*)key, (unsigned char*)iv) == 0)
  16405. return -8685;
  16406. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  16407. return -8686;
  16408. if(outlen != 0)
  16409. return -8687;
  16410. total += outlen;
  16411. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  16412. return -8688;
  16413. if(outlen != 16)
  16414. return -8689;
  16415. total += outlen;
  16416. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16417. return -8690;
  16418. if(outlen != 16)
  16419. return -8691;
  16420. total += outlen;
  16421. if(total != 32)
  16422. return 3438;
  16423. total = 0;
  16424. EVP_CIPHER_CTX_init(de);
  16425. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  16426. (unsigned char*)key, (unsigned char*)iv) == 0)
  16427. return -8692;
  16428. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16429. return -8693;
  16430. if(outlen != 0)
  16431. return -8694;
  16432. total += outlen;
  16433. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  16434. return -8695;
  16435. if(outlen != 0)
  16436. total += outlen;
  16437. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  16438. return -8696;
  16439. if(outlen != 16)
  16440. return -8697;
  16441. total += outlen;
  16442. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  16443. return -8698;
  16444. if(outlen != 2)
  16445. return -8699;
  16446. total += outlen;
  16447. if(total != 18)
  16448. return 3447;
  16449. if (XMEMCMP(plain, cbcPlain, 18))
  16450. return -8700;
  16451. if (EVP_CIPHER_key_length(NULL) != 0)
  16452. return -8701;
  16453. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  16454. return -8702;
  16455. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  16456. return -8703;
  16457. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  16458. return -8704;
  16459. EVP_CIPHER_CTX_init(en);
  16460. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  16461. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16462. return -8705;
  16463. EVP_CIPHER_CTX_init(en);
  16464. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16465. (unsigned char*)key, (unsigned char*)iv) == 0)
  16466. return -8706;
  16467. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16468. return -8707;
  16469. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16470. return -8708;
  16471. EVP_CIPHER_CTX_init(de);
  16472. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  16473. (unsigned char*)key, (unsigned char*)iv) == 0)
  16474. return -8709;
  16475. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16476. return -8710;
  16477. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  16478. return -8711;
  16479. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  16480. return -8712;
  16481. EVP_CIPHER_CTX_init(en);
  16482. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  16483. (unsigned char*)key, (unsigned char*)iv);
  16484. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  16485. return -8713;
  16486. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  16487. return -8714;
  16488. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  16489. return -8715;
  16490. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  16491. return -8716;
  16492. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  16493. return -8717;
  16494. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  16495. EVP_CIPHER_CTX_set_flags(en, 42);
  16496. if (en->flags != 42)
  16497. return -8718;
  16498. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  16499. return -8719;
  16500. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  16501. return -8720;
  16502. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  16503. return -8721;
  16504. #ifdef WOLFSSL_SMALL_STACK
  16505. wolfSSL_EVP_CIPHER_CTX_free(en);
  16506. wolfSSL_EVP_CIPHER_CTX_free(de);
  16507. #endif
  16508. }
  16509. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  16510. #endif /* ifndef NO_AES */
  16511. return 0;
  16512. }
  16513. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  16514. {
  16515. int ret = 0;
  16516. #if !defined(NO_SHA256) && !defined(NO_SHA)
  16517. WOLFSSL_EVP_MD_CTX* ctx;
  16518. WOLFSSL_EVP_MD_CTX* ctx2;
  16519. ctx = EVP_MD_CTX_create();
  16520. ctx2 = EVP_MD_CTX_create();
  16521. ret = EVP_DigestInit(ctx, EVP_sha256());
  16522. if (ret != SSL_SUCCESS) {
  16523. ret = -8800;
  16524. goto openSSL_evpMD_test_done;
  16525. }
  16526. ret = EVP_MD_CTX_copy(ctx2, ctx);
  16527. if (ret != SSL_SUCCESS) {
  16528. ret = -8801;
  16529. goto openSSL_evpMD_test_done;
  16530. }
  16531. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  16532. ret = -8802;
  16533. goto openSSL_evpMD_test_done;
  16534. }
  16535. ret = EVP_DigestInit(ctx, EVP_sha1());
  16536. if (ret != SSL_SUCCESS) {
  16537. ret = -8803;
  16538. goto openSSL_evpMD_test_done;
  16539. }
  16540. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  16541. ret = -8804;
  16542. goto openSSL_evpMD_test_done;
  16543. }
  16544. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  16545. if (ret != SSL_SUCCESS) {
  16546. ret = -8805;
  16547. goto openSSL_evpMD_test_done;
  16548. }
  16549. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  16550. ret = -8806;
  16551. goto openSSL_evpMD_test_done;
  16552. }
  16553. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  16554. ret = -8807;
  16555. goto openSSL_evpMD_test_done;
  16556. }
  16557. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  16558. ret = -8808;
  16559. goto openSSL_evpMD_test_done;
  16560. }
  16561. if (EVP_add_digest(NULL) != 0) {
  16562. ret = -8809;
  16563. goto openSSL_evpMD_test_done;
  16564. }
  16565. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  16566. ret = -8810;
  16567. goto openSSL_evpMD_test_done;
  16568. }
  16569. ret = 0; /* got to success state without jumping to end with a fail */
  16570. openSSL_evpMD_test_done:
  16571. EVP_MD_CTX_destroy(ctx);
  16572. EVP_MD_CTX_destroy(ctx2);
  16573. #endif /* NO_SHA256 */
  16574. return ret;
  16575. }
  16576. #ifdef DEBUG_SIGN
  16577. static void show(const char *title, const char *p, unsigned int s) {
  16578. char* i;
  16579. printf("%s: ", title);
  16580. for (i = p;
  16581. i < p + s;
  16582. printf("%c", *i), i++);
  16583. printf("\n");
  16584. }
  16585. #else
  16586. #define show(a,b,c)
  16587. #endif
  16588. #define FOURK_BUFF 4096
  16589. #define ERR_BASE_PKEY -5000
  16590. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  16591. {
  16592. int ret = 0;
  16593. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  16594. byte* prvTmp;
  16595. byte* pubTmp;
  16596. int prvBytes;
  16597. int pubBytes;
  16598. RSA *prvRsa = NULL;
  16599. RSA *pubRsa = NULL;
  16600. EVP_PKEY *prvPkey = NULL;
  16601. EVP_PKEY *pubPkey = NULL;
  16602. EVP_PKEY_CTX *enc = NULL;
  16603. EVP_PKEY_CTX *dec = NULL;
  16604. byte in[] = TEST_STRING;
  16605. byte out[256];
  16606. size_t outlen;
  16607. size_t keySz;
  16608. byte plain[256];
  16609. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16610. XFILE keyFile;
  16611. XFILE keypubFile;
  16612. char cliKey[] = "./certs/client-key.der";
  16613. char cliKeypub[] = "./certs/client-keyPub.der";
  16614. #endif
  16615. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16616. if (prvTmp == NULL)
  16617. return ERR_BASE_PKEY-1;
  16618. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16619. if (pubTmp == NULL) {
  16620. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16621. return ERR_BASE_PKEY-2;
  16622. }
  16623. #ifdef USE_CERT_BUFFERS_1024
  16624. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  16625. prvBytes = sizeof_client_key_der_1024;
  16626. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16627. pubBytes = sizeof_client_keypub_der_1024;
  16628. #elif defined(USE_CERT_BUFFERS_2048)
  16629. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  16630. prvBytes = sizeof_client_key_der_2048;
  16631. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16632. pubBytes = sizeof_client_keypub_der_2048;
  16633. #else
  16634. keyFile = XFOPEN(cliKey, "rb");
  16635. if (!keyFile) {
  16636. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16637. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16638. err_sys("can't open ./certs/client-key.der, "
  16639. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  16640. return ERR_BASE_PKEY-3;
  16641. }
  16642. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  16643. XFCLOSE(keyFile);
  16644. keypubFile = XFOPEN(cliKeypub, "rb");
  16645. if (!keypubFile) {
  16646. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16647. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16648. err_sys("can't open ./certs/client-cert.der, "
  16649. "Please run from wolfSSL home dir", -4);
  16650. return ERR_BASE_PKEY-4;
  16651. }
  16652. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  16653. XFCLOSE(keypubFile);
  16654. #endif /* USE_CERT_BUFFERS */
  16655. prvRsa = wolfSSL_RSA_new();
  16656. pubRsa = wolfSSL_RSA_new();
  16657. if((prvRsa == NULL) || (pubRsa == NULL)){
  16658. printf("error with RSA_new\n");
  16659. ret = ERR_BASE_PKEY-10;
  16660. goto openssl_pkey0_test_done;
  16661. }
  16662. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  16663. if(ret != SSL_SUCCESS){
  16664. printf("error with RSA_LoadDer_ex\n");
  16665. ret = ERR_BASE_PKEY-11;
  16666. goto openssl_pkey0_test_done;
  16667. }
  16668. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  16669. if(ret != SSL_SUCCESS){
  16670. printf("error with RSA_LoadDer_ex\n");
  16671. ret = ERR_BASE_PKEY-12;
  16672. goto openssl_pkey0_test_done;
  16673. }
  16674. keySz = (size_t)RSA_size(pubRsa);
  16675. prvPkey = wolfSSL_EVP_PKEY_new();
  16676. pubPkey = wolfSSL_EVP_PKEY_new();
  16677. if((prvPkey == NULL) || (pubPkey == NULL)){
  16678. printf("error with PKEY_new\n");
  16679. ret = ERR_BASE_PKEY-13;
  16680. goto openssl_pkey0_test_done;
  16681. }
  16682. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  16683. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  16684. if(ret != 2){
  16685. printf("error with PKEY_set1_RSA\n");
  16686. ret = ERR_BASE_PKEY-14;
  16687. goto openssl_pkey0_test_done;
  16688. }
  16689. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  16690. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  16691. if((dec == NULL)||(enc==NULL)){
  16692. printf("error with EVP_PKEY_CTX_new\n");
  16693. ret = ERR_BASE_PKEY-15;
  16694. goto openssl_pkey0_test_done;
  16695. }
  16696. ret = EVP_PKEY_decrypt_init(dec);
  16697. if (ret != 1) {
  16698. printf("error with decrypt init\n");
  16699. ret = ERR_BASE_PKEY-16;
  16700. goto openssl_pkey0_test_done;
  16701. }
  16702. ret = EVP_PKEY_encrypt_init(enc);
  16703. if (ret != 1) {
  16704. printf("error with encrypt init\n");
  16705. ret = ERR_BASE_PKEY-17;
  16706. goto openssl_pkey0_test_done;
  16707. }
  16708. XMEMSET(out, 0, sizeof(out));
  16709. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  16710. if (ret != 1) {
  16711. printf("error encrypting msg\n");
  16712. ret = ERR_BASE_PKEY-18;
  16713. goto openssl_pkey0_test_done;
  16714. }
  16715. show("encrypted msg", out, outlen);
  16716. XMEMSET(plain, 0, sizeof(plain));
  16717. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  16718. if (ret != 1) {
  16719. printf("error decrypting msg\n");
  16720. ret = ERR_BASE_PKEY-19;
  16721. goto openssl_pkey0_test_done;
  16722. }
  16723. show("decrypted msg", plain, outlen);
  16724. /* RSA_PKCS1_OAEP_PADDING test */
  16725. ret = EVP_PKEY_decrypt_init(dec);
  16726. if (ret != 1) {
  16727. printf("error with decrypt init\n");
  16728. ret = ERR_BASE_PKEY-30;
  16729. goto openssl_pkey0_test_done;
  16730. }
  16731. ret = EVP_PKEY_encrypt_init(enc);
  16732. if (ret != 1) {
  16733. printf("error with encrypt init\n");
  16734. ret = ERR_BASE_PKEY-31;
  16735. goto openssl_pkey0_test_done;
  16736. }
  16737. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  16738. printf("first set rsa padding error\n");
  16739. ret = ERR_BASE_PKEY-32;
  16740. goto openssl_pkey0_test_done;
  16741. }
  16742. #ifndef HAVE_FIPS
  16743. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  16744. printf("second set rsa padding error\n");
  16745. ret = ERR_BASE_PKEY-33;
  16746. goto openssl_pkey0_test_done;
  16747. }
  16748. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  16749. printf("third set rsa padding error\n");
  16750. ret = ERR_BASE_PKEY-34;
  16751. goto openssl_pkey0_test_done;
  16752. }
  16753. #endif
  16754. XMEMSET(out, 0, sizeof(out));
  16755. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  16756. if (ret != 1) {
  16757. printf("error encrypting msg\n");
  16758. ret = ERR_BASE_PKEY-35;
  16759. goto openssl_pkey0_test_done;
  16760. }
  16761. show("encrypted msg", out, outlen);
  16762. XMEMSET(plain, 0, sizeof(plain));
  16763. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  16764. if (ret != 1) {
  16765. printf("error decrypting msg\n");
  16766. ret = ERR_BASE_PKEY-36;
  16767. goto openssl_pkey0_test_done;
  16768. }
  16769. show("decrypted msg", plain, outlen);
  16770. ret = 0; /* made it to this point without error then set success */
  16771. openssl_pkey0_test_done:
  16772. wolfSSL_RSA_free(prvRsa);
  16773. wolfSSL_RSA_free(pubRsa);
  16774. EVP_PKEY_free(pubPkey);
  16775. EVP_PKEY_free(prvPkey);
  16776. EVP_PKEY_CTX_free(dec);
  16777. EVP_PKEY_CTX_free(enc);
  16778. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16779. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16780. #endif /* NO_RSA */
  16781. return ret;
  16782. }
  16783. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  16784. {
  16785. int ret = 0;
  16786. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  16787. !defined(NO_SHA)
  16788. EVP_PKEY_CTX* dec = NULL;
  16789. EVP_PKEY_CTX* enc = NULL;
  16790. EVP_PKEY* pubKey = NULL;
  16791. EVP_PKEY* prvKey = NULL;
  16792. X509* x509 = NULL;
  16793. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  16794. const unsigned char* clikey;
  16795. long cliKeySz;
  16796. size_t outlen;
  16797. int keyLenBits = 2048;
  16798. #ifdef WOLFSSL_SMALL_STACK
  16799. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16800. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16801. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16802. if ((tmp == NULL) ||
  16803. (cipher == NULL) ||
  16804. (plain == NULL)) {
  16805. ret = -9015;
  16806. goto openssl_pkey1_test_done;
  16807. }
  16808. #else
  16809. unsigned char tmp[FOURK_BUF];
  16810. unsigned char cipher[RSA_TEST_BYTES];
  16811. unsigned char plain[RSA_TEST_BYTES];
  16812. #endif
  16813. #if defined(USE_CERT_BUFFERS_1024)
  16814. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16815. cliKeySz = (long)sizeof_client_key_der_1024;
  16816. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  16817. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  16818. keyLenBits = 1024;
  16819. #elif defined(USE_CERT_BUFFERS_2048)
  16820. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16821. cliKeySz = (long)sizeof_client_key_der_2048;
  16822. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  16823. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  16824. #elif defined(USE_CERT_BUFFERS_3072)
  16825. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  16826. cliKeySz = (long)sizeof_client_key_der_3072;
  16827. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  16828. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  16829. keyLenBits = 3072;
  16830. #elif defined(USE_CERT_BUFFERS_4096)
  16831. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  16832. cliKeySz = (long)sizeof_client_key_der_4096;
  16833. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  16834. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  16835. keyLenBits = 4096;
  16836. #else
  16837. XFILE f;
  16838. f = XFOPEN(clientKey, "rb");
  16839. if (!f) {
  16840. err_sys("can't open ./certs/client-key.der, "
  16841. "Please run from wolfSSL home dir", -41);
  16842. ret = -9000;
  16843. goto openssl_pkey1_test_done;
  16844. }
  16845. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  16846. XFCLOSE(f);
  16847. /* using existing wolfSSL api to get public and private key */
  16848. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  16849. #endif /* USE_CERT_BUFFERS */
  16850. clikey = tmp;
  16851. if ((prvKey = EVP_PKEY_new()) == NULL) {
  16852. ret = -9001;
  16853. goto openssl_pkey1_test_done;
  16854. }
  16855. EVP_PKEY_free(prvKey);
  16856. prvKey = NULL;
  16857. if (x509 == NULL) {
  16858. ret = -9002;
  16859. goto openssl_pkey1_test_done;
  16860. }
  16861. pubKey = X509_get_pubkey(x509);
  16862. if (pubKey == NULL) {
  16863. ret = -9003;
  16864. goto openssl_pkey1_test_done;
  16865. }
  16866. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  16867. if (prvKey == NULL) {
  16868. ret = -9004;
  16869. goto openssl_pkey1_test_done;
  16870. }
  16871. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  16872. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  16873. ret = -9005;
  16874. goto openssl_pkey1_test_done;
  16875. }
  16876. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  16877. ret = -9006;
  16878. goto openssl_pkey1_test_done;
  16879. }
  16880. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  16881. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  16882. if (dec == NULL || enc == NULL) {
  16883. ret = -9007;
  16884. goto openssl_pkey1_test_done;
  16885. }
  16886. if (EVP_PKEY_decrypt_init(dec) != 1) {
  16887. ret = -9008;
  16888. goto openssl_pkey1_test_done;
  16889. }
  16890. if (EVP_PKEY_encrypt_init(enc) != 1) {
  16891. ret = -9009;
  16892. goto openssl_pkey1_test_done;
  16893. }
  16894. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  16895. ret = -9010;
  16896. goto openssl_pkey1_test_done;
  16897. }
  16898. #ifndef HAVE_FIPS
  16899. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  16900. ret = -9011;
  16901. goto openssl_pkey1_test_done;
  16902. }
  16903. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  16904. ret = -9012;
  16905. goto openssl_pkey1_test_done;
  16906. }
  16907. #endif
  16908. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  16909. outlen = keyLenBits/8;
  16910. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  16911. ret = -9013;
  16912. goto openssl_pkey1_test_done;
  16913. }
  16914. XMEMSET(plain, 0, RSA_TEST_BYTES);
  16915. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  16916. ret = -9014;
  16917. goto openssl_pkey1_test_done;
  16918. }
  16919. openssl_pkey1_test_done:
  16920. if (pubKey != NULL) {
  16921. EVP_PKEY_free(pubKey);
  16922. }
  16923. if (prvKey != NULL) {
  16924. EVP_PKEY_free(prvKey);
  16925. }
  16926. if (dec != NULL) {
  16927. EVP_PKEY_CTX_free(dec);
  16928. }
  16929. if (enc != NULL) {
  16930. EVP_PKEY_CTX_free(enc);
  16931. }
  16932. if (x509 != NULL) {
  16933. X509_free(x509);
  16934. }
  16935. #ifdef WOLFSSL_SMALL_STACK
  16936. if (tmp != NULL)
  16937. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16938. if (cipher != NULL)
  16939. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16940. if (plain != NULL)
  16941. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16942. #endif
  16943. #endif
  16944. return ret;
  16945. }
  16946. #define ERR_BASE_EVPSIG -5100
  16947. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  16948. {
  16949. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  16950. byte* prvTmp;
  16951. byte* pubTmp;
  16952. int prvBytes;
  16953. int pubBytes;
  16954. RSA *prvRsa;
  16955. RSA *pubRsa;
  16956. EVP_PKEY *prvPkey;
  16957. EVP_PKEY *pubPkey;
  16958. EVP_MD_CTX* sign;
  16959. EVP_MD_CTX* verf;
  16960. char msg[] = "see spot run";
  16961. unsigned char sig[256];
  16962. unsigned int sigSz;
  16963. const void* pt;
  16964. unsigned int count;
  16965. int ret, ret1, ret2;
  16966. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  16967. XFILE keyFile;
  16968. XFILE keypubFile;
  16969. char cliKey[] = "./certs/client-key.der";
  16970. char cliKeypub[] = "./certs/client-keyPub.der";
  16971. #endif
  16972. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16973. if (prvTmp == NULL)
  16974. return ERR_BASE_EVPSIG-1;
  16975. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16976. if (pubTmp == NULL) {
  16977. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16978. return ERR_BASE_EVPSIG-2;
  16979. }
  16980. #ifdef USE_CERT_BUFFERS_1024
  16981. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  16982. prvBytes = sizeof_client_key_der_1024;
  16983. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16984. pubBytes = sizeof_client_keypub_der_1024;
  16985. #elif defined(USE_CERT_BUFFERS_2048)
  16986. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  16987. prvBytes = sizeof_client_key_der_2048;
  16988. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16989. pubBytes = sizeof_client_keypub_der_2048;
  16990. #else
  16991. keyFile = XFOPEN(cliKey, "rb");
  16992. if (!keyFile) {
  16993. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16994. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  16995. err_sys("can't open ./certs/client-key.der, "
  16996. "Please run from wolfSSL home dir", -40);
  16997. return ERR_BASE_EVPSIG-3;
  16998. }
  16999. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  17000. XFCLOSE(keyFile);
  17001. keypubFile = XFOPEN(cliKeypub, "rb");
  17002. if (!keypubFile) {
  17003. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17004. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17005. err_sys("can't open ./certs/client-cert.der, "
  17006. "Please run from wolfSSL home dir", -41);
  17007. return ERR_BASE_EVPSIG-4;
  17008. }
  17009. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  17010. XFCLOSE(keypubFile);
  17011. #endif /* USE_CERT_BUFFERS */
  17012. prvRsa = wolfSSL_RSA_new();
  17013. pubRsa = wolfSSL_RSA_new();
  17014. if((prvRsa == NULL) || (pubRsa == NULL)){
  17015. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17016. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17017. err_sys("ERROR with RSA_new", -9100);
  17018. return ERR_BASE_EVPSIG-5;
  17019. }
  17020. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  17021. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  17022. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  17023. printf("error with RSA_LoadDer_ex\n");
  17024. return ERR_BASE_EVPSIG-6;
  17025. }
  17026. prvPkey = wolfSSL_EVP_PKEY_new();
  17027. pubPkey = wolfSSL_EVP_PKEY_new();
  17028. if((prvPkey == NULL) || (pubPkey == NULL)){
  17029. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17030. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17031. printf("error with KEY_new\n");
  17032. return ERR_BASE_EVPSIG-7;
  17033. }
  17034. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  17035. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  17036. if((ret1 != 1) || (ret2 != 1)){
  17037. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17038. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17039. printf("error with EVP_PKEY_set1_RSA\n");
  17040. return ERR_BASE_EVPSIG-8;
  17041. }
  17042. /****************** sign and verify *******************/
  17043. sign = EVP_MD_CTX_create();
  17044. verf = EVP_MD_CTX_create();
  17045. if((sign == NULL)||(verf == NULL)){
  17046. printf("error with EVP_MD_CTX_create\n");
  17047. EVP_MD_CTX_destroy(sign);
  17048. EVP_MD_CTX_destroy(verf);
  17049. return ERR_BASE_EVPSIG-10;
  17050. }
  17051. ret = EVP_SignInit(sign, EVP_sha1());
  17052. if (ret != SSL_SUCCESS){
  17053. printf("error with EVP_SignInit\n");
  17054. EVP_MD_CTX_destroy(sign);
  17055. EVP_MD_CTX_destroy(verf);
  17056. return ERR_BASE_EVPSIG-11;
  17057. }
  17058. count = sizeof(msg);
  17059. show("message = ", (char *)msg, count);
  17060. /* sign */
  17061. XMEMSET(sig, 0, sizeof(sig));
  17062. pt = (const void*)msg;
  17063. ret1 = EVP_SignUpdate(sign, pt, count);
  17064. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  17065. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  17066. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17067. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17068. EVP_MD_CTX_destroy(sign);
  17069. EVP_MD_CTX_destroy(verf);
  17070. printf("error with EVP_MD_CTX_create\n");
  17071. return ERR_BASE_EVPSIG-12;
  17072. }
  17073. show("signature = ", (char *)sig, sigSz);
  17074. /* verify */
  17075. pt = (const void*)msg;
  17076. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  17077. ret2 = EVP_VerifyUpdate(verf, pt, count);
  17078. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  17079. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17080. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17081. EVP_MD_CTX_destroy(sign);
  17082. EVP_MD_CTX_destroy(verf);
  17083. printf("error with EVP_Verify\n");
  17084. return ERR_BASE_EVPSIG-13;
  17085. }
  17086. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  17087. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17088. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17089. EVP_MD_CTX_destroy(sign);
  17090. EVP_MD_CTX_destroy(verf);
  17091. printf("error with EVP_VerifyFinal\n");
  17092. return ERR_BASE_EVPSIG-14;
  17093. }
  17094. /* expect fail without update */
  17095. EVP_VerifyInit(verf, EVP_sha1());
  17096. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  17097. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17098. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17099. EVP_MD_CTX_destroy(sign);
  17100. EVP_MD_CTX_destroy(verf);
  17101. printf("EVP_VerifyInit without update not detected\n");
  17102. return ERR_BASE_EVPSIG-15;
  17103. }
  17104. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17105. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17106. EVP_MD_CTX_destroy(sign);
  17107. EVP_MD_CTX_destroy(verf);
  17108. wolfSSL_RSA_free(prvRsa);
  17109. wolfSSL_RSA_free(pubRsa);
  17110. EVP_PKEY_free(pubPkey);
  17111. EVP_PKEY_free(prvPkey);
  17112. #endif /* NO_RSA */
  17113. return 0;
  17114. }
  17115. #endif /* OPENSSL_EXTRA */
  17116. #ifndef NO_PWDBASED
  17117. #ifdef HAVE_SCRYPT
  17118. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  17119. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  17120. {
  17121. #ifdef HAVE_FIPS
  17122. /* RFC 7914 test vector keys are too short for FIPS. */
  17123. #else
  17124. int ret;
  17125. byte derived[64];
  17126. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  17127. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  17128. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  17129. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  17130. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  17131. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  17132. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  17133. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  17134. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  17135. };
  17136. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17137. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  17138. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  17139. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  17140. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  17141. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  17142. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  17143. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  17144. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  17145. };
  17146. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17147. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17148. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  17149. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  17150. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  17151. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  17152. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  17153. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  17154. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  17155. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  17156. };
  17157. #endif
  17158. #ifdef SCRYPT_TEST_ALL
  17159. /* Test case is very slow.
  17160. * Use for confirmation after code change or new platform.
  17161. */
  17162. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17163. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  17164. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  17165. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  17166. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  17167. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  17168. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  17169. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  17170. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  17171. };
  17172. #endif
  17173. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  17174. if (ret != 0)
  17175. return -9200;
  17176. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  17177. return -9201;
  17178. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  17179. sizeof(verify2));
  17180. if (ret != 0)
  17181. return -9202;
  17182. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17183. return -9203;
  17184. /* Don't run these test on embedded, since they use large mallocs */
  17185. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  17186. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17187. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  17188. if (ret != 0)
  17189. return -9204;
  17190. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  17191. return -9205;
  17192. #ifdef SCRYPT_TEST_ALL
  17193. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  17194. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  17195. if (ret != 0)
  17196. return -9206;
  17197. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  17198. return -9207;
  17199. #endif
  17200. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  17201. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  17202. 8, 16, sizeof(verify2));
  17203. if (ret != 0)
  17204. return -9208;
  17205. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  17206. return -9209;
  17207. #endif /* !HAVE_FIPS */
  17208. return 0;
  17209. }
  17210. #endif
  17211. #ifdef HAVE_PKCS12
  17212. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  17213. {
  17214. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  17215. 0x00, 0x00 };
  17216. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  17217. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  17218. 0x00, 0x67, 0x00, 0x00 };
  17219. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  17220. byte derived[64];
  17221. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17222. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  17223. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  17224. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  17225. };
  17226. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17227. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  17228. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  17229. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  17230. };
  17231. int id = 1;
  17232. int kLen = 24;
  17233. int iterations = 1;
  17234. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  17235. iterations, kLen, WC_SHA256, id);
  17236. if (ret < 0)
  17237. return -9300;
  17238. if (XMEMCMP(derived, verify, kLen) != 0)
  17239. return -9301;
  17240. iterations = 1000;
  17241. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  17242. iterations, kLen, WC_SHA256, id);
  17243. if (ret < 0)
  17244. return -9302;
  17245. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  17246. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  17247. if (ret < 0)
  17248. return -9303;
  17249. if (XMEMCMP(derived, verify2, 24) != 0)
  17250. return -9304;
  17251. return 0;
  17252. }
  17253. #endif /* HAVE_PKCS12 */
  17254. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17255. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  17256. {
  17257. char passwd[] = "passwordpassword";
  17258. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17259. int iterations = 2048;
  17260. int kLen = 24;
  17261. byte derived[64];
  17262. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17263. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  17264. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  17265. };
  17266. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  17267. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  17268. if (ret != 0)
  17269. return ret;
  17270. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17271. return -9400;
  17272. return 0;
  17273. }
  17274. #endif /* HAVE_PBKDF2 && !NO_SHA256 */
  17275. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17276. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  17277. {
  17278. char passwd[] = "password";
  17279. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  17280. int iterations = 1000;
  17281. int kLen = 16;
  17282. byte derived[16];
  17283. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17284. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  17285. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  17286. };
  17287. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  17288. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  17289. HEAP_HINT);
  17290. if (ret != 0)
  17291. return ret;
  17292. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  17293. return -9500;
  17294. return 0;
  17295. }
  17296. #endif /* HAVE_PBKDF2 && !NO_SHA */
  17297. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  17298. {
  17299. int ret = 0;
  17300. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  17301. ret = pbkdf1_test();
  17302. if (ret != 0)
  17303. return ret;
  17304. #endif
  17305. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  17306. ret = pbkdf2_test();
  17307. if (ret != 0)
  17308. return ret;
  17309. #endif
  17310. #ifdef HAVE_PKCS12
  17311. ret = pkcs12_test();
  17312. if (ret != 0)
  17313. return ret;
  17314. #endif
  17315. #ifdef HAVE_SCRYPT
  17316. ret = scrypt_test();
  17317. if (ret != 0)
  17318. return ret;
  17319. #endif
  17320. return ret;
  17321. }
  17322. #endif /* NO_PWDBASED */
  17323. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  17324. /* WOLFSSL_TEST_SUBROUTINE */ static int hkdf_test(void)
  17325. {
  17326. int ret;
  17327. int L = 42;
  17328. byte okm1[42];
  17329. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17330. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  17331. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  17332. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17333. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  17334. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  17335. 0xf8, 0xf9 };
  17336. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  17337. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  17338. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  17339. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  17340. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  17341. 0x49, 0x18 };
  17342. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  17343. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  17344. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  17345. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  17346. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  17347. 0xf8, 0x96 };
  17348. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  17349. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  17350. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  17351. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  17352. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  17353. 0x96, 0xc8 };
  17354. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  17355. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  17356. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  17357. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  17358. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  17359. 0x58, 0x65 };
  17360. (void)res1;
  17361. (void)res2;
  17362. (void)res3;
  17363. (void)res4;
  17364. (void)salt1;
  17365. (void)info1;
  17366. #ifndef NO_SHA
  17367. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17368. if (ret != 0)
  17369. return -9700;
  17370. if (XMEMCMP(okm1, res1, L) != 0)
  17371. return -9701;
  17372. #ifndef HAVE_FIPS
  17373. /* fips can't have key size under 14 bytes, salt is key too */
  17374. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  17375. if (ret != 0)
  17376. return -9702;
  17377. if (XMEMCMP(okm1, res2, L) != 0)
  17378. return -9703;
  17379. #endif /* HAVE_FIPS */
  17380. #endif /* NO_SHA */
  17381. #ifndef NO_SHA256
  17382. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  17383. if (ret != 0)
  17384. return -9704;
  17385. if (XMEMCMP(okm1, res3, L) != 0)
  17386. return -9705;
  17387. #ifndef HAVE_FIPS
  17388. /* fips can't have key size under 14 bytes, salt is key too */
  17389. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  17390. if (ret != 0)
  17391. return -9706;
  17392. if (XMEMCMP(okm1, res4, L) != 0)
  17393. return -9707;
  17394. #endif /* HAVE_FIPS */
  17395. #endif /* NO_SHA256 */
  17396. return 0;
  17397. }
  17398. #endif /* HAVE_HKDF */
  17399. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  17400. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  17401. {
  17402. int ret;
  17403. byte kek[128];
  17404. #ifndef NO_SHA
  17405. /* SHA-1, COUNT = 0
  17406. * shared secret length: 192
  17407. * SharedInfo length: 0
  17408. * key data length: 128
  17409. */
  17410. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  17411. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  17412. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  17413. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  17414. };
  17415. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17416. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  17417. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  17418. };
  17419. #endif
  17420. #ifndef NO_SHA256
  17421. /* SHA-256, COUNT = 3
  17422. * shared secret length: 192
  17423. * SharedInfo length: 0
  17424. * key data length: 128
  17425. */
  17426. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  17427. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  17428. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  17429. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  17430. };
  17431. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17432. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  17433. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  17434. };
  17435. #endif
  17436. #ifdef WOLFSSL_SHA512
  17437. /* SHA-512, COUNT = 0
  17438. * shared secret length: 192
  17439. * SharedInfo length: 0
  17440. * key data length: 128
  17441. */
  17442. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  17443. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  17444. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  17445. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  17446. };
  17447. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  17448. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  17449. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  17450. };
  17451. /* SHA-512, COUNT = 0
  17452. * shared secret length: 521
  17453. * SharedInfo length: 128
  17454. * key data length: 1024
  17455. */
  17456. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  17457. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  17458. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  17459. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  17460. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  17461. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  17462. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  17463. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  17464. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  17465. 0x5a, 0x2d
  17466. };
  17467. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  17468. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  17469. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  17470. };
  17471. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  17472. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  17473. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  17474. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  17475. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  17476. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  17477. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  17478. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  17479. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  17480. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  17481. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  17482. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  17483. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  17484. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  17485. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  17486. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  17487. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  17488. };
  17489. #endif
  17490. #ifndef NO_SHA
  17491. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  17492. kek, sizeof(verify));
  17493. if (ret != 0)
  17494. return -9800;
  17495. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  17496. return -9801;
  17497. #endif
  17498. #ifndef NO_SHA256
  17499. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  17500. kek, sizeof(verify2));
  17501. if (ret != 0)
  17502. return -9802;
  17503. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  17504. return -9803;
  17505. #endif
  17506. #ifdef WOLFSSL_SHA512
  17507. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  17508. kek, sizeof(verify3));
  17509. if (ret != 0)
  17510. return -9804;
  17511. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  17512. return -9805;
  17513. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  17514. sizeof(info4), kek, sizeof(verify4));
  17515. if (ret != 0)
  17516. return -9806;
  17517. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  17518. return -9807;
  17519. #endif
  17520. return 0;
  17521. }
  17522. #endif /* HAVE_X963_KDF */
  17523. #ifdef HAVE_ECC
  17524. /* size to use for ECC key gen tests */
  17525. #ifndef ECC_KEYGEN_SIZE
  17526. #ifndef NO_ECC256
  17527. #define ECC_KEYGEN_SIZE 32
  17528. #elif defined(HAVE_ECC384)
  17529. #define ECC_KEYGEN_SIZE 48
  17530. #elif defined(HAVE_ECC224)
  17531. #define ECC_KEYGEN_SIZE 28
  17532. #elif defined(HAVE_ECC521)
  17533. #define ECC_KEYGEN_SIZE 66
  17534. #else
  17535. #error No ECC keygen size defined for test
  17536. #endif
  17537. #endif
  17538. #ifdef BENCH_EMBEDDED
  17539. #define ECC_SHARED_SIZE 128
  17540. #else
  17541. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  17542. #endif
  17543. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  17544. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  17545. #ifndef NO_ECC_VECTOR_TEST
  17546. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  17547. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  17548. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  17549. #define HAVE_ECC_VECTOR_TEST
  17550. #endif
  17551. #endif
  17552. #ifdef HAVE_ECC_VECTOR_TEST
  17553. typedef struct eccVector {
  17554. const char* msg; /* SHA-1 Encoded Message */
  17555. const char* Qx;
  17556. const char* Qy;
  17557. const char* d; /* Private Key */
  17558. const char* R;
  17559. const char* S;
  17560. const char* curveName;
  17561. word32 msgLen;
  17562. word32 keySize;
  17563. #ifndef NO_ASN
  17564. const byte* r;
  17565. word32 rSz;
  17566. const byte* s;
  17567. word32 sSz;
  17568. #endif
  17569. } eccVector;
  17570. static int ecc_test_vector_item(const eccVector* vector)
  17571. {
  17572. int ret = 0, verify = 0;
  17573. word32 sigSz;
  17574. #ifdef WOLFSSL_SMALL_STACK
  17575. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17576. #else
  17577. ecc_key userA[1];
  17578. #endif
  17579. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  17580. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17581. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  17582. DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  17583. DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  17584. DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  17585. #endif
  17586. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  17587. if (sig == NULL)
  17588. ERROR_OUT(MEMORY_E, done);
  17589. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17590. if (sigRaw == NULL || r == NULL || s == NULL)
  17591. ERROR_OUT(MEMORY_E, done);
  17592. #endif
  17593. #endif
  17594. #ifdef WOLFSSL_SMALL_STACK
  17595. if (userA == NULL)
  17596. ERROR_OUT(MEMORY_E, done);
  17597. #endif
  17598. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  17599. if (ret != 0)
  17600. goto done;
  17601. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  17602. vector->d, vector->curveName);
  17603. if (ret != 0)
  17604. goto done;
  17605. XMEMSET(sig, 0, ECC_SIG_SIZE);
  17606. sigSz = ECC_SIG_SIZE;
  17607. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  17608. if (ret != 0)
  17609. goto done;
  17610. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17611. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  17612. sigRawSz = ECC_SIG_SIZE;
  17613. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  17614. sigRaw, &sigRawSz);
  17615. if (ret != 0)
  17616. goto done;
  17617. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  17618. ret = -9810;
  17619. goto done;
  17620. }
  17621. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  17622. if (ret != 0)
  17623. goto done;
  17624. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  17625. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  17626. ret = -9811;
  17627. goto done;
  17628. }
  17629. #endif
  17630. do {
  17631. #if defined(WOLFSSL_ASYNC_CRYPT)
  17632. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17633. #endif
  17634. if (ret == 0)
  17635. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  17636. vector->msgLen, &verify, userA);
  17637. } while (ret == WC_PENDING_E);
  17638. if (ret != 0)
  17639. goto done;
  17640. TEST_SLEEP();
  17641. if (verify != 1)
  17642. ret = -9812;
  17643. done:
  17644. #ifdef WOLFSSL_SMALL_STACK
  17645. if (userA != NULL) {
  17646. wc_ecc_free(userA);
  17647. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17648. }
  17649. #else
  17650. wc_ecc_free(userA);
  17651. #endif
  17652. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  17653. FREE_VAR(sigRaw, HEAP_HINT);
  17654. FREE_VAR(r, HEAP_HINT);
  17655. FREE_VAR(s, HEAP_HINT);
  17656. #endif
  17657. FREE_VAR(sig, HEAP_HINT);
  17658. return ret;
  17659. }
  17660. static int ecc_test_vector(int keySize)
  17661. {
  17662. int ret;
  17663. eccVector vec;
  17664. XMEMSET(&vec, 0, sizeof(vec));
  17665. vec.keySize = (word32)keySize;
  17666. switch(keySize) {
  17667. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  17668. case 14:
  17669. return 0;
  17670. #endif /* HAVE_ECC112 */
  17671. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  17672. case 16:
  17673. return 0;
  17674. #endif /* HAVE_ECC128 */
  17675. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  17676. case 20:
  17677. return 0;
  17678. #endif /* HAVE_ECC160 */
  17679. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  17680. case 24:
  17681. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  17682. #if 1
  17683. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  17684. vec.msgLen = 20;
  17685. #else
  17686. /* This is the raw message prior to SHA-1 */
  17687. vec.msg =
  17688. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  17689. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  17690. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  17691. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  17692. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  17693. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  17694. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  17695. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  17696. vec.msgLen = 128;
  17697. #endif
  17698. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  17699. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  17700. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  17701. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  17702. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  17703. vec.curveName = "SECP192R1";
  17704. #ifndef NO_ASN
  17705. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  17706. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  17707. vec.rSz = 24;
  17708. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  17709. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  17710. vec.sSz = 24;
  17711. #endif
  17712. break;
  17713. #endif /* HAVE_ECC192 */
  17714. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  17715. case 28:
  17716. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  17717. #if 1
  17718. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  17719. vec.msgLen = 20;
  17720. #else
  17721. /* This is the raw message prior to SHA-1 */
  17722. vec.msg =
  17723. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  17724. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  17725. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  17726. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  17727. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  17728. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  17729. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  17730. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  17731. vec.msgLen = 128;
  17732. #endif
  17733. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  17734. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  17735. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  17736. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  17737. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  17738. vec.curveName = "SECP224R1";
  17739. #ifndef NO_ASN
  17740. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  17741. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  17742. "\xbc\x16\x71\xa7";
  17743. vec.rSz = 28;
  17744. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  17745. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  17746. "\x6a\xf3\xad\x5b";
  17747. vec.sSz = 28;
  17748. #endif
  17749. break;
  17750. #endif /* HAVE_ECC224 */
  17751. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  17752. case 30:
  17753. return 0;
  17754. #endif /* HAVE_ECC239 */
  17755. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  17756. case 32:
  17757. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  17758. #if 1
  17759. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  17760. vec.msgLen = 20;
  17761. #else
  17762. /* This is the raw message prior to SHA-1 */
  17763. vec.msg =
  17764. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  17765. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  17766. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  17767. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  17768. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  17769. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  17770. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  17771. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  17772. vec.msgLen = 128;
  17773. #endif
  17774. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  17775. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  17776. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  17777. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  17778. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  17779. #ifndef NO_ASN
  17780. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  17781. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  17782. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  17783. vec.rSz = 32;
  17784. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  17785. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  17786. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  17787. vec.sSz = 32;
  17788. #endif
  17789. vec.curveName = "SECP256R1";
  17790. break;
  17791. #endif /* !NO_ECC256 */
  17792. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  17793. case 40:
  17794. return 0;
  17795. #endif /* HAVE_ECC320 */
  17796. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  17797. case 48:
  17798. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  17799. #if 1
  17800. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  17801. vec.msgLen = 20;
  17802. #else
  17803. /* This is the raw message prior to SHA-1 */
  17804. vec.msg =
  17805. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  17806. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  17807. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  17808. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  17809. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  17810. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  17811. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  17812. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  17813. vec.msgLen = 128;
  17814. #endif
  17815. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  17816. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  17817. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  17818. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  17819. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  17820. vec.curveName = "SECP384R1";
  17821. #ifndef NO_ASN
  17822. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  17823. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  17824. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  17825. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  17826. vec.rSz = 48;
  17827. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  17828. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  17829. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  17830. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  17831. vec.sSz = 48;
  17832. #endif
  17833. break;
  17834. #endif /* HAVE_ECC384 */
  17835. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  17836. case 64:
  17837. return 0;
  17838. #endif /* HAVE_ECC512 */
  17839. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  17840. case 66:
  17841. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  17842. #if 1
  17843. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  17844. vec.msgLen = 20;
  17845. #else
  17846. /* This is the raw message prior to SHA-1 */
  17847. vec.msg =
  17848. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  17849. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  17850. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  17851. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  17852. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  17853. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  17854. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  17855. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  17856. vec.msgLen = 128;
  17857. #endif
  17858. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  17859. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  17860. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  17861. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  17862. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  17863. vec.curveName = "SECP521R1";
  17864. #ifndef NO_ASN
  17865. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  17866. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  17867. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  17868. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  17869. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  17870. "\xa3\xbb\x15\x21\xbe";
  17871. vec.rSz = 65;
  17872. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  17873. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  17874. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  17875. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  17876. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  17877. "\x3d\x22\xf2\x48\x0c";
  17878. vec.sSz = 65;
  17879. #endif
  17880. break;
  17881. #endif /* HAVE_ECC521 */
  17882. default:
  17883. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  17884. }; /* Switch */
  17885. ret = ecc_test_vector_item(&vec);
  17886. if (ret < 0) {
  17887. return ret;
  17888. }
  17889. return 0;
  17890. }
  17891. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  17892. static int ecc_test_sign_vectors(WC_RNG* rng)
  17893. {
  17894. int ret;
  17895. ecc_key key;
  17896. byte sig[72];
  17897. word32 sigSz;
  17898. unsigned char hash[32] = "test wolfSSL deterministic sign";
  17899. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  17900. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  17901. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  17902. const byte k[1] = { 0x02 };
  17903. const byte expSig[71] = {
  17904. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  17905. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  17906. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  17907. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  17908. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  17909. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  17910. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  17911. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  17912. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  17913. };
  17914. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  17915. if (ret != 0) {
  17916. return ret;
  17917. }
  17918. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  17919. if (ret != 0) {
  17920. goto done;
  17921. }
  17922. ret = wc_ecc_sign_set_k(k, sizeof(k), &key);
  17923. if (ret != 0) {
  17924. goto done;
  17925. }
  17926. sigSz = sizeof(sig);
  17927. do {
  17928. #if defined(WOLFSSL_ASYNC_CRYPT)
  17929. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17930. #endif
  17931. if (ret == 0)
  17932. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  17933. } while (ret == WC_PENDING_E);
  17934. if (ret != 0) {
  17935. goto done;
  17936. }
  17937. TEST_SLEEP();
  17938. if (sigSz != sizeof(expSig)) {
  17939. ret = -9830;
  17940. goto done;
  17941. }
  17942. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  17943. ret = -9831;
  17944. goto done;
  17945. }
  17946. sigSz = sizeof(sig);
  17947. do {
  17948. #if defined(WOLFSSL_ASYNC_CRYPT)
  17949. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  17950. #endif
  17951. if (ret == 0)
  17952. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  17953. } while (ret == WC_PENDING_E);
  17954. if (ret != 0) {
  17955. goto done;
  17956. }
  17957. TEST_SLEEP();
  17958. done:
  17959. wc_ecc_free(&key);
  17960. return ret;
  17961. }
  17962. #endif
  17963. #ifdef HAVE_ECC_CDH
  17964. static int ecc_test_cdh_vectors(WC_RNG* rng)
  17965. {
  17966. int ret;
  17967. #ifdef WOLFSSL_SMALL_STACK
  17968. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17969. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17970. #else
  17971. ecc_key pub_key[1], priv_key[1];
  17972. #endif
  17973. byte sharedA[32] = {0}, sharedB[32] = {0};
  17974. word32 x, z;
  17975. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  17976. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  17977. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  17978. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  17979. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  17980. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  17981. #ifdef WOLFSSL_SMALL_STACK
  17982. if ((pub_key == NULL) ||
  17983. (priv_key == NULL)) {
  17984. ret = MEMORY_E;
  17985. goto done;
  17986. }
  17987. #endif
  17988. XMEMSET(pub_key, 0, sizeof *pub_key);
  17989. XMEMSET(priv_key, 0, sizeof *priv_key);
  17990. /* setup private and public keys */
  17991. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  17992. if (ret != 0)
  17993. goto done;
  17994. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  17995. if (ret != 0)
  17996. goto done;
  17997. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  17998. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  17999. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  18000. if (ret != 0)
  18001. goto done;
  18002. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  18003. if (ret != 0)
  18004. goto done;
  18005. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18006. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18007. !defined(HAVE_SELFTEST)
  18008. ret = wc_ecc_set_rng(priv_key, rng);
  18009. if (ret != 0)
  18010. goto done;
  18011. #else
  18012. (void)rng;
  18013. #endif
  18014. /* compute ECC Cofactor shared secret */
  18015. x = sizeof(sharedA);
  18016. do {
  18017. #if defined(WOLFSSL_ASYNC_CRYPT)
  18018. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18019. #endif
  18020. if (ret == 0)
  18021. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  18022. } while (ret == WC_PENDING_E);
  18023. if (ret != 0) {
  18024. goto done;
  18025. }
  18026. TEST_SLEEP();
  18027. /* read in expected Z */
  18028. z = sizeof(sharedB);
  18029. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  18030. if (ret != 0)
  18031. goto done;
  18032. /* compare results */
  18033. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  18034. ERROR_OUT(-9840, done);
  18035. }
  18036. done:
  18037. #ifdef WOLFSSL_SMALL_STACK
  18038. if (priv_key) {
  18039. wc_ecc_free(priv_key);
  18040. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18041. }
  18042. if (pub_key) {
  18043. wc_ecc_free(pub_key);
  18044. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18045. }
  18046. #else
  18047. wc_ecc_free(priv_key);
  18048. wc_ecc_free(pub_key);
  18049. #endif
  18050. return ret;
  18051. }
  18052. #endif /* HAVE_ECC_CDH */
  18053. #endif /* HAVE_ECC_VECTOR_TEST */
  18054. #ifdef HAVE_ECC_KEY_IMPORT
  18055. /* returns 0 on success */
  18056. static int ecc_test_make_pub(WC_RNG* rng)
  18057. {
  18058. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18059. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18060. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18061. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18062. #endif
  18063. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18064. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18065. #else
  18066. ecc_key key[1];
  18067. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  18068. ecc_key pub[1];
  18069. #endif
  18070. byte exportBuf[ECC_BUFSIZE];
  18071. byte tmp[ECC_BUFSIZE];
  18072. #endif
  18073. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  18074. #if defined(HAVE_ECC_KEY_EXPORT)
  18075. word32 x;
  18076. #endif
  18077. word32 tmpSz;
  18078. int ret = 0;
  18079. ecc_point* pubPoint = NULL;
  18080. #ifdef HAVE_ECC_VERIFY
  18081. int verify = 0;
  18082. #endif
  18083. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18084. if ((key == NULL) ||
  18085. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18086. (pub == NULL) ||
  18087. #endif
  18088. (exportBuf == NULL) ||
  18089. (tmp == NULL))
  18090. ERROR_OUT(MEMORY_E, done);
  18091. #endif
  18092. (void)msg;
  18093. (void)verify;
  18094. (void)exportBuf;
  18095. (void)rng;
  18096. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18097. #ifndef NO_ECC256
  18098. #ifdef USE_CERT_BUFFERS_256
  18099. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  18100. tmpSz = (size_t)sizeof_ecc_key_der_256;
  18101. #else
  18102. {
  18103. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  18104. if (!file) {
  18105. ERROR_OUT(-9850, done);
  18106. }
  18107. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  18108. XFCLOSE(file);
  18109. }
  18110. #endif /* USE_CERT_BUFFERS_256 */
  18111. /* import private only then test with */
  18112. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  18113. if (ret == 0) {
  18114. ERROR_OUT(-9851, done);
  18115. }
  18116. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  18117. if (ret == 0) {
  18118. ERROR_OUT(-9852, done);
  18119. }
  18120. x = 0;
  18121. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  18122. if (ret != 0) {
  18123. ERROR_OUT(-9853, done);
  18124. }
  18125. #ifdef HAVE_ECC_KEY_EXPORT
  18126. x = ECC_BUFSIZE;
  18127. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18128. if (ret != 0) {
  18129. ERROR_OUT(-9854, done);
  18130. }
  18131. /* make private only key */
  18132. wc_ecc_free(key);
  18133. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18134. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18135. if (ret != 0) {
  18136. ERROR_OUT(-9855, done);
  18137. }
  18138. x = ECC_BUFSIZE;
  18139. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18140. if (ret == 0) {
  18141. ERROR_OUT(-9856, done);
  18142. }
  18143. #endif /* HAVE_ECC_KEY_EXPORT */
  18144. ret = wc_ecc_make_pub(NULL, NULL);
  18145. if (ret == 0) {
  18146. ERROR_OUT(-9857, done);
  18147. }
  18148. TEST_SLEEP();
  18149. #ifndef WOLFSSL_NO_MALLOC
  18150. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  18151. if (pubPoint == NULL) {
  18152. ERROR_OUT(-9858, done);
  18153. }
  18154. ret = wc_ecc_make_pub(key, pubPoint);
  18155. if (ret != 0) {
  18156. ERROR_OUT(-9859, done);
  18157. }
  18158. TEST_SLEEP();
  18159. #ifdef HAVE_ECC_KEY_EXPORT
  18160. /* export should still fail, is private only key */
  18161. x = ECC_BUFSIZE;
  18162. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18163. if (ret == 0) {
  18164. ERROR_OUT(-9860, done);
  18165. }
  18166. #endif /* HAVE_ECC_KEY_EXPORT */
  18167. #endif /* !WOLFSSL_NO_MALLOC */
  18168. #endif /* !NO_ECC256 */
  18169. /* create a new key since above test for loading key is not supported */
  18170. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  18171. defined(WOLFSSL_QNX_CAAM)
  18172. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  18173. if (ret != 0) {
  18174. ERROR_OUT(-9861, done);
  18175. }
  18176. #endif
  18177. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  18178. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG)))
  18179. tmpSz = ECC_BUFSIZE;
  18180. ret = 0;
  18181. do {
  18182. #if defined(WOLFSSL_ASYNC_CRYPT)
  18183. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18184. #endif
  18185. if (ret == 0)
  18186. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp, &tmpSz, rng, key);
  18187. } while (ret == WC_PENDING_E);
  18188. if (ret != 0) {
  18189. ERROR_OUT(-9862, done);
  18190. }
  18191. TEST_SLEEP();
  18192. #ifdef HAVE_ECC_VERIFY
  18193. /* try verify with private only key */
  18194. ret = 0;
  18195. do {
  18196. #if defined(WOLFSSL_ASYNC_CRYPT)
  18197. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18198. #endif
  18199. if (ret == 0)
  18200. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, (word32)XSTRLEN((const char* )msg), &verify, key);
  18201. } while (ret == WC_PENDING_E);
  18202. if (ret != 0) {
  18203. ERROR_OUT(-9863, done);
  18204. }
  18205. if (verify != 1) {
  18206. ERROR_OUT(-9864, done);
  18207. }
  18208. TEST_SLEEP();
  18209. #ifdef HAVE_ECC_KEY_EXPORT
  18210. /* exporting the public part should now work */
  18211. x = ECC_BUFSIZE;
  18212. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18213. if (ret != 0) {
  18214. ERROR_OUT(-9865, done);
  18215. }
  18216. #endif /* HAVE_ECC_KEY_EXPORT */
  18217. #endif /* HAVE_ECC_VERIFY */
  18218. #endif /* HAVE_ECC_SIGN */
  18219. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  18220. /* now test private only key with creating a shared secret */
  18221. x = ECC_BUFSIZE;
  18222. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  18223. if (ret != 0) {
  18224. ERROR_OUT(-9866, done);
  18225. }
  18226. #ifndef WOLFSSL_QNX_CAAM
  18227. /* make private only key */
  18228. wc_ecc_free(key);
  18229. wc_ecc_init_ex(key, HEAP_HINT, devId);
  18230. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  18231. if (ret != 0) {
  18232. ERROR_OUT(-9867, done);
  18233. }
  18234. /* check that public export fails with private only key */
  18235. x = ECC_BUFSIZE;
  18236. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  18237. if (ret == 0) {
  18238. ERROR_OUT(-9868, done);
  18239. }
  18240. #endif /* WOLFSSL_QNX_CAAM */
  18241. /* make public key for shared secret */
  18242. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  18243. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  18244. #if defined(WOLFSSL_ASYNC_CRYPT)
  18245. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  18246. #endif
  18247. if (ret != 0) {
  18248. ERROR_OUT(-9869, done);
  18249. }
  18250. TEST_SLEEP();
  18251. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18252. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18253. !defined(HAVE_SELFTEST)
  18254. ret = wc_ecc_set_rng(key, rng);
  18255. if (ret != 0)
  18256. goto done;
  18257. #endif
  18258. x = ECC_BUFSIZE;
  18259. do {
  18260. #if defined(WOLFSSL_ASYNC_CRYPT)
  18261. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18262. #endif
  18263. if (ret == 0) {
  18264. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  18265. }
  18266. } while (ret == WC_PENDING_E);
  18267. wc_ecc_free(pub);
  18268. if (ret != 0) {
  18269. ERROR_OUT(-9870, done);
  18270. }
  18271. TEST_SLEEP();
  18272. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  18273. ret = 0;
  18274. done:
  18275. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  18276. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18277. if (key != NULL) {
  18278. wc_ecc_free(key);
  18279. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18280. }
  18281. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  18282. if (pub != NULL)
  18283. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18284. #endif
  18285. if (exportBuf != NULL)
  18286. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18287. if (tmp != NULL)
  18288. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18289. #else
  18290. wc_ecc_free(key);
  18291. #endif
  18292. return ret;
  18293. }
  18294. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  18295. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  18296. {
  18297. int ret;
  18298. #ifdef WOLFSSL_SMALL_STACK
  18299. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18300. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18301. #else
  18302. ecc_key eccKey[1];
  18303. byte tmpBuf[ECC_BUFSIZE];
  18304. #endif
  18305. word32 tmpSz;
  18306. word32 idx;
  18307. #ifdef WOLFSSL_SMALL_STACK
  18308. if ((eccKey == NULL) || (tmpBuf == NULL))
  18309. ERROR_OUT(MEMORY_E, done);
  18310. #endif
  18311. ret = wc_ecc_init(eccKey);
  18312. if (ret != 0) {
  18313. goto done;
  18314. }
  18315. ret = wc_ecc_make_key(rng, keySize, eccKey);
  18316. #if defined(WOLFSSL_ASYNC_CRYPT)
  18317. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  18318. #endif
  18319. if (ret != 0) {
  18320. goto done;
  18321. }
  18322. tmpSz = ECC_BUFSIZE;
  18323. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  18324. wc_ecc_free(eccKey);
  18325. if (ret < 0) {
  18326. goto done;
  18327. }
  18328. tmpSz = ret;
  18329. ret = wc_ecc_init(eccKey);
  18330. if (ret != 0) {
  18331. goto done;
  18332. }
  18333. idx = 0;
  18334. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18335. if (ret != 0) {
  18336. goto done;
  18337. }
  18338. wc_ecc_free(eccKey);
  18339. ret = wc_ecc_init(eccKey);
  18340. if (ret != 0) {
  18341. goto done;
  18342. }
  18343. idx = 0;
  18344. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  18345. if (ret != 0) {
  18346. goto done;
  18347. }
  18348. ret = 0;
  18349. done:
  18350. #ifdef WOLFSSL_SMALL_STACK
  18351. if (eccKey != NULL) {
  18352. wc_ecc_free(eccKey);
  18353. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18354. }
  18355. if (tmpBuf != NULL)
  18356. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18357. #else
  18358. wc_ecc_free(eccKey);
  18359. #endif
  18360. return ret;
  18361. }
  18362. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18363. #endif /* HAVE_ECC_KEY_IMPORT */
  18364. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  18365. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  18366. {
  18367. int ret = 0;
  18368. int derSz;
  18369. #ifdef HAVE_PKCS8
  18370. word32 pkcs8Sz;
  18371. #endif
  18372. #ifdef WOLFSSL_SMALL_STACK
  18373. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18374. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18375. #else
  18376. byte der[ECC_BUFSIZE];
  18377. ecc_key userA[1];
  18378. #endif
  18379. #ifdef WOLFSSL_SMALL_STACK
  18380. if ((der == NULL) || (userA == NULL))
  18381. ERROR_OUT(MEMORY_E, done);
  18382. #endif
  18383. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18384. if (ret != 0)
  18385. goto done;
  18386. ret = wc_ecc_make_key(rng, keySize, userA);
  18387. #if defined(WOLFSSL_ASYNC_CRYPT)
  18388. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18389. #endif
  18390. if (ret != 0)
  18391. goto done;
  18392. TEST_SLEEP();
  18393. ret = wc_ecc_check_key(userA);
  18394. if (ret != 0)
  18395. goto done;
  18396. TEST_SLEEP();
  18397. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  18398. if (derSz < 0) {
  18399. ERROR_OUT(derSz, done);
  18400. }
  18401. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  18402. ECC_PRIVATEKEY_TYPE, -8347);
  18403. if (ret != 0) {
  18404. goto done;
  18405. }
  18406. /* test export of public key */
  18407. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  18408. if (derSz < 0) {
  18409. ERROR_OUT(derSz, done);
  18410. }
  18411. if (derSz == 0) {
  18412. ERROR_OUT(-9890, done);
  18413. }
  18414. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  18415. if (ret != 0) {
  18416. goto done;
  18417. }
  18418. #ifdef HAVE_PKCS8
  18419. /* test export of PKCS#8 unencrypted private key */
  18420. pkcs8Sz = FOURK_BUF;
  18421. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  18422. if (derSz < 0) {
  18423. ERROR_OUT(derSz, done);
  18424. }
  18425. if (derSz == 0) {
  18426. ERROR_OUT(-9891, done);
  18427. }
  18428. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  18429. if (ret != 0) {
  18430. goto done;
  18431. }
  18432. #endif /* HAVE_PKCS8 */
  18433. done:
  18434. #ifdef WOLFSSL_SMALL_STACK
  18435. if (der != NULL)
  18436. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18437. if (userA != NULL) {
  18438. wc_ecc_free(userA);
  18439. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18440. }
  18441. #else
  18442. wc_ecc_free(userA);
  18443. #endif
  18444. return ret;
  18445. }
  18446. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  18447. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  18448. int curve_id, const ecc_set_type* dp)
  18449. {
  18450. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  18451. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18452. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  18453. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  18454. #endif
  18455. #ifdef HAVE_ECC_KEY_EXPORT
  18456. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  18457. DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  18458. #endif
  18459. word32 x = 0;
  18460. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  18461. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18462. word32 y;
  18463. #endif
  18464. #ifdef HAVE_ECC_SIGN
  18465. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  18466. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  18467. int i;
  18468. #ifdef HAVE_ECC_VERIFY
  18469. int verify;
  18470. #endif /* HAVE_ECC_VERIFY */
  18471. #endif /* HAVE_ECC_SIGN */
  18472. int ret;
  18473. #ifdef WOLFSSL_SMALL_STACK
  18474. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18475. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18476. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18477. #else
  18478. ecc_key userA[1];
  18479. ecc_key userB[1];
  18480. ecc_key pubKey[1];
  18481. #endif
  18482. #ifndef WC_NO_RNG
  18483. int curveSize;
  18484. #endif
  18485. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  18486. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  18487. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18488. if (sharedA == NULL || sharedB == NULL)
  18489. ERROR_OUT(-9900, done);
  18490. #endif
  18491. #ifdef HAVE_ECC_KEY_EXPORT
  18492. if (exportBuf == NULL)
  18493. ERROR_OUT(-9901, done);
  18494. #endif
  18495. #ifdef HAVE_ECC_SIGN
  18496. if (sig == NULL || digest == NULL)
  18497. ERROR_OUT(-9902, done);
  18498. #endif
  18499. #endif /* WOLFSSL_SMALL_STACK */
  18500. (void)testVerifyCount;
  18501. (void)dp;
  18502. (void)x;
  18503. #ifdef WOLFSSL_SMALL_STACK
  18504. if ((userA == NULL) ||
  18505. (userB == NULL) ||
  18506. (pubKey == NULL))
  18507. ERROR_OUT(-9903, done);
  18508. #endif
  18509. XMEMSET(userA, 0, sizeof *userA);
  18510. XMEMSET(userB, 0, sizeof *userB);
  18511. XMEMSET(pubKey, 0, sizeof *pubKey);
  18512. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  18513. if (ret != 0)
  18514. ERROR_OUT(-9904, done);
  18515. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  18516. if (ret != 0)
  18517. ERROR_OUT(-9905, done);
  18518. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  18519. if (ret != 0)
  18520. ERROR_OUT(-9906, done);
  18521. #ifdef WOLFSSL_CUSTOM_CURVES
  18522. if (dp != NULL) {
  18523. ret = wc_ecc_set_custom_curve(userA, dp);
  18524. if (ret != 0)
  18525. ERROR_OUT(-9907, done);
  18526. ret = wc_ecc_set_custom_curve(userB, dp);
  18527. if (ret != 0)
  18528. ERROR_OUT(-9908, done);
  18529. }
  18530. #endif
  18531. #ifndef WC_NO_RNG
  18532. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  18533. #if defined(WOLFSSL_ASYNC_CRYPT)
  18534. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  18535. #endif
  18536. if (ret != 0)
  18537. ERROR_OUT(-9910, done);
  18538. TEST_SLEEP();
  18539. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  18540. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  18541. if (curveSize != userA->dp->size)
  18542. ERROR_OUT(-9911, done);
  18543. }
  18544. ret = wc_ecc_check_key(userA);
  18545. if (ret != 0)
  18546. ERROR_OUT(-9912, done);
  18547. TEST_SLEEP();
  18548. /* ATECC508/608 configuration may not support more than one ECDH key */
  18549. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18550. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  18551. #if defined(WOLFSSL_ASYNC_CRYPT)
  18552. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  18553. #endif
  18554. if (ret != 0)
  18555. ERROR_OUT(-9914, done);
  18556. TEST_SLEEP();
  18557. /* only perform the below tests if the key size matches */
  18558. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  18559. ERROR_OUT(ECC_CURVE_OID_E, done);
  18560. #ifdef HAVE_ECC_DHE
  18561. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  18562. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  18563. !defined(HAVE_SELFTEST)
  18564. ret = wc_ecc_set_rng(userA, rng);
  18565. if (ret != 0)
  18566. ERROR_OUT(-9915, done);
  18567. ret = wc_ecc_set_rng(userB, rng);
  18568. if (ret != 0)
  18569. ERROR_OUT(-9916, done);
  18570. #endif
  18571. x = ECC_SHARED_SIZE;
  18572. do {
  18573. #if defined(WOLFSSL_ASYNC_CRYPT)
  18574. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18575. #endif
  18576. if (ret == 0)
  18577. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  18578. } while (ret == WC_PENDING_E);
  18579. if (ret != 0) {
  18580. ERROR_OUT(-9917, done);
  18581. }
  18582. TEST_SLEEP();
  18583. y = ECC_SHARED_SIZE;
  18584. do {
  18585. #if defined(WOLFSSL_ASYNC_CRYPT)
  18586. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18587. #endif
  18588. if (ret == 0)
  18589. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  18590. } while (ret == WC_PENDING_E);
  18591. if (ret != 0)
  18592. ERROR_OUT(-9918, done);
  18593. if (y != x)
  18594. ERROR_OUT(-9919, done);
  18595. if (XMEMCMP(sharedA, sharedB, x))
  18596. ERROR_OUT(-9920, done);
  18597. TEST_SLEEP();
  18598. #endif /* HAVE_ECC_DHE */
  18599. #ifdef HAVE_ECC_CDH
  18600. /* add cofactor flag */
  18601. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  18602. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  18603. x = ECC_SHARED_SIZE;
  18604. do {
  18605. #if defined(WOLFSSL_ASYNC_CRYPT)
  18606. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18607. #endif
  18608. if (ret == 0)
  18609. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  18610. } while (ret == WC_PENDING_E);
  18611. if (ret != 0)
  18612. ERROR_OUT(-9921, done);
  18613. TEST_SLEEP();
  18614. y = ECC_SHARED_SIZE;
  18615. do {
  18616. #if defined(WOLFSSL_ASYNC_CRYPT)
  18617. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18618. #endif
  18619. if (ret == 0)
  18620. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  18621. } while (ret == WC_PENDING_E);
  18622. if (ret != 0)
  18623. ERROR_OUT(-9922, done);
  18624. if (y != x)
  18625. ERROR_OUT(-9923, done);
  18626. if (XMEMCMP(sharedA, sharedB, x))
  18627. ERROR_OUT(-9924, done);
  18628. TEST_SLEEP();
  18629. /* remove cofactor flag */
  18630. wc_ecc_set_flags(userA, 0);
  18631. wc_ecc_set_flags(userB, 0);
  18632. #endif /* HAVE_ECC_CDH */
  18633. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  18634. #ifdef HAVE_ECC_KEY_EXPORT
  18635. x = ECC_KEY_EXPORT_BUF_SIZE;
  18636. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  18637. if (ret != 0)
  18638. ERROR_OUT(-9925, done);
  18639. #ifdef HAVE_ECC_KEY_IMPORT
  18640. #ifdef WOLFSSL_CUSTOM_CURVES
  18641. if (dp != NULL) {
  18642. ret = wc_ecc_set_custom_curve(pubKey, dp);
  18643. if (ret != 0)
  18644. ERROR_OUT(-9926, done);
  18645. }
  18646. #endif
  18647. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  18648. if (ret != 0)
  18649. ERROR_OUT(-9927, done);
  18650. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  18651. #ifdef HAVE_ECC_DHE
  18652. y = ECC_SHARED_SIZE;
  18653. do {
  18654. #if defined(WOLFSSL_ASYNC_CRYPT)
  18655. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18656. #endif
  18657. if (ret == 0)
  18658. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  18659. } while (ret == WC_PENDING_E);
  18660. if (ret != 0)
  18661. ERROR_OUT(-9928, done);
  18662. if (XMEMCMP(sharedA, sharedB, y))
  18663. ERROR_OUT(-9929, done);
  18664. TEST_SLEEP();
  18665. #endif /* HAVE_ECC_DHE */
  18666. #ifdef HAVE_COMP_KEY
  18667. /* try compressed export / import too */
  18668. x = ECC_KEY_EXPORT_BUF_SIZE;
  18669. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  18670. if (ret != 0)
  18671. ERROR_OUT(-9930, done);
  18672. wc_ecc_free(pubKey);
  18673. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  18674. if (ret != 0)
  18675. ERROR_OUT(-9931, done);
  18676. #ifdef WOLFSSL_CUSTOM_CURVES
  18677. if (dp != NULL) {
  18678. ret = wc_ecc_set_custom_curve(pubKey, dp);
  18679. if (ret != 0)
  18680. ERROR_OUT(-9932, done);
  18681. }
  18682. #endif
  18683. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  18684. if (ret != 0)
  18685. ERROR_OUT(-9933, done);
  18686. #ifdef HAVE_ECC_DHE
  18687. y = ECC_SHARED_SIZE;
  18688. do {
  18689. #if defined(WOLFSSL_ASYNC_CRYPT)
  18690. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18691. #endif
  18692. if (ret == 0)
  18693. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  18694. } while (ret == WC_PENDING_E);
  18695. if (ret != 0)
  18696. ERROR_OUT(-9934, done);
  18697. if (XMEMCMP(sharedA, sharedB, y))
  18698. ERROR_OUT(-9935, done);
  18699. TEST_SLEEP();
  18700. #endif /* HAVE_ECC_DHE */
  18701. #endif /* HAVE_COMP_KEY */
  18702. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  18703. #endif /* !WC_NO_RNG */
  18704. #endif /* HAVE_ECC_KEY_IMPORT */
  18705. #endif /* HAVE_ECC_KEY_EXPORT */
  18706. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))
  18707. #ifdef HAVE_ECC_SIGN
  18708. /* ECC w/out Shamir has issue with all 0 digest */
  18709. /* WC_BIGINT doesn't have 0 len well on hardware */
  18710. /* Cryptocell has issues with all 0 digest */
  18711. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  18712. !defined(WOLFSSL_CRYPTOCELL)
  18713. /* test DSA sign hash with zeros */
  18714. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  18715. digest[i] = 0;
  18716. }
  18717. x = ECC_SIG_SIZE;
  18718. do {
  18719. #if defined(WOLFSSL_ASYNC_CRYPT)
  18720. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18721. #endif
  18722. if (ret == 0)
  18723. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  18724. userA);
  18725. } while (ret == WC_PENDING_E);
  18726. if (ret != 0)
  18727. ERROR_OUT(-9936, done);
  18728. TEST_SLEEP();
  18729. #ifdef HAVE_ECC_VERIFY
  18730. for (i=0; i<testVerifyCount; i++) {
  18731. verify = 0;
  18732. do {
  18733. #if defined(WOLFSSL_ASYNC_CRYPT)
  18734. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18735. #endif
  18736. if (ret == 0)
  18737. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  18738. &verify, userA);
  18739. } while (ret == WC_PENDING_E);
  18740. if (ret != 0)
  18741. ERROR_OUT(-9937, done);
  18742. if (verify != 1)
  18743. ERROR_OUT(-9938, done);
  18744. TEST_SLEEP();
  18745. }
  18746. #endif /* HAVE_ECC_VERIFY */
  18747. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  18748. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  18749. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  18750. digest[i] = (byte)i;
  18751. }
  18752. x = ECC_SIG_SIZE;
  18753. do {
  18754. #if defined(WOLFSSL_ASYNC_CRYPT)
  18755. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18756. #endif
  18757. if (ret == 0)
  18758. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  18759. userA);
  18760. } while (ret == WC_PENDING_E);
  18761. if (ret != 0)
  18762. ERROR_OUT(-9939, done);
  18763. TEST_SLEEP();
  18764. #ifdef HAVE_ECC_VERIFY
  18765. for (i=0; i<testVerifyCount; i++) {
  18766. verify = 0;
  18767. do {
  18768. #if defined(WOLFSSL_ASYNC_CRYPT)
  18769. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  18770. #endif
  18771. if (ret == 0)
  18772. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  18773. &verify, userA);
  18774. } while (ret == WC_PENDING_E);
  18775. if (ret != 0)
  18776. ERROR_OUT(-9940, done);
  18777. if (verify != 1)
  18778. ERROR_OUT(-9941, done);
  18779. TEST_SLEEP();
  18780. }
  18781. #endif /* HAVE_ECC_VERIFY */
  18782. #endif /* HAVE_ECC_SIGN */
  18783. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  18784. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  18785. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A)
  18786. x = ECC_KEY_EXPORT_BUF_SIZE;
  18787. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  18788. if (ret != 0)
  18789. ERROR_OUT(-9942, done);
  18790. #else
  18791. (void)exportBuf;
  18792. #endif /* HAVE_ECC_KEY_EXPORT */
  18793. done:
  18794. #ifdef WOLFSSL_SMALL_STACK
  18795. if (userA != NULL) {
  18796. wc_ecc_free(userA);
  18797. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18798. }
  18799. if (userB != NULL) {
  18800. wc_ecc_free(userB);
  18801. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18802. }
  18803. if (pubKey != NULL) {
  18804. wc_ecc_free(pubKey);
  18805. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18806. }
  18807. #else
  18808. wc_ecc_free(pubKey);
  18809. wc_ecc_free(userB);
  18810. wc_ecc_free(userA);
  18811. #endif
  18812. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  18813. FREE_VAR(sharedA, HEAP_HINT);
  18814. FREE_VAR(sharedB, HEAP_HINT);
  18815. #endif
  18816. #ifdef HAVE_ECC_KEY_EXPORT
  18817. FREE_VAR(exportBuf, HEAP_HINT);
  18818. #endif
  18819. #ifdef HAVE_ECC_SIGN
  18820. FREE_VAR(sig, HEAP_HINT);
  18821. FREE_VAR(digest, HEAP_HINT);
  18822. #endif
  18823. (void)keySize;
  18824. (void)curve_id;
  18825. (void)rng;
  18826. return ret;
  18827. }
  18828. #undef ECC_TEST_VERIFY_COUNT
  18829. #define ECC_TEST_VERIFY_COUNT 2
  18830. static int ecc_test_curve(WC_RNG* rng, int keySize)
  18831. {
  18832. int ret;
  18833. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  18834. ECC_CURVE_DEF, NULL);
  18835. if (ret < 0) {
  18836. if (ret == ECC_CURVE_OID_E) {
  18837. /* ignore error for curves not found */
  18838. /* some curve sizes are only available with:
  18839. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  18840. and HAVE_ECC_KOBLITZ */
  18841. }
  18842. else {
  18843. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  18844. return ret;
  18845. }
  18846. }
  18847. #ifdef HAVE_ECC_VECTOR_TEST
  18848. ret = ecc_test_vector(keySize);
  18849. if (ret < 0) {
  18850. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  18851. return ret;
  18852. }
  18853. #endif
  18854. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  18855. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  18856. ret = ecc_test_key_decode(rng, keySize);
  18857. if (ret < 0) {
  18858. if (ret == ECC_CURVE_OID_E) {
  18859. /* ignore error for curves not found */
  18860. }
  18861. else {
  18862. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  18863. return ret;
  18864. }
  18865. }
  18866. #endif
  18867. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  18868. ret = ecc_test_key_gen(rng, keySize);
  18869. if (ret < 0) {
  18870. if (ret == ECC_CURVE_OID_E) {
  18871. /* ignore error for curves not found */
  18872. }
  18873. else {
  18874. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  18875. return ret;
  18876. }
  18877. }
  18878. #endif
  18879. return 0;
  18880. }
  18881. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  18882. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  18883. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  18884. !defined(WOLFSSL_NO_MALLOC)
  18885. static int ecc_point_test(void)
  18886. {
  18887. int ret;
  18888. ecc_point* point;
  18889. ecc_point* point2;
  18890. #ifdef HAVE_COMP_KEY
  18891. ecc_point* point3;
  18892. ecc_point* point4;
  18893. #endif
  18894. word32 outLen;
  18895. byte out[65];
  18896. byte der[] = { 0x04, /* = Uncompressed */
  18897. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18898. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18899. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18900. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18901. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18902. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18903. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18904. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18905. #ifdef HAVE_COMP_KEY
  18906. byte derComp0[] = { 0x02, /* = Compressed, y even */
  18907. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18908. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18909. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18910. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18911. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  18912. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18913. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18914. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18915. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  18916. #endif
  18917. byte altDer[] = { 0x04, /* = Uncompressed */
  18918. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18919. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18920. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18921. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18922. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18923. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18924. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18925. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  18926. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  18927. /* if curve P256 is not enabled then test should not fail */
  18928. if (curve_idx == ECC_CURVE_INVALID)
  18929. return 0;
  18930. outLen = sizeof(out);
  18931. point = wc_ecc_new_point();
  18932. if (point == NULL)
  18933. return -10000;
  18934. point2 = wc_ecc_new_point();
  18935. if (point2 == NULL) {
  18936. wc_ecc_del_point(point);
  18937. return -10001;
  18938. }
  18939. #ifdef HAVE_COMP_KEY
  18940. point3 = wc_ecc_new_point();
  18941. if (point3 == NULL) {
  18942. wc_ecc_del_point(point2);
  18943. wc_ecc_del_point(point);
  18944. return -10002;
  18945. }
  18946. point4 = wc_ecc_new_point();
  18947. if (point4 == NULL) {
  18948. wc_ecc_del_point(point3);
  18949. wc_ecc_del_point(point2);
  18950. wc_ecc_del_point(point);
  18951. return -10003;
  18952. }
  18953. #endif
  18954. /* Parameter Validation testing. */
  18955. wc_ecc_del_point(NULL);
  18956. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  18957. if (ret != ECC_BAD_ARG_E) {
  18958. ret = -10004;
  18959. goto done;
  18960. }
  18961. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  18962. if (ret != ECC_BAD_ARG_E) {
  18963. ret = -10005;
  18964. goto done;
  18965. }
  18966. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  18967. if (ret != ECC_BAD_ARG_E) {
  18968. ret = -10006;
  18969. goto done;
  18970. }
  18971. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  18972. if (ret != ECC_BAD_ARG_E) {
  18973. ret = -10007;
  18974. goto done;
  18975. }
  18976. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  18977. if (ret != ECC_BAD_ARG_E) {
  18978. ret = -10008;
  18979. goto done;
  18980. }
  18981. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  18982. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  18983. ret = -10009;
  18984. goto done;
  18985. }
  18986. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  18987. if (ret != ECC_BAD_ARG_E) {
  18988. ret = -10010;
  18989. goto done;
  18990. }
  18991. outLen = 0;
  18992. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  18993. if (ret != BUFFER_E) {
  18994. ret = -10011;
  18995. goto done;
  18996. }
  18997. ret = wc_ecc_copy_point(NULL, NULL);
  18998. if (ret != ECC_BAD_ARG_E) {
  18999. ret = -10012;
  19000. goto done;
  19001. }
  19002. ret = wc_ecc_copy_point(NULL, point2);
  19003. if (ret != ECC_BAD_ARG_E) {
  19004. ret = -10013;
  19005. goto done;
  19006. }
  19007. ret = wc_ecc_copy_point(point, NULL);
  19008. if (ret != ECC_BAD_ARG_E) {
  19009. ret = -10014;
  19010. goto done;
  19011. }
  19012. ret = wc_ecc_cmp_point(NULL, NULL);
  19013. if (ret != BAD_FUNC_ARG) {
  19014. ret = -10015;
  19015. goto done;
  19016. }
  19017. ret = wc_ecc_cmp_point(NULL, point2);
  19018. if (ret != BAD_FUNC_ARG) {
  19019. ret = -10016;
  19020. goto done;
  19021. }
  19022. ret = wc_ecc_cmp_point(point, NULL);
  19023. if (ret != BAD_FUNC_ARG) {
  19024. ret = -10017;
  19025. goto done;
  19026. }
  19027. /* Use API. */
  19028. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  19029. if (ret != 0) {
  19030. ret = -10018;
  19031. goto done;
  19032. }
  19033. outLen = sizeof(out);
  19034. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  19035. if (ret != 0) {
  19036. ret = -10019;
  19037. goto done;
  19038. }
  19039. if (outLen != sizeof(der)) {
  19040. ret = -10020;
  19041. goto done;
  19042. }
  19043. if (XMEMCMP(out, der, outLen) != 0) {
  19044. ret = -10021;
  19045. goto done;
  19046. }
  19047. ret = wc_ecc_copy_point(point2, point);
  19048. if (ret != MP_OKAY) {
  19049. ret = -10022;
  19050. goto done;
  19051. }
  19052. ret = wc_ecc_cmp_point(point2, point);
  19053. if (ret != MP_EQ) {
  19054. ret = -10023;
  19055. goto done;
  19056. }
  19057. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  19058. if (ret != 0) {
  19059. ret = -10024;
  19060. goto done;
  19061. }
  19062. ret = wc_ecc_cmp_point(point2, point);
  19063. if (ret != MP_GT) {
  19064. ret = -10025;
  19065. goto done;
  19066. }
  19067. #ifdef HAVE_COMP_KEY
  19068. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  19069. if (ret != 0) {
  19070. ret = -10026;
  19071. goto done;
  19072. }
  19073. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  19074. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  19075. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  19076. if (ret != 0) {
  19077. ret = -10027;
  19078. goto done;
  19079. }
  19080. #endif
  19081. ret = wc_ecc_cmp_point(point3, point4);
  19082. if (ret != MP_EQ) {
  19083. ret = -10028;
  19084. goto done;
  19085. }
  19086. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  19087. if (ret != 0) {
  19088. ret = -10029;
  19089. goto done;
  19090. }
  19091. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  19092. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  19093. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  19094. if (ret != 0) {
  19095. ret = -10030;
  19096. goto done;
  19097. }
  19098. #endif
  19099. ret = wc_ecc_cmp_point(point3, point4);
  19100. if (ret != MP_EQ) {
  19101. ret = -10031;
  19102. goto done;
  19103. }
  19104. #endif
  19105. done:
  19106. #ifdef HAVE_COMP_KEY
  19107. wc_ecc_del_point(point4);
  19108. wc_ecc_del_point(point3);
  19109. #endif
  19110. wc_ecc_del_point(point2);
  19111. wc_ecc_del_point(point);
  19112. return ret;
  19113. }
  19114. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  19115. #ifndef NO_SIG_WRAPPER
  19116. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  19117. {
  19118. int ret;
  19119. word32 sigSz;
  19120. int size;
  19121. byte out[ECC_MAX_SIG_SIZE];
  19122. byte in[] = TEST_STRING;
  19123. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  19124. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  19125. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  19126. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  19127. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  19128. };
  19129. word32 inLen = (word32)XSTRLEN((char*)in);
  19130. size = wc_ecc_sig_size(key);
  19131. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  19132. if (ret != size)
  19133. return -10040;
  19134. sigSz = (word32)ret;
  19135. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  19136. inLen, out, &sigSz, key, sizeof(*key), rng);
  19137. if (ret != 0)
  19138. return -10041;
  19139. TEST_SLEEP();
  19140. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  19141. inLen, out, sigSz, key, sizeof(*key));
  19142. if (ret != 0)
  19143. return -10042;
  19144. TEST_SLEEP();
  19145. sigSz = (word32)sizeof(out);
  19146. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19147. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  19148. if (ret != 0)
  19149. return -10043;
  19150. TEST_SLEEP();
  19151. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  19152. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  19153. if (ret != 0)
  19154. return -10044;
  19155. TEST_SLEEP();
  19156. return 0;
  19157. }
  19158. #endif
  19159. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19160. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19161. !defined(WOLFSSL_QNX_CAAM)
  19162. static int ecc_exp_imp_test(ecc_key* key)
  19163. {
  19164. int ret;
  19165. int curve_id;
  19166. #ifdef WOLFSSL_SMALL_STACK
  19167. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);;
  19168. #else
  19169. ecc_key keyImp[1];
  19170. #endif
  19171. byte priv[32];
  19172. word32 privLen;
  19173. byte pub[65];
  19174. word32 pubLen, pubLenX, pubLenY;
  19175. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  19176. "ce46cbc947616d0cbaa82323818a793d";
  19177. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  19178. "922f8b30ea6e8811742ac7238fe87308";
  19179. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  19180. "2e7a493ec1153b3a95feb8a4873f8d08";
  19181. #ifdef WOLFSSL_SMALL_STACK
  19182. if (keyImp == NULL)
  19183. ERROR_OUT(-10050, done);
  19184. #endif
  19185. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19186. privLen = sizeof(priv);
  19187. ret = wc_ecc_export_private_only(key, priv, &privLen);
  19188. if (ret != 0) {
  19189. ret = -10051;
  19190. goto done;
  19191. }
  19192. pubLen = sizeof(pub);
  19193. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  19194. if (ret != 0) {
  19195. ret = -10052;
  19196. goto done;
  19197. }
  19198. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  19199. if (ret != 0) {
  19200. ret = -10053;
  19201. goto done;
  19202. }
  19203. wc_ecc_free(keyImp);
  19204. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19205. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  19206. if (ret != 0) {
  19207. ret = -10054;
  19208. goto done;
  19209. }
  19210. wc_ecc_free(keyImp);
  19211. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19212. curve_id = wc_ecc_get_curve_id(key->idx);
  19213. if (curve_id < 0) {
  19214. ret = -10055;
  19215. goto done;
  19216. }
  19217. /* test import private only */
  19218. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  19219. curve_id);
  19220. if (ret != 0) {
  19221. ret = -10056;
  19222. goto done;
  19223. }
  19224. wc_ecc_free(keyImp);
  19225. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19226. /* test export public raw */
  19227. pubLenX = pubLenY = 32;
  19228. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  19229. if (ret != 0) {
  19230. ret = -10057;
  19231. goto done;
  19232. }
  19233. #ifndef HAVE_SELFTEST
  19234. /* test import of public */
  19235. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  19236. if (ret != 0) {
  19237. ret = -10058;
  19238. goto done;
  19239. }
  19240. #endif
  19241. wc_ecc_free(keyImp);
  19242. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  19243. /* test export private and public raw */
  19244. pubLenX = pubLenY = privLen = 32;
  19245. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  19246. priv, &privLen);
  19247. if (ret != 0) {
  19248. ret = -10059;
  19249. goto done;
  19250. }
  19251. #ifndef HAVE_SELFTEST
  19252. /* test import of private and public */
  19253. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  19254. if (ret != 0) {
  19255. ret = -10060;
  19256. goto done;
  19257. }
  19258. #endif
  19259. done:
  19260. #ifdef WOLFSSL_SMALL_STACK
  19261. if (keyImp != NULL) {
  19262. wc_ecc_free(keyImp);
  19263. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19264. }
  19265. #else
  19266. wc_ecc_free(keyImp);
  19267. #endif
  19268. return ret;
  19269. }
  19270. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  19271. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19272. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_QNX_CAAM)
  19273. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19274. static int ecc_mulmod_test(ecc_key* key1)
  19275. {
  19276. int ret;
  19277. #ifdef WOLFSSL_SMALL_STACK
  19278. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19279. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19280. #else
  19281. ecc_key key2[1];
  19282. ecc_key key3[1];
  19283. #endif
  19284. #ifdef WOLFSSL_SMALL_STACK
  19285. if ((key2 == NULL) || (key3 == NULL))
  19286. ERROR_OUT(MEMORY_E, done);
  19287. #endif
  19288. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  19289. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  19290. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  19291. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  19292. * private key in key2.
  19293. */
  19294. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  19295. ECC_SECP256R1);
  19296. if (ret != 0)
  19297. goto done;
  19298. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  19299. * in key3.
  19300. */
  19301. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  19302. key1->dp->prime, ECC_SECP256R1);
  19303. if (ret != 0)
  19304. goto done;
  19305. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  19306. 1);
  19307. if (ret != 0) {
  19308. ret = -10070;
  19309. goto done;
  19310. }
  19311. done:
  19312. #ifdef WOLFSSL_SMALL_STACK
  19313. if (key2 != NULL) {
  19314. wc_ecc_free(key2);
  19315. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19316. }
  19317. if (key3 != NULL) {
  19318. wc_ecc_free(key3);
  19319. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19320. }
  19321. #else
  19322. wc_ecc_free(key3);
  19323. wc_ecc_free(key2);
  19324. #endif
  19325. return ret;
  19326. }
  19327. #endif
  19328. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  19329. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  19330. {
  19331. int ret;
  19332. byte out[128];
  19333. word32 outLen = sizeof(out);
  19334. /* Parameter Validation testing. */
  19335. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  19336. if (ret != BAD_FUNC_ARG)
  19337. return -10080;
  19338. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  19339. if (ret != BAD_FUNC_ARG)
  19340. return -10081;
  19341. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  19342. if (ret != BAD_FUNC_ARG)
  19343. return -10082;
  19344. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  19345. if (ret != BAD_FUNC_ARG)
  19346. return -10083;
  19347. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19348. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19349. !defined(HAVE_SELFTEST)
  19350. ret = wc_ecc_set_rng(key, rng);
  19351. if (ret != 0)
  19352. return -10084;
  19353. #else
  19354. (void)rng;
  19355. #endif
  19356. /* Use API. */
  19357. ret = 0;
  19358. do {
  19359. #if defined(WOLFSSL_ASYNC_CRYPT)
  19360. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19361. #endif
  19362. if (ret == 0)
  19363. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  19364. } while (ret == WC_PENDING_E);
  19365. if (ret != 0)
  19366. return -10085;
  19367. TEST_SLEEP();
  19368. return 0;
  19369. }
  19370. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  19371. #endif
  19372. static int ecc_def_curve_test(WC_RNG *rng)
  19373. {
  19374. int ret;
  19375. #ifdef WOLFSSL_SMALL_STACK
  19376. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19377. #else
  19378. ecc_key key[1];
  19379. #endif
  19380. #ifdef WC_NO_RNG
  19381. word32 idx = 0;
  19382. #endif
  19383. #ifdef WOLFSSL_SMALL_STACK
  19384. if (key == NULL)
  19385. ERROR_OUT(MEMORY_E, done);
  19386. #endif
  19387. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19388. /* Use API */
  19389. ret = wc_ecc_set_flags(NULL, 0);
  19390. if (ret != BAD_FUNC_ARG) {
  19391. ret = -10090;
  19392. goto done;
  19393. }
  19394. ret = wc_ecc_set_flags(key, 0);
  19395. if (ret != 0) {
  19396. ret = -10091;
  19397. goto done;
  19398. }
  19399. #ifndef WC_NO_RNG
  19400. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19401. #if defined(WOLFSSL_ASYNC_CRYPT)
  19402. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  19403. #endif
  19404. #else
  19405. /* use test ECC key */
  19406. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  19407. (word32)sizeof_ecc_key_der_256);
  19408. (void)rng;
  19409. #endif
  19410. if (ret != 0) {
  19411. ret = -10092;
  19412. goto done;
  19413. }
  19414. TEST_SLEEP();
  19415. #ifndef NO_SIG_WRAPPER
  19416. ret = ecc_sig_test(rng, key);
  19417. if (ret < 0)
  19418. goto done;
  19419. #endif
  19420. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  19421. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19422. !defined(WOLFSSL_QNX_CAAM)
  19423. ret = ecc_exp_imp_test(key);
  19424. if (ret < 0)
  19425. goto done;
  19426. #endif
  19427. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  19428. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_QNX_CAAM)
  19429. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  19430. ret = ecc_mulmod_test(key);
  19431. if (ret < 0)
  19432. goto done;
  19433. #endif
  19434. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  19435. ret = ecc_ssh_test(key, rng);
  19436. if (ret < 0)
  19437. goto done;
  19438. #endif
  19439. #endif /* WOLFSSL_ATECC508A */
  19440. done:
  19441. #ifdef WOLFSSL_SMALL_STACK
  19442. if (key != NULL) {
  19443. wc_ecc_free(key);
  19444. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19445. }
  19446. #else
  19447. wc_ecc_free(key);
  19448. #endif
  19449. return ret;
  19450. }
  19451. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  19452. #ifdef WOLFSSL_CERT_EXT
  19453. static int ecc_decode_test(void)
  19454. {
  19455. int ret;
  19456. word32 inSz;
  19457. word32 inOutIdx;
  19458. #ifdef WOLFSSL_SMALL_STACK
  19459. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19460. #else
  19461. ecc_key key[1];
  19462. #endif
  19463. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  19464. /* This is ecc_clikeypub_der_256. */
  19465. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  19466. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  19467. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  19468. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  19469. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  19470. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  19471. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  19472. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  19473. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  19474. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  19475. 0xb4 };
  19476. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  19477. 0x00, 0x04, 0x01, 0x01 };
  19478. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  19479. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19480. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  19481. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19482. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  19483. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19484. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  19485. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19486. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  19487. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  19488. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19489. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  19490. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  19491. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19492. 0x03, 0x03, 0x04, 0x01, 0x01 };
  19493. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  19494. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  19495. 0x03, 0x03, 0x00, 0x04, 0x01 };
  19496. #ifdef WOLFSSL_SMALL_STACK
  19497. if (key == NULL)
  19498. ERROR_OUT(MEMORY_E, done);
  19499. #endif
  19500. XMEMSET(key, 0, sizeof *key);
  19501. wc_ecc_init_ex(key, HEAP_HINT, devId);
  19502. inSz = sizeof(good);
  19503. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  19504. if (ret != BAD_FUNC_ARG) {
  19505. ret = -10100;
  19506. goto done;
  19507. }
  19508. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  19509. if (ret != BAD_FUNC_ARG) {
  19510. ret = -10101;
  19511. goto done;
  19512. }
  19513. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  19514. if (ret != BAD_FUNC_ARG) {
  19515. ret = -10102;
  19516. goto done;
  19517. }
  19518. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  19519. if (ret != BAD_FUNC_ARG) {
  19520. ret = -10103;
  19521. goto done;
  19522. }
  19523. /* Change offset to produce bad input data. */
  19524. inOutIdx = 2;
  19525. inSz = sizeof(good) - inOutIdx;
  19526. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19527. if (ret != ASN_PARSE_E) {
  19528. ret = -10104;
  19529. goto done;
  19530. }
  19531. inOutIdx = 4;
  19532. inSz = sizeof(good) - inOutIdx;
  19533. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19534. if (ret != ASN_PARSE_E) {
  19535. ret = -10105;
  19536. goto done;
  19537. }
  19538. /* Bad data. */
  19539. inSz = sizeof(badNoObjId);
  19540. inOutIdx = 0;
  19541. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  19542. if (ret != ASN_OBJECT_ID_E) {
  19543. ret = -10106;
  19544. goto done;
  19545. }
  19546. inSz = sizeof(badOneObjId);
  19547. inOutIdx = 0;
  19548. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  19549. if (ret != ASN_OBJECT_ID_E) {
  19550. ret = -10107;
  19551. goto done;
  19552. }
  19553. inSz = sizeof(badObjId1Len);
  19554. inOutIdx = 0;
  19555. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  19556. if (ret != ASN_PARSE_E) {
  19557. ret = -10108;
  19558. goto done;
  19559. }
  19560. inSz = sizeof(badObj2d1Len);
  19561. inOutIdx = 0;
  19562. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  19563. if (ret != ASN_PARSE_E) {
  19564. ret = -10109;
  19565. goto done;
  19566. }
  19567. inSz = sizeof(badNotBitStr);
  19568. inOutIdx = 0;
  19569. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  19570. if (ret != ASN_BITSTR_E) {
  19571. ret = -10110;
  19572. goto done;
  19573. }
  19574. inSz = sizeof(badBitStrLen);
  19575. inOutIdx = 0;
  19576. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  19577. if (ret != ASN_PARSE_E) {
  19578. ret = -10111;
  19579. goto done;
  19580. }
  19581. inSz = sizeof(badNoBitStrZero);
  19582. inOutIdx = 0;
  19583. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  19584. if (ret != ASN_EXPECT_0_E) {
  19585. ret = -10112;
  19586. goto done;
  19587. }
  19588. inSz = sizeof(badPoint);
  19589. inOutIdx = 0;
  19590. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  19591. if (ret != ASN_ECC_KEY_E) {
  19592. ret = -10113;
  19593. goto done;
  19594. }
  19595. inSz = sizeof(good);
  19596. inOutIdx = 0;
  19597. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  19598. if (ret != 0) {
  19599. ret = -10114;
  19600. goto done;
  19601. }
  19602. done:
  19603. #ifdef WOLFSSL_SMALL_STACK
  19604. if (key != NULL) {
  19605. wc_ecc_free(key);
  19606. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19607. }
  19608. #else
  19609. wc_ecc_free(key);
  19610. #endif
  19611. return ret;
  19612. }
  19613. #endif /* WOLFSSL_CERT_EXT */
  19614. #ifdef WOLFSSL_CUSTOM_CURVES
  19615. static const byte eccKeyExplicitCurve[] = {
  19616. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  19617. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  19618. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  19619. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  19620. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  19621. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19622. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19623. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  19624. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  19625. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  19626. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  19627. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  19628. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  19629. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  19630. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  19631. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  19632. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  19633. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  19634. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19635. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19636. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  19637. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  19638. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  19639. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  19640. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  19641. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  19642. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  19643. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  19644. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  19645. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  19646. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  19647. };
  19648. static int ecc_test_custom_curves(WC_RNG* rng)
  19649. {
  19650. int ret;
  19651. word32 inOutIdx;
  19652. #ifdef WOLFSSL_SMALL_STACK
  19653. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19654. #else
  19655. ecc_key key[1];
  19656. #endif
  19657. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  19658. #ifndef WOLFSSL_ECC_CURVE_STATIC
  19659. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  19660. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  19661. };
  19662. #define ecc_oid_brainpoolp256r1_sz \
  19663. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  19664. #else
  19665. #define ecc_oid_brainpoolp256r1 { \
  19666. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  19667. }
  19668. #define ecc_oid_brainpoolp256r1_sz 9
  19669. #endif
  19670. #define ecc_oid_brainpoolp256r1_sum 104
  19671. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  19672. 32, /* size/bytes */
  19673. ECC_CURVE_CUSTOM, /* ID */
  19674. "BRAINPOOLP256R1", /* curve name */
  19675. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  19676. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  19677. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  19678. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  19679. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  19680. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  19681. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  19682. ecc_oid_brainpoolp256r1_sz,
  19683. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  19684. 1, /* cofactor */
  19685. };
  19686. #ifdef WOLFSSL_SMALL_STACK
  19687. if (! key) {
  19688. ret = MEMORY_E;
  19689. goto done;
  19690. }
  19691. #endif
  19692. XMEMSET(key, 0, sizeof *key);
  19693. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  19694. &ecc_dp_brainpool256r1);
  19695. if (ret != 0) {
  19696. printf("ECC test for custom curve failed! %d\n", ret);
  19697. goto done;
  19698. }
  19699. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  19700. {
  19701. int curve_id;
  19702. #ifdef HAVE_ECC_BRAINPOOL
  19703. curve_id = ECC_BRAINPOOLP256R1;
  19704. #else
  19705. curve_id = ECC_SECP256K1;
  19706. #endif
  19707. /* Test and demonstrate use of non-SECP curve */
  19708. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  19709. if (ret < 0) {
  19710. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  19711. goto done;
  19712. }
  19713. }
  19714. #endif
  19715. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  19716. if (ret != 0) {
  19717. ret = -10120;
  19718. goto done;
  19719. }
  19720. inOutIdx = 0;
  19721. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  19722. sizeof(eccKeyExplicitCurve));
  19723. if (ret != 0)
  19724. ret = -10121;
  19725. done:
  19726. #ifdef WOLFSSL_SMALL_STACK
  19727. if (key) {
  19728. wc_ecc_free(key);
  19729. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19730. }
  19731. #else
  19732. wc_ecc_free(key);
  19733. #endif
  19734. return ret;
  19735. }
  19736. #endif /* WOLFSSL_CUSTOM_CURVES */
  19737. #ifdef WOLFSSL_CERT_GEN
  19738. /* Make Cert / Sign example for ECC cert and ECC CA */
  19739. static int ecc_test_cert_gen(WC_RNG* rng)
  19740. {
  19741. int ret;
  19742. #ifdef WOLFSSL_SMALL_STACK
  19743. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19744. #ifdef WOLFSSL_TEST_CERT
  19745. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19746. #endif
  19747. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19748. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19749. #else
  19750. Cert myCert[1];
  19751. #ifdef WOLFSSL_TEST_CERT
  19752. DecodedCert decode[1];
  19753. #endif
  19754. ecc_key caEccKey[1];
  19755. ecc_key certPubKey[1];
  19756. #endif
  19757. int certSz;
  19758. size_t bytes;
  19759. word32 idx = 0;
  19760. #ifndef USE_CERT_BUFFERS_256
  19761. XFILE file;
  19762. #endif
  19763. #ifdef WOLFSSL_SMALL_STACK
  19764. byte* der = NULL;
  19765. #else
  19766. byte der[FOURK_BUF];
  19767. #endif
  19768. #ifdef WOLFSSL_SMALL_STACK
  19769. if ((myCert == NULL)
  19770. #ifdef WOLFSSL_TEST_CERT
  19771. || (decode == NULL)
  19772. #endif
  19773. || (caEccKey == NULL) || (certPubKey == NULL))
  19774. ERROR_OUT(MEMORY_E, exit);
  19775. #endif
  19776. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  19777. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  19778. #ifdef WOLFSSL_SMALL_STACK
  19779. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19780. if (der == NULL) {
  19781. ERROR_OUT(-10130, exit);
  19782. }
  19783. #endif
  19784. /* Get cert private key */
  19785. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19786. /* Get Cert Key 384 */
  19787. #ifdef USE_CERT_BUFFERS_256
  19788. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  19789. bytes = sizeof_ca_ecc_key_der_384;
  19790. #else
  19791. file = XFOPEN(eccCaKey384File, "rb");
  19792. if (!file) {
  19793. ERROR_OUT(-10131, exit);
  19794. }
  19795. bytes = XFREAD(der, 1, FOURK_BUF, file);
  19796. XFCLOSE(file);
  19797. (void)eccCaKeyFile;
  19798. #endif /* USE_CERT_BUFFERS_256 */
  19799. #else
  19800. #ifdef USE_CERT_BUFFERS_256
  19801. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  19802. bytes = sizeof_ca_ecc_key_der_256;
  19803. #else
  19804. file = XFOPEN(eccCaKeyFile, "rb");
  19805. if (!file) {
  19806. ERROR_OUT(-10132, exit);
  19807. }
  19808. bytes = XFREAD(der, 1, FOURK_BUF, file);
  19809. XFCLOSE(file);
  19810. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19811. (void)eccCaKey384File;
  19812. #endif
  19813. #endif /* USE_CERT_BUFFERS_256 */
  19814. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  19815. /* Get CA Key */
  19816. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  19817. if (ret != 0) {
  19818. ERROR_OUT(-10133, exit);
  19819. }
  19820. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  19821. if (ret != 0) {
  19822. ERROR_OUT(-10134, exit);
  19823. }
  19824. /* Make a public key */
  19825. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  19826. if (ret != 0) {
  19827. ERROR_OUT(-10135, exit);
  19828. }
  19829. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  19830. #if defined(WOLFSSL_ASYNC_CRYPT)
  19831. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  19832. #endif
  19833. if (ret != 0) {
  19834. ERROR_OUT(-10136, exit);
  19835. }
  19836. TEST_SLEEP();
  19837. /* Setup Certificate */
  19838. if (wc_InitCert(myCert)) {
  19839. ERROR_OUT(-10137, exit);
  19840. }
  19841. #ifndef NO_SHA256
  19842. myCert->sigType = CTC_SHA256wECDSA;
  19843. #else
  19844. myCert->sigType = CTC_SHAwECDSA;
  19845. #endif
  19846. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  19847. #ifdef WOLFSSL_CERT_EXT
  19848. /* add Policies */
  19849. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  19850. CTC_MAX_CERTPOL_SZ);
  19851. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  19852. CTC_MAX_CERTPOL_SZ);
  19853. myCert->certPoliciesNb = 2;
  19854. /* add SKID from the Public Key */
  19855. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  19856. ERROR_OUT(-10138, exit);
  19857. }
  19858. /* add AKID from the Public Key */
  19859. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  19860. ERROR_OUT(-10139, exit);
  19861. }
  19862. /* add Key Usage */
  19863. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  19864. ERROR_OUT(-10140, exit);
  19865. }
  19866. #endif /* WOLFSSL_CERT_EXT */
  19867. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19868. #if defined(USE_CERT_BUFFERS_256)
  19869. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  19870. sizeof_ca_ecc_cert_der_384);
  19871. #else
  19872. ret = wc_SetIssuer(myCert, eccCaCert384File);
  19873. (void)eccCaCertFile;
  19874. #endif
  19875. #else
  19876. #if defined(USE_CERT_BUFFERS_256)
  19877. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  19878. sizeof_ca_ecc_cert_der_256);
  19879. #else
  19880. ret = wc_SetIssuer(myCert, eccCaCertFile);
  19881. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  19882. (void)eccCaCert384File;
  19883. #endif
  19884. #endif
  19885. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  19886. if (ret < 0) {
  19887. ERROR_OUT(-10141, exit);
  19888. }
  19889. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  19890. if (certSz < 0) {
  19891. ERROR_OUT(-10142, exit);
  19892. }
  19893. ret = 0;
  19894. do {
  19895. #if defined(WOLFSSL_ASYNC_CRYPT)
  19896. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19897. #endif
  19898. if (ret >= 0) {
  19899. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  19900. FOURK_BUF, NULL, caEccKey, rng);
  19901. }
  19902. } while (ret == WC_PENDING_E);
  19903. if (ret < 0) {
  19904. ERROR_OUT(-10143, exit);
  19905. }
  19906. certSz = ret;
  19907. TEST_SLEEP();
  19908. #ifdef WOLFSSL_TEST_CERT
  19909. InitDecodedCert(decode, der, certSz, 0);
  19910. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  19911. if (ret != 0) {
  19912. FreeDecodedCert(decode);
  19913. ERROR_OUT(-10144, exit);
  19914. }
  19915. FreeDecodedCert(decode);
  19916. #endif
  19917. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  19918. CERT_TYPE, -6735);
  19919. if (ret != 0) {
  19920. goto exit;
  19921. }
  19922. exit:
  19923. #ifdef WOLFSSL_SMALL_STACK
  19924. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19925. #endif
  19926. #ifdef WOLFSSL_SMALL_STACK
  19927. if (myCert != NULL)
  19928. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19929. #ifdef WOLFSSL_TEST_CERT
  19930. if (decode != NULL)
  19931. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19932. #endif
  19933. if (caEccKey != NULL) {
  19934. wc_ecc_free(caEccKey);
  19935. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19936. }
  19937. if (certPubKey != NULL) {
  19938. wc_ecc_free(certPubKey);
  19939. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19940. }
  19941. #else
  19942. wc_ecc_free(certPubKey);
  19943. wc_ecc_free(caEccKey);
  19944. #endif
  19945. return ret;
  19946. }
  19947. #endif /* WOLFSSL_CERT_GEN */
  19948. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  19949. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  19950. static int ecc_test_allocator(WC_RNG* rng)
  19951. {
  19952. int ret = 0;
  19953. ecc_key* key;
  19954. #ifdef WC_NO_RNG
  19955. word32 idx = 0;
  19956. #endif
  19957. key = wc_ecc_key_new(HEAP_HINT);
  19958. if (key == NULL) {
  19959. ERROR_OUT(-10150, exit);
  19960. }
  19961. #ifndef WC_NO_RNG
  19962. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  19963. if (ret != 0) {
  19964. ERROR_OUT(-10151, exit);
  19965. }
  19966. #else
  19967. /* use test ECC key */
  19968. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  19969. (word32)sizeof_ecc_key_der_256);
  19970. (void)rng;
  19971. #endif
  19972. exit:
  19973. wc_ecc_key_free(key);
  19974. return ret;
  19975. }
  19976. #endif
  19977. /* ECC Non-blocking tests for Sign and Verify */
  19978. /* Requires SP math and supports P384 or P256 */
  19979. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  19980. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  19981. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  19982. /* Test Data - Random */
  19983. static const uint8_t kMsg[] = {
  19984. 0x69, 0xbc, 0x9f, 0xce, 0x68, 0x17, 0xc2, 0x10, 0xea, 0xfc, 0x10, 0x65, 0x67, 0x52, 0xed, 0x78,
  19985. 0x6e, 0xb8, 0x83, 0x9c, 0x9a, 0xb4, 0x56, 0x0d, 0xc1, 0x0d, 0x1f, 0x78, 0x6e, 0x75, 0xd7, 0xbe,
  19986. 0x92, 0x6b, 0x12, 0xf6, 0x76, 0x60, 0x8e, 0xb1, 0xf4, 0x19, 0x0c, 0x81, 0xe7, 0x54, 0x5e, 0xbc,
  19987. 0xe0, 0xae, 0xc2, 0x7d, 0x1b, 0xc4, 0x6e, 0xec, 0xb1, 0x99, 0x6c, 0xbf, 0x0e, 0x38, 0xa8, 0x01,
  19988. 0xa6, 0x9a, 0x48, 0x12, 0xe4, 0xc9, 0x3b, 0xf0, 0x63, 0x46, 0x15, 0xb4, 0x61, 0xa8, 0x1a, 0x60,
  19989. 0x71, 0x87, 0x98, 0xd7, 0x6f, 0x98, 0x7b, 0x2d, 0xb9, 0x19, 0x1b, 0x21, 0x9c, 0x70, 0x58, 0xe8,
  19990. 0x0d, 0x0f, 0xe9, 0x2d, 0x9a, 0x9a, 0xf1, 0x55, 0xa0, 0x4c, 0xd3, 0x07, 0xbd, 0x97, 0x48, 0xec,
  19991. 0x88, 0x0a, 0xaf, 0xb3, 0x80, 0x78, 0xa4, 0x59, 0x43, 0x57, 0xd3, 0xa7, 0x01, 0x66, 0x0e, 0xfc
  19992. };
  19993. /* ECC Private Key "d" */
  19994. static const uint8_t kPrivKey[] = {
  19995. #ifdef HAVE_ECC384
  19996. /* SECP384R1 */
  19997. /* d */
  19998. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  19999. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  20000. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  20001. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  20002. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  20003. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  20004. #else
  20005. /* SECP256R1 */
  20006. /* d */
  20007. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  20008. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  20009. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  20010. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  20011. #endif
  20012. };
  20013. /* ECC public key Qx/Qy */
  20014. static const uint8_t kPubKey[] = {
  20015. #ifdef HAVE_ECC384
  20016. /* SECP384R1 */
  20017. /* Qx */
  20018. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  20019. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  20020. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  20021. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  20022. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  20023. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  20024. /* Qy */
  20025. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  20026. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  20027. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  20028. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  20029. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  20030. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  20031. #else
  20032. /* SECP256R1 */
  20033. /* Qx */
  20034. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  20035. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  20036. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  20037. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  20038. /* Qy */
  20039. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  20040. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  20041. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  20042. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  20043. #endif
  20044. };
  20045. /* ECC Curve */
  20046. #ifdef HAVE_ECC384
  20047. /* SECP384R1 */
  20048. #define ECC_CURVE_SZ 48
  20049. #define ECC_CURVE_ID ECC_SECP384R1
  20050. #else
  20051. /* SECP256R1 */
  20052. #define ECC_CURVE_SZ 32
  20053. #define ECC_CURVE_ID ECC_SECP256R1
  20054. #endif
  20055. /* Hash Algorithm */
  20056. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  20057. #define HASH_DIGEST_SZ WC_SHA3_384_DIGEST_SIZE
  20058. #define HASH_SHA_VER 3
  20059. #define CRYPTO_HASH_FN crypto_sha3_384
  20060. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  20061. #define HASH_DIGEST_SZ WC_SHA384_DIGEST_SIZE
  20062. #define HASH_SHA_VER 2
  20063. #define CRYPTO_HASH_FN crypto_sha2_384
  20064. #elif !defined(NO_SHA256)
  20065. #define HASH_DIGEST_SZ WC_SHA256_DIGEST_SIZE
  20066. #define HASH_SHA_VER 2
  20067. #define CRYPTO_HASH_FN crypto_sha2_256
  20068. #else
  20069. #error test configuration not supported
  20070. #endif
  20071. #if defined(HAVE_ECC384) && defined(WOLFSSL_SHA3)
  20072. /* helper to perform hashing block by block */
  20073. static int crypto_sha3_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  20074. uint32_t hashSz, uint32_t blkSz)
  20075. {
  20076. int ret;
  20077. uint32_t i = 0, chunk;
  20078. wc_Sha3 sha3;
  20079. /* validate arguments */
  20080. if ((buf == NULL && len > 0) || hash == NULL ||
  20081. hashSz < WC_SHA3_384_DIGEST_SIZE || blkSz == 0)
  20082. {
  20083. return BAD_FUNC_ARG;
  20084. }
  20085. /* Init Sha3_384 structure */
  20086. ret = wc_InitSha3_384(&sha3, NULL, INVALID_DEVID);
  20087. if (ret != 0) {
  20088. return ret;
  20089. }
  20090. while (i < len) {
  20091. chunk = blkSz;
  20092. if ((chunk + i) > len)
  20093. chunk = len - i;
  20094. /* Perform chunked update */
  20095. ret = wc_Sha3_384_Update(&sha3, (buf + i), chunk);
  20096. if (ret != 0) {
  20097. break;
  20098. }
  20099. i += chunk;
  20100. }
  20101. if (ret == 0) {
  20102. /* Get final digest result */
  20103. ret = wc_Sha3_384_Final(&sha3, hash);
  20104. }
  20105. return ret;
  20106. }
  20107. #elif defined(HAVE_ECC384) && defined(WOLFSSL_SHA384)
  20108. /* helper to perform hashing block by block */
  20109. static int crypto_sha2_384(const uint8_t *buf, uint32_t len, uint8_t *hash,
  20110. uint32_t hashSz, uint32_t blkSz)
  20111. {
  20112. int ret;
  20113. uint32_t i = 0, chunk;
  20114. wc_Sha384 sha384;
  20115. /* validate arguments */
  20116. if ((buf == NULL && len > 0) || hash == NULL ||
  20117. hashSz < WC_SHA384_DIGEST_SIZE || blkSz == 0)
  20118. {
  20119. return BAD_FUNC_ARG;
  20120. }
  20121. /* Init Sha384 structure */
  20122. ret = wc_InitSha384(&sha384);
  20123. if (ret != 0) {
  20124. return ret;
  20125. }
  20126. while (i < len) {
  20127. chunk = blkSz;
  20128. if ((chunk + i) > len)
  20129. chunk = len - i;
  20130. /* Perform chunked update */
  20131. ret = wc_Sha384Update(&sha384, (buf + i), chunk);
  20132. if (ret != 0) {
  20133. break;
  20134. }
  20135. i += chunk;
  20136. }
  20137. if (ret == 0) {
  20138. /* Get final digest result */
  20139. ret = wc_Sha384Final(&sha384, hash);
  20140. }
  20141. return ret;
  20142. }
  20143. #elif !defined(NO_SHA256)
  20144. /* helper to perform hashing block by block */
  20145. static int crypto_sha2_256(const uint8_t *buf, uint32_t len, uint8_t *hash,
  20146. uint32_t hashSz, uint32_t blkSz)
  20147. {
  20148. int ret;
  20149. uint32_t i = 0, chunk;
  20150. wc_Sha256 sha256;
  20151. /* validate arguments */
  20152. if ((buf == NULL && len > 0) || hash == NULL ||
  20153. hashSz < WC_SHA256_DIGEST_SIZE || blkSz == 0)
  20154. {
  20155. return BAD_FUNC_ARG;
  20156. }
  20157. /* Init Sha256 structure */
  20158. ret = wc_InitSha256(&sha256);
  20159. if (ret != 0) {
  20160. return ret;
  20161. }
  20162. while (i < len) {
  20163. chunk = blkSz;
  20164. if ((chunk + i) > len)
  20165. chunk = len - i;
  20166. /* Perform chunked update */
  20167. ret = wc_Sha256Update(&sha256, (buf + i), chunk);
  20168. if (ret != 0) {
  20169. break;
  20170. }
  20171. i += chunk;
  20172. }
  20173. if (ret == 0) {
  20174. /* Get final digest result */
  20175. ret = wc_Sha256Final(&sha256, hash);
  20176. }
  20177. return ret;
  20178. }
  20179. #endif
  20180. /* perform verify of signature and hash using public key */
  20181. /* key is public Qx + public Qy */
  20182. /* sig is r + s */
  20183. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  20184. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  20185. uint32_t curveSz, int curveId)
  20186. {
  20187. int ret, verify_res = 0, count = 0;
  20188. mp_int r, s;
  20189. ecc_key ecc;
  20190. ecc_nb_ctx_t nb_ctx;
  20191. /* validate arguments */
  20192. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  20193. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  20194. {
  20195. return BAD_FUNC_ARG;
  20196. }
  20197. /* Setup the ECC key */
  20198. ret = wc_ecc_init(&ecc);
  20199. if (ret < 0) {
  20200. return ret;
  20201. }
  20202. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20203. if (ret != MP_OKAY) {
  20204. wc_ecc_free(&ecc);
  20205. return ret;
  20206. }
  20207. /* Setup the signature r/s variables */
  20208. ret = mp_init(&r);
  20209. if (ret != MP_OKAY) {
  20210. wc_ecc_free(&ecc);
  20211. return ret;
  20212. }
  20213. ret = mp_init(&s);
  20214. if (ret != MP_OKAY) {
  20215. mp_clear(&r);
  20216. wc_ecc_free(&ecc);
  20217. return ret;
  20218. }
  20219. /* Import public key x/y */
  20220. ret = wc_ecc_import_unsigned(
  20221. &ecc,
  20222. (byte*)key, /* Public "x" Coordinate */
  20223. (byte*)(key + curveSz), /* Public "y" Coordinate */
  20224. NULL, /* Private "d" (optional) */
  20225. curveId /* ECC Curve Id */
  20226. );
  20227. /* Make sure it was a public key imported */
  20228. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  20229. ret = ECC_BAD_ARG_E;
  20230. }
  20231. /* Import signature r/s */
  20232. if (ret == 0) {
  20233. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  20234. }
  20235. if (ret == 0) {
  20236. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  20237. }
  20238. /* Verify ECC Signature */
  20239. if (ret == 0) {
  20240. do {
  20241. ret = wc_ecc_verify_hash_ex(
  20242. &r, &s, /* r/s as mp_int */
  20243. hash, hashSz, /* computed hash digest */
  20244. &verify_res, /* verification result 1=success */
  20245. &ecc
  20246. );
  20247. count++;
  20248. /* TODO: Real-time work can be called here */
  20249. } while (ret == FP_WOULDBLOCK);
  20250. #ifdef DEBUG_WOLFSSL
  20251. printf("ECC non-block verify: %d times\n", count);
  20252. #endif
  20253. }
  20254. /* check verify result */
  20255. if (ret == 0 && verify_res == 0) {
  20256. ret = SIG_VERIFY_E;
  20257. }
  20258. mp_clear(&r);
  20259. mp_clear(&s);
  20260. wc_ecc_free(&ecc);
  20261. (void)count;
  20262. return ret;
  20263. }
  20264. /* perform signature operation against hash using private key */
  20265. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  20266. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  20267. uint32_t curveSz, int curveId, WC_RNG* rng)
  20268. {
  20269. int ret, count = 0;
  20270. mp_int r, s;
  20271. ecc_key ecc;
  20272. ecc_nb_ctx_t nb_ctx;
  20273. /* validate arguments */
  20274. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  20275. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  20276. {
  20277. return BAD_FUNC_ARG;
  20278. }
  20279. /* Initialize signature result */
  20280. memset(sig, 0, curveSz*2);
  20281. /* Setup the ECC key */
  20282. ret = wc_ecc_init(&ecc);
  20283. if (ret < 0) {
  20284. return ret;
  20285. }
  20286. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  20287. if (ret != MP_OKAY) {
  20288. wc_ecc_free(&ecc);
  20289. return ret;
  20290. }
  20291. /* Setup the signature r/s variables */
  20292. ret = mp_init(&r);
  20293. if (ret != MP_OKAY) {
  20294. wc_ecc_free(&ecc);
  20295. return ret;
  20296. }
  20297. ret = mp_init(&s);
  20298. if (ret != MP_OKAY) {
  20299. mp_clear(&r);
  20300. wc_ecc_free(&ecc);
  20301. return ret;
  20302. }
  20303. /* Import private key "k" */
  20304. ret = wc_ecc_import_private_key_ex(
  20305. key, keySz, /* private key "d" */
  20306. NULL, 0, /* public (optional) */
  20307. &ecc,
  20308. curveId /* ECC Curve Id */
  20309. );
  20310. if (ret == 0) {
  20311. do {
  20312. /* Verify ECC Signature */
  20313. ret = wc_ecc_sign_hash_ex(
  20314. hash, hashSz, /* computed hash digest */
  20315. rng, &ecc, /* random and key context */
  20316. &r, &s /* r/s as mp_int */
  20317. );
  20318. count++;
  20319. /* TODO: Real-time work can be called here */
  20320. } while (ret == FP_WOULDBLOCK);
  20321. #ifdef DEBUG_WOLFSSL
  20322. printf("ECC non-block sign: %d times\n", count);
  20323. #endif
  20324. }
  20325. if (ret == 0) {
  20326. /* export r/s */
  20327. mp_to_unsigned_bin(&r, sig);
  20328. mp_to_unsigned_bin(&s, sig + curveSz);
  20329. }
  20330. mp_clear(&r);
  20331. mp_clear(&s);
  20332. wc_ecc_free(&ecc);
  20333. (void)count;
  20334. return ret;
  20335. }
  20336. static int ecc_test_nonblock(WC_RNG* rng)
  20337. {
  20338. int ret;
  20339. uint8_t hash[HASH_DIGEST_SZ];
  20340. uint8_t sig[ECC_CURVE_SZ*2];
  20341. uint32_t sigSz = sizeof(sig);
  20342. ret = CRYPTO_HASH_FN(
  20343. kMsg, sizeof(kMsg), /* input message */
  20344. hash, sizeof(hash), /* hash digest result */
  20345. 32 /* configurable block / chunk size */
  20346. );
  20347. if (ret == 0) {
  20348. /* Sign hash using private key */
  20349. /* Note: result of an ECC sign varies for each call even with same
  20350. private key and hash. This is because a new random public key is
  20351. used for each operation. */
  20352. ret = crypto_ecc_sign(
  20353. kPrivKey, sizeof(kPrivKey), /* private key */
  20354. hash, sizeof(hash), /* computed hash digest */
  20355. sig, &sigSz, /* signature r/s */
  20356. ECC_CURVE_SZ, /* curve size in bytes */
  20357. ECC_CURVE_ID, /* curve id */
  20358. rng
  20359. );
  20360. }
  20361. if (ret == 0) {
  20362. /* Verify generated signature is valid */
  20363. ret = crypto_ecc_verify(
  20364. kPubKey, sizeof(kPubKey), /* public key point x/y */
  20365. hash, sizeof(hash), /* computed hash digest */
  20366. sig, sigSz, /* signature r/s */
  20367. ECC_CURVE_SZ, /* curve size in bytes */
  20368. ECC_CURVE_ID /* curve id */
  20369. );
  20370. }
  20371. return ret;
  20372. }
  20373. #endif /* WC_ECC_NONBLOCK && WOLFSSL_PUBLIC_MP && HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  20374. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  20375. {
  20376. int ret;
  20377. WC_RNG rng;
  20378. #ifdef WOLFSSL_CERT_EXT
  20379. ret = ecc_decode_test();
  20380. if (ret < 0)
  20381. return ret;
  20382. #endif
  20383. #ifndef HAVE_FIPS
  20384. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20385. #else
  20386. ret = wc_InitRng(&rng);
  20387. #endif
  20388. #ifndef WC_NO_RNG
  20389. if (ret != 0)
  20390. return -10300;
  20391. #endif
  20392. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  20393. ret = ecc_test_curve(&rng, 14);
  20394. if (ret < 0) {
  20395. goto done;
  20396. }
  20397. #endif /* HAVE_ECC112 */
  20398. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  20399. ret = ecc_test_curve(&rng, 16);
  20400. if (ret < 0) {
  20401. goto done;
  20402. }
  20403. #endif /* HAVE_ECC128 */
  20404. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  20405. ret = ecc_test_curve(&rng, 20);
  20406. if (ret < 0) {
  20407. goto done;
  20408. }
  20409. #endif /* HAVE_ECC160 */
  20410. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  20411. ret = ecc_test_curve(&rng, 24);
  20412. if (ret < 0) {
  20413. goto done;
  20414. }
  20415. #endif /* HAVE_ECC192 */
  20416. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  20417. ret = ecc_test_curve(&rng, 28);
  20418. if (ret < 0) {
  20419. goto done;
  20420. }
  20421. #endif /* HAVE_ECC224 */
  20422. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  20423. ret = ecc_test_curve(&rng, 30);
  20424. if (ret < 0) {
  20425. goto done;
  20426. }
  20427. #endif /* HAVE_ECC239 */
  20428. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  20429. ret = ecc_test_curve(&rng, 32);
  20430. if (ret < 0) {
  20431. goto done;
  20432. }
  20433. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20434. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  20435. !defined(WOLFSSL_NO_MALLOC)
  20436. ret = ecc_point_test();
  20437. if (ret < 0) {
  20438. goto done;
  20439. }
  20440. #endif
  20441. ret = ecc_def_curve_test(&rng);
  20442. if (ret < 0) {
  20443. goto done;
  20444. }
  20445. #endif /* !NO_ECC256 */
  20446. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  20447. ret = ecc_test_curve(&rng, 40);
  20448. if (ret < 0) {
  20449. goto done;
  20450. }
  20451. #endif /* HAVE_ECC320 */
  20452. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  20453. ret = ecc_test_curve(&rng, 48);
  20454. if (ret < 0) {
  20455. goto done;
  20456. }
  20457. #endif /* HAVE_ECC384 */
  20458. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  20459. ret = ecc_test_curve(&rng, 64);
  20460. if (ret < 0) {
  20461. goto done;
  20462. }
  20463. #endif /* HAVE_ECC512 */
  20464. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  20465. ret = ecc_test_curve(&rng, 66);
  20466. if (ret < 0) {
  20467. goto done;
  20468. }
  20469. #endif /* HAVE_ECC521 */
  20470. #if defined(WOLFSSL_CUSTOM_CURVES)
  20471. ret = ecc_test_custom_curves(&rng);
  20472. if (ret != 0) {
  20473. goto done;
  20474. }
  20475. #endif
  20476. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K)
  20477. ret = ecc_test_sign_vectors(&rng);
  20478. if (ret != 0) {
  20479. printf("ecc_test_sign_vectors failed! %d\n", ret);
  20480. goto done;
  20481. }
  20482. #endif
  20483. #ifdef HAVE_ECC_CDH
  20484. ret = ecc_test_cdh_vectors(&rng);
  20485. if (ret != 0) {
  20486. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  20487. goto done;
  20488. }
  20489. #endif
  20490. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  20491. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL)
  20492. ret = ecc_test_make_pub(&rng);
  20493. if (ret != 0) {
  20494. printf("ecc_test_make_pub failed!: %d\n", ret);
  20495. goto done;
  20496. }
  20497. #elif defined(HAVE_ECC_KEY_IMPORT)
  20498. (void) ecc_test_make_pub;/* for compiler warning */
  20499. #endif
  20500. #ifdef WOLFSSL_CERT_GEN
  20501. ret = ecc_test_cert_gen(&rng);
  20502. if (ret != 0) {
  20503. printf("ecc_test_cert_gen failed!: %d\n", ret);
  20504. goto done;
  20505. }
  20506. #endif
  20507. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  20508. ret = ecc_test_allocator(&rng);
  20509. if (ret != 0) {
  20510. printf("ecc_test_allocator failed!: %d\n", ret);
  20511. goto done;
  20512. }
  20513. #endif
  20514. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  20515. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  20516. ret = ecc_test_nonblock(&rng);
  20517. if (ret != 0) {
  20518. printf("ecc_test_nonblock failed!: %d\n", ret);
  20519. goto done;
  20520. }
  20521. #endif
  20522. done:
  20523. wc_FreeRng(&rng);
  20524. return ret;
  20525. }
  20526. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  20527. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  20528. {
  20529. WC_RNG rng;
  20530. int ret = 0;
  20531. #ifdef WOLFSSL_SMALL_STACK
  20532. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  20533. *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20534. #else
  20535. ecc_key userA[1], userB[1];
  20536. #endif
  20537. byte msg[48];
  20538. byte plain[48];
  20539. byte out[80];
  20540. word32 outSz = sizeof(out);
  20541. word32 plainSz = sizeof(plain);
  20542. int i;
  20543. ecEncCtx* cliCtx = NULL;
  20544. ecEncCtx* srvCtx = NULL;
  20545. byte cliSalt[EXCHANGE_SALT_SZ];
  20546. byte srvSalt[EXCHANGE_SALT_SZ];
  20547. const byte* tmpSalt;
  20548. byte msg2[48];
  20549. byte plain2[48];
  20550. byte out2[80];
  20551. word32 outSz2 = sizeof(out2);
  20552. word32 plainSz2 = sizeof(plain2);
  20553. #ifndef HAVE_FIPS
  20554. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20555. #else
  20556. ret = wc_InitRng(&rng);
  20557. #endif
  20558. if (ret != 0)
  20559. return -10400;
  20560. #ifdef WOLFSSL_SMALL_STACK
  20561. if ((userA == NULL) ||
  20562. (userB == NULL))
  20563. ERROR_OUT(MEMORY_E, done);
  20564. #endif
  20565. XMEMSET(userA, 0, sizeof *userA);
  20566. XMEMSET(userB, 0, sizeof *userB);
  20567. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20568. if (ret != 0)
  20569. goto done;
  20570. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  20571. if (ret != 0)
  20572. goto done;
  20573. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  20574. #if defined(WOLFSSL_ASYNC_CRYPT)
  20575. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  20576. #endif
  20577. if (ret != 0){
  20578. ret = -10401; goto done;
  20579. }
  20580. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  20581. #if defined(WOLFSSL_ASYNC_CRYPT)
  20582. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  20583. #endif
  20584. if (ret != 0){
  20585. ret = -10402; goto done;
  20586. }
  20587. /* set message to incrementing 0,1,2,etc... */
  20588. for (i = 0; i < (int)sizeof(msg); i++)
  20589. msg[i] = i;
  20590. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20591. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20592. !defined(HAVE_SELFTEST)
  20593. ret = wc_ecc_set_rng(userA, &rng);
  20594. if (ret != 0) {
  20595. ret = -10403; goto done;
  20596. }
  20597. ret = wc_ecc_set_rng(userB, &rng);
  20598. if (ret != 0) {
  20599. ret = -10404; goto done;
  20600. }
  20601. #endif
  20602. /* encrypt msg to B */
  20603. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  20604. if (ret != 0) {
  20605. ret = -10405; goto done;
  20606. }
  20607. /* decrypt msg from A */
  20608. ret = wc_ecc_decrypt(userB, userA, out, outSz, plain, &plainSz, NULL);
  20609. if (ret != 0) {
  20610. ret = -10406; goto done;
  20611. }
  20612. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  20613. ret = -10407; goto done;
  20614. }
  20615. /* let's verify message exchange works, A is client, B is server */
  20616. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  20617. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  20618. if (cliCtx == NULL || srvCtx == NULL) {
  20619. ret = -10408; goto done;
  20620. }
  20621. /* get salt to send to peer */
  20622. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  20623. if (tmpSalt == NULL) {
  20624. ret = -10409; goto done;
  20625. }
  20626. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  20627. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  20628. if (tmpSalt == NULL) {
  20629. ret = -10410; goto done;
  20630. }
  20631. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  20632. /* in actual use, we'd get the peer's salt over the transport */
  20633. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  20634. if (ret != 0)
  20635. goto done;
  20636. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  20637. if (ret != 0)
  20638. goto done;
  20639. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  20640. if (ret != 0)
  20641. goto done;
  20642. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  20643. if (ret != 0)
  20644. goto done;
  20645. /* get encrypted msg (request) to send to B */
  20646. outSz = sizeof(out);
  20647. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  20648. if (ret != 0)
  20649. goto done;
  20650. /* B decrypts msg (request) from A */
  20651. plainSz = sizeof(plain);
  20652. ret = wc_ecc_decrypt(userB, userA, out, outSz, plain, &plainSz, srvCtx);
  20653. if (ret != 0)
  20654. goto done;
  20655. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  20656. ret = -10411; goto done;
  20657. }
  20658. /* msg2 (response) from B to A */
  20659. for (i = 0; i < (int)sizeof(msg2); i++)
  20660. msg2[i] = i + sizeof(msg2);
  20661. /* get encrypted msg (response) to send to B */
  20662. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  20663. &outSz2, srvCtx);
  20664. if (ret != 0)
  20665. goto done;
  20666. /* A decrypts msg (response) from B */
  20667. ret = wc_ecc_decrypt(userA, userB, out2, outSz2, plain2, &plainSz2,
  20668. cliCtx);
  20669. if (ret != 0)
  20670. goto done;
  20671. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  20672. ret = -10412; goto done;
  20673. }
  20674. done:
  20675. /* cleanup */
  20676. wc_ecc_ctx_free(srvCtx);
  20677. wc_ecc_ctx_free(cliCtx);
  20678. #ifdef WOLFSSL_SMALL_STACK
  20679. if (userA != NULL) {
  20680. wc_ecc_free(userA);
  20681. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20682. }
  20683. if (userB != NULL) {
  20684. wc_ecc_free(userB);
  20685. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20686. }
  20687. #else
  20688. wc_ecc_free(userB);
  20689. wc_ecc_free(userA);
  20690. #endif
  20691. wc_FreeRng(&rng);
  20692. return ret;
  20693. }
  20694. #endif /* HAVE_ECC_ENCRYPT */
  20695. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  20696. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  20697. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN)
  20698. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  20699. {
  20700. size_t bytes;
  20701. #ifdef WOLFSSL_SMALL_STACK
  20702. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20703. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20704. #else
  20705. ecc_key cliKey[1];
  20706. ecc_key servKey[1];
  20707. #endif
  20708. WC_RNG rng;
  20709. word32 idx = 0;
  20710. int ret;
  20711. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  20712. byte in[] = "Everyone gets Friday off. ecc p";
  20713. word32 inLen = (word32)XSTRLEN((char*)in);
  20714. byte out[256];
  20715. byte plain[256];
  20716. int verify = 0;
  20717. word32 x;
  20718. #ifdef WOLFSSL_SMALL_STACK
  20719. if ((cliKey == NULL) || (servKey == NULL))
  20720. ERROR_OUT(MEMORY_E, done);
  20721. #endif
  20722. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  20723. if (ret != 0)
  20724. ERROR_OUT(-10420, done);
  20725. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  20726. if (ret != 0)
  20727. ERROR_OUT(-10421, done);
  20728. bytes = (size_t)sizeof_ecc_clikey_der_256;
  20729. /* place client key into ecc_key struct cliKey */
  20730. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  20731. (word32)bytes);
  20732. if (ret != 0)
  20733. ERROR_OUT(-10422, done);
  20734. idx = 0;
  20735. bytes = (size_t)sizeof_ecc_key_der_256;
  20736. /* place server key into ecc_key struct servKey */
  20737. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  20738. (word32)bytes);
  20739. if (ret != 0)
  20740. ERROR_OUT(-10423, done);
  20741. #ifndef WC_NO_RNG
  20742. #ifndef HAVE_FIPS
  20743. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  20744. #else
  20745. ret = wc_InitRng(&rng);
  20746. #endif
  20747. if (ret != 0)
  20748. ERROR_OUT(-10424, done);
  20749. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20750. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20751. !defined(HAVE_SELFTEST)
  20752. ret = wc_ecc_set_rng(cliKey, &rng);
  20753. if (ret != 0) {
  20754. ERROR_OUT(-10425, done);
  20755. }
  20756. #endif
  20757. #endif /* !WC_NO_RNG */
  20758. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF)
  20759. {
  20760. word32 y;
  20761. /* test encrypt and decrypt if they're available */
  20762. x = sizeof(out);
  20763. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  20764. if (ret < 0)
  20765. ERROR_OUT(-10426, done);
  20766. y = sizeof(plain);
  20767. ret = wc_ecc_decrypt(cliKey, servKey, out, x, plain, &y, NULL);
  20768. if (ret < 0)
  20769. ERROR_OUT(-10427, done);
  20770. if (XMEMCMP(plain, in, inLen))
  20771. ERROR_OUT(-10428, done);
  20772. }
  20773. #endif
  20774. x = sizeof(out);
  20775. do {
  20776. #if defined(WOLFSSL_ASYNC_CRYPT)
  20777. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20778. #endif
  20779. if (ret == 0)
  20780. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  20781. } while (ret == WC_PENDING_E);
  20782. if (ret < 0)
  20783. ERROR_OUT(-10429, done);
  20784. TEST_SLEEP();
  20785. XMEMSET(plain, 0, sizeof(plain));
  20786. do {
  20787. #if defined(WOLFSSL_ASYNC_CRYPT)
  20788. ret = wc_AsyncWait(ret, cliKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20789. #endif
  20790. if (ret == 0)
  20791. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  20792. cliKey);
  20793. } while (ret == WC_PENDING_E);
  20794. if (ret < 0)
  20795. ERROR_OUT(-10430, done);
  20796. if (verify != 1)
  20797. ERROR_OUT(-10431, done);
  20798. TEST_SLEEP();
  20799. #ifdef WOLFSSL_CERT_EXT
  20800. idx = 0;
  20801. bytes = sizeof_ecc_clikeypub_der_256;
  20802. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  20803. (word32) bytes);
  20804. if (ret != 0)
  20805. ERROR_OUT(-10432, done);
  20806. #endif
  20807. ret = 0;
  20808. done:
  20809. #ifdef WOLFSSL_SMALL_STACK
  20810. if (cliKey != NULL) {
  20811. wc_ecc_free(cliKey);
  20812. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20813. }
  20814. if (servKey != NULL) {
  20815. wc_ecc_free(servKey);
  20816. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20817. }
  20818. #else
  20819. wc_ecc_free(cliKey);
  20820. wc_ecc_free(servKey);
  20821. #endif
  20822. wc_FreeRng(&rng);
  20823. return ret;
  20824. }
  20825. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  20826. #endif /* HAVE_ECC */
  20827. #ifdef HAVE_CURVE25519
  20828. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  20829. defined(HAVE_CURVE25519_KEY_IMPORT)
  20830. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20831. #define X25519_TEST_CNT 5
  20832. #else
  20833. #define X25519_TEST_CNT 1
  20834. #endif
  20835. static int curve25519_overflow_test(void)
  20836. {
  20837. /* secret key for party a */
  20838. byte sa[X25519_TEST_CNT][32] = {
  20839. {
  20840. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  20841. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  20842. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  20843. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  20844. },
  20845. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20846. {
  20847. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  20848. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  20849. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  20850. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  20851. },
  20852. {
  20853. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20854. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20855. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20856. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20857. },
  20858. {
  20859. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20860. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20861. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20862. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20863. },
  20864. {
  20865. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  20866. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  20867. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  20868. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  20869. }
  20870. #endif
  20871. };
  20872. /* public key for party b */
  20873. byte pb[X25519_TEST_CNT][32] = {
  20874. {
  20875. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20876. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20877. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20878. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  20879. },
  20880. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20881. {
  20882. /* 0xff first byte in original - invalid! */
  20883. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20884. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20885. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  20886. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  20887. },
  20888. {
  20889. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  20890. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  20891. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  20892. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  20893. },
  20894. {
  20895. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  20896. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  20897. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  20898. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  20899. },
  20900. {
  20901. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  20902. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  20903. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  20904. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  20905. }
  20906. #endif
  20907. };
  20908. /* expected shared key */
  20909. byte ss[X25519_TEST_CNT][32] = {
  20910. {
  20911. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  20912. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  20913. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  20914. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  20915. },
  20916. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  20917. {
  20918. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  20919. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  20920. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  20921. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  20922. },
  20923. {
  20924. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20925. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20926. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20927. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  20928. },
  20929. {
  20930. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20931. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  20934. },
  20935. {
  20936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20938. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20939. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  20940. }
  20941. #endif
  20942. };
  20943. int i;
  20944. word32 y;
  20945. byte shared[32];
  20946. curve25519_key userA;
  20947. wc_curve25519_init(&userA);
  20948. for (i = 0; i < X25519_TEST_CNT; i++) {
  20949. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  20950. sizeof(pb[i]), &userA) != 0)
  20951. return -10500 - i;
  20952. /* test against known test vector */
  20953. XMEMSET(shared, 0, sizeof(shared));
  20954. y = sizeof(shared);
  20955. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0)
  20956. return -10510 - i;
  20957. if (XMEMCMP(ss[i], shared, y))
  20958. return -10520 - i;
  20959. }
  20960. return 0;
  20961. }
  20962. /* Test the wc_curve25519_check_public API.
  20963. *
  20964. * returns 0 on success and -ve on failure.
  20965. */
  20966. static int curve25519_check_public_test(void)
  20967. {
  20968. /* Little-endian values that will fail */
  20969. byte fail_le[][CURVE25519_KEYSIZE] = {
  20970. {
  20971. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20972. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20973. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20974. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20975. },
  20976. {
  20977. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20978. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20979. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20980. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20981. },
  20982. {
  20983. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20984. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20985. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20986. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  20987. },
  20988. };
  20989. /* Big-endian values that will fail */
  20990. byte fail_be[][CURVE25519_KEYSIZE] = {
  20991. {
  20992. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20993. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20994. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20995. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  20996. },
  20997. {
  20998. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  20999. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21000. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21001. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21002. },
  21003. {
  21004. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21005. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21006. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21007. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21008. },
  21009. };
  21010. /* Good or valid public value */
  21011. byte good[CURVE25519_KEYSIZE] = {
  21012. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21013. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21014. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21015. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  21016. };
  21017. int i;
  21018. /* Parameter checks */
  21019. /* NULL pointer */
  21020. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  21021. BAD_FUNC_ARG) {
  21022. return -10600;
  21023. }
  21024. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  21025. BAD_FUNC_ARG) {
  21026. return -10601;
  21027. }
  21028. /* Length of 0 treated differently to other invalid lengths for TLS */
  21029. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  21030. return -10602;
  21031. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  21032. return -10603;
  21033. /* Length not CURVE25519_KEYSIZE */
  21034. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  21035. if (i == CURVE25519_KEYSIZE)
  21036. continue;
  21037. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  21038. ECC_BAD_ARG_E) {
  21039. return -10604 - i;
  21040. }
  21041. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  21042. ECC_BAD_ARG_E) {
  21043. return -10614 - i;
  21044. }
  21045. }
  21046. /* Little-endian fail cases */
  21047. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  21048. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  21049. EC25519_LITTLE_ENDIAN) == 0) {
  21050. return -10624 - i;
  21051. }
  21052. }
  21053. /* Big-endian fail cases */
  21054. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  21055. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  21056. EC25519_BIG_ENDIAN) == 0) {
  21057. return -10634 - i;
  21058. }
  21059. }
  21060. /* Check a valid public value works! */
  21061. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  21062. EC25519_LITTLE_ENDIAN) != 0) {
  21063. return -10644;
  21064. }
  21065. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  21066. EC25519_BIG_ENDIAN) != 0) {
  21067. return -10645;
  21068. }
  21069. return 0;
  21070. }
  21071. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  21072. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  21073. {
  21074. WC_RNG rng;
  21075. int ret;
  21076. #ifdef HAVE_CURVE25519_SHARED_SECRET
  21077. byte sharedA[32];
  21078. byte sharedB[32];
  21079. word32 y;
  21080. #endif
  21081. #ifdef HAVE_CURVE25519_KEY_EXPORT
  21082. byte exportBuf[32];
  21083. #endif
  21084. word32 x = 0;
  21085. curve25519_key userA, userB, pubKey;
  21086. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  21087. defined(HAVE_CURVE25519_KEY_IMPORT)
  21088. /* test vectors from
  21089. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  21090. */
  21091. /* secret key for party a */
  21092. byte sa[] = {
  21093. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  21094. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  21095. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  21096. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  21097. };
  21098. /* public key for party a */
  21099. byte pa[] = {
  21100. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  21101. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  21102. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  21103. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  21104. };
  21105. /* secret key for party b */
  21106. byte sb[] = {
  21107. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  21108. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  21109. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  21110. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  21111. };
  21112. /* public key for party b */
  21113. byte pb[] = {
  21114. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  21115. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  21116. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  21117. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  21118. };
  21119. /* expected shared key */
  21120. byte ss[] = {
  21121. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  21122. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  21123. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  21124. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  21125. };
  21126. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  21127. (void)x;
  21128. #ifndef HAVE_FIPS
  21129. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21130. #else
  21131. ret = wc_InitRng(&rng);
  21132. #endif
  21133. if (ret != 0)
  21134. return -10700;
  21135. wc_curve25519_init(&userA);
  21136. wc_curve25519_init(&userB);
  21137. wc_curve25519_init(&pubKey);
  21138. /* make curve25519 keys */
  21139. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  21140. return -10701;
  21141. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  21142. return -10702;
  21143. #ifdef HAVE_CURVE25519_SHARED_SECRET
  21144. /* find shared secret key */
  21145. x = sizeof(sharedA);
  21146. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  21147. return -10703;
  21148. y = sizeof(sharedB);
  21149. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21150. return -10704;
  21151. /* compare shared secret keys to test they are the same */
  21152. if (y != x)
  21153. return -10705;
  21154. if (XMEMCMP(sharedA, sharedB, x))
  21155. return -10706;
  21156. #endif
  21157. #ifdef HAVE_CURVE25519_KEY_EXPORT
  21158. /* export a public key and import it for another user */
  21159. x = sizeof(exportBuf);
  21160. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  21161. return -10707;
  21162. #ifdef HAVE_CURVE25519_KEY_IMPORT
  21163. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  21164. return -10708;
  21165. #endif
  21166. #endif
  21167. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  21168. defined(HAVE_CURVE25519_KEY_IMPORT)
  21169. /* test shared key after importing a public key */
  21170. XMEMSET(sharedB, 0, sizeof(sharedB));
  21171. y = sizeof(sharedB);
  21172. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  21173. return -10709;
  21174. if (XMEMCMP(sharedA, sharedB, y))
  21175. return -10710;
  21176. /* import RFC test vectors and compare shared key */
  21177. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  21178. != 0)
  21179. return -10711;
  21180. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  21181. != 0)
  21182. return -10712;
  21183. /* test against known test vector */
  21184. XMEMSET(sharedB, 0, sizeof(sharedB));
  21185. y = sizeof(sharedB);
  21186. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  21187. return -10713;
  21188. if (XMEMCMP(ss, sharedB, y))
  21189. return -10714;
  21190. /* test swapping roles of keys and generating same shared key */
  21191. XMEMSET(sharedB, 0, sizeof(sharedB));
  21192. y = sizeof(sharedB);
  21193. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21194. return -10715;
  21195. if (XMEMCMP(ss, sharedB, y))
  21196. return -10716;
  21197. /* test with 1 generated key and 1 from known test vector */
  21198. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  21199. != 0)
  21200. return -10717;
  21201. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  21202. return -10718;
  21203. x = sizeof(sharedA);
  21204. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  21205. return -10719;
  21206. y = sizeof(sharedB);
  21207. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  21208. return -10720;
  21209. /* compare shared secret keys to test they are the same */
  21210. if (y != x)
  21211. return -10721;
  21212. if (XMEMCMP(sharedA, sharedB, x))
  21213. return -10722;
  21214. ret = curve25519_overflow_test();
  21215. if (ret != 0)
  21216. return ret;
  21217. ret = curve25519_check_public_test();
  21218. if (ret != 0)
  21219. return ret;
  21220. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  21221. /* clean up keys when done */
  21222. wc_curve25519_free(&pubKey);
  21223. wc_curve25519_free(&userB);
  21224. wc_curve25519_free(&userA);
  21225. wc_FreeRng(&rng);
  21226. return 0;
  21227. }
  21228. #endif /* HAVE_CURVE25519 */
  21229. #ifdef HAVE_ED25519
  21230. #ifdef WOLFSSL_TEST_CERT
  21231. static int ed25519_test_cert(void)
  21232. {
  21233. DecodedCert cert[2];
  21234. DecodedCert* serverCert = NULL;
  21235. DecodedCert* caCert = NULL;
  21236. #ifdef HAVE_ED25519_VERIFY
  21237. ed25519_key key;
  21238. ed25519_key* pubKey = NULL;
  21239. int verify;
  21240. #endif /* HAVE_ED25519_VERIFY */
  21241. int ret;
  21242. byte* tmp;
  21243. size_t bytes;
  21244. XFILE file;
  21245. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21246. if (tmp == NULL) {
  21247. ERROR_OUT(-10730, done);
  21248. }
  21249. #ifdef USE_CERT_BUFFERS_256
  21250. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  21251. bytes = sizeof_ca_ed25519_cert;
  21252. #elif !defined(NO_FILESYSTEM)
  21253. file = XFOPEN(caEd25519Cert, "rb");
  21254. if (file == NULL) {
  21255. ERROR_OUT(-10731, done);
  21256. }
  21257. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  21258. XFCLOSE(file);
  21259. #else
  21260. /* No certificate to use. */
  21261. ERROR_OUT(-10732, done);
  21262. #endif
  21263. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  21264. caCert = &cert[0];
  21265. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  21266. if (ret != 0) {
  21267. ERROR_OUT(-10733, done);
  21268. }
  21269. #ifdef USE_CERT_BUFFERS_256
  21270. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  21271. bytes = sizeof_server_ed25519_cert;
  21272. #elif !defined(NO_FILESYSTEM)
  21273. file = XFOPEN(serverEd25519Cert, "rb");
  21274. if (file == NULL) {
  21275. ERROR_OUT(-10734, done);
  21276. }
  21277. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  21278. XFCLOSE(file);
  21279. #else
  21280. /* No certificate to use. */
  21281. ERROR_OUT(-10735, done);
  21282. #endif
  21283. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  21284. serverCert = &cert[1];
  21285. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  21286. if (ret != 0) {
  21287. ERROR_OUT(-10736, done);
  21288. }
  21289. #ifdef HAVE_ED25519_VERIFY
  21290. ret = wc_ed25519_init(&key);
  21291. if (ret < 0) {
  21292. ERROR_OUT(-10737, done);
  21293. }
  21294. pubKey = &key;
  21295. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  21296. pubKey);
  21297. if (ret < 0) {
  21298. ERROR_OUT(-10738, done);
  21299. }
  21300. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  21301. serverCert->source + serverCert->certBegin,
  21302. serverCert->sigIndex - serverCert->certBegin,
  21303. &verify, pubKey) < 0 || verify != 1) {
  21304. ERROR_OUT(-10739, done);
  21305. }
  21306. #endif /* HAVE_ED25519_VERIFY */
  21307. done:
  21308. if (tmp != NULL)
  21309. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21310. #ifdef HAVE_ED25519_VERIFY
  21311. wc_ed25519_free(pubKey);
  21312. #endif /* HAVE_ED25519_VERIFY */
  21313. if (caCert != NULL)
  21314. FreeDecodedCert(caCert);
  21315. if (serverCert != NULL)
  21316. FreeDecodedCert(serverCert);
  21317. return ret;
  21318. }
  21319. static int ed25519_test_make_cert(void)
  21320. {
  21321. WC_RNG rng;
  21322. Cert cert;
  21323. DecodedCert decode;
  21324. ed25519_key key;
  21325. ed25519_key* privKey = NULL;
  21326. int ret = 0;
  21327. byte* tmp = NULL;
  21328. wc_InitCert(&cert);
  21329. #ifndef HAVE_FIPS
  21330. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21331. #else
  21332. ret = wc_InitRng(&rng);
  21333. #endif
  21334. if (ret != 0)
  21335. return -10750;
  21336. wc_ed25519_init(&key);
  21337. privKey = &key;
  21338. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  21339. cert.daysValid = 365 * 2;
  21340. cert.selfSigned = 1;
  21341. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  21342. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  21343. cert.isCA = 0;
  21344. #ifdef WOLFSSL_CERT_EXT
  21345. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  21346. if (ret < 0) {
  21347. ERROR_OUT(-10751, done);
  21348. }
  21349. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  21350. if (ret < 0) {
  21351. ERROR_OUT(-10752, done);
  21352. }
  21353. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  21354. if (ret < 0) {
  21355. ERROR_OUT(-10753, done);
  21356. }
  21357. #endif
  21358. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21359. if (tmp == NULL) {
  21360. ERROR_OUT(-10754, done);
  21361. }
  21362. cert.sigType = CTC_ED25519;
  21363. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  21364. if (ret < 0) {
  21365. ERROR_OUT(-10755, done);
  21366. }
  21367. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  21368. ED25519_TYPE, privKey, &rng);
  21369. if (ret < 0) {
  21370. ERROR_OUT(-10756, done);
  21371. }
  21372. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  21373. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  21374. FreeDecodedCert(&decode);
  21375. if (ret != 0) {
  21376. ERROR_OUT(-10757, done);
  21377. }
  21378. done:
  21379. if (tmp != NULL)
  21380. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21381. wc_ed25519_free(privKey);
  21382. wc_FreeRng(&rng);
  21383. return ret;
  21384. }
  21385. #endif /* WOLFSSL_TEST_CERT */
  21386. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21387. defined(HAVE_ED25519_KEY_IMPORT)
  21388. static int ed25519ctx_test(void)
  21389. {
  21390. byte out[ED25519_SIG_SIZE];
  21391. word32 outlen;
  21392. #ifdef HAVE_ED25519_VERIFY
  21393. int verify;
  21394. #endif /* HAVE_ED25519_VERIFY */
  21395. ed25519_key key;
  21396. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  21397. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  21398. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  21399. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  21400. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  21401. };
  21402. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  21403. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  21404. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  21405. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  21406. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  21407. };
  21408. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  21409. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  21410. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  21411. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  21412. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  21413. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  21414. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  21415. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  21416. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  21417. };
  21418. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  21419. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  21420. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  21421. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  21422. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  21423. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  21424. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  21425. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  21426. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  21427. };
  21428. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  21429. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  21430. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  21431. };
  21432. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  21433. 0x66,0x6f,0x6f
  21434. };
  21435. outlen = sizeof(out);
  21436. XMEMSET(out, 0, sizeof(out));
  21437. if (wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  21438. sizeof(pKeyCtx), &key) != 0)
  21439. return -10800;
  21440. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  21441. contextCtx, sizeof(contextCtx)) != 0)
  21442. return -10801;
  21443. if (XMEMCMP(out, sigCtx1, 64))
  21444. return -10802;
  21445. #if defined(HAVE_ED25519_VERIFY)
  21446. /* test verify on good msg */
  21447. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  21448. &key, contextCtx, sizeof(contextCtx)) != 0 ||
  21449. verify != 1)
  21450. return -10803;
  21451. #endif
  21452. if (wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key, NULL,
  21453. 0) != 0)
  21454. return -10804;
  21455. if (XMEMCMP(out, sigCtx2, 64))
  21456. return -10805;
  21457. #if defined(HAVE_ED25519_VERIFY)
  21458. /* test verify on good msg */
  21459. if (wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify,
  21460. &key, NULL, 0) != 0 || verify != 1)
  21461. return -10806;
  21462. #endif
  21463. wc_ed25519_free(&key);
  21464. return 0;
  21465. }
  21466. static int ed25519ph_test(void)
  21467. {
  21468. byte out[ED25519_SIG_SIZE];
  21469. word32 outlen;
  21470. #ifdef HAVE_ED25519_VERIFY
  21471. int verify;
  21472. #endif /* HAVE_ED25519_VERIFY */
  21473. ed25519_key key;
  21474. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  21475. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  21476. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  21477. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  21478. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  21479. };
  21480. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  21481. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  21482. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  21483. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  21484. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  21485. };
  21486. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  21487. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  21488. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  21489. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  21490. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  21491. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  21492. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  21493. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  21494. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  21495. };
  21496. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  21497. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  21498. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  21499. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  21500. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  21501. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  21502. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  21503. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  21504. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  21505. };
  21506. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  21507. 0x61,0x62,0x63
  21508. };
  21509. /* SHA-512 hash of msgPh */
  21510. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  21511. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  21512. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  21513. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  21514. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  21515. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  21516. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  21517. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  21518. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  21519. };
  21520. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  21521. 0x66,0x6f,0x6f
  21522. };
  21523. outlen = sizeof(out);
  21524. XMEMSET(out, 0, sizeof(out));
  21525. if (wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  21526. sizeof(pKeyPh), &key) != 0) {
  21527. return -10900;
  21528. }
  21529. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  21530. 0) != 0) {
  21531. return -10901;
  21532. }
  21533. if (XMEMCMP(out, sigPh1, 64))
  21534. return -10902;
  21535. #if defined(HAVE_ED25519_VERIFY)
  21536. /* test verify on good msg */
  21537. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  21538. &key, NULL, 0) != 0 ||
  21539. verify != 1) {
  21540. return -10903;
  21541. }
  21542. #endif
  21543. if (wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  21544. contextPh2, sizeof(contextPh2)) != 0) {
  21545. return -10904;
  21546. }
  21547. if (XMEMCMP(out, sigPh2, 64))
  21548. return -10905;
  21549. #if defined(HAVE_ED25519_VERIFY)
  21550. /* test verify on good msg */
  21551. if (wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  21552. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  21553. verify != 1) {
  21554. return -10906;
  21555. }
  21556. #endif
  21557. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  21558. 0) != 0) {
  21559. return -10907;
  21560. }
  21561. if (XMEMCMP(out, sigPh1, 64))
  21562. return -10908;
  21563. #if defined(HAVE_ED25519_VERIFY)
  21564. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  21565. &key, NULL, 0) != 0 ||
  21566. verify != 1) {
  21567. return -10909;
  21568. }
  21569. #endif
  21570. if (wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  21571. contextPh2, sizeof(contextPh2)) != 0) {
  21572. return -10910;
  21573. }
  21574. if (XMEMCMP(out, sigPh2, 64))
  21575. return -10911;
  21576. #if defined(HAVE_ED25519_VERIFY)
  21577. if (wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  21578. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  21579. verify != 1) {
  21580. return -10912;
  21581. }
  21582. #endif
  21583. wc_ed25519_free(&key);
  21584. return 0;
  21585. }
  21586. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21587. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  21588. {
  21589. int ret;
  21590. WC_RNG rng;
  21591. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  21592. defined(HAVE_ED25519_KEY_IMPORT)
  21593. byte out[ED25519_SIG_SIZE];
  21594. byte exportPKey[ED25519_KEY_SIZE];
  21595. byte exportSKey[ED25519_KEY_SIZE];
  21596. word32 exportPSz;
  21597. word32 exportSSz;
  21598. int i;
  21599. word32 outlen;
  21600. #ifdef HAVE_ED25519_VERIFY
  21601. int verify;
  21602. #endif /* HAVE_ED25519_VERIFY */
  21603. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21604. word32 keySz, sigSz;
  21605. ed25519_key key;
  21606. ed25519_key key2;
  21607. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21608. defined(HAVE_ED25519_KEY_IMPORT)
  21609. /* test vectors from
  21610. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  21611. */
  21612. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  21613. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21614. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21615. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21616. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21617. };
  21618. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  21619. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  21620. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  21621. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  21622. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  21623. };
  21624. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  21625. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  21626. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  21627. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  21628. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  21629. };
  21630. /* uncompressed test */
  21631. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  21632. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21633. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21634. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21635. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21636. };
  21637. /* compressed prefix test */
  21638. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  21639. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21640. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21641. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21642. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21643. };
  21644. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  21645. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  21646. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  21647. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  21648. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  21649. };
  21650. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  21651. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  21652. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  21653. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  21654. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  21655. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  21656. };
  21657. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  21658. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  21659. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  21660. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  21661. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  21662. };
  21663. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  21664. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  21665. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  21666. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  21667. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  21668. };
  21669. /* uncompressed test */
  21670. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  21671. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  21672. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  21673. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  21674. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  21675. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  21676. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  21677. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  21678. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  21679. 0xd7
  21680. };
  21681. /* compressed prefix */
  21682. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  21683. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  21684. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  21685. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  21686. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  21687. };
  21688. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  21689. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  21690. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  21691. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  21692. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  21693. };
  21694. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  21695. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  21696. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  21697. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  21698. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21699. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21700. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21701. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21702. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21703. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21704. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21705. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21706. };
  21707. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  21708. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  21709. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  21710. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  21711. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  21712. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  21713. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  21714. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  21715. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  21716. };
  21717. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  21718. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  21719. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  21720. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  21721. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  21722. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  21723. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  21724. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  21725. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  21726. };
  21727. /* uncompressed test */
  21728. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  21729. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21730. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21731. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21732. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21733. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21734. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21735. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21736. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21737. };
  21738. /* compressed prefix */
  21739. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  21740. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  21741. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  21742. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  21743. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  21744. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  21745. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  21746. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  21747. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  21748. };
  21749. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  21750. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  21751. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  21752. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  21753. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  21754. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  21755. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  21756. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  21757. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  21758. };
  21759. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  21760. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  21761. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  21762. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  21763. /* test of a 1024 byte long message */
  21764. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  21765. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  21766. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  21767. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  21768. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  21769. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  21770. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  21771. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  21772. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  21773. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  21774. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  21775. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  21776. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  21777. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  21778. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  21779. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  21780. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  21781. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  21782. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  21783. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  21784. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  21785. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  21786. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  21787. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  21788. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  21789. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  21790. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  21791. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  21792. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  21793. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  21794. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  21795. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  21796. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  21797. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  21798. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  21799. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  21800. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  21801. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  21802. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  21803. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  21804. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  21805. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  21806. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  21807. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  21808. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  21809. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  21810. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  21811. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  21812. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  21813. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  21814. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  21815. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  21816. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  21817. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  21818. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  21819. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  21820. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  21821. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  21822. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  21823. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  21824. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  21825. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  21826. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  21827. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  21828. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  21829. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  21830. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  21831. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  21832. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  21833. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  21834. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  21835. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  21836. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  21837. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  21838. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  21839. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  21840. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  21841. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  21842. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  21843. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  21844. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  21845. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  21846. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  21847. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  21848. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  21849. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  21850. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  21851. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  21852. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  21853. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  21854. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  21855. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  21856. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  21857. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  21858. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  21859. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  21860. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  21861. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  21862. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  21863. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  21864. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  21865. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  21866. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  21867. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  21868. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  21869. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  21870. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  21871. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  21872. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  21873. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  21874. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  21875. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  21876. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  21877. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  21878. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  21879. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  21880. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  21881. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  21882. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  21883. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  21884. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  21885. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  21886. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  21887. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  21888. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  21889. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  21890. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  21891. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  21892. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  21893. };
  21894. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  21895. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  21896. sizeof(msg2),
  21897. sizeof(msg3),
  21898. 0 /*sizeof(msg1)*/,
  21899. 0 /*sizeof(msg1)*/,
  21900. sizeof(msg4)
  21901. };
  21902. #ifndef NO_ASN
  21903. static byte privateEd25519[] = {
  21904. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  21905. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  21906. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21907. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21908. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21909. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  21910. };
  21911. static byte publicEd25519[] = {
  21912. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  21913. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  21914. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  21915. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  21916. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  21917. 0xf7,0x07,0x51,0x1a
  21918. };
  21919. static byte privPubEd25519[] = {
  21920. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  21921. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  21922. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  21923. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  21924. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  21925. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  21926. 0xa1,0x22,0x04,0x20,0xd7,0x5a,0x98,0x01,
  21927. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  21928. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  21929. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  21930. 0xf7,0x07,0x51,0x1a
  21931. };
  21932. word32 idx;
  21933. ed25519_key key3;
  21934. #endif /* NO_ASN */
  21935. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  21936. /* create ed25519 keys */
  21937. #ifndef HAVE_FIPS
  21938. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21939. #else
  21940. ret = wc_InitRng(&rng);
  21941. #endif
  21942. if (ret != 0)
  21943. return -11000;
  21944. wc_ed25519_init(&key);
  21945. wc_ed25519_init(&key2);
  21946. #ifndef NO_ASN
  21947. wc_ed25519_init(&key3);
  21948. #endif
  21949. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  21950. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  21951. /* helper functions for signature and key size */
  21952. keySz = wc_ed25519_size(&key);
  21953. sigSz = wc_ed25519_sig_size(&key);
  21954. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  21955. defined(HAVE_ED25519_KEY_IMPORT)
  21956. for (i = 0; i < 6; i++) {
  21957. outlen = sizeof(out);
  21958. XMEMSET(out, 0, sizeof(out));
  21959. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  21960. pKeySz[i], &key) != 0)
  21961. return -11001 - i;
  21962. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  21963. return -11011 - i;
  21964. if (XMEMCMP(out, sigs[i], 64))
  21965. return -11021 - i;
  21966. #if defined(HAVE_ED25519_VERIFY)
  21967. /* test verify on good msg */
  21968. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21969. &key) != 0 || verify != 1)
  21970. return -11031 - i;
  21971. /* test verify on bad msg */
  21972. out[outlen-1] = out[outlen-1] + 1;
  21973. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21974. &key) == 0 || verify == 1)
  21975. return -11041 - i;
  21976. #endif /* HAVE_ED25519_VERIFY */
  21977. /* test api for import/exporting keys */
  21978. exportPSz = sizeof(exportPKey);
  21979. exportSSz = sizeof(exportSKey);
  21980. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  21981. return -11051 - i;
  21982. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  21983. return -11061 - i;
  21984. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  21985. return -11071 - i;
  21986. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  21987. exportPKey, exportPSz, &key2) != 0)
  21988. return -11081 - i;
  21989. /* clear "out" buffer and test sign with imported keys */
  21990. outlen = sizeof(out);
  21991. XMEMSET(out, 0, sizeof(out));
  21992. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  21993. return -11091 - i;
  21994. #if defined(HAVE_ED25519_VERIFY)
  21995. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  21996. &key2) != 0 || verify != 1)
  21997. return -11101 - i;
  21998. if (XMEMCMP(out, sigs[i], 64))
  21999. return -11111 - i;
  22000. #endif /* HAVE_ED25519_VERIFY */
  22001. }
  22002. ret = ed25519ctx_test();
  22003. if (ret != 0)
  22004. return ret;
  22005. ret = ed25519ph_test();
  22006. if (ret != 0)
  22007. return ret;
  22008. #ifndef NO_ASN
  22009. /* Try ASN.1 encoded private-only key and public key. */
  22010. idx = 0;
  22011. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  22012. sizeof(privateEd25519)) != 0)
  22013. return -11121 - i;
  22014. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  22015. != BAD_FUNC_ARG)
  22016. return -11131 - i;
  22017. idx = 0;
  22018. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  22019. sizeof(publicEd25519)) != 0)
  22020. return -11141 - i;
  22021. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  22022. return -11151 - i;
  22023. if (XMEMCMP(out, sigs[0], 64))
  22024. return -11161 - i;
  22025. #if defined(HAVE_ED25519_VERIFY)
  22026. /* test verify on good msg */
  22027. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  22028. != 0 || verify != 1)
  22029. return -11171 - i;
  22030. #endif /* HAVE_ED25519_VERIFY */
  22031. wc_ed25519_free(&key3);
  22032. wc_ed25519_init(&key3);
  22033. idx = 0;
  22034. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  22035. sizeof(privPubEd25519)) != 0)
  22036. return -11181 - i;
  22037. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  22038. return -11191 - i;
  22039. if (XMEMCMP(out, sigs[0], 64))
  22040. return -11201 - i;
  22041. wc_ed25519_free(&key3);
  22042. #endif /* NO_ASN */
  22043. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  22044. /* clean up keys when done */
  22045. wc_ed25519_free(&key);
  22046. wc_ed25519_free(&key2);
  22047. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  22048. wc_FreeRng(&rng);
  22049. #endif
  22050. /* hush warnings of unused keySz and sigSz */
  22051. (void)keySz;
  22052. (void)sigSz;
  22053. #ifdef WOLFSSL_TEST_CERT
  22054. ret = ed25519_test_cert();
  22055. if (ret < 0)
  22056. return ret;
  22057. #ifdef WOLFSSL_CERT_GEN
  22058. ret = ed25519_test_make_cert();
  22059. if (ret < 0)
  22060. return ret;
  22061. #endif /* WOLFSSL_CERT_GEN */
  22062. #endif /* WOLFSSL_TEST_CERT */
  22063. return 0;
  22064. }
  22065. #endif /* HAVE_ED25519 */
  22066. #ifdef HAVE_CURVE448
  22067. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22068. defined(HAVE_CURVE448_KEY_IMPORT)
  22069. /* Test the wc_curve448_check_public API.
  22070. *
  22071. * returns 0 on success and -ve on failure.
  22072. */
  22073. static int curve448_check_public_test(void)
  22074. {
  22075. /* Little-endian values that will fail */
  22076. byte fail_le[][CURVE448_KEY_SIZE] = {
  22077. {
  22078. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22079. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22080. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22081. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22082. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22083. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22084. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  22085. },
  22086. {
  22087. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22088. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22089. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22090. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22091. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22092. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22093. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  22094. },
  22095. };
  22096. /* Big-endian values that will fail */
  22097. byte fail_be[][CURVE448_KEY_SIZE] = {
  22098. {
  22099. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22100. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22101. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22103. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22104. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22105. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  22106. },
  22107. {
  22108. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22109. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22110. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22111. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  22115. },
  22116. };
  22117. /* Good or valid public value */
  22118. byte good[CURVE448_KEY_SIZE] = {
  22119. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22120. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22121. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22123. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22124. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22125. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  22126. };
  22127. int i;
  22128. /* Parameter checks */
  22129. /* NULL pointer */
  22130. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  22131. BAD_FUNC_ARG) {
  22132. return -11300;
  22133. }
  22134. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  22135. return -11301;
  22136. }
  22137. /* Length of 0 treated differently to other invalid lengths for TLS */
  22138. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  22139. return -11302;
  22140. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  22141. return -11303;
  22142. /* Length not CURVE448_KEY_SIZE */
  22143. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  22144. if (i == CURVE448_KEY_SIZE)
  22145. continue;
  22146. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  22147. ECC_BAD_ARG_E) {
  22148. return -11304 - i;
  22149. }
  22150. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  22151. ECC_BAD_ARG_E) {
  22152. return -11314 - i;
  22153. }
  22154. }
  22155. /* Little-endian fail cases */
  22156. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  22157. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  22158. EC448_LITTLE_ENDIAN) == 0) {
  22159. return -11324 - i;
  22160. }
  22161. }
  22162. /* Big-endian fail cases */
  22163. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  22164. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  22165. EC448_BIG_ENDIAN) == 0) {
  22166. return -11334 - i;
  22167. }
  22168. }
  22169. /* Check a valid public value works! */
  22170. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  22171. EC448_LITTLE_ENDIAN) != 0) {
  22172. return -11344;
  22173. }
  22174. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  22175. EC448_BIG_ENDIAN) != 0) {
  22176. return -11345;
  22177. }
  22178. return 0;
  22179. }
  22180. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  22181. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  22182. {
  22183. WC_RNG rng;
  22184. int ret;
  22185. #ifdef HAVE_CURVE448_SHARED_SECRET
  22186. byte sharedA[CURVE448_KEY_SIZE];
  22187. byte sharedB[CURVE448_KEY_SIZE];
  22188. word32 y;
  22189. #endif
  22190. #ifdef HAVE_CURVE448_KEY_EXPORT
  22191. byte exportBuf[CURVE448_KEY_SIZE];
  22192. #endif
  22193. word32 x;
  22194. curve448_key userA, userB, pubKey;
  22195. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22196. defined(HAVE_CURVE448_KEY_IMPORT)
  22197. /* test vectors from
  22198. https://www.rfc-editor.org/rfc/rfc7748.html
  22199. */
  22200. /* secret key for party a */
  22201. byte sa[] = {
  22202. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  22203. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  22204. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  22205. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  22206. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  22207. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  22208. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  22209. };
  22210. /* public key for party a */
  22211. byte pa[] = {
  22212. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  22213. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  22214. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  22215. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  22216. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  22217. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  22218. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  22219. };
  22220. /* secret key for party b */
  22221. byte sb[] = {
  22222. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  22223. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  22224. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  22225. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  22226. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  22227. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  22228. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  22229. };
  22230. /* public key for party b */
  22231. byte pb[] = {
  22232. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  22233. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  22234. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  22235. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  22236. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  22237. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  22238. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  22239. };
  22240. /* expected shared key */
  22241. byte ss[] = {
  22242. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  22243. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  22244. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  22245. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  22246. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  22247. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  22248. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  22249. };
  22250. #endif /* HAVE_CURVE448_SHARED_SECRET */
  22251. (void)x;
  22252. #ifndef HAVE_FIPS
  22253. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22254. #else
  22255. ret = wc_InitRng(&rng);
  22256. #endif
  22257. if (ret != 0)
  22258. return -11400;
  22259. wc_curve448_init(&userA);
  22260. wc_curve448_init(&userB);
  22261. wc_curve448_init(&pubKey);
  22262. /* make curve448 keys */
  22263. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  22264. return -11401;
  22265. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  22266. return -11402;
  22267. #ifdef HAVE_CURVE448_SHARED_SECRET
  22268. /* find shared secret key */
  22269. x = sizeof(sharedA);
  22270. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22271. return -11403;
  22272. y = sizeof(sharedB);
  22273. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22274. return -11404;
  22275. /* compare shared secret keys to test they are the same */
  22276. if (y != x)
  22277. return -11405;
  22278. if (XMEMCMP(sharedA, sharedB, x))
  22279. return -11406;
  22280. #endif
  22281. #ifdef HAVE_CURVE448_KEY_EXPORT
  22282. /* export a public key and import it for another user */
  22283. x = sizeof(exportBuf);
  22284. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  22285. return -11407;
  22286. #ifdef HAVE_CURVE448_KEY_IMPORT
  22287. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  22288. return -11408;
  22289. #endif
  22290. #endif
  22291. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  22292. defined(HAVE_CURVE448_KEY_IMPORT)
  22293. /* test shared key after importing a public key */
  22294. XMEMSET(sharedB, 0, sizeof(sharedB));
  22295. y = sizeof(sharedB);
  22296. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  22297. return -11409;
  22298. if (XMEMCMP(sharedA, sharedB, y))
  22299. return -11410;
  22300. /* import RFC test vectors and compare shared key */
  22301. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22302. != 0)
  22303. return -11411;
  22304. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  22305. != 0)
  22306. return -11412;
  22307. /* test against known test vector */
  22308. XMEMSET(sharedB, 0, sizeof(sharedB));
  22309. y = sizeof(sharedB);
  22310. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  22311. return -11413;
  22312. if (XMEMCMP(ss, sharedB, y))
  22313. return -11414;
  22314. /* test swapping roles of keys and generating same shared key */
  22315. XMEMSET(sharedB, 0, sizeof(sharedB));
  22316. y = sizeof(sharedB);
  22317. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22318. return -11415;
  22319. if (XMEMCMP(ss, sharedB, y))
  22320. return -11416;
  22321. /* test with 1 generated key and 1 from known test vector */
  22322. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  22323. != 0)
  22324. return -11417;
  22325. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  22326. return -11418;
  22327. x = sizeof(sharedA);
  22328. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  22329. return -11419;
  22330. y = sizeof(sharedB);
  22331. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  22332. return -11420;
  22333. /* compare shared secret keys to test they are the same */
  22334. if (y != x)
  22335. return -11421;
  22336. if (XMEMCMP(sharedA, sharedB, x))
  22337. return -11422;
  22338. ret = curve448_check_public_test();
  22339. if (ret != 0)
  22340. return ret;
  22341. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  22342. /* clean up keys when done */
  22343. wc_curve448_free(&pubKey);
  22344. wc_curve448_free(&userB);
  22345. wc_curve448_free(&userA);
  22346. wc_FreeRng(&rng);
  22347. return 0;
  22348. }
  22349. #endif /* HAVE_CURVE448 */
  22350. #ifdef HAVE_ED448
  22351. #ifdef WOLFSSL_TEST_CERT
  22352. static int ed448_test_cert(void)
  22353. {
  22354. DecodedCert cert[2];
  22355. DecodedCert* serverCert = NULL;
  22356. DecodedCert* caCert = NULL;
  22357. #ifdef HAVE_ED448_VERIFY
  22358. ed448_key key;
  22359. ed448_key* pubKey = NULL;
  22360. int verify;
  22361. #endif /* HAVE_ED448_VERIFY */
  22362. int ret;
  22363. byte* tmp;
  22364. size_t bytes;
  22365. XFILE file;
  22366. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22367. if (tmp == NULL) {
  22368. ERROR_OUT(-11430, done);
  22369. }
  22370. #ifdef USE_CERT_BUFFERS_256
  22371. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  22372. bytes = sizeof_ca_ed448_cert;
  22373. #elif !defined(NO_FILESYSTEM)
  22374. file = XFOPEN(caEd448Cert, "rb");
  22375. if (file == NULL) {
  22376. ERROR_OUT(-11431, done);
  22377. }
  22378. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22379. XFCLOSE(file);
  22380. #else
  22381. /* No certificate to use. */
  22382. ERROR_OUT(-11432, done);
  22383. #endif
  22384. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  22385. caCert = &cert[0];
  22386. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  22387. if (ret != 0) {
  22388. ERROR_OUT(-11433, done);
  22389. }
  22390. #ifdef USE_CERT_BUFFERS_256
  22391. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  22392. bytes = sizeof_server_ed448_cert;
  22393. #elif !defined(NO_FILESYSTEM)
  22394. file = XFOPEN(serverEd448Cert, "rb");
  22395. if (file == NULL) {
  22396. ERROR_OUT(-11434, done);
  22397. }
  22398. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  22399. XFCLOSE(file);
  22400. #else
  22401. /* No certificate to use. */
  22402. ERROR_OUT(-11435, done);
  22403. #endif
  22404. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  22405. serverCert = &cert[1];
  22406. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  22407. if (ret != 0) {
  22408. ERROR_OUT(-11436, done);
  22409. }
  22410. #ifdef HAVE_ED448_VERIFY
  22411. ret = wc_ed448_init(&key);
  22412. if (ret < 0) {
  22413. ERROR_OUT(-11437, done);
  22414. }
  22415. pubKey = &key;
  22416. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  22417. if (ret < 0) {
  22418. ERROR_OUT(-11438, done);
  22419. }
  22420. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  22421. serverCert->source + serverCert->certBegin,
  22422. serverCert->sigIndex - serverCert->certBegin,
  22423. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  22424. ERROR_OUT(-11439, done);
  22425. }
  22426. #endif /* HAVE_ED448_VERIFY */
  22427. done:
  22428. if (tmp != NULL)
  22429. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22430. #ifdef HAVE_ED448_VERIFY
  22431. wc_ed448_free(pubKey);
  22432. #endif /* HAVE_ED448_VERIFY */
  22433. if (caCert != NULL)
  22434. FreeDecodedCert(caCert);
  22435. if (serverCert != NULL)
  22436. FreeDecodedCert(serverCert);
  22437. return ret;
  22438. }
  22439. static int ed448_test_make_cert(void)
  22440. {
  22441. WC_RNG rng;
  22442. Cert cert;
  22443. DecodedCert decode;
  22444. ed448_key key;
  22445. ed448_key* privKey = NULL;
  22446. int ret = 0;
  22447. byte* tmp = NULL;
  22448. wc_InitCert(&cert);
  22449. #ifndef HAVE_FIPS
  22450. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22451. #else
  22452. ret = wc_InitRng(&rng);
  22453. #endif
  22454. if (ret != 0)
  22455. return -11450;
  22456. wc_ed448_init(&key);
  22457. privKey = &key;
  22458. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  22459. cert.daysValid = 365 * 2;
  22460. cert.selfSigned = 1;
  22461. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  22462. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  22463. cert.isCA = 0;
  22464. #ifdef WOLFSSL_CERT_EXT
  22465. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  22466. if (ret < 0) {
  22467. ERROR_OUT(-11451, done);
  22468. }
  22469. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  22470. if (ret < 0) {
  22471. ERROR_OUT(-11452, done);
  22472. }
  22473. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  22474. if (ret < 0) {
  22475. ERROR_OUT(-11453, done);
  22476. }
  22477. #endif
  22478. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22479. if (tmp == NULL) {
  22480. ERROR_OUT(-11454, done);
  22481. }
  22482. cert.sigType = CTC_ED448;
  22483. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  22484. if (ret < 0) {
  22485. ERROR_OUT(-11455, done);
  22486. }
  22487. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  22488. privKey, &rng);
  22489. if (ret < 0) {
  22490. ERROR_OUT(-11456, done);
  22491. }
  22492. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  22493. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  22494. FreeDecodedCert(&decode);
  22495. if (ret != 0) {
  22496. ERROR_OUT(-11457, done);
  22497. }
  22498. done:
  22499. if (tmp != NULL)
  22500. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22501. wc_ed448_free(privKey);
  22502. wc_FreeRng(&rng);
  22503. return ret;
  22504. }
  22505. #endif /* WOLFSSL_TEST_CERT */
  22506. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  22507. defined(HAVE_ED448_KEY_IMPORT)
  22508. static int ed448_ctx_test(void)
  22509. {
  22510. byte out[ED448_SIG_SIZE];
  22511. word32 outlen;
  22512. #ifdef HAVE_ED448_VERIFY
  22513. int verify;
  22514. #endif /* HAVE_ED448_VERIFY */
  22515. ed448_key key;
  22516. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  22517. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  22518. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  22519. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  22520. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  22521. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  22522. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  22523. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  22524. 0x4e
  22525. };
  22526. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  22527. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  22528. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  22529. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  22530. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  22531. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  22532. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  22533. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  22534. 0x80
  22535. };
  22536. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  22537. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  22538. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  22539. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  22540. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  22541. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  22542. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  22543. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  22544. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  22545. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  22546. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  22547. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  22548. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  22549. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  22550. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  22551. 0x3c, 0x00
  22552. };
  22553. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  22554. 0x03
  22555. };
  22556. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  22557. 0x66,0x6f,0x6f
  22558. };
  22559. outlen = sizeof(out);
  22560. XMEMSET(out, 0, sizeof(out));
  22561. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  22562. sizeof(pKeyCtx), &key) != 0)
  22563. return -11500;
  22564. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  22565. contextCtx, sizeof(contextCtx)) != 0)
  22566. return -11501;
  22567. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  22568. return -11502;
  22569. #if defined(HAVE_ED448_VERIFY)
  22570. /* test verify on good msg */
  22571. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  22572. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  22573. return -11503;
  22574. #endif
  22575. wc_ed448_free(&key);
  22576. return 0;
  22577. }
  22578. static int ed448ph_test(void)
  22579. {
  22580. byte out[ED448_SIG_SIZE];
  22581. word32 outlen;
  22582. #ifdef HAVE_ED448_VERIFY
  22583. int verify;
  22584. #endif /* HAVE_ED448_VERIFY */
  22585. ed448_key key;
  22586. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  22587. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  22588. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  22589. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  22590. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  22591. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  22592. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  22593. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  22594. 0x49
  22595. };
  22596. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  22597. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  22598. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  22599. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  22600. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  22601. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  22602. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  22603. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  22604. 0x80
  22605. };
  22606. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  22607. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  22608. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  22609. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  22610. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  22611. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  22612. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  22613. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  22614. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  22615. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  22616. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  22617. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  22618. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  22619. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  22620. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  22621. 0x0f, 0x00
  22622. };
  22623. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  22624. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  22625. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  22626. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  22627. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  22628. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  22629. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  22630. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  22631. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  22632. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  22633. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  22634. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  22635. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  22636. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  22637. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  22638. 0x21, 0x00
  22639. };
  22640. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  22641. 0x61,0x62,0x63
  22642. };
  22643. /* SHA-512 hash of msgPh */
  22644. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  22645. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  22646. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  22647. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  22648. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  22649. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  22650. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  22651. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  22652. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  22653. };
  22654. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  22655. 0x66,0x6f,0x6f
  22656. };
  22657. outlen = sizeof(out);
  22658. XMEMSET(out, 0, sizeof(out));
  22659. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  22660. sizeof(pKeyPh), &key) != 0) {
  22661. return -11600;
  22662. }
  22663. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  22664. 0) != 0) {
  22665. return -11601;
  22666. }
  22667. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  22668. return -11602;
  22669. #if defined(HAVE_ED448_VERIFY)
  22670. /* test verify on good msg */
  22671. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  22672. NULL, 0) != 0 || verify != 1) {
  22673. return -11603;
  22674. }
  22675. #endif
  22676. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  22677. contextPh2, sizeof(contextPh2)) != 0) {
  22678. return -11604;
  22679. }
  22680. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  22681. return -11605;
  22682. #if defined(HAVE_ED448_VERIFY)
  22683. /* test verify on good msg */
  22684. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  22685. contextPh2, sizeof(contextPh2)) != 0 ||
  22686. verify != 1) {
  22687. return -11606;
  22688. }
  22689. #endif
  22690. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  22691. 0) != 0) {
  22692. return -11607;
  22693. }
  22694. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  22695. return -11608;
  22696. #if defined(HAVE_ED448_VERIFY)
  22697. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22698. &key, NULL, 0) != 0 || verify != 1) {
  22699. return -11609;
  22700. }
  22701. #endif
  22702. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  22703. contextPh2, sizeof(contextPh2)) != 0) {
  22704. return -11610;
  22705. }
  22706. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  22707. return -11611;
  22708. #if defined(HAVE_ED448_VERIFY)
  22709. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  22710. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  22711. verify != 1) {
  22712. return -11612;
  22713. }
  22714. #endif
  22715. wc_ed448_free(&key);
  22716. return 0;
  22717. }
  22718. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  22719. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  22720. {
  22721. int ret;
  22722. WC_RNG rng;
  22723. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  22724. defined(HAVE_ED448_KEY_IMPORT)
  22725. byte out[ED448_SIG_SIZE];
  22726. byte exportPKey[ED448_KEY_SIZE];
  22727. byte exportSKey[ED448_KEY_SIZE];
  22728. word32 exportPSz;
  22729. word32 exportSSz;
  22730. int i;
  22731. word32 outlen;
  22732. #ifdef HAVE_ED448_VERIFY
  22733. int verify;
  22734. #endif /* HAVE_ED448_VERIFY */
  22735. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  22736. word32 keySz, sigSz;
  22737. ed448_key key;
  22738. ed448_key key2;
  22739. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  22740. defined(HAVE_ED448_KEY_IMPORT)
  22741. /* test vectors from
  22742. https://tools.ietf.org/html/rfc8032
  22743. */
  22744. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  22745. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22746. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22747. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22748. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22749. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22750. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22751. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22752. 0x5b
  22753. };
  22754. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  22755. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  22756. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  22757. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  22758. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  22759. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  22760. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  22761. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  22762. 0x4e
  22763. };
  22764. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  22765. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  22766. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  22767. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  22768. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  22769. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  22770. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  22771. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  22772. 0x1b
  22773. };
  22774. /* uncompressed test */
  22775. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  22776. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22777. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22778. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22779. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22780. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22781. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22782. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22783. 0x5b
  22784. };
  22785. /* compressed prefix test */
  22786. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  22787. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  22788. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  22789. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  22790. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  22791. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  22792. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  22793. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  22794. 0x5b
  22795. };
  22796. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  22797. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  22798. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  22799. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  22800. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  22801. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  22802. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  22803. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  22804. 0xe8
  22805. };
  22806. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  22807. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  22808. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22809. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22810. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22811. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22812. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22813. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22814. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22815. 0x80
  22816. };
  22817. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  22818. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  22819. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  22820. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  22821. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  22822. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  22823. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  22824. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  22825. 0x80
  22826. };
  22827. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  22828. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  22829. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  22830. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  22831. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  22832. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  22833. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  22834. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  22835. 0x80
  22836. };
  22837. /* uncompressed test */
  22838. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  22839. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22840. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22841. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22842. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22843. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22844. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22845. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22846. 0x80
  22847. };
  22848. /* compressed prefix */
  22849. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  22850. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  22851. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  22852. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  22853. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  22854. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  22855. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  22856. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  22857. 0x80
  22858. };
  22859. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  22860. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  22861. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  22862. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  22863. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  22864. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  22865. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  22866. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  22867. 0x00
  22868. };
  22869. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  22870. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  22871. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  22872. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  22873. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22874. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22875. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22876. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22877. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22878. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22879. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22880. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22881. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22882. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22883. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22884. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22885. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22886. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22887. 0x26, 0x00
  22888. };
  22889. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  22890. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  22891. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  22892. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  22893. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  22894. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  22895. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  22896. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  22897. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  22898. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  22899. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  22900. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  22901. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  22902. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  22903. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  22904. 0x3a, 0x00
  22905. };
  22906. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  22907. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  22908. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  22909. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  22910. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  22911. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  22912. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  22913. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  22914. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  22915. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  22916. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  22917. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  22918. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  22919. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  22920. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  22921. 0x3c, 0x00
  22922. };
  22923. /* uncompressed test */
  22924. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  22925. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22926. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22927. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22928. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22929. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22930. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22931. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22932. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22933. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22934. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22935. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22936. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22937. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22938. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22939. 0x26, 0x00
  22940. };
  22941. /* compressed prefix */
  22942. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  22943. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  22944. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  22945. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  22946. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  22947. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  22948. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  22949. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  22950. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  22951. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  22952. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  22953. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  22954. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  22955. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  22956. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  22957. 0x26, 0x00
  22958. };
  22959. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  22960. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  22961. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  22962. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  22963. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  22964. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  22965. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  22966. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  22967. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  22968. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  22969. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  22970. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  22971. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  22972. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  22973. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  22974. 0x1a, 0x00
  22975. };
  22976. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  22977. #define SIGSZ sizeof(sig1)
  22978. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  22979. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  22980. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  22981. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  22982. /* test of a 1023 byte long message */
  22983. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  22984. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  22985. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  22986. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  22987. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  22988. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  22989. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  22990. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  22991. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  22992. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  22993. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  22994. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  22995. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  22996. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  22997. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  22998. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  22999. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  23000. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  23001. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  23002. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  23003. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  23004. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  23005. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  23006. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  23007. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  23008. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  23009. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  23010. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  23011. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  23012. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  23013. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  23014. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  23015. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  23016. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  23017. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  23018. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  23019. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  23020. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  23021. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  23022. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  23023. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  23024. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  23025. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  23026. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  23027. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  23028. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  23029. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  23030. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  23031. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  23032. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  23033. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  23034. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  23035. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  23036. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  23037. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  23038. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  23039. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  23040. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  23041. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  23042. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  23043. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  23044. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  23045. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  23046. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  23047. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  23048. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  23049. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  23050. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  23051. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  23052. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  23053. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  23054. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  23055. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  23056. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  23057. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  23058. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  23059. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  23060. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  23061. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  23062. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  23063. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  23064. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  23065. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  23066. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  23067. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  23068. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  23069. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  23070. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  23071. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  23072. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  23073. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  23074. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  23075. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  23076. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  23077. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  23078. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  23079. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  23080. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  23081. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  23082. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  23083. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  23084. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  23085. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  23086. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  23087. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  23088. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  23089. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  23090. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  23091. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  23092. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  23093. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  23094. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  23095. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  23096. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  23097. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  23098. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  23099. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  23100. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  23101. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  23102. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  23103. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  23104. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  23105. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  23106. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  23107. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  23108. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  23109. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  23110. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  23111. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  23112. };
  23113. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  23114. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  23115. sizeof(msg2),
  23116. sizeof(msg3),
  23117. 0 /*sizeof(msg1)*/,
  23118. 0 /*sizeof(msg1)*/,
  23119. sizeof(msg4)
  23120. };
  23121. #ifndef NO_ASN
  23122. static byte privateEd448[] = {
  23123. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  23124. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  23125. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  23126. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  23127. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  23128. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  23129. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  23130. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  23131. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  23132. 0x5b
  23133. };
  23134. static byte publicEd448[] = {
  23135. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  23136. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  23137. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  23138. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  23139. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  23140. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  23141. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  23142. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  23143. 0xaf, 0xe8, 0x25, 0x61, 0x80
  23144. };
  23145. static byte privPubEd448[] = {
  23146. 0x30, 0x81, 0x84, 0x02, 0x01, 0x00, 0x30, 0x05,
  23147. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  23148. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  23149. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  23150. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  23151. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  23152. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  23153. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  23154. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  23155. 0xf9, 0x5b, 0xa1, 0x3b, 0x04, 0x39, 0x5f, 0xd7,
  23156. 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  23157. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1,
  23158. 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e,
  23159. 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1,
  23160. 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6,
  23161. 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  23162. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  23163. };
  23164. word32 idx;
  23165. ed448_key key3;
  23166. #endif /* NO_ASN */
  23167. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23168. /* create ed448 keys */
  23169. #ifndef HAVE_FIPS
  23170. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23171. #else
  23172. ret = wc_InitRng(&rng);
  23173. #endif
  23174. if (ret != 0)
  23175. return -11700;
  23176. wc_ed448_init(&key);
  23177. wc_ed448_init(&key2);
  23178. #ifndef NO_ASN
  23179. wc_ed448_init(&key3);
  23180. #endif
  23181. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  23182. wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key2);
  23183. /* helper functions for signature and key size */
  23184. keySz = wc_ed448_size(&key);
  23185. sigSz = wc_ed448_sig_size(&key);
  23186. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  23187. defined(HAVE_ED448_KEY_IMPORT)
  23188. for (i = 0; i < 6; i++) {
  23189. outlen = sizeof(out);
  23190. XMEMSET(out, 0, sizeof(out));
  23191. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  23192. pKeySz[i], &key) != 0)
  23193. return -11701 - i;
  23194. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key, NULL,
  23195. 0) != 0) {
  23196. return -11711 - i;
  23197. }
  23198. if (XMEMCMP(out, sigs[i], 114))
  23199. return -11721 - i;
  23200. #if defined(HAVE_ED448_VERIFY)
  23201. /* test verify on good msg */
  23202. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  23203. NULL, 0) != 0 || verify != 1) {
  23204. return -11731 - i;
  23205. }
  23206. /* test verify on bad msg */
  23207. out[outlen-2] = out[outlen-2] + 1;
  23208. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key,
  23209. NULL, 0) == 0 || verify == 1) {
  23210. return -11741 - i;
  23211. }
  23212. #endif /* HAVE_ED448_VERIFY */
  23213. /* test api for import/exporting keys */
  23214. exportPSz = sizeof(exportPKey);
  23215. exportSSz = sizeof(exportSKey);
  23216. if (wc_ed448_export_public(&key, exportPKey, &exportPSz) != 0)
  23217. return -11751 - i;
  23218. if (wc_ed448_import_public(exportPKey, exportPSz, &key2) != 0)
  23219. return -11761 - i;
  23220. if (wc_ed448_export_private_only(&key, exportSKey, &exportSSz) != 0)
  23221. return -11771 - i;
  23222. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  23223. exportPKey, exportPSz, &key2) != 0)
  23224. return -11781 - i;
  23225. /* clear "out" buffer and test sign with imported keys */
  23226. outlen = sizeof(out);
  23227. XMEMSET(out, 0, sizeof(out));
  23228. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2, NULL,
  23229. 0) != 0) {
  23230. return -11791 - i;
  23231. }
  23232. #if defined(HAVE_ED448_VERIFY)
  23233. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, &key2,
  23234. NULL, 0) != 0 || verify != 1)
  23235. return -11801 - i;
  23236. if (XMEMCMP(out, sigs[i], SIGSZ))
  23237. return -11811 - i;
  23238. #endif /* HAVE_ED448_VERIFY */
  23239. }
  23240. ret = ed448_ctx_test();
  23241. if (ret != 0)
  23242. return ret;
  23243. ret = ed448ph_test();
  23244. if (ret != 0)
  23245. return ret;
  23246. #ifndef NO_ASN
  23247. /* Try ASN.1 encoded private-only key and public key. */
  23248. idx = 0;
  23249. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, &key3,
  23250. sizeof(privateEd448)) != 0)
  23251. return -11821 - i;
  23252. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0)
  23253. != BAD_FUNC_ARG)
  23254. return -11831 - i;
  23255. idx = 0;
  23256. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, &key3,
  23257. sizeof(publicEd448)) != 0)
  23258. return -11841 - i;
  23259. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  23260. return -11851 - i;
  23261. if (XMEMCMP(out, sigs[0], SIGSZ))
  23262. return -11861 - i;
  23263. #if defined(HAVE_ED448_VERIFY)
  23264. /* test verify on good msg */
  23265. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3,
  23266. NULL, 0) != 0 || verify != 1)
  23267. return -11871 - i;
  23268. #endif /* HAVE_ED448_VERIFY */
  23269. wc_ed448_free(&key3);
  23270. wc_ed448_init(&key3);
  23271. idx = 0;
  23272. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, &key3,
  23273. sizeof(privPubEd448)) != 0)
  23274. return -11881 - i;
  23275. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3, NULL, 0) != 0)
  23276. return -11891 - i;
  23277. if (XMEMCMP(out, sigs[0], SIGSZ))
  23278. return -11901 - i;
  23279. wc_ed448_free(&key3);
  23280. #endif /* NO_ASN */
  23281. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  23282. /* clean up keys when done */
  23283. wc_ed448_free(&key);
  23284. wc_ed448_free(&key2);
  23285. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  23286. wc_FreeRng(&rng);
  23287. #endif
  23288. /* hush warnings of unused keySz and sigSz */
  23289. (void)keySz;
  23290. (void)sigSz;
  23291. #ifdef WOLFSSL_TEST_CERT
  23292. ret = ed448_test_cert();
  23293. if (ret < 0)
  23294. return ret;
  23295. #ifdef WOLFSSL_CERT_GEN
  23296. ret = ed448_test_make_cert();
  23297. if (ret < 0)
  23298. return ret;
  23299. #endif /* WOLFSSL_CERT_GEN */
  23300. #endif /* WOLFSSL_TEST_CERT */
  23301. return 0;
  23302. }
  23303. #endif /* HAVE_ED448 */
  23304. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  23305. typedef struct CMAC_Test_Case {
  23306. int type;
  23307. int partial;
  23308. const byte* m;
  23309. word32 mSz;
  23310. const byte* k;
  23311. word32 kSz;
  23312. const byte* t;
  23313. word32 tSz;
  23314. } CMAC_Test_Case;
  23315. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  23316. {
  23317. #ifdef WOLFSSL_AES_128
  23318. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  23319. {
  23320. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  23321. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  23322. };
  23323. #define KLEN_128 (sizeof(k128))
  23324. #endif
  23325. #ifdef WOLFSSL_AES_192
  23326. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  23327. {
  23328. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  23329. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  23330. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  23331. };
  23332. #define KLEN_192 (sizeof(k192))
  23333. #endif
  23334. #ifdef WOLFSSL_AES_256
  23335. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  23336. {
  23337. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  23338. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  23339. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  23340. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  23341. };
  23342. #define KLEN_256 (sizeof(k256))
  23343. #endif
  23344. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  23345. {
  23346. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  23347. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  23348. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  23349. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  23350. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  23351. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  23352. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  23353. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  23354. };
  23355. #define MLEN_0 (0)
  23356. #define MLEN_128 (128/8)
  23357. #define MLEN_320 (320/8)
  23358. #define MLEN_319 (MLEN_320 - 1)
  23359. #define MLEN_512 (512/8)
  23360. #ifdef WOLFSSL_AES_128
  23361. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  23362. {
  23363. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  23364. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  23365. };
  23366. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  23367. {
  23368. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  23369. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  23370. };
  23371. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  23372. {
  23373. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  23374. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  23375. };
  23376. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  23377. {
  23378. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  23379. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  23380. };
  23381. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  23382. {
  23383. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  23384. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  23385. };
  23386. #endif
  23387. #ifdef WOLFSSL_AES_192
  23388. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  23389. {
  23390. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  23391. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  23392. };
  23393. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  23394. {
  23395. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  23396. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  23397. };
  23398. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  23399. {
  23400. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  23401. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  23402. };
  23403. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  23404. {
  23405. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  23406. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  23407. };
  23408. #endif
  23409. #ifdef WOLFSSL_AES_256
  23410. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  23411. {
  23412. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  23413. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  23414. };
  23415. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  23416. {
  23417. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  23418. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  23419. };
  23420. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  23421. {
  23422. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  23423. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  23424. };
  23425. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  23426. {
  23427. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  23428. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  23429. };
  23430. #endif
  23431. const CMAC_Test_Case testCases[] =
  23432. {
  23433. #ifdef WOLFSSL_AES_128
  23434. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  23435. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  23436. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  23437. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  23438. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  23439. #endif
  23440. #ifdef WOLFSSL_AES_192
  23441. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  23442. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  23443. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  23444. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  23445. #endif
  23446. #ifdef WOLFSSL_AES_256
  23447. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  23448. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  23449. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  23450. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  23451. #endif
  23452. #ifdef WOLFSSL_AES_128
  23453. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  23454. #endif
  23455. };
  23456. #ifdef WOLFSSL_SMALL_STACK
  23457. Cmac *cmac;
  23458. #else
  23459. Cmac cmac[1];
  23460. #endif
  23461. byte tag[AES_BLOCK_SIZE];
  23462. const CMAC_Test_Case* tc;
  23463. word32 i, tagSz;
  23464. int ret;
  23465. #ifdef WOLFSSL_SMALL_STACK
  23466. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  23467. ERROR_OUT(-12009, out);
  23468. #endif
  23469. for (i = 0, tc = testCases;
  23470. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  23471. i++, tc++) {
  23472. XMEMSET(tag, 0, sizeof(tag));
  23473. tagSz = AES_BLOCK_SIZE;
  23474. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  23475. ERROR_OUT(-12000, out);
  23476. if (tc->partial) {
  23477. if (wc_CmacUpdate(cmac, tc->m,
  23478. tc->mSz/2 - tc->partial) != 0)
  23479. ERROR_OUT(-12001, out);
  23480. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  23481. tc->mSz/2 + tc->partial) != 0)
  23482. ERROR_OUT(-12002, out);
  23483. }
  23484. else {
  23485. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  23486. ERROR_OUT(-12003, out);
  23487. }
  23488. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  23489. ERROR_OUT(-12004, out);
  23490. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  23491. ERROR_OUT(-12005, out);
  23492. XMEMSET(tag, 0, sizeof(tag));
  23493. tagSz = sizeof(tag);
  23494. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  23495. tc->k, tc->kSz) != 0)
  23496. ERROR_OUT(-12006, out);
  23497. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  23498. ERROR_OUT(-12007, out);
  23499. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  23500. tc->k, tc->kSz) != 0)
  23501. ERROR_OUT(-12008, out);
  23502. }
  23503. ret = 0;
  23504. out:
  23505. #ifdef WOLFSSL_SMALL_STACK
  23506. if (cmac)
  23507. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  23508. #endif
  23509. return ret;
  23510. }
  23511. #endif /* NO_AES && WOLFSSL_CMAC */
  23512. #ifdef HAVE_LIBZ
  23513. static const byte sample_text[] =
  23514. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  23515. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  23516. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  23517. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  23518. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  23519. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  23520. "\n"
  23521. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  23522. "four loko you probably haven't heard of them high life. Messenger bag\n"
  23523. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  23524. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  23525. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  23526. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  23527. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  23528. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  23529. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  23530. "four loko whatever street art yr farm-to-table.\n"
  23531. "\n"
  23532. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  23533. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  23534. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  23535. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  23536. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  23537. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  23538. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  23539. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  23540. "locavore.\n"
  23541. "\n"
  23542. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  23543. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  23544. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  23545. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  23546. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  23547. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  23548. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  23549. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  23550. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  23551. "neutra PBR selvage.\n"
  23552. "\n"
  23553. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  23554. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  23555. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  23556. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  23557. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  23558. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  23559. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  23560. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  23561. "mollit magna, sriracha sartorial helvetica.\n"
  23562. "\n"
  23563. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  23564. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  23565. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  23566. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  23567. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  23568. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  23569. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  23570. "\n"
  23571. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  23572. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  23573. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  23574. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  23575. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  23576. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  23577. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  23578. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  23579. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  23580. "\n"
  23581. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  23582. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  23583. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  23584. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  23585. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  23586. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  23587. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  23588. "bag dolor terry richardson sapiente.\n";
  23589. static const byte sample_text_gz[] = {
  23590. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  23591. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  23592. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  23593. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  23594. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  23595. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  23596. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  23597. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  23598. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  23599. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  23600. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  23601. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  23602. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  23603. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  23604. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  23605. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  23606. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  23607. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  23608. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  23609. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  23610. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  23611. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  23612. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  23613. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  23614. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  23615. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  23616. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  23617. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  23618. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  23619. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  23620. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  23621. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  23622. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  23623. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  23624. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  23625. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  23626. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  23627. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  23628. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  23629. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  23630. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  23631. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  23632. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  23633. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  23634. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  23635. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  23636. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  23637. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  23638. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  23639. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  23640. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  23641. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  23642. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  23643. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  23644. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  23645. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  23646. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  23647. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  23648. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  23649. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  23650. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  23651. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  23652. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  23653. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  23654. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  23655. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  23656. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  23657. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  23658. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  23659. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  23660. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  23661. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  23662. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  23663. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  23664. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  23665. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  23666. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  23667. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  23668. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  23669. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  23670. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  23671. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  23672. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  23673. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  23674. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  23675. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  23676. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  23677. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  23678. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  23679. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  23680. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  23681. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  23682. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  23683. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  23684. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  23685. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  23686. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  23687. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  23688. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  23689. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  23690. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  23691. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  23692. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  23693. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  23694. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  23695. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  23696. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  23697. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  23698. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  23699. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  23700. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  23701. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  23702. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  23703. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  23704. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  23705. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  23706. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  23707. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  23708. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  23709. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  23710. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  23711. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  23712. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  23713. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  23714. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  23715. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  23716. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  23717. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  23718. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  23719. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  23720. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  23721. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  23722. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  23723. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  23724. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  23725. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  23726. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  23727. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  23728. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  23729. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  23730. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  23731. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  23732. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  23733. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  23734. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  23735. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  23736. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  23737. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  23738. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  23739. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  23740. };
  23741. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  23742. {
  23743. int ret = 0;
  23744. word32 dSz = sizeof(sample_text);
  23745. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  23746. byte *c;
  23747. byte *d;
  23748. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23749. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23750. if (c == NULL || d == NULL) {
  23751. ERROR_OUT(-12100, exit);
  23752. }
  23753. /* follow calloc and initialize to 0 */
  23754. XMEMSET(c, 0, cSz);
  23755. XMEMSET(d, 0, dSz);
  23756. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  23757. ERROR_OUT(-12101, exit);
  23758. }
  23759. cSz = (word32)ret;
  23760. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  23761. ERROR_OUT(-12102, exit);
  23762. }
  23763. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23764. ERROR_OUT(-12103, exit);
  23765. }
  23766. /* GZIP tests */
  23767. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  23768. XMEMSET(c, 0, cSz);
  23769. XMEMSET(d, 0, dSz);
  23770. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  23771. if (ret < 0) {
  23772. ERROR_OUT(-12104, exit);
  23773. }
  23774. cSz = (word32)ret;
  23775. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  23776. if (ret < 0) {
  23777. ERROR_OUT(-12105, exit);
  23778. }
  23779. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23780. ERROR_OUT(-12106, exit);
  23781. }
  23782. /* Try with gzip generated output */
  23783. XMEMSET(d, 0, dSz);
  23784. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  23785. LIBZ_WINBITS_GZIP);
  23786. if (ret < 0) {
  23787. ERROR_OUT(-12107, exit);
  23788. }
  23789. dSz = (word32)ret;
  23790. if (XMEMCMP(d, sample_text, dSz) != 0) {
  23791. ERROR_OUT(-12108, exit);
  23792. }
  23793. ret = 0; /* success */
  23794. exit:
  23795. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23796. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23797. return ret;
  23798. }
  23799. #endif /* HAVE_LIBZ */
  23800. #ifdef HAVE_PKCS7
  23801. /* External Debugging/Testing Note:
  23802. *
  23803. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  23804. * additional testing. To dump bundles to files DER encoded files, please
  23805. * define:
  23806. *
  23807. * #define PKCS7_OUTPUT_TEST_BUNDLES
  23808. */
  23809. /* Loads certs and keys for use with PKCS7 tests, from either files
  23810. * or buffers.
  23811. *
  23812. * rsaClientCertBuf - output buffer for RSA client cert
  23813. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  23814. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  23815. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  23816. *
  23817. * rsaServerCertBuf - output buffer for RSA server cert
  23818. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  23819. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  23820. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  23821. *
  23822. * rsaCaCertBuf - output buffer for RSA CA cert
  23823. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  23824. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  23825. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  23826. *
  23827. * eccClientCertBuf - output buffer for ECC cert
  23828. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  23829. * eccClientPrivKeyBuf - output buffer for ECC private key
  23830. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  23831. *
  23832. * Returns 0 on success, negative on error
  23833. */
  23834. static int pkcs7_load_certs_keys(
  23835. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  23836. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  23837. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  23838. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  23839. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  23840. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  23841. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  23842. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  23843. {
  23844. #ifndef NO_FILESYSTEM
  23845. XFILE certFile;
  23846. XFILE keyFile;
  23847. (void)certFile;
  23848. (void)keyFile;
  23849. #endif
  23850. #ifndef NO_RSA
  23851. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  23852. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  23853. return BAD_FUNC_ARG;
  23854. #endif
  23855. #ifdef HAVE_ECC
  23856. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  23857. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  23858. return BAD_FUNC_ARG;
  23859. #endif
  23860. /* RSA */
  23861. #ifndef NO_RSA
  23862. #ifdef USE_CERT_BUFFERS_1024
  23863. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  23864. return -12110;
  23865. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  23866. sizeof_client_cert_der_1024);
  23867. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  23868. if (rsaServerCertBuf != NULL) {
  23869. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  23870. return -12111;
  23871. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  23872. sizeof_server_cert_der_1024);
  23873. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  23874. }
  23875. if (rsaCaCertBuf != NULL) {
  23876. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  23877. return -12112;
  23878. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  23879. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  23880. }
  23881. #elif defined(USE_CERT_BUFFERS_2048)
  23882. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  23883. return -12113;
  23884. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  23885. sizeof_client_cert_der_2048);
  23886. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  23887. if (rsaServerCertBuf != NULL) {
  23888. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  23889. return -12114;
  23890. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  23891. sizeof_server_cert_der_2048);
  23892. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  23893. }
  23894. if (rsaCaCertBuf != NULL) {
  23895. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  23896. return -12115;
  23897. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  23898. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  23899. }
  23900. #else
  23901. certFile = XFOPEN(clientCert, "rb");
  23902. if (!certFile)
  23903. return -12116;
  23904. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  23905. *rsaClientCertBufSz, certFile);
  23906. XFCLOSE(certFile);
  23907. if (rsaServerCertBuf != NULL) {
  23908. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  23909. if (!certFile)
  23910. return -12117;
  23911. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  23912. *rsaServerCertBufSz, certFile);
  23913. XFCLOSE(certFile);
  23914. }
  23915. if (rsaCaCertBuf != NULL) {
  23916. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  23917. if (!certFile)
  23918. return -12118;
  23919. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  23920. certFile);
  23921. XFCLOSE(certFile);
  23922. }
  23923. #endif
  23924. #ifdef USE_CERT_BUFFERS_1024
  23925. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  23926. return -12119;
  23927. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  23928. sizeof_client_key_der_1024);
  23929. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  23930. if (rsaServerPrivKeyBuf != NULL) {
  23931. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  23932. return -12120;
  23933. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  23934. sizeof_server_key_der_1024);
  23935. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  23936. }
  23937. if (rsaCaPrivKeyBuf != NULL) {
  23938. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  23939. return -12121;
  23940. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  23941. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  23942. }
  23943. #elif defined(USE_CERT_BUFFERS_2048)
  23944. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  23945. return -12122;
  23946. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  23947. sizeof_client_key_der_2048);
  23948. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  23949. if (rsaServerPrivKeyBuf != NULL) {
  23950. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  23951. return -12123;
  23952. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  23953. sizeof_server_key_der_2048);
  23954. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  23955. }
  23956. if (rsaCaPrivKeyBuf != NULL) {
  23957. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  23958. return -12124;
  23959. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  23960. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  23961. }
  23962. #else
  23963. keyFile = XFOPEN(clientKey, "rb");
  23964. if (!keyFile)
  23965. return -12125;
  23966. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  23967. *rsaClientPrivKeyBufSz, keyFile);
  23968. XFCLOSE(keyFile);
  23969. if (rsaServerPrivKeyBuf != NULL) {
  23970. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  23971. if (!keyFile)
  23972. return -12126;
  23973. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  23974. *rsaServerPrivKeyBufSz, keyFile);
  23975. XFCLOSE(keyFile);
  23976. }
  23977. if (rsaCaPrivKeyBuf != NULL) {
  23978. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  23979. if (!keyFile)
  23980. return -12127;
  23981. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  23982. *rsaCaPrivKeyBufSz, keyFile);
  23983. XFCLOSE(keyFile);
  23984. }
  23985. #endif /* USE_CERT_BUFFERS */
  23986. #endif /* NO_RSA */
  23987. /* ECC */
  23988. #ifdef HAVE_ECC
  23989. #ifdef USE_CERT_BUFFERS_256
  23990. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  23991. return -12128;
  23992. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23993. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  23994. #else
  23995. certFile = XFOPEN(eccClientCert, "rb");
  23996. if (!certFile)
  23997. return -12129;
  23998. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  23999. *eccClientCertBufSz, certFile);
  24000. XFCLOSE(certFile);
  24001. #endif /* USE_CERT_BUFFERS_256 */
  24002. #ifdef USE_CERT_BUFFERS_256
  24003. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  24004. return -12130;
  24005. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24006. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  24007. #else
  24008. keyFile = XFOPEN(eccClientKey, "rb");
  24009. if (!keyFile)
  24010. return -12131;
  24011. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  24012. *eccClientPrivKeyBufSz, keyFile);
  24013. XFCLOSE(keyFile);
  24014. #endif /* USE_CERT_BUFFERS_256 */
  24015. #endif /* HAVE_ECC */
  24016. #ifdef NO_RSA
  24017. (void)rsaClientCertBuf;
  24018. (void)rsaClientCertBufSz;
  24019. (void)rsaClientPrivKeyBuf;
  24020. (void)rsaClientPrivKeyBufSz;
  24021. (void)rsaServerCertBuf;
  24022. (void)rsaServerCertBufSz;
  24023. (void)rsaServerPrivKeyBuf;
  24024. (void)rsaServerPrivKeyBufSz;
  24025. (void)rsaCaCertBuf;
  24026. (void)rsaCaCertBufSz;
  24027. (void)rsaCaPrivKeyBuf;
  24028. (void)rsaCaPrivKeyBufSz;
  24029. #endif
  24030. #ifndef HAVE_ECC
  24031. (void)eccClientCertBuf;
  24032. (void)eccClientCertBufSz;
  24033. (void)eccClientPrivKeyBuf;
  24034. (void)eccClientPrivKeyBufSz;
  24035. #endif
  24036. #ifndef NO_FILESYSTEM
  24037. (void)certFile;
  24038. (void)keyFile;
  24039. #endif
  24040. return 0;
  24041. }
  24042. typedef struct {
  24043. const byte* content;
  24044. word32 contentSz;
  24045. int contentOID;
  24046. int encryptOID;
  24047. int keyWrapOID;
  24048. int keyAgreeOID;
  24049. byte* cert;
  24050. size_t certSz;
  24051. byte* privateKey;
  24052. word32 privateKeySz;
  24053. byte* optionalUkm;
  24054. word32 optionalUkmSz;
  24055. int ktriOptions; /* KTRI options flags */
  24056. int kariOptions; /* KARI options flags */
  24057. /* KEKRI specific */
  24058. byte* secretKey; /* key, only for kekri RecipientInfo types */
  24059. word32 secretKeySz; /* size of secretKey, bytes */
  24060. byte* secretKeyId; /* key identifier */
  24061. word32 secretKeyIdSz; /* size of key identifier, bytes */
  24062. void* timePtr; /* time_t pointer */
  24063. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  24064. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  24065. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  24066. word32 otherAttrSz; /* size of otherAttr, bytes */
  24067. int kekriOptions; /* KEKRI options flags */
  24068. /* PWRI specific */
  24069. char* password;
  24070. word32 passwordSz;
  24071. byte* salt;
  24072. word32 saltSz;
  24073. int kdfOID;
  24074. int hashOID;
  24075. int kdfIterations;
  24076. int pwriOptions; /* PWRI options flags */
  24077. /* ORI specific */
  24078. int isOri;
  24079. int oriOptions; /* ORI options flags */
  24080. const char* outFileName;
  24081. } pkcs7EnvelopedVector;
  24082. static const byte asnDataOid[] = {
  24083. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  24084. };
  24085. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  24086. * and giving wolfCrypt the value for oriOID and oriValue to place in
  24087. * OtherRecipientInfo.
  24088. *
  24089. * Returns 0 on success, negative upon error. */
  24090. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  24091. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  24092. void* ctx)
  24093. {
  24094. int i;
  24095. /* make sure buffers are large enough */
  24096. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  24097. return -12140;
  24098. /* our simple encryption algorithm will be take the bitwise complement */
  24099. oriValue[0] = 0x04; /*ASN OCTET STRING */
  24100. oriValue[1] = (byte)cekSz; /* length */
  24101. for (i = 0; i < (int)cekSz; i++) {
  24102. oriValue[2 + i] = ~cek[i];
  24103. }
  24104. *oriValueSz = 2 + cekSz;
  24105. /* set oriType to ASN.1 encoded data OID */
  24106. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  24107. *oriTypeSz = sizeof(asnDataOid);
  24108. (void)pkcs7;
  24109. (void)ctx;
  24110. return 0;
  24111. }
  24112. /* ORI decrypt callback, responsible for providing a decrypted content
  24113. * encryption key (CEK) placed into decryptedKey and size placed into
  24114. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  24115. * in decrypting the encrypted CEK.
  24116. *
  24117. * Returns 0 on success, negative upon error. */
  24118. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  24119. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  24120. word32* decryptedKeySz, void* ctx)
  24121. {
  24122. int i;
  24123. /* make sure oriType matches what we expect */
  24124. if (oriTypeSz != sizeof(asnDataOid))
  24125. return -12150;
  24126. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  24127. return -12151;
  24128. /* make sure decrypted buffer is large enough */
  24129. if (*decryptedKeySz < oriValueSz)
  24130. return -12152;
  24131. /* decrypt encrypted CEK using simple bitwise complement,
  24132. only for example */
  24133. for (i = 0; i < (int)oriValueSz - 2; i++) {
  24134. decryptedKey[i] = ~oriValue[2 + i];
  24135. }
  24136. *decryptedKeySz = oriValueSz - 2;
  24137. (void)pkcs7;
  24138. (void)ctx;
  24139. return 0;
  24140. }
  24141. #ifndef NO_AES
  24142. /* returns 0 on success */
  24143. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24144. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24145. byte* in, int inSz, byte* out, void* usrCtx)
  24146. {
  24147. int keyId = -1, ret, keySz;
  24148. word32 keyIdSz = 8;
  24149. const byte* key;
  24150. byte keyIdRaw[8];
  24151. #ifdef WOLFSSL_SMALL_STACK
  24152. Aes *aes;
  24153. #else
  24154. Aes aes[1];
  24155. #endif
  24156. /* looking for KEY ID
  24157. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  24158. */
  24159. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  24160. /* 0x06, 0x0B do not pass in tag and length */
  24161. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24162. 0x01, 0x09, 0x10, 0x02, 0x25
  24163. };
  24164. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  24165. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24166. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24167. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24168. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24169. };
  24170. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  24171. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24172. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24173. };
  24174. /* test user context passed in */
  24175. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  24176. return -12160;
  24177. }
  24178. #ifdef WOLFSSL_SMALL_STACK
  24179. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  24180. return -12164;
  24181. #endif
  24182. /* if needing to find keyIdSz can call with NULL */
  24183. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  24184. &keyIdSz);
  24185. if (ret != LENGTH_ONLY_E) {
  24186. printf("Unexpected error %d when getting keyIdSz\n", ret);
  24187. printf("Possibly no KEY ID attribute set\n");
  24188. ERROR_OUT(-12161, out);
  24189. }
  24190. else {
  24191. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  24192. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  24193. &keyIdSz);
  24194. if (ret < 0) {
  24195. goto out;
  24196. }
  24197. if (keyIdSz < 3) {
  24198. printf("keyIdSz is smaller than expected\n");
  24199. ERROR_OUT(-12162, out);
  24200. }
  24201. if (keyIdSz > 2 + sizeof(int)) {
  24202. printf("example case was only expecting a keyId of int size\n");
  24203. ERROR_OUT(-12163, out);
  24204. }
  24205. /* keyIdRaw[0] OCTET TAG */
  24206. /* keyIdRaw[1] Length */
  24207. #ifdef BIG_ENDIAN_ORDER
  24208. if (keyIdRaw[1] == 0x01) {
  24209. keyId = 1;
  24210. }
  24211. #else
  24212. keyId = *(int*)(keyIdRaw + 2);
  24213. #endif
  24214. }
  24215. /* Use keyID here if found to select key and decrypt in HSM or in this
  24216. * example just select key and do software decryption */
  24217. if (keyId == 1) {
  24218. key = altKey;
  24219. keySz = sizeof(altKey);
  24220. }
  24221. else {
  24222. key = defKey;
  24223. keySz = sizeof(defKey);
  24224. }
  24225. switch (encryptOID) {
  24226. case AES256CBCb:
  24227. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  24228. ERROR_OUT(BAD_FUNC_ARG, out);
  24229. break;
  24230. case AES128CBCb:
  24231. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  24232. ERROR_OUT(BAD_FUNC_ARG, out);
  24233. break;
  24234. default:
  24235. printf("Unsupported content cipher type for example");
  24236. ERROR_OUT(ALGO_ID_E, out);
  24237. };
  24238. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  24239. if (ret == 0) {
  24240. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  24241. if (ret == 0)
  24242. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  24243. wc_AesFree(aes);
  24244. }
  24245. out:
  24246. #ifdef WOLFSSL_SMALL_STACK
  24247. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24248. #endif
  24249. (void)aad;
  24250. (void)aadSz;
  24251. (void)authTag;
  24252. (void)authTagSz;
  24253. return ret;
  24254. }
  24255. #endif /* NO_AES */
  24256. #define PKCS7_BUF_SIZE 2048
  24257. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  24258. byte* rsaPrivKey, word32 rsaPrivKeySz,
  24259. byte* eccCert, word32 eccCertSz,
  24260. byte* eccPrivKey, word32 eccPrivKeySz)
  24261. {
  24262. int ret = 0, testSz, i;
  24263. int envelopedSz, decodedSz;
  24264. byte *enveloped;
  24265. byte *decoded;
  24266. PKCS7* pkcs7;
  24267. #ifdef ECC_TIMING_RESISTANT
  24268. WC_RNG rng;
  24269. #endif
  24270. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24271. XFILE pkcs7File;
  24272. #endif
  24273. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  24274. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24275. 0x72,0x6c,0x64
  24276. };
  24277. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  24278. defined(WOLFSSL_SHA512)
  24279. byte optionalUkm[] = {
  24280. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24281. };
  24282. #endif /* NO_AES */
  24283. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24284. /* encryption key for kekri recipient types */
  24285. byte secretKey[] = {
  24286. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  24287. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24288. };
  24289. /* encryption key identifier */
  24290. byte secretKeyId[] = {
  24291. 0x02,0x02,0x03,0x04
  24292. };
  24293. #endif
  24294. #if !defined(NO_PWDBASED) && !defined(NO_AES) && \
  24295. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24296. #ifndef HAVE_FIPS
  24297. char password[] = "password"; /* NOTE: Password is too short for FIPS */
  24298. #else
  24299. char password[] = "passwordFIPS_MODE";
  24300. #endif
  24301. byte salt[] = {
  24302. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  24303. };
  24304. #endif
  24305. const pkcs7EnvelopedVector testVectors[] =
  24306. {
  24307. /* key transport key encryption technique */
  24308. #ifndef NO_RSA
  24309. #ifndef NO_DES3
  24310. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  24311. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24312. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24313. "pkcs7envelopedDataDES3.der"},
  24314. #endif
  24315. #ifndef NO_AES
  24316. #ifdef WOLFSSL_AES_128
  24317. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  24318. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24319. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24320. "pkcs7envelopedDataAES128CBC.der"},
  24321. #endif
  24322. #ifdef WOLFSSL_AES_192
  24323. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  24324. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24325. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24326. "pkcs7envelopedDataAES192CBC.der"},
  24327. #endif
  24328. #ifdef WOLFSSL_AES_256
  24329. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24330. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  24331. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24332. "pkcs7envelopedDataAES256CBC.der"},
  24333. /* explicitly using SKID for SubjectKeyIdentifier */
  24334. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24335. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  24336. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24337. "pkcs7envelopedDataAES256CBC_SKID.der"},
  24338. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  24339. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  24340. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  24341. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  24342. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der"},
  24343. #endif
  24344. #endif /* NO_AES */
  24345. #endif
  24346. /* key agreement key encryption technique*/
  24347. #ifdef HAVE_ECC
  24348. #ifndef NO_AES
  24349. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24350. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  24351. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24352. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24353. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24354. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  24355. #endif
  24356. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  24357. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24358. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24359. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24360. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24361. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  24362. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  24363. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  24364. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24365. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24366. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  24367. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24368. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  24369. /* with optional user keying material (ukm) */
  24370. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  24371. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24372. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  24373. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24374. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  24375. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  24376. #endif /* NO_AES */
  24377. #endif
  24378. /* kekri (KEKRecipientInfo) recipient types */
  24379. #ifndef NO_AES
  24380. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24381. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  24382. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  24383. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  24384. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  24385. "pkcs7envelopedDataAES128CBC_KEKRI.der"},
  24386. #endif
  24387. #endif
  24388. /* pwri (PasswordRecipientInfo) recipient types */
  24389. #if !defined(NO_PWDBASED) && !defined(NO_AES)
  24390. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24391. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  24392. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24393. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  24394. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  24395. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der"},
  24396. #endif
  24397. #endif
  24398. #if !defined(NO_AES) && !defined(NO_AES_128)
  24399. /* ori (OtherRecipientInfo) recipient types */
  24400. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  24401. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  24402. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der"},
  24403. #endif
  24404. };
  24405. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24406. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24407. if ((! enveloped) || (! decoded)) {
  24408. ERROR_OUT(-12170, out);
  24409. }
  24410. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  24411. #ifdef ECC_TIMING_RESISTANT
  24412. #ifndef HAVE_FIPS
  24413. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24414. #else
  24415. ret = wc_InitRng(&rng);
  24416. #endif
  24417. if (ret != 0) {
  24418. ERROR_OUT(-12171, out);
  24419. }
  24420. #endif
  24421. for (i = 0; i < testSz; i++) {
  24422. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  24423. #ifdef WOLFSSL_ASYNC_CRYPT
  24424. INVALID_DEVID /* async PKCS7 is not supported */
  24425. #else
  24426. devId
  24427. #endif
  24428. );
  24429. if (pkcs7 == NULL) {
  24430. ERROR_OUT(-12172, out);
  24431. }
  24432. if (testVectors[i].secretKey != NULL) {
  24433. /* KEKRI recipient type */
  24434. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24435. if (ret != 0) {
  24436. ERROR_OUT(-12173, out);
  24437. }
  24438. pkcs7->content = (byte*)testVectors[i].content;
  24439. pkcs7->contentSz = testVectors[i].contentSz;
  24440. pkcs7->contentOID = testVectors[i].contentOID;
  24441. pkcs7->encryptOID = testVectors[i].encryptOID;
  24442. pkcs7->ukm = testVectors[i].optionalUkm;
  24443. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24444. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  24445. testVectors[i].secretKey, testVectors[i].secretKeySz,
  24446. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  24447. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  24448. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  24449. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  24450. if (ret < 0) {
  24451. wc_PKCS7_Free(pkcs7);
  24452. ERROR_OUT(-12174, out);
  24453. }
  24454. /* set key, for decryption */
  24455. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  24456. testVectors[i].secretKeySz);
  24457. if (ret != 0) {
  24458. wc_PKCS7_Free(pkcs7);
  24459. ERROR_OUT(-12175, out);
  24460. }
  24461. } else if (testVectors[i].password != NULL) {
  24462. #ifndef NO_PWDBASED
  24463. /* PWRI recipient type */
  24464. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24465. if (ret != 0) {
  24466. ERROR_OUT(-12176, out);
  24467. }
  24468. pkcs7->content = (byte*)testVectors[i].content;
  24469. pkcs7->contentSz = testVectors[i].contentSz;
  24470. pkcs7->contentOID = testVectors[i].contentOID;
  24471. pkcs7->encryptOID = testVectors[i].encryptOID;
  24472. pkcs7->ukm = testVectors[i].optionalUkm;
  24473. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24474. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  24475. (byte*)testVectors[i].password,
  24476. testVectors[i].passwordSz, testVectors[i].salt,
  24477. testVectors[i].saltSz, testVectors[i].kdfOID,
  24478. testVectors[i].hashOID, testVectors[i].kdfIterations,
  24479. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  24480. if (ret < 0) {
  24481. wc_PKCS7_Free(pkcs7);
  24482. ERROR_OUT(-12177, out);
  24483. }
  24484. /* set password, for decryption */
  24485. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  24486. testVectors[i].passwordSz);
  24487. if (ret < 0) {
  24488. wc_PKCS7_Free(pkcs7);
  24489. ERROR_OUT(-12178, out);
  24490. }
  24491. #endif /* NO_PWDBASED */
  24492. } else if (testVectors[i].isOri == 1) {
  24493. /* ORI recipient type */
  24494. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24495. if (ret != 0) {
  24496. ERROR_OUT(-12179, out);
  24497. }
  24498. pkcs7->content = (byte*)testVectors[i].content;
  24499. pkcs7->contentSz = testVectors[i].contentSz;
  24500. pkcs7->contentOID = testVectors[i].contentOID;
  24501. pkcs7->encryptOID = testVectors[i].encryptOID;
  24502. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  24503. testVectors[i].oriOptions);
  24504. if (ret < 0) {
  24505. wc_PKCS7_Free(pkcs7);
  24506. ERROR_OUT(-12180, out);
  24507. }
  24508. /* set decrypt callback for decryption */
  24509. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  24510. if (ret < 0) {
  24511. wc_PKCS7_Free(pkcs7);
  24512. ERROR_OUT(-12181, out);
  24513. }
  24514. } else {
  24515. /* KTRI or KARI recipient types */
  24516. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  24517. if (ret != 0) {
  24518. ERROR_OUT(-12182, out);
  24519. }
  24520. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  24521. (word32)testVectors[i].certSz);
  24522. if (ret != 0) {
  24523. wc_PKCS7_Free(pkcs7);
  24524. ERROR_OUT(-12183, out);
  24525. }
  24526. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  24527. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  24528. pkcs7->privateKey = testVectors[i].privateKey;
  24529. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  24530. pkcs7->content = (byte*)testVectors[i].content;
  24531. pkcs7->contentSz = testVectors[i].contentSz;
  24532. pkcs7->contentOID = testVectors[i].contentOID;
  24533. pkcs7->encryptOID = testVectors[i].encryptOID;
  24534. pkcs7->ukm = testVectors[i].optionalUkm;
  24535. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  24536. /* set SubjectIdentifier type for KTRI types */
  24537. if (testVectors[i].ktriOptions & CMS_SKID) {
  24538. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  24539. if (ret != 0) {
  24540. wc_PKCS7_Free(pkcs7);
  24541. ERROR_OUT(-12184, out);
  24542. }
  24543. } else if (testVectors[i].ktriOptions &
  24544. CMS_ISSUER_AND_SERIAL_NUMBER) {
  24545. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  24546. CMS_ISSUER_AND_SERIAL_NUMBER);
  24547. if (ret != 0) {
  24548. wc_PKCS7_Free(pkcs7);
  24549. ERROR_OUT(-12185, out);
  24550. }
  24551. }
  24552. }
  24553. #ifdef ECC_TIMING_RESISTANT
  24554. pkcs7->rng = &rng;
  24555. #endif
  24556. /* encode envelopedData */
  24557. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  24558. PKCS7_BUF_SIZE);
  24559. if (envelopedSz <= 0) {
  24560. wc_PKCS7_Free(pkcs7);
  24561. ERROR_OUT(-12186, out);
  24562. }
  24563. /* decode envelopedData */
  24564. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  24565. decoded, PKCS7_BUF_SIZE);
  24566. if (decodedSz <= 0) {
  24567. wc_PKCS7_Free(pkcs7);
  24568. ERROR_OUT(-12187, out);
  24569. }
  24570. /* test decode result */
  24571. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  24572. wc_PKCS7_Free(pkcs7);
  24573. ERROR_OUT(-12188, out);
  24574. }
  24575. #ifndef NO_PKCS7_STREAM
  24576. { /* test reading byte by byte */
  24577. int z;
  24578. for (z = 0; z < envelopedSz; z++) {
  24579. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  24580. decoded, PKCS7_BUF_SIZE);
  24581. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  24582. printf("unexpected error %d\n", decodedSz);
  24583. ERROR_OUT(-12189, out);
  24584. }
  24585. }
  24586. /* test decode result */
  24587. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  24588. printf("stream read compare failed\n");
  24589. wc_PKCS7_Free(pkcs7);
  24590. ERROR_OUT(-12190, out);
  24591. }
  24592. }
  24593. #endif
  24594. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24595. /* output pkcs7 envelopedData for external testing */
  24596. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  24597. if (!pkcs7File) {
  24598. wc_PKCS7_Free(pkcs7);
  24599. ERROR_OUT(-12191, out);
  24600. }
  24601. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  24602. XFCLOSE(pkcs7File);
  24603. if (ret != envelopedSz) {
  24604. wc_PKCS7_Free(pkcs7);
  24605. ERROR_OUT(-12192, out);
  24606. } else {
  24607. /* reset ret to 0 for success */
  24608. ret = 0;
  24609. }
  24610. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  24611. wc_PKCS7_Free(pkcs7);
  24612. pkcs7 = NULL;
  24613. }
  24614. #ifdef ECC_TIMING_RESISTANT
  24615. wc_FreeRng(&rng);
  24616. #endif
  24617. (void)eccCert;
  24618. (void)eccCertSz;
  24619. (void)eccPrivKey;
  24620. (void)eccPrivKeySz;
  24621. (void)rsaCert;
  24622. (void)rsaCertSz;
  24623. (void)rsaPrivKey;
  24624. (void)rsaPrivKeySz;
  24625. out:
  24626. if (enveloped)
  24627. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24628. if (decoded)
  24629. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24630. return ret;
  24631. }
  24632. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  24633. {
  24634. int ret = 0;
  24635. byte* rsaCert = NULL;
  24636. byte* rsaPrivKey = NULL;
  24637. word32 rsaCertSz = 0;
  24638. word32 rsaPrivKeySz = 0;
  24639. byte* eccCert = NULL;
  24640. byte* eccPrivKey = NULL;
  24641. word32 eccCertSz = 0;
  24642. word32 eccPrivKeySz = 0;
  24643. #ifndef NO_RSA
  24644. /* read client RSA cert and key in DER format */
  24645. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24646. if (rsaCert == NULL)
  24647. return -12200;
  24648. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24649. if (rsaPrivKey == NULL) {
  24650. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24651. return -12201;
  24652. }
  24653. rsaCertSz = FOURK_BUF;
  24654. rsaPrivKeySz = FOURK_BUF;
  24655. #endif /* NO_RSA */
  24656. #ifdef HAVE_ECC
  24657. /* read client ECC cert and key in DER format */
  24658. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24659. if (eccCert == NULL) {
  24660. #ifndef NO_RSA
  24661. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24662. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24663. #endif
  24664. return -12202;
  24665. }
  24666. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24667. if (eccPrivKey == NULL) {
  24668. #ifndef NO_RSA
  24669. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24670. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24671. #endif
  24672. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24673. return -12203;
  24674. }
  24675. eccCertSz = FOURK_BUF;
  24676. eccPrivKeySz = FOURK_BUF;
  24677. #endif /* HAVE_ECC */
  24678. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  24679. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  24680. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  24681. eccPrivKey, &eccPrivKeySz);
  24682. if (ret < 0) {
  24683. #ifndef NO_RSA
  24684. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24685. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24686. #endif
  24687. #ifdef HAVE_ECC
  24688. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24689. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24690. #endif
  24691. return -12204;
  24692. }
  24693. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  24694. rsaPrivKey, (word32)rsaPrivKeySz,
  24695. eccCert, (word32)eccCertSz,
  24696. eccPrivKey, (word32)eccPrivKeySz);
  24697. #ifndef NO_RSA
  24698. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24699. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24700. #endif
  24701. #ifdef HAVE_ECC
  24702. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24703. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24704. #endif
  24705. return ret;
  24706. }
  24707. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  24708. typedef struct {
  24709. const byte* content;
  24710. word32 contentSz;
  24711. int contentOID;
  24712. int encryptOID;
  24713. int keyWrapOID;
  24714. int keyAgreeOID;
  24715. byte* cert;
  24716. size_t certSz;
  24717. byte* privateKey;
  24718. word32 privateKeySz;
  24719. PKCS7Attrib* authAttribs;
  24720. word32 authAttribsSz;
  24721. PKCS7Attrib* unauthAttribs;
  24722. word32 unauthAttribsSz;
  24723. /* KARI / KTRI specific */
  24724. byte* optionalUkm;
  24725. word32 optionalUkmSz;
  24726. int ktriOptions; /* KTRI options flags */
  24727. int kariOptions; /* KARI options flags */
  24728. /* KEKRI specific */
  24729. byte* secretKey; /* key, only for kekri RecipientInfo types */
  24730. word32 secretKeySz; /* size of secretKey, bytes */
  24731. byte* secretKeyId; /* key identifier */
  24732. word32 secretKeyIdSz; /* size of key identifier, bytes */
  24733. void* timePtr; /* time_t pointer */
  24734. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  24735. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  24736. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  24737. word32 otherAttrSz; /* size of otherAttr, bytes */
  24738. int kekriOptions; /* KEKRI options flags */
  24739. /* PWRI specific */
  24740. char* password; /* password */
  24741. word32 passwordSz; /* password size, bytes */
  24742. byte* salt; /* KDF salt */
  24743. word32 saltSz; /* KDF salt size, bytes */
  24744. int kdfOID; /* KDF OID */
  24745. int hashOID; /* KDF hash algorithm OID */
  24746. int kdfIterations; /* KDF iterations */
  24747. int kekEncryptOID; /* KEK encryption algorithm OID */
  24748. int pwriOptions; /* PWRI options flags */
  24749. /* ORI specific */
  24750. int isOri;
  24751. int oriOptions; /* ORI options flags */
  24752. const char* outFileName;
  24753. } pkcs7AuthEnvelopedVector;
  24754. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  24755. byte* rsaPrivKey, word32 rsaPrivKeySz,
  24756. byte* eccCert, word32 eccCertSz,
  24757. byte* eccPrivKey, word32 eccPrivKeySz)
  24758. {
  24759. int ret = 0, testSz = 0, i;
  24760. int envelopedSz, decodedSz;
  24761. byte *enveloped = NULL;
  24762. byte *decoded = NULL;
  24763. WC_RNG rng;
  24764. PKCS7* pkcs7;
  24765. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  24766. XFILE pkcs7File;
  24767. #endif
  24768. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  24769. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24770. 0x72,0x6c,0x64
  24771. };
  24772. byte senderNonce[PKCS7_NONCE_SZ + 2];
  24773. #ifdef HAVE_ECC
  24774. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  24775. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24776. 0x09, 0x05 };
  24777. PKCS7Attrib attribs[] =
  24778. {
  24779. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  24780. sizeof(senderNonce) }
  24781. };
  24782. #endif
  24783. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  24784. defined(WOLFSSL_SHA512)
  24785. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  24786. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24787. };
  24788. #endif /* NO_AES */
  24789. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24790. /* encryption key for kekri recipient types */
  24791. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  24792. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  24793. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  24794. };
  24795. /* encryption key identifier */
  24796. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  24797. 0x02,0x02,0x03,0x04
  24798. };
  24799. #endif
  24800. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  24801. !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24802. #ifndef HAVE_FIPS
  24803. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  24804. #else
  24805. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  24806. #endif
  24807. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  24808. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  24809. };
  24810. #endif
  24811. pkcs7AuthEnvelopedVector *testVectors = NULL;
  24812. {
  24813. #define ADD_PKCS7_TEST_VEC(...) { \
  24814. const pkcs7AuthEnvelopedVector vec = __VA_ARGS__; \
  24815. testVectors = (pkcs7AuthEnvelopedVector *) \
  24816. XREALLOC(testVectors, \
  24817. sizeof *testVectors * (testSz + 1), \
  24818. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); \
  24819. if (testVectors == NULL) \
  24820. ERROR_OUT(-12233, out); \
  24821. XMEMCPY(&testVectors[testSz++], &vec, sizeof *testVectors); \
  24822. }
  24823. /* key transport key encryption technique */
  24824. #ifndef NO_RSA
  24825. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24826. #ifdef WOLFSSL_AES_128
  24827. ADD_PKCS7_TEST_VEC(
  24828. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  24829. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24830. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24831. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der"});
  24832. #endif
  24833. #ifdef WOLFSSL_AES_192
  24834. ADD_PKCS7_TEST_VEC(
  24835. {data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  24836. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24837. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24838. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der"});
  24839. #endif
  24840. #ifdef WOLFSSL_AES_256
  24841. ADD_PKCS7_TEST_VEC(
  24842. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24843. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24844. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  24845. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der"});
  24846. /* test with contentType set to FirmwarePkgData */
  24847. ADD_PKCS7_TEST_VEC(
  24848. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  24849. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  24850. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  24851. 0, 0, 0, 0, 0, 0, 0, 0,
  24852. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der"});
  24853. /* explicitly using SKID for SubjectKeyIdentifier */
  24854. ADD_PKCS7_TEST_VEC(
  24855. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24856. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  24857. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  24858. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der"});
  24859. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  24860. ADD_PKCS7_TEST_VEC(
  24861. {data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  24862. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  24863. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  24864. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24865. "pkcs7authEnvelopedDataAES256GCM_IANDS.der"});
  24866. #endif
  24867. #endif /* NO_AES */
  24868. #endif
  24869. /* key agreement key encryption technique*/
  24870. #ifdef HAVE_ECC
  24871. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24872. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24873. ADD_PKCS7_TEST_VEC(
  24874. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  24875. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24876. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  24877. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24878. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der"});
  24879. #endif
  24880. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  24881. ADD_PKCS7_TEST_VEC(
  24882. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24883. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24884. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  24885. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24886. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der"});
  24887. /* with authenticated attributes */
  24888. ADD_PKCS7_TEST_VEC(
  24889. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24890. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24891. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24892. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24893. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  24894. 0, 0, 0,
  24895. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der"});
  24896. /* with unauthenticated attributes */
  24897. ADD_PKCS7_TEST_VEC(
  24898. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24899. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24900. eccPrivKeySz, NULL, 0, attribs,
  24901. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  24902. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  24903. 0, 0, 0,
  24904. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der"});
  24905. /* with authenticated AND unauthenticated attributes */
  24906. ADD_PKCS7_TEST_VEC(
  24907. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24908. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24909. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24910. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  24911. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24912. 0, 0, 0, 0, 0, 0,
  24913. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der"});
  24914. /* with authenticated AND unauthenticated attributes AND
  24915. * contentType of FirmwarePkgData */
  24916. ADD_PKCS7_TEST_VEC(
  24917. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  24918. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24919. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  24920. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  24921. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24922. 0, 0, 0, 0, 0, 0,
  24923. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der"});
  24924. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  24925. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  24926. ADD_PKCS7_TEST_VEC(
  24927. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24928. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24929. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  24930. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24931. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der"});
  24932. /* with optional user keying material (ukm) */
  24933. ADD_PKCS7_TEST_VEC(
  24934. {data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  24935. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  24936. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  24937. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  24938. 0, 0, 0, 0, 0, 0,
  24939. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der"});
  24940. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  24941. #endif /* NO_AES */
  24942. #endif
  24943. /* kekri (KEKRecipientInfo) recipient types */
  24944. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24945. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24946. ADD_PKCS7_TEST_VEC(
  24947. {data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  24948. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  24949. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  24950. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  24951. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der"});
  24952. #endif
  24953. #endif
  24954. /* pwri (PasswordRecipientInfo) recipient types */
  24955. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  24956. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  24957. ADD_PKCS7_TEST_VEC(
  24958. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  24959. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  24960. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  24961. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  24962. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der"});
  24963. #endif
  24964. #endif
  24965. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  24966. #ifdef WOLFSSL_AES_128
  24967. /* ori (OtherRecipientInfo) recipient types */
  24968. ADD_PKCS7_TEST_VEC(
  24969. {data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  24970. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  24971. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  24972. "pkcs7authEnvelopedDataAES128GCM_ORI.der"});
  24973. #endif
  24974. #endif
  24975. }
  24976. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24977. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24978. if ((! enveloped) || (! decoded)) {
  24979. ERROR_OUT(-12210, out);
  24980. }
  24981. /* generate senderNonce */
  24982. {
  24983. #ifndef HAVE_FIPS
  24984. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24985. #else
  24986. ret = wc_InitRng(&rng);
  24987. #endif
  24988. if (ret != 0) {
  24989. ERROR_OUT(-12211, out);
  24990. }
  24991. senderNonce[0] = 0x04;
  24992. senderNonce[1] = PKCS7_NONCE_SZ;
  24993. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  24994. if (ret != 0) {
  24995. wc_FreeRng(&rng);
  24996. ERROR_OUT(-12212, out);
  24997. }
  24998. }
  24999. for (i = 0; i < testSz; i++) {
  25000. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  25001. #ifdef WOLFSSL_ASYNC_CRYPT
  25002. INVALID_DEVID /* async PKCS7 is not supported */
  25003. #else
  25004. devId
  25005. #endif
  25006. );
  25007. if (pkcs7 == NULL) {
  25008. ERROR_OUT(-12213, out);
  25009. }
  25010. if (testVectors[i].secretKey != NULL) {
  25011. /* KEKRI recipient type */
  25012. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  25013. if (ret != 0) {
  25014. ERROR_OUT(-12214, out);
  25015. }
  25016. pkcs7->content = (byte*)testVectors[i].content;
  25017. pkcs7->contentSz = testVectors[i].contentSz;
  25018. pkcs7->contentOID = testVectors[i].contentOID;
  25019. pkcs7->encryptOID = testVectors[i].encryptOID;
  25020. pkcs7->ukm = testVectors[i].optionalUkm;
  25021. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  25022. pkcs7->authAttribs = testVectors[i].authAttribs;
  25023. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  25024. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  25025. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  25026. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  25027. testVectors[i].secretKey, testVectors[i].secretKeySz,
  25028. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  25029. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  25030. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  25031. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  25032. if (ret < 0) {
  25033. wc_PKCS7_Free(pkcs7);
  25034. ERROR_OUT(-12215, out);
  25035. }
  25036. /* set key, for decryption */
  25037. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  25038. testVectors[i].secretKeySz);
  25039. if (ret != 0) {
  25040. wc_PKCS7_Free(pkcs7);
  25041. ERROR_OUT(-12216, out);
  25042. }
  25043. } else if (testVectors[i].password != NULL) {
  25044. #ifndef NO_PWDBASED
  25045. /* PWRI recipient type */
  25046. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  25047. if (ret != 0) {
  25048. ERROR_OUT(-12217, out);
  25049. }
  25050. pkcs7->content = (byte*)testVectors[i].content;
  25051. pkcs7->contentSz = testVectors[i].contentSz;
  25052. pkcs7->contentOID = testVectors[i].contentOID;
  25053. pkcs7->encryptOID = testVectors[i].encryptOID;
  25054. pkcs7->ukm = testVectors[i].optionalUkm;
  25055. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  25056. pkcs7->authAttribs = testVectors[i].authAttribs;
  25057. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  25058. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  25059. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  25060. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  25061. (byte*)testVectors[i].password,
  25062. testVectors[i].passwordSz, testVectors[i].salt,
  25063. testVectors[i].saltSz, testVectors[i].kdfOID,
  25064. testVectors[i].hashOID, testVectors[i].kdfIterations,
  25065. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  25066. if (ret < 0) {
  25067. wc_PKCS7_Free(pkcs7);
  25068. ERROR_OUT(-12218, out);
  25069. }
  25070. /* set password, for decryption */
  25071. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  25072. testVectors[i].passwordSz);
  25073. if (ret < 0) {
  25074. wc_PKCS7_Free(pkcs7);
  25075. ERROR_OUT(-12219, out);
  25076. }
  25077. #endif /* NO_PWDBASED */
  25078. } else if (testVectors[i].isOri == 1) {
  25079. /* ORI recipient type */
  25080. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  25081. if (ret != 0) {
  25082. ERROR_OUT(-12220, out);
  25083. }
  25084. pkcs7->content = (byte*)testVectors[i].content;
  25085. pkcs7->contentSz = testVectors[i].contentSz;
  25086. pkcs7->contentOID = testVectors[i].contentOID;
  25087. pkcs7->encryptOID = testVectors[i].encryptOID;
  25088. pkcs7->authAttribs = testVectors[i].authAttribs;
  25089. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  25090. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  25091. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  25092. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  25093. testVectors[i].oriOptions);
  25094. if (ret < 0) {
  25095. wc_PKCS7_Free(pkcs7);
  25096. ERROR_OUT(-12221, out);
  25097. }
  25098. /* set decrypt callback for decryption */
  25099. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  25100. if (ret < 0) {
  25101. wc_PKCS7_Free(pkcs7);
  25102. ERROR_OUT(-12222, out);
  25103. }
  25104. } else {
  25105. /* KTRI or KARI recipient types */
  25106. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  25107. (word32)testVectors[i].certSz);
  25108. if (ret != 0) {
  25109. wc_PKCS7_Free(pkcs7);
  25110. ERROR_OUT(-12223, out);
  25111. }
  25112. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  25113. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  25114. pkcs7->privateKey = testVectors[i].privateKey;
  25115. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  25116. pkcs7->content = (byte*)testVectors[i].content;
  25117. pkcs7->contentSz = testVectors[i].contentSz;
  25118. pkcs7->contentOID = testVectors[i].contentOID;
  25119. pkcs7->encryptOID = testVectors[i].encryptOID;
  25120. pkcs7->ukm = testVectors[i].optionalUkm;
  25121. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  25122. pkcs7->authAttribs = testVectors[i].authAttribs;
  25123. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  25124. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  25125. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  25126. /* set SubjectIdentifier type for KTRI types */
  25127. if (testVectors[i].ktriOptions & CMS_SKID) {
  25128. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  25129. if (ret != 0) {
  25130. wc_PKCS7_Free(pkcs7);
  25131. ERROR_OUT(-12224, out);
  25132. }
  25133. } else if (testVectors[i].ktriOptions &
  25134. CMS_ISSUER_AND_SERIAL_NUMBER) {
  25135. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  25136. CMS_ISSUER_AND_SERIAL_NUMBER);
  25137. if (ret != 0) {
  25138. wc_PKCS7_Free(pkcs7);
  25139. ERROR_OUT(-12225, out);
  25140. }
  25141. }
  25142. }
  25143. #ifdef ECC_TIMING_RESISTANT
  25144. pkcs7->rng = &rng;
  25145. #endif
  25146. /* encode envelopedData */
  25147. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  25148. PKCS7_BUF_SIZE);
  25149. if (envelopedSz <= 0) {
  25150. wc_PKCS7_Free(pkcs7);
  25151. ERROR_OUT(-12226, out);
  25152. }
  25153. #ifndef NO_PKCS7_STREAM
  25154. { /* test reading byte by byte */
  25155. int z;
  25156. for (z = 0; z < envelopedSz; z++) {
  25157. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  25158. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  25159. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  25160. printf("unexpected error %d\n", decodedSz);
  25161. ERROR_OUT(-12227, out);
  25162. }
  25163. }
  25164. /* test decode result */
  25165. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25166. printf("stream read compare failed\n");
  25167. wc_PKCS7_Free(pkcs7);
  25168. ERROR_OUT(-12228, out);
  25169. }
  25170. }
  25171. #endif
  25172. /* decode envelopedData */
  25173. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  25174. envelopedSz, decoded,
  25175. PKCS7_BUF_SIZE);
  25176. if (decodedSz <= 0) {
  25177. wc_PKCS7_Free(pkcs7);
  25178. ERROR_OUT(-12229, out);
  25179. }
  25180. /* test decode result */
  25181. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  25182. wc_PKCS7_Free(pkcs7);
  25183. ERROR_OUT(-12230, out);
  25184. }
  25185. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25186. /* output pkcs7 envelopedData for external testing */
  25187. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25188. if (!pkcs7File) {
  25189. wc_PKCS7_Free(pkcs7);
  25190. ERROR_OUT(-12231, out);
  25191. }
  25192. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  25193. XFCLOSE(pkcs7File);
  25194. if (ret != envelopedSz) {
  25195. wc_PKCS7_Free(pkcs7);
  25196. ERROR_OUT(-12232, out);
  25197. } else {
  25198. /* reset ret to 0 for success */
  25199. ret = 0;
  25200. }
  25201. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  25202. wc_PKCS7_Free(pkcs7);
  25203. pkcs7 = NULL;
  25204. }
  25205. wc_FreeRng(&rng);
  25206. #if !defined(HAVE_ECC) || defined(NO_AES)
  25207. (void)eccCert;
  25208. (void)eccCertSz;
  25209. (void)eccPrivKey;
  25210. (void)eccPrivKeySz;
  25211. (void)secretKey;
  25212. (void)secretKeyId;
  25213. #endif
  25214. #ifdef NO_RSA
  25215. (void)rsaCert;
  25216. (void)rsaCertSz;
  25217. (void)rsaPrivKey;
  25218. (void)rsaPrivKeySz;
  25219. #endif
  25220. out:
  25221. if (testVectors)
  25222. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25223. if (enveloped)
  25224. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25225. if (decoded)
  25226. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25227. return ret;
  25228. }
  25229. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  25230. {
  25231. int ret = 0;
  25232. byte* rsaCert = NULL;
  25233. byte* rsaPrivKey = NULL;
  25234. word32 rsaCertSz = 0;
  25235. word32 rsaPrivKeySz = 0;
  25236. byte* eccCert = NULL;
  25237. byte* eccPrivKey = NULL;
  25238. word32 eccCertSz = 0;
  25239. word32 eccPrivKeySz = 0;
  25240. #ifndef NO_RSA
  25241. /* read client RSA cert and key in DER format */
  25242. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25243. if (rsaCert == NULL)
  25244. return -12300;
  25245. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25246. if (rsaPrivKey == NULL) {
  25247. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25248. return -12301;
  25249. }
  25250. rsaCertSz = FOURK_BUF;
  25251. rsaPrivKeySz = FOURK_BUF;
  25252. #endif /* NO_RSA */
  25253. #ifdef HAVE_ECC
  25254. /* read client ECC cert and key in DER format */
  25255. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25256. if (eccCert == NULL) {
  25257. #ifndef NO_RSA
  25258. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25259. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25260. #endif
  25261. return -12302;
  25262. }
  25263. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25264. if (eccPrivKey == NULL) {
  25265. #ifndef NO_RSA
  25266. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25267. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25268. #endif
  25269. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25270. return -12303;
  25271. }
  25272. eccCertSz = FOURK_BUF;
  25273. eccPrivKeySz = FOURK_BUF;
  25274. #endif /* HAVE_ECC */
  25275. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  25276. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  25277. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  25278. eccPrivKey, &eccPrivKeySz);
  25279. if (ret < 0) {
  25280. #ifndef NO_RSA
  25281. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25282. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25283. #endif
  25284. #ifdef HAVE_ECC
  25285. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25286. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25287. #endif
  25288. return -12304;
  25289. }
  25290. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  25291. rsaPrivKey, (word32)rsaPrivKeySz,
  25292. eccCert, (word32)eccCertSz,
  25293. eccPrivKey, (word32)eccPrivKeySz);
  25294. #ifndef NO_RSA
  25295. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25296. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25297. #endif
  25298. #ifdef HAVE_ECC
  25299. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25300. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25301. #endif
  25302. return ret;
  25303. }
  25304. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  25305. #ifndef NO_AES
  25306. static const byte p7DefKey[] = {
  25307. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25308. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25309. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25310. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25311. };
  25312. static const byte p7AltKey[] = {
  25313. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25314. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25315. };
  25316. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25317. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25318. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25319. {
  25320. int ret;
  25321. if (cek == NULL || out == NULL)
  25322. return BAD_FUNC_ARG;
  25323. /* test case sanity checks */
  25324. if (keyIdSz != 1) {
  25325. return -12310;
  25326. }
  25327. if (keyId[0] != 0x00) {
  25328. return -12311;
  25329. }
  25330. if (type != (int)PKCS7_KEKRI) {
  25331. return -12312;
  25332. }
  25333. switch (keyWrapAlgo) {
  25334. case AES256_WRAP:
  25335. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  25336. out, outSz, NULL);
  25337. if (ret <= 0)
  25338. return ret;
  25339. break;
  25340. default:
  25341. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25342. return BAD_KEYWRAP_ALG_E;
  25343. };
  25344. (void)pkcs7;
  25345. (void)direction;
  25346. (void)orginKey; /* used with KAKRI */
  25347. (void)orginKeySz;
  25348. return ret;
  25349. }
  25350. /* returns key size on success */
  25351. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  25352. {
  25353. int ret;
  25354. word32 atrSz;
  25355. byte atr[256];
  25356. /* Additionally can look for fwWrappedFirmwareKey
  25357. * 1.2.840.113529.1.9.16.1.16 */
  25358. const unsigned char fwWrappedFirmwareKey[] = {
  25359. /* 0x06, 0x0B */
  25360. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25361. 0x01, 0x09, 0x10, 0x02, 0x27
  25362. };
  25363. /* find keyID in fwWrappedFirmwareKey */
  25364. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  25365. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  25366. if (ret == LENGTH_ONLY_E) {
  25367. XMEMSET(atr, 0, sizeof(atr));
  25368. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  25369. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  25370. /* keyIdRaw[0] OCTET TAG */
  25371. /* keyIdRaw[1] Length */
  25372. if (ret > 0) {
  25373. PKCS7* envPkcs7;
  25374. envPkcs7 = wc_PKCS7_New(NULL, 0);
  25375. if (envPkcs7 == NULL) {
  25376. return MEMORY_E;
  25377. }
  25378. wc_PKCS7_Init(envPkcs7, NULL, 0);
  25379. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  25380. if (ret == 0) {
  25381. /* expecting FIRMWARE_PKG_DATA content */
  25382. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  25383. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  25384. key, keySz);
  25385. }
  25386. wc_PKCS7_Free(envPkcs7);
  25387. }
  25388. }
  25389. return ret;
  25390. }
  25391. /* create a KEKRI enveloped data
  25392. * return size on success */
  25393. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  25394. word32 outSz)
  25395. {
  25396. int ret;
  25397. PKCS7* pkcs7;
  25398. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  25399. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  25400. if (pkcs7 == NULL)
  25401. return -12330;
  25402. pkcs7->content = in;
  25403. pkcs7->contentSz = inSz;
  25404. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  25405. pkcs7->encryptOID = AES256CBCb;
  25406. pkcs7->ukm = NULL;
  25407. pkcs7->ukmSz = 0;
  25408. /* add recipient (KEKRI type) */
  25409. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  25410. sizeof(p7DefKey), (byte*)keyId,
  25411. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  25412. if (ret < 0) {
  25413. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  25414. wc_PKCS7_Free(pkcs7);
  25415. return -12331;
  25416. }
  25417. /* encode envelopedData, returns size */
  25418. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  25419. if (ret <= 0) {
  25420. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  25421. wc_PKCS7_Free(pkcs7);
  25422. return -12332;
  25423. }
  25424. wc_PKCS7_Free(pkcs7);
  25425. return ret;
  25426. }
  25427. /*
  25428. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  25429. * returns size of buffer output on success
  25430. */
  25431. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  25432. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  25433. byte* key, word32 keySz)
  25434. {
  25435. int ret, attribNum = 1;
  25436. PKCS7* pkcs7;
  25437. /* KEY ID
  25438. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  25439. */
  25440. const unsigned char fwDecryptKeyID[] = {
  25441. 0x06, 0x0B,
  25442. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25443. 0x01, 0x09, 0x10, 0x02, 0x25
  25444. };
  25445. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  25446. const unsigned char fwWrappedFirmwareKey[] = {
  25447. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25448. 0x01, 0x09, 0x10, 0x02, 0x27
  25449. };
  25450. byte keyID[] = { 0x04, 0x01, 0x00 };
  25451. byte env[256];
  25452. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  25453. PKCS7Attrib attribs[] =
  25454. {
  25455. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  25456. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  25457. };
  25458. keyID[2] = keyHint;
  25459. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  25460. if (keyHint == 0) {
  25461. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  25462. sizeof(env));
  25463. if (ret <= 0) {
  25464. return ret;
  25465. }
  25466. attribs[1].valueSz = ret;
  25467. attribNum++;
  25468. }
  25469. /* init PKCS7 */
  25470. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  25471. if (pkcs7 == NULL)
  25472. return -12340;
  25473. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  25474. if (ret != 0) {
  25475. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  25476. wc_PKCS7_Free(pkcs7);
  25477. return -12341;
  25478. }
  25479. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  25480. if (ret != 0) {
  25481. wc_PKCS7_Free(pkcs7);
  25482. return -12342;
  25483. }
  25484. /* encode Signed Encrypted FirmwarePkgData */
  25485. if (encryptKeySz == 16) {
  25486. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  25487. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  25488. (byte*)data, sizeof(data), NULL, 0,
  25489. attribs, attribNum, out, *outSz);
  25490. }
  25491. else {
  25492. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  25493. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  25494. (byte*)data, sizeof(data), NULL, 0,
  25495. attribs, attribNum, out, *outSz);
  25496. }
  25497. if (ret <= 0) {
  25498. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  25499. "ret = %d\n", ret);
  25500. wc_PKCS7_Free(pkcs7);
  25501. return -12343;
  25502. } else {
  25503. *outSz = ret;
  25504. }
  25505. wc_PKCS7_Free(pkcs7);
  25506. return ret;
  25507. }
  25508. /* test verification and decryption of PKCS7 bundle
  25509. * return 0 on success
  25510. */
  25511. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  25512. {
  25513. int ret = 0;
  25514. int usrCtx = 1; /* test value to pass as user context to callback */
  25515. PKCS7* pkcs7;
  25516. byte* sid;
  25517. word32 sidSz;
  25518. byte key[256];
  25519. word32 keySz = sizeof(key);
  25520. byte decoded[FOURK_BUF/2];
  25521. int decodedSz = FOURK_BUF/2;
  25522. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  25523. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  25524. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  25525. 0xD7, 0x85, 0x65, 0xC0
  25526. };
  25527. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  25528. if (pkcs7 == NULL) {
  25529. return MEMORY_E;
  25530. }
  25531. /* Test verify */
  25532. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  25533. if (ret != 0) {
  25534. wc_PKCS7_Free(pkcs7);
  25535. return ret;
  25536. }
  25537. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  25538. if (ret != 0) {
  25539. wc_PKCS7_Free(pkcs7);
  25540. return ret;
  25541. }
  25542. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  25543. if (ret != 0) {
  25544. wc_PKCS7_Free(pkcs7);
  25545. return ret;
  25546. }
  25547. /* Get size of SID and print it out */
  25548. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  25549. if (ret != LENGTH_ONLY_E) {
  25550. wc_PKCS7_Free(pkcs7);
  25551. return ret;
  25552. }
  25553. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25554. if (sid == NULL) {
  25555. wc_PKCS7_Free(pkcs7);
  25556. return ret;
  25557. }
  25558. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  25559. if (ret != 0) {
  25560. wc_PKCS7_Free(pkcs7);
  25561. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25562. return ret;
  25563. }
  25564. ret = XMEMCMP(sid, expectedSid, sidSz);
  25565. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25566. if (ret != 0) {
  25567. wc_PKCS7_Free(pkcs7);
  25568. return ret;
  25569. }
  25570. /* get expected fwWrappedFirmwareKey */
  25571. if (keyHint == 0) {
  25572. ret = getFirmwareKey(pkcs7, key, keySz);
  25573. if (ret < 0) {
  25574. wc_PKCS7_Free(pkcs7);
  25575. return ret;
  25576. }
  25577. pkcs7->encryptionKey = key;
  25578. pkcs7->encryptionKeySz = ret;
  25579. }
  25580. else {
  25581. decodedSz = PKCS7_BUF_SIZE;
  25582. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  25583. if (ret != 0) {
  25584. wc_PKCS7_Free(pkcs7);
  25585. return ret;
  25586. }
  25587. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  25588. if (ret != 0) {
  25589. wc_PKCS7_Free(pkcs7);
  25590. return ret;
  25591. }
  25592. }
  25593. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  25594. pkcs7->contentSz, decoded, decodedSz);
  25595. if (decodedSz < 0) {
  25596. ret = decodedSz;
  25597. wc_PKCS7_Free(pkcs7);
  25598. return ret;
  25599. }
  25600. wc_PKCS7_Free(pkcs7);
  25601. return 0;
  25602. }
  25603. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  25604. {
  25605. int ret = 0;
  25606. word32 derSz;
  25607. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25608. if (! derBuf)
  25609. ERROR_OUT(-12360, out);
  25610. /* Doing default generation and verify */
  25611. derSz = FOURK_BUF;
  25612. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  25613. certSz, key, keySz);
  25614. if (ret <= 0) {
  25615. ERROR_OUT(-12361, out);
  25616. }
  25617. ret = verifyBundle(derBuf, derSz, 0);
  25618. if (ret != 0) {
  25619. ERROR_OUT(-12362, out);
  25620. }
  25621. /* test choosing other key with keyID */
  25622. derSz = FOURK_BUF;
  25623. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  25624. cert, certSz, key, keySz);
  25625. if (ret <= 0) {
  25626. ERROR_OUT(-12363, out);
  25627. }
  25628. ret = verifyBundle(derBuf, derSz, 1);
  25629. if (ret != 0) {
  25630. ERROR_OUT(-12364, out);
  25631. }
  25632. /* test fail case with wrong keyID */
  25633. derSz = FOURK_BUF;
  25634. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  25635. cert, certSz, key, keySz);
  25636. if (ret <= 0) {
  25637. ERROR_OUT(-12365, out);
  25638. }
  25639. ret = verifyBundle(derBuf, derSz, 1);
  25640. if (ret == 0) {
  25641. ERROR_OUT(-12366, out);
  25642. }
  25643. ret = 0;
  25644. out:
  25645. if (derBuf)
  25646. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25647. return ret;
  25648. }
  25649. #endif /* NO_AES */
  25650. #ifndef NO_PKCS7_ENCRYPTED_DATA
  25651. typedef struct {
  25652. const byte* content;
  25653. word32 contentSz;
  25654. int contentOID;
  25655. int encryptOID;
  25656. byte* encryptionKey;
  25657. word32 encryptionKeySz;
  25658. PKCS7Attrib* attribs;
  25659. word32 attribsSz;
  25660. const char* outFileName;
  25661. } pkcs7EncryptedVector;
  25662. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  25663. {
  25664. int ret = 0;
  25665. int i, testSz;
  25666. int encryptedSz, decodedSz, attribIdx;
  25667. PKCS7* pkcs7;
  25668. byte *encrypted;
  25669. byte *decoded;
  25670. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25671. XFILE pkcs7File;
  25672. #endif
  25673. PKCS7Attrib* expectedAttrib;
  25674. PKCS7DecodedAttrib* decodedAttrib;
  25675. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25676. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25677. 0x72,0x6c,0x64
  25678. };
  25679. #ifndef NO_DES3
  25680. byte desKey[] = {
  25681. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25682. };
  25683. byte des3Key[] = {
  25684. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25685. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25686. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25687. };
  25688. #endif
  25689. #ifndef NO_AES
  25690. #ifdef WOLFSSL_AES_128
  25691. byte aes128Key[] = {
  25692. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25693. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25694. };
  25695. #endif
  25696. #ifdef WOLFSSL_AES_192
  25697. byte aes192Key[] = {
  25698. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25699. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25700. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25701. };
  25702. #endif
  25703. #ifdef WOLFSSL_AES_256
  25704. byte aes256Key[] = {
  25705. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25706. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25707. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25708. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25709. };
  25710. #endif
  25711. #ifdef WOLFSSL_AES_256
  25712. /* Attribute example from RFC 4134, Section 7.2
  25713. * OID = 1.2.5555
  25714. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  25715. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  25716. static byte genAttr[] = { 0x04, 47,
  25717. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  25718. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  25719. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  25720. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  25721. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  25722. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  25723. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  25724. static byte genAttr2[] = { 0x04, 47,
  25725. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  25726. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  25727. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  25728. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  25729. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  25730. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  25731. PKCS7Attrib attribs[] =
  25732. {
  25733. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  25734. };
  25735. PKCS7Attrib multiAttribs[] =
  25736. {
  25737. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  25738. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  25739. };
  25740. #endif
  25741. #endif /* NO_AES */
  25742. const pkcs7EncryptedVector testVectors[] =
  25743. {
  25744. #ifndef NO_DES3
  25745. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  25746. NULL, 0, "pkcs7encryptedDataDES3.der"},
  25747. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  25748. NULL, 0, "pkcs7encryptedDataDES.der"},
  25749. #endif /* NO_DES3 */
  25750. #ifndef NO_AES
  25751. #ifdef WOLFSSL_AES_128
  25752. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25753. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  25754. #endif
  25755. #ifdef WOLFSSL_AES_192
  25756. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25757. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  25758. #endif
  25759. #ifdef WOLFSSL_AES_256
  25760. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25761. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  25762. /* test with optional unprotected attributes */
  25763. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25764. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  25765. "pkcs7encryptedDataAES256CBC_attribs.der"},
  25766. /* test with multiple optional unprotected attributes */
  25767. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25768. sizeof(aes256Key), multiAttribs,
  25769. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  25770. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  25771. /* test with contentType set to FirmwarePkgData */
  25772. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  25773. sizeof(aes256Key), NULL, 0,
  25774. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  25775. #endif
  25776. #endif /* NO_AES */
  25777. };
  25778. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25779. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25780. if ((! encrypted) || (! decoded)) {
  25781. ERROR_OUT(MEMORY_E, out);
  25782. }
  25783. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25784. for (i = 0; i < testSz; i++) {
  25785. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  25786. if (pkcs7 == NULL) {
  25787. ERROR_OUT(-12400, out);
  25788. }
  25789. pkcs7->content = (byte*)testVectors[i].content;
  25790. pkcs7->contentSz = testVectors[i].contentSz;
  25791. pkcs7->contentOID = testVectors[i].contentOID;
  25792. pkcs7->encryptOID = testVectors[i].encryptOID;
  25793. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25794. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25795. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  25796. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  25797. /* encode encryptedData */
  25798. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25799. PKCS7_BUF_SIZE);
  25800. if (encryptedSz <= 0) {
  25801. wc_PKCS7_Free(pkcs7);
  25802. ERROR_OUT(-12401, out);
  25803. }
  25804. /* decode encryptedData */
  25805. #ifndef NO_PKCS7_STREAM
  25806. { /* test reading byte by byte */
  25807. int z;
  25808. for (z = 0; z < encryptedSz; z++) {
  25809. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  25810. decoded, PKCS7_BUF_SIZE);
  25811. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  25812. printf("unexpected error %d\n", decodedSz);
  25813. ERROR_OUT(-12402, out);
  25814. }
  25815. }
  25816. /* test decode result */
  25817. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25818. printf("stream read failed\n");
  25819. wc_PKCS7_Free(pkcs7);
  25820. ERROR_OUT(-12403, out);
  25821. }
  25822. }
  25823. #endif
  25824. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25825. decoded, PKCS7_BUF_SIZE);
  25826. if (decodedSz <= 0){
  25827. wc_PKCS7_Free(pkcs7);
  25828. ERROR_OUT(-12404, out);
  25829. }
  25830. /* test decode result */
  25831. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  25832. wc_PKCS7_Free(pkcs7);
  25833. ERROR_OUT(-12405, out);
  25834. }
  25835. /* verify decoded unprotected attributes */
  25836. if (pkcs7->decodedAttrib != NULL) {
  25837. decodedAttrib = pkcs7->decodedAttrib;
  25838. attribIdx = 1;
  25839. while (decodedAttrib != NULL) {
  25840. /* expected attribute, stored list is reversed */
  25841. expectedAttrib = &(pkcs7->unprotectedAttribs
  25842. [pkcs7->unprotectedAttribsSz - attribIdx]);
  25843. /* verify oid */
  25844. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  25845. decodedAttrib->oidSz) != 0) {
  25846. wc_PKCS7_Free(pkcs7);
  25847. ERROR_OUT(-12406, out);
  25848. }
  25849. /* verify value */
  25850. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  25851. decodedAttrib->valueSz) != 0) {
  25852. wc_PKCS7_Free(pkcs7);
  25853. ERROR_OUT(-12407, out);
  25854. }
  25855. decodedAttrib = decodedAttrib->next;
  25856. attribIdx++;
  25857. }
  25858. }
  25859. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25860. /* output pkcs7 envelopedData for external testing */
  25861. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25862. if (!pkcs7File) {
  25863. wc_PKCS7_Free(pkcs7);
  25864. ERROR_OUT(-12408, out);
  25865. }
  25866. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  25867. XFCLOSE(pkcs7File);
  25868. if (ret > 0)
  25869. ret = 0;
  25870. #endif
  25871. wc_PKCS7_Free(pkcs7);
  25872. }
  25873. out:
  25874. if (encrypted)
  25875. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25876. if (decoded)
  25877. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25878. return ret;
  25879. }
  25880. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  25881. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  25882. typedef struct {
  25883. const byte* content;
  25884. word32 contentSz;
  25885. int contentOID;
  25886. const char* outFileName;
  25887. } pkcs7CompressedVector;
  25888. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  25889. {
  25890. int ret = 0;
  25891. int i, testSz;
  25892. int compressedSz, decodedSz;
  25893. PKCS7* pkcs7;
  25894. #ifdef WOLFSSL_SMALL_STACK
  25895. byte *compressed;
  25896. byte *decoded;
  25897. #else
  25898. byte compressed[PKCS7_BUF_SIZE];
  25899. byte decoded[PKCS7_BUF_SIZE];
  25900. #endif
  25901. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25902. XFILE pkcs7File;
  25903. #endif
  25904. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  25905. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25906. 0x72,0x6c,0x64
  25907. };
  25908. const pkcs7CompressedVector testVectors[] =
  25909. {
  25910. {data, (word32)sizeof(data), DATA,
  25911. "pkcs7compressedData_data_zlib.der"},
  25912. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  25913. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  25914. };
  25915. #ifdef WOLFSSL_SMALL_STACK
  25916. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25917. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25918. if ((! compressed) || (! decoded)) {
  25919. ERROR_OUT(MEMORY_E, out);
  25920. }
  25921. #endif
  25922. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  25923. for (i = 0; i < testSz; i++) {
  25924. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  25925. if (pkcs7 == NULL) {
  25926. ERROR_OUT(-12500, out);
  25927. }
  25928. pkcs7->content = (byte*)testVectors[i].content;
  25929. pkcs7->contentSz = testVectors[i].contentSz;
  25930. pkcs7->contentOID = testVectors[i].contentOID;
  25931. /* encode compressedData */
  25932. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  25933. PKCS7_BUF_SIZE);
  25934. if (compressedSz <= 0) {
  25935. wc_PKCS7_Free(pkcs7);
  25936. ERROR_OUT(-12501, out);
  25937. }
  25938. /* decode compressedData */
  25939. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  25940. compressedSz, decoded,
  25941. PKCS7_BUF_SIZE);
  25942. if (decodedSz <= 0){
  25943. wc_PKCS7_Free(pkcs7);
  25944. ERROR_OUT(-12502, out);
  25945. }
  25946. /* test decode result */
  25947. if (XMEMCMP(decoded, testVectors[i].content,
  25948. testVectors[i].contentSz) != 0) {
  25949. wc_PKCS7_Free(pkcs7);
  25950. ERROR_OUT(-12503, out);
  25951. }
  25952. /* make sure content type is the same */
  25953. if (testVectors[i].contentOID != pkcs7->contentOID) {
  25954. ERROR_OUT(-12504, out);
  25955. }
  25956. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  25957. /* output pkcs7 compressedData for external testing */
  25958. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  25959. if (!pkcs7File) {
  25960. wc_PKCS7_Free(pkcs7);
  25961. ERROR_OUT(-12505, out);
  25962. }
  25963. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  25964. XFCLOSE(pkcs7File);
  25965. if (ret > 0)
  25966. ret = 0;
  25967. #endif
  25968. wc_PKCS7_Free(pkcs7);
  25969. }
  25970. out:
  25971. #ifdef WOLFSSL_SMALL_STACK
  25972. if (compressed)
  25973. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25974. if (decoded)
  25975. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25976. #endif
  25977. return ret;
  25978. } /* pkcs7compressed_test() */
  25979. #undef PKCS7_BUF_SIZE
  25980. #endif /* HAVE_LIBZ */
  25981. typedef struct {
  25982. const byte* content;
  25983. word32 contentSz;
  25984. int hashOID;
  25985. int signOID;
  25986. byte* privateKey;
  25987. word32 privateKeySz;
  25988. byte* cert;
  25989. size_t certSz;
  25990. byte* caCert;
  25991. size_t caCertSz;
  25992. PKCS7Attrib* signedAttribs;
  25993. word32 signedAttribsSz;
  25994. const char* outFileName;
  25995. int contentOID;
  25996. byte* contentType;
  25997. word32 contentTypeSz;
  25998. int sidType;
  25999. int encryptOID; /* for single-shot encrypt alg OID */
  26000. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  26001. byte* encryptKey; /* for single-shot, encryptedData */
  26002. word32 encryptKeySz; /* for single-shot, encryptedData */
  26003. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  26004. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  26005. word16 detachedSignature; /* generate detached signature (0:1) */
  26006. } pkcs7SignedVector;
  26007. static int pkcs7signed_run_vectors(
  26008. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  26009. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  26010. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  26011. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  26012. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  26013. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  26014. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  26015. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  26016. {
  26017. int ret, testSz, i;
  26018. int encodedSz;
  26019. byte* out;
  26020. word32 outSz;
  26021. WC_RNG rng;
  26022. PKCS7* pkcs7;
  26023. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26024. XFILE file;
  26025. #endif
  26026. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  26027. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26028. 0x72,0x6c,0x64
  26029. };
  26030. static byte transIdOid[] =
  26031. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26032. 0x09, 0x07 };
  26033. static byte messageTypeOid[] =
  26034. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26035. 0x09, 0x02 };
  26036. static byte senderNonceOid[] =
  26037. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26038. 0x09, 0x05 };
  26039. #ifndef NO_SHA
  26040. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  26041. #else
  26042. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  26043. #endif
  26044. static byte messageType[] = { 0x13, 2, '1', '9' };
  26045. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  26046. static PKCS7Attrib attribs[] =
  26047. {
  26048. { transIdOid, sizeof(transIdOid), transId,
  26049. sizeof(transId) - 1 }, /* take off the null */
  26050. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26051. sizeof(messageType) },
  26052. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  26053. sizeof(senderNonce) }
  26054. };
  26055. /* for testing custom contentType, FirmwarePkgData */
  26056. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  26057. 0x48, 0x86, 0xF7, 0x0D,
  26058. 0x01, 0x09, 0x10, 0x01, 0x10 };
  26059. const pkcs7SignedVector testVectors[] =
  26060. {
  26061. #ifndef NO_RSA
  26062. #ifndef NO_SHA
  26063. /* RSA with SHA */
  26064. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  26065. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26066. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26067. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  26068. 0, 0},
  26069. /* RSA with SHA, no signed attributes */
  26070. {data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  26071. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  26072. NULL, 0, NULL, 0,
  26073. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26074. NULL, 0, 0},
  26075. #endif
  26076. #ifdef WOLFSSL_SHA224
  26077. /* RSA with SHA224 */
  26078. {data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  26079. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26080. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26081. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26082. NULL, 0, 0},
  26083. #endif
  26084. #ifndef NO_SHA256
  26085. /* RSA with SHA256 */
  26086. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26087. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26088. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26089. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26090. NULL, 0, 0},
  26091. /* RSA with SHA256, detached signature */
  26092. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26093. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26094. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26095. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  26096. NULL, 0, NULL, 0, 1},
  26097. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  26098. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26099. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26100. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26101. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  26102. NULL, 0, NULL, 0, 0},
  26103. /* RSA with SHA256 and custom contentType */
  26104. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26105. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26106. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26107. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  26108. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  26109. NULL, 0, 0},
  26110. /* RSA with SHA256 and FirmwarePkgData contentType */
  26111. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26112. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26113. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26114. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  26115. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26116. /* RSA with SHA256 using server cert and ca cert */
  26117. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  26118. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  26119. rsaCaCertBuf, rsaCaCertBufSz,
  26120. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26121. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  26122. NULL, 0, NULL, 0, 0},
  26123. #endif
  26124. #if defined(WOLFSSL_SHA384)
  26125. /* RSA with SHA384 */
  26126. {data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  26127. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26128. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26129. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26130. NULL, 0, 0},
  26131. #endif
  26132. #if defined(WOLFSSL_SHA512)
  26133. /* RSA with SHA512 */
  26134. {data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  26135. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26136. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26137. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26138. NULL, 0, 0},
  26139. #endif
  26140. #endif /* NO_RSA */
  26141. #ifdef HAVE_ECC
  26142. #ifndef NO_SHA
  26143. /* ECDSA with SHA */
  26144. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  26145. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26146. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26147. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26148. NULL, 0, 0},
  26149. /* ECDSA with SHA, no signed attributes */
  26150. {data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  26151. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  26152. NULL, 0, NULL, 0,
  26153. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26154. NULL, 0, 0},
  26155. #endif
  26156. #ifdef WOLFSSL_SHA224
  26157. /* ECDSA with SHA224 */
  26158. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  26159. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26160. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26161. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26162. NULL, 0, 0},
  26163. #endif
  26164. #ifndef NO_SHA256
  26165. /* ECDSA with SHA256 */
  26166. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26167. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26168. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26169. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26170. NULL, 0, 0},
  26171. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  26172. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26173. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26174. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26175. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  26176. NULL, 0, NULL, 0, 0},
  26177. /* ECDSA with SHA256 and custom contentType */
  26178. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26179. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26180. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26181. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  26182. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  26183. NULL, 0, 0},
  26184. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  26185. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26186. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26187. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26188. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  26189. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26190. #endif
  26191. #ifdef WOLFSSL_SHA384
  26192. /* ECDSA with SHA384 */
  26193. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  26194. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26195. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26196. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26197. NULL, 0, 0},
  26198. #endif
  26199. #ifdef WOLFSSL_SHA512
  26200. /* ECDSA with SHA512 */
  26201. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  26202. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26203. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26204. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  26205. NULL, 0, 0},
  26206. #endif
  26207. #endif /* HAVE_ECC */
  26208. };
  26209. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  26210. outSz = FOURK_BUF;
  26211. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26212. if (out == NULL)
  26213. return -12510;
  26214. XMEMSET(out, 0, outSz);
  26215. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  26216. if (ret < 0) {
  26217. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26218. return -12511;
  26219. }
  26220. #ifndef HAVE_FIPS
  26221. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26222. #else
  26223. ret = wc_InitRng(&rng);
  26224. #endif
  26225. if (ret != 0) {
  26226. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26227. return -12512;
  26228. }
  26229. for (i = 0; i < testSz; i++) {
  26230. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26231. if (pkcs7 == NULL)
  26232. return -12513;
  26233. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  26234. (word32)testVectors[i].certSz);
  26235. if (ret != 0) {
  26236. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26237. wc_PKCS7_Free(pkcs7);
  26238. return -12514;
  26239. }
  26240. /* load CA certificate, if present */
  26241. if (testVectors[i].caCert != NULL) {
  26242. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  26243. (word32)testVectors[i].caCertSz);
  26244. if (ret != 0) {
  26245. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26246. wc_PKCS7_Free(pkcs7);
  26247. return -12515;
  26248. }
  26249. }
  26250. pkcs7->rng = &rng;
  26251. pkcs7->content = (byte*)testVectors[i].content;
  26252. pkcs7->contentSz = testVectors[i].contentSz;
  26253. pkcs7->contentOID = testVectors[i].contentOID;
  26254. pkcs7->hashOID = testVectors[i].hashOID;
  26255. pkcs7->encryptOID = testVectors[i].signOID;
  26256. pkcs7->privateKey = testVectors[i].privateKey;
  26257. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  26258. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  26259. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  26260. /* optional custom contentType, default is DATA,
  26261. overrides contentOID if set */
  26262. if (testVectors[i].contentType != NULL) {
  26263. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  26264. testVectors[i].contentTypeSz);
  26265. if (ret != 0) {
  26266. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26267. wc_PKCS7_Free(pkcs7);
  26268. return -12516;
  26269. }
  26270. }
  26271. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  26272. default is IssuerAndSerialNumber */
  26273. if (testVectors[i].sidType == CMS_SKID) {
  26274. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  26275. if (ret != 0) {
  26276. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26277. wc_PKCS7_Free(pkcs7);
  26278. return -12517;
  26279. }
  26280. }
  26281. /* generate senderNonce */
  26282. {
  26283. senderNonce[0] = 0x04;
  26284. senderNonce[1] = PKCS7_NONCE_SZ;
  26285. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  26286. if (ret != 0) {
  26287. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26288. wc_PKCS7_Free(pkcs7);
  26289. return -12518;
  26290. }
  26291. }
  26292. /* generate transactionID (used with SCEP) */
  26293. {
  26294. #ifndef NO_SHA
  26295. wc_Sha sha;
  26296. byte digest[WC_SHA_DIGEST_SIZE];
  26297. #else
  26298. wc_Sha256 sha;
  26299. byte digest[WC_SHA256_DIGEST_SIZE];
  26300. #endif
  26301. int j,k;
  26302. transId[0] = 0x13;
  26303. transId[1] = sizeof(digest) * 2;
  26304. #ifndef NO_SHA
  26305. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  26306. if (ret != 0) {
  26307. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26308. wc_PKCS7_Free(pkcs7);
  26309. return -12519;
  26310. }
  26311. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  26312. wc_ShaFinal(&sha, digest);
  26313. wc_ShaFree(&sha);
  26314. #else
  26315. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  26316. if (ret != 0) {
  26317. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26318. wc_PKCS7_Free(pkcs7);
  26319. return -12520;
  26320. }
  26321. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  26322. wc_Sha256Final(&sha, digest);
  26323. wc_Sha256Free(&sha);
  26324. #endif
  26325. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  26326. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  26327. }
  26328. }
  26329. /* enable detached signature generation, if set */
  26330. if (testVectors[i].detachedSignature == 1) {
  26331. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  26332. if (ret != 0) {
  26333. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26334. wc_PKCS7_Free(pkcs7);
  26335. return -12521;
  26336. }
  26337. }
  26338. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  26339. if (encodedSz < 0) {
  26340. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26341. wc_PKCS7_Free(pkcs7);
  26342. return -12522;
  26343. }
  26344. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26345. /* write PKCS#7 to output file for more testing */
  26346. file = XFOPEN(testVectors[i].outFileName, "wb");
  26347. if (!file) {
  26348. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26349. wc_PKCS7_Free(pkcs7);
  26350. return -12523;
  26351. }
  26352. ret = (int)XFWRITE(out, 1, encodedSz, file);
  26353. XFCLOSE(file);
  26354. if (ret != (int)encodedSz) {
  26355. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26356. wc_PKCS7_Free(pkcs7);
  26357. return -12524;
  26358. }
  26359. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26360. wc_PKCS7_Free(pkcs7);
  26361. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26362. if (pkcs7 == NULL)
  26363. return -12525;
  26364. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  26365. if (testVectors[i].detachedSignature == 1) {
  26366. /* set content for verifying detached signatures */
  26367. pkcs7->content = (byte*)testVectors[i].content;
  26368. pkcs7->contentSz = testVectors[i].contentSz;
  26369. }
  26370. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  26371. if (ret < 0) {
  26372. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26373. wc_PKCS7_Free(pkcs7);
  26374. return -12526;
  26375. }
  26376. /* verify contentType extracted successfully for custom content types */
  26377. if (testVectors[i].contentTypeSz > 0) {
  26378. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  26379. return -12527;
  26380. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  26381. pkcs7->contentTypeSz) != 0) {
  26382. return -12528;
  26383. }
  26384. }
  26385. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  26386. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26387. wc_PKCS7_Free(pkcs7);
  26388. return -12529;
  26389. }
  26390. {
  26391. /* check getting signed attributes */
  26392. #ifndef NO_SHA
  26393. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  26394. #else
  26395. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  26396. #endif
  26397. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  26398. int oidSz = (int)sizeof(transIdOid) - 2;
  26399. int bufSz = 0;
  26400. if (testVectors[i].signedAttribs != NULL &&
  26401. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  26402. NULL, (word32*)&bufSz) != LENGTH_ONLY_E) {
  26403. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26404. wc_PKCS7_Free(pkcs7);
  26405. return -12530;
  26406. }
  26407. if (bufSz > (int)sizeof(buf)) {
  26408. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26409. wc_PKCS7_Free(pkcs7);
  26410. return -12531;
  26411. }
  26412. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  26413. buf, (word32*)&bufSz);
  26414. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  26415. (testVectors[i].signedAttribs == NULL && bufSz > 0)) {
  26416. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26417. wc_PKCS7_Free(pkcs7);
  26418. return -12532;
  26419. }
  26420. }
  26421. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26422. file = XFOPEN("./pkcs7cert.der", "wb");
  26423. if (!file) {
  26424. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26425. wc_PKCS7_Free(pkcs7);
  26426. return -12533;
  26427. }
  26428. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  26429. XFCLOSE(file);
  26430. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26431. wc_PKCS7_Free(pkcs7);
  26432. }
  26433. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26434. wc_FreeRng(&rng);
  26435. if (ret > 0)
  26436. return 0;
  26437. (void)rsaClientCertBuf;
  26438. (void)rsaClientCertBufSz;
  26439. (void)rsaClientPrivKeyBuf;
  26440. (void)rsaClientPrivKeyBufSz;
  26441. (void)rsaServerCertBuf;
  26442. (void)rsaServerCertBufSz;
  26443. (void)rsaServerPrivKeyBuf;
  26444. (void)rsaServerPrivKeyBufSz;
  26445. (void)rsaCaCertBuf;
  26446. (void)rsaCaCertBufSz;
  26447. (void)rsaCaPrivKeyBuf;
  26448. (void)rsaCaPrivKeyBufSz;
  26449. (void)eccClientCertBuf;
  26450. (void)eccClientCertBufSz;
  26451. (void)eccClientPrivKeyBuf;
  26452. (void)eccClientPrivKeyBufSz;
  26453. return ret;
  26454. }
  26455. static int pkcs7signed_run_SingleShotVectors(
  26456. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  26457. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  26458. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  26459. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  26460. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  26461. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  26462. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  26463. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  26464. {
  26465. int ret, testSz, i;
  26466. int encodedSz;
  26467. byte* out;
  26468. word32 outSz;
  26469. WC_RNG rng;
  26470. PKCS7* pkcs7;
  26471. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26472. XFILE file;
  26473. #endif
  26474. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  26475. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26476. 0x72,0x6c,0x64
  26477. };
  26478. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26479. static byte aes256Key[] = {
  26480. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26481. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26482. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26483. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26484. };
  26485. #endif
  26486. static byte messageTypeOid[] =
  26487. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26488. 0x09, 0x02 };
  26489. static byte messageType[] = { 0x13, 2, '1', '9' };
  26490. PKCS7Attrib attribs[] =
  26491. {
  26492. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26493. sizeof(messageType) },
  26494. };
  26495. const pkcs7SignedVector testVectors[] =
  26496. {
  26497. #ifndef NO_RSA
  26498. #ifndef NO_SHA256
  26499. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  26500. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26501. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26502. NULL, 0,
  26503. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  26504. 0, 0, NULL, 0, NULL, 0, 0},
  26505. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  26506. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26507. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26508. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26509. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  26510. NULL, 0, NULL, 0, 0},
  26511. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  26512. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26513. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26514. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26515. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  26516. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  26517. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  26518. {data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  26519. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  26520. rsaCaCertBuf, rsaCaCertBufSz,
  26521. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26522. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  26523. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26524. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26525. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  26526. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26527. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26528. NULL, 0,
  26529. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  26530. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  26531. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  26532. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26533. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26534. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26535. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  26536. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  26537. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26538. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  26539. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26540. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  26541. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26542. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26543. NULL, 0,
  26544. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  26545. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26546. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  26547. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26548. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26549. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26550. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  26551. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26552. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26553. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  26554. no attribs */
  26555. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26556. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26557. NULL, 0,
  26558. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  26559. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  26560. 0, 0},
  26561. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  26562. attribs */
  26563. {data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  26564. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  26565. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26566. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  26567. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  26568. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26569. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  26570. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26571. #endif /* NO_SHA256 */
  26572. #endif /* NO_RSA */
  26573. #ifdef HAVE_ECC
  26574. #ifndef NO_SHA256
  26575. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  26576. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26577. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26578. NULL, 0,
  26579. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26580. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26581. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  26582. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26583. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26584. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26585. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26586. 0, 0, 0, 0, NULL, 0, NULL, 0, 0},
  26587. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  26588. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26589. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26590. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26591. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  26592. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0},
  26593. #if defined(WOLFSSL_AES_256) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26594. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  26595. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26596. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26597. NULL, 0,
  26598. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26599. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0},
  26600. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  26601. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26602. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26603. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26604. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26605. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  26606. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26607. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  26608. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26609. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  26610. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26611. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26612. NULL, 0,
  26613. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  26614. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26615. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  26616. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26617. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26618. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26619. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  26620. 0, 0, 0, 2, NULL, 0, NULL, 0, 0},
  26621. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26622. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  26623. no attribs */
  26624. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26625. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26626. NULL, 0,
  26627. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  26628. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  26629. 0, 0},
  26630. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  26631. attribs */
  26632. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  26633. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  26634. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  26635. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  26636. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  26637. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0},
  26638. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  26639. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26640. #endif /* NO_SHA256 */
  26641. #endif /* HAVE_ECC */
  26642. };
  26643. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  26644. outSz = FOURK_BUF;
  26645. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26646. if (out == NULL)
  26647. return -12540;
  26648. XMEMSET(out, 0, outSz);
  26649. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  26650. if (ret < 0) {
  26651. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26652. return -12541;
  26653. }
  26654. #ifndef HAVE_FIPS
  26655. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26656. #else
  26657. ret = wc_InitRng(&rng);
  26658. #endif
  26659. if (ret != 0) {
  26660. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26661. return -12542;
  26662. }
  26663. for (i = 0; i < testSz; i++) {
  26664. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26665. if (pkcs7 == NULL)
  26666. return -12543;
  26667. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  26668. (word32)testVectors[i].certSz);
  26669. if (ret != 0) {
  26670. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26671. wc_PKCS7_Free(pkcs7);
  26672. return -12544;
  26673. }
  26674. /* load CA certificate, if present */
  26675. if (testVectors[i].caCert != NULL) {
  26676. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  26677. (word32)testVectors[i].caCertSz);
  26678. if (ret != 0) {
  26679. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26680. wc_PKCS7_Free(pkcs7);
  26681. return -12545;
  26682. }
  26683. }
  26684. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  26685. default is IssuerAndSerialNumber */
  26686. if (testVectors[i].sidType == CMS_SKID) {
  26687. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  26688. if (ret != 0) {
  26689. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26690. wc_PKCS7_Free(pkcs7);
  26691. return -12546;
  26692. }
  26693. }
  26694. if (testVectors[i].encCompFlag == 0) {
  26695. /* encode Signed FirmwarePkgData */
  26696. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  26697. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26698. testVectors[i].signOID, testVectors[i].hashOID,
  26699. (byte*)testVectors[i].content, testVectors[i].contentSz,
  26700. testVectors[i].signedAttribs,
  26701. testVectors[i].signedAttribsSz, out, outSz);
  26702. if (encodedSz < 0) {
  26703. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26704. wc_PKCS7_Free(pkcs7);
  26705. return -12547;
  26706. }
  26707. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26708. } else if (testVectors[i].encCompFlag == 1) {
  26709. /* encode Signed Encrypted FirmwarePkgData */
  26710. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  26711. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  26712. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26713. testVectors[i].encryptOID, testVectors[i].signOID,
  26714. testVectors[i].hashOID, (byte*)testVectors[i].content,
  26715. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  26716. testVectors[i].unprotectedAttribsSz,
  26717. testVectors[i].signedAttribs,
  26718. testVectors[i].signedAttribsSz, out, outSz);
  26719. if (encodedSz <= 0) {
  26720. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26721. wc_PKCS7_Free(pkcs7);
  26722. return -12548;
  26723. }
  26724. #endif
  26725. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26726. } else if (testVectors[i].encCompFlag == 2) {
  26727. /* encode Signed Compressed FirmwarePkgData */
  26728. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  26729. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26730. testVectors[i].signOID, testVectors[i].hashOID,
  26731. (byte*)testVectors[i].content, testVectors[i].contentSz,
  26732. testVectors[i].signedAttribs,
  26733. testVectors[i].signedAttribsSz, out, outSz);
  26734. if (encodedSz <= 0) {
  26735. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26736. wc_PKCS7_Free(pkcs7);
  26737. return -12549;
  26738. }
  26739. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26740. } else if (testVectors[i].encCompFlag == 3) {
  26741. /* encode Signed Encrypted Compressed FirmwarePkgData */
  26742. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  26743. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  26744. testVectors[i].privateKey, testVectors[i].privateKeySz,
  26745. testVectors[i].encryptOID, testVectors[i].signOID,
  26746. testVectors[i].hashOID, (byte*)testVectors[i].content,
  26747. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  26748. testVectors[i].unprotectedAttribsSz,
  26749. testVectors[i].signedAttribs,
  26750. testVectors[i].signedAttribsSz, out, outSz);
  26751. if (encodedSz <= 0) {
  26752. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26753. wc_PKCS7_Free(pkcs7);
  26754. return -12550;
  26755. }
  26756. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  26757. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26758. } else {
  26759. /* unsupported SignedData single-shot combination */
  26760. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26761. wc_PKCS7_Free(pkcs7);
  26762. return -12551;
  26763. }
  26764. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  26765. /* write PKCS#7 to output file for more testing */
  26766. file = XFOPEN(testVectors[i].outFileName, "wb");
  26767. if (!file) {
  26768. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26769. wc_PKCS7_Free(pkcs7);
  26770. return -12552;
  26771. }
  26772. ret = (int)XFWRITE(out, 1, encodedSz, file);
  26773. XFCLOSE(file);
  26774. if (ret != (int)encodedSz) {
  26775. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26776. wc_PKCS7_Free(pkcs7);
  26777. return -12553;
  26778. }
  26779. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  26780. wc_PKCS7_Free(pkcs7);
  26781. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  26782. if (pkcs7 == NULL)
  26783. return -12554;
  26784. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  26785. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  26786. if (ret < 0) {
  26787. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26788. wc_PKCS7_Free(pkcs7);
  26789. return -12555;
  26790. }
  26791. #ifndef NO_PKCS7_STREAM
  26792. {
  26793. word32 z;
  26794. for (z = 0; z < outSz && ret != 0; z++) {
  26795. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  26796. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  26797. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26798. wc_PKCS7_Free(pkcs7);
  26799. printf("unexpected error %d\n", ret);
  26800. return -12556;
  26801. }
  26802. }
  26803. }
  26804. #endif
  26805. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  26806. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26807. wc_PKCS7_Free(pkcs7);
  26808. return -12557;
  26809. }
  26810. if (testVectors[i].encCompFlag == 0) {
  26811. /* verify decoded content matches expected */
  26812. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  26813. XMEMCMP(pkcs7->content, testVectors[i].content,
  26814. pkcs7->contentSz)) {
  26815. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26816. wc_PKCS7_Free(pkcs7);
  26817. return -12558;
  26818. }
  26819. }
  26820. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26821. else if (testVectors[i].encCompFlag == 1) {
  26822. /* decrypt inner encryptedData */
  26823. pkcs7->encryptionKey = testVectors[i].encryptKey;
  26824. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  26825. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  26826. pkcs7->contentSz, out, outSz);
  26827. if (ret < 0) {
  26828. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26829. wc_PKCS7_Free(pkcs7);
  26830. return -12559;
  26831. }
  26832. /* compare decrypted to expected */
  26833. if (((word32)ret != testVectors[i].contentSz) ||
  26834. XMEMCMP(out, testVectors[i].content, ret)) {
  26835. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26836. wc_PKCS7_Free(pkcs7);
  26837. return -12560;
  26838. }
  26839. }
  26840. #endif
  26841. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  26842. else if (testVectors[i].encCompFlag == 2) {
  26843. /* decompress inner compressedData */
  26844. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  26845. pkcs7->contentSz, out, outSz);
  26846. if (ret < 0) {
  26847. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26848. wc_PKCS7_Free(pkcs7);
  26849. return -12561;
  26850. }
  26851. /* compare decompressed to expected */
  26852. if (((word32)ret != testVectors[i].contentSz) ||
  26853. XMEMCMP(out, testVectors[i].content, ret)) {
  26854. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26855. wc_PKCS7_Free(pkcs7);
  26856. return -12562;
  26857. }
  26858. }
  26859. #ifndef NO_PKCS7_ENCRYPTED_DATA
  26860. else if (testVectors[i].encCompFlag == 3) {
  26861. byte* encryptedTmp;
  26862. int encryptedTmpSz;
  26863. encryptedTmpSz = FOURK_BUF;
  26864. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  26865. DYNAMIC_TYPE_TMP_BUFFER);
  26866. if (encryptedTmp == NULL) {
  26867. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26868. wc_PKCS7_Free(pkcs7);
  26869. return -12563;
  26870. }
  26871. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  26872. /* decrypt inner encryptedData */
  26873. pkcs7->encryptionKey = testVectors[i].encryptKey;
  26874. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  26875. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  26876. pkcs7->contentSz, encryptedTmp,
  26877. encryptedTmpSz);
  26878. if (encryptedTmpSz < 0) {
  26879. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26880. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26881. wc_PKCS7_Free(pkcs7);
  26882. return -12564;
  26883. }
  26884. /* decompress inner compressedData */
  26885. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  26886. encryptedTmpSz, out, outSz);
  26887. if (ret < 0) {
  26888. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26889. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26890. wc_PKCS7_Free(pkcs7);
  26891. return -12565;
  26892. }
  26893. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26894. /* compare decompressed to expected */
  26895. if (((word32)ret != testVectors[i].contentSz) ||
  26896. XMEMCMP(out, testVectors[i].content, ret)) {
  26897. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26898. wc_PKCS7_Free(pkcs7);
  26899. return -12566;
  26900. }
  26901. }
  26902. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  26903. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  26904. wc_PKCS7_Free(pkcs7);
  26905. }
  26906. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26907. wc_FreeRng(&rng);
  26908. if (ret > 0)
  26909. return 0;
  26910. (void)eccClientCertBuf;
  26911. (void)eccClientCertBufSz;
  26912. (void)eccClientPrivKeyBuf;
  26913. (void)eccClientPrivKeyBufSz;
  26914. (void)rsaClientCertBuf;
  26915. (void)rsaClientCertBufSz;
  26916. (void)rsaClientPrivKeyBuf;
  26917. (void)rsaClientPrivKeyBufSz;
  26918. (void)rsaServerCertBuf;
  26919. (void)rsaServerCertBufSz;
  26920. (void)rsaServerPrivKeyBuf;
  26921. (void)rsaServerPrivKeyBufSz;
  26922. (void)rsaCaCertBuf;
  26923. (void)rsaCaCertBufSz;
  26924. (void)rsaCaPrivKeyBuf;
  26925. (void)rsaCaPrivKeyBufSz;
  26926. return ret;
  26927. }
  26928. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  26929. {
  26930. int ret = 0;
  26931. byte* rsaClientCertBuf = NULL;
  26932. byte* rsaServerCertBuf = NULL;
  26933. byte* rsaCaCertBuf = NULL;
  26934. byte* eccClientCertBuf = NULL;
  26935. byte* rsaClientPrivKeyBuf = NULL;
  26936. byte* rsaServerPrivKeyBuf = NULL;
  26937. byte* rsaCaPrivKeyBuf = NULL;
  26938. byte* eccClientPrivKeyBuf = NULL;
  26939. word32 rsaClientCertBufSz = 0;
  26940. word32 rsaServerCertBufSz = 0;
  26941. word32 rsaCaCertBufSz = 0;
  26942. word32 eccClientCertBufSz = 0;
  26943. word32 rsaClientPrivKeyBufSz = 0;
  26944. word32 rsaServerPrivKeyBufSz = 0;
  26945. word32 rsaCaPrivKeyBufSz = 0;
  26946. word32 eccClientPrivKeyBufSz = 0;
  26947. #ifndef NO_RSA
  26948. /* read client RSA cert and key in DER format */
  26949. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26950. DYNAMIC_TYPE_TMP_BUFFER);
  26951. if (rsaClientCertBuf == NULL)
  26952. ret = -12600;
  26953. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26954. DYNAMIC_TYPE_TMP_BUFFER);
  26955. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  26956. ret = -12601;
  26957. }
  26958. rsaClientCertBufSz = FOURK_BUF;
  26959. rsaClientPrivKeyBufSz = FOURK_BUF;
  26960. /* read server RSA cert and key in DER format */
  26961. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26962. DYNAMIC_TYPE_TMP_BUFFER);
  26963. if (ret == 0 && rsaServerCertBuf == NULL)
  26964. ret = -12602;
  26965. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26966. DYNAMIC_TYPE_TMP_BUFFER);
  26967. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  26968. ret = -12603;
  26969. }
  26970. rsaServerCertBufSz = FOURK_BUF;
  26971. rsaServerPrivKeyBufSz = FOURK_BUF;
  26972. /* read CA RSA cert and key in DER format, for use with server cert */
  26973. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26974. DYNAMIC_TYPE_TMP_BUFFER);
  26975. if (ret == 0 && rsaCaCertBuf == NULL)
  26976. ret = -12604;
  26977. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26978. DYNAMIC_TYPE_TMP_BUFFER);
  26979. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  26980. ret = -12605;
  26981. }
  26982. rsaCaCertBufSz = FOURK_BUF;
  26983. rsaCaPrivKeyBufSz = FOURK_BUF;
  26984. #endif /* NO_RSA */
  26985. #ifdef HAVE_ECC
  26986. /* read client ECC cert and key in DER format */
  26987. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26988. DYNAMIC_TYPE_TMP_BUFFER);
  26989. if (ret == 0 && eccClientCertBuf == NULL) {
  26990. ret = -12606;
  26991. }
  26992. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26993. DYNAMIC_TYPE_TMP_BUFFER);
  26994. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  26995. ret = -12607;
  26996. }
  26997. eccClientCertBufSz = FOURK_BUF;
  26998. eccClientPrivKeyBufSz = FOURK_BUF;
  26999. #endif /* HAVE_ECC */
  27000. if (ret >= 0)
  27001. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  27002. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  27003. rsaServerCertBuf, &rsaServerCertBufSz,
  27004. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  27005. rsaCaCertBuf, &rsaCaCertBufSz,
  27006. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  27007. eccClientCertBuf, &eccClientCertBufSz,
  27008. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  27009. if (ret < 0) {
  27010. ret = -12608;
  27011. }
  27012. if (ret >= 0)
  27013. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  27014. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  27015. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  27016. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  27017. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  27018. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  27019. eccClientCertBuf, (word32)eccClientCertBufSz,
  27020. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  27021. if (ret >= 0)
  27022. ret = pkcs7signed_run_SingleShotVectors(
  27023. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  27024. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  27025. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  27026. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  27027. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  27028. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  27029. eccClientCertBuf, (word32)eccClientCertBufSz,
  27030. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  27031. #ifndef NO_AES
  27032. if (ret >= 0)
  27033. ret = pkcs7callback_test(
  27034. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  27035. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  27036. #endif
  27037. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27038. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27039. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27040. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27041. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27042. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27043. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27044. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27045. return ret;
  27046. }
  27047. #endif /* HAVE_PKCS7 */
  27048. #ifdef HAVE_VALGRIND
  27049. /* Need a static build to have access to symbols. */
  27050. /* Maximum number of bytes in a number to test. */
  27051. #define MP_MAX_TEST_BYTE_LEN 32
  27052. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  27053. {
  27054. byte d[MP_MAX_TEST_BYTE_LEN];
  27055. int ret;
  27056. (void)heap;
  27057. do {
  27058. ret = wc_RNG_GenerateBlock(rng, d, len);
  27059. if (ret != 0)
  27060. return ret;
  27061. ret = mp_read_unsigned_bin(n, d, len);
  27062. if (ret != 0)
  27063. return ret;
  27064. } while (mp_iszero(n));
  27065. return 0;
  27066. }
  27067. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  27068. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  27069. {
  27070. int i, j;
  27071. mp_digit rem;
  27072. mp_digit rem2;
  27073. for (i = 0; i < 10; i++) {
  27074. for (j = 1; j < 10; j++) {
  27075. if (randNum(a, j, rng, NULL) != 0)
  27076. return -12620;
  27077. if (mp_div_3(a, r, &rem) != 0)
  27078. return -12621;
  27079. if (mp_mul_d(r, 3, r) != 0)
  27080. return -12622;
  27081. if (mp_add_d(r, rem, r) != 0)
  27082. return -12623;
  27083. if (mp_cmp(r, a) != MP_EQ)
  27084. return -12624;
  27085. }
  27086. }
  27087. if (mp_div_3(a, r, &rem) != 0)
  27088. return -12625;
  27089. if (mp_div_3(a, a, NULL) != 0)
  27090. return -12626;
  27091. if (mp_cmp(r, a) != MP_EQ)
  27092. return -12627;
  27093. #if defined(WOLFSSL_SP_MATH_ALL)
  27094. if (mp_div_d(a, 10, r, &rem) != 0)
  27095. return -12628;
  27096. if (mp_div_d(a, 10, a, NULL) != 0)
  27097. return -12629;
  27098. if (mp_cmp(r, a) != MP_EQ)
  27099. return -12630;
  27100. if (mp_div_d(a, 12, r, &rem) != 0)
  27101. return -12631;
  27102. if (mp_div_d(a, 12, a, NULL) != 0)
  27103. return -12632;
  27104. if (mp_cmp(r, a) != MP_EQ)
  27105. return -12633;
  27106. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  27107. return -12634;
  27108. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  27109. return -12635;
  27110. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  27111. return -12636;
  27112. if (mp_cmp(r, a) != MP_EQ)
  27113. return -12637;
  27114. if (rem != rem2)
  27115. return -12638;
  27116. #else
  27117. (void)rem2;
  27118. #endif
  27119. return 0;
  27120. }
  27121. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  27122. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  27123. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  27124. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  27125. {
  27126. int ret;
  27127. int i, j;
  27128. int size;
  27129. char str[30];
  27130. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  27131. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  27132. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  27133. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  27134. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  27135. for (i = 0; i < 10; i++) {
  27136. for (j = 2; j < 12; j++) {
  27137. if (randNum(a, j, rng, NULL) != 0)
  27138. return -12640;
  27139. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  27140. return -12641;
  27141. mp_toradix(a, str, MP_RADIX_DEC);
  27142. if ((int)XSTRLEN(str) != size - 1)
  27143. return -12642;
  27144. mp_read_radix(r, str, MP_RADIX_DEC);
  27145. if (mp_cmp(a, r) != MP_EQ)
  27146. return -12643;
  27147. }
  27148. }
  27149. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  27150. return -12644;
  27151. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  27152. return -12645;
  27153. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  27154. return -12646;
  27155. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  27156. return -12647;
  27157. if (!mp_iszero(r))
  27158. return -12648;
  27159. mp_set(r, 1);
  27160. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  27161. return -12649;
  27162. if (!mp_iszero(r))
  27163. return -12650;
  27164. mp_zero(a);
  27165. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  27166. if (ret != 0)
  27167. return -12651;
  27168. if (size != 2)
  27169. return -12652;
  27170. ret = mp_toradix(a, str, MP_RADIX_DEC);
  27171. if (ret != 0)
  27172. return -12653;
  27173. if ((int)XSTRLEN(str) != size - 1)
  27174. return -12654;
  27175. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  27176. if (ret != 0)
  27177. return -12655;
  27178. if (!mp_iszero(r))
  27179. return -12656;
  27180. return 0;
  27181. }
  27182. #endif
  27183. #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC)
  27184. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  27185. {
  27186. int ret;
  27187. int i, j;
  27188. int size;
  27189. char str[30];
  27190. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  27191. static char longStr[2 * sizeof(a->dp) + 2];
  27192. #endif
  27193. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  27194. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  27195. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  27196. for (i = 0; i < 10; i++) {
  27197. for (j = 2; j < 12; j++) {
  27198. if (randNum(a, j, rng, NULL) != 0)
  27199. return -12660;
  27200. mp_radix_size(a, MP_RADIX_HEX, &size);
  27201. mp_toradix(a, str, MP_RADIX_HEX);
  27202. if ((int)XSTRLEN(str) != size - 1)
  27203. return -12661;
  27204. mp_read_radix(r, str, MP_RADIX_HEX);
  27205. if (mp_cmp(a, r) != MP_EQ)
  27206. return -12662;
  27207. }
  27208. }
  27209. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  27210. return -12663;
  27211. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  27212. return -12664;
  27213. mp_set(r, 1);
  27214. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  27215. return -12665;
  27216. if (!mp_iszero(r))
  27217. return -12666;
  27218. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  27219. /* Fixed MP data size - string can be too long. */
  27220. longStr[0] = '8';
  27221. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  27222. longStr[sizeof(longStr)-1] = '\0';
  27223. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  27224. return -12667;
  27225. #endif
  27226. mp_zero(a);
  27227. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  27228. if (ret != 0)
  27229. return -12668;
  27230. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  27231. if (size != 3)
  27232. #else
  27233. if (size != 2)
  27234. #endif
  27235. return -12669;
  27236. ret = mp_toradix(a, str, MP_RADIX_HEX);
  27237. if (ret != 0)
  27238. return -12670;
  27239. if ((int)XSTRLEN(str) != size - 1)
  27240. return -12671;
  27241. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  27242. if (ret != 0)
  27243. return -12672;
  27244. if (!mp_iszero(r))
  27245. return -12673;
  27246. #ifdef WOLFSSL_SP_MATH
  27247. ret = mp_toradix(a, str, 8);
  27248. if (ret != MP_VAL)
  27249. return -12674;
  27250. ret = mp_radix_size(a, 8, &size);
  27251. if (ret != MP_VAL)
  27252. return -12675;
  27253. #endif
  27254. return 0;
  27255. }
  27256. #endif
  27257. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  27258. {
  27259. int i;
  27260. if (randNum(a, 4, rng, NULL) != 0)
  27261. return -12680;
  27262. for (i = 0; i < 4; i++) {
  27263. mp_copy(r1, a);
  27264. if (mp_lshd(r1, i) != MP_OKAY)
  27265. return -12681;
  27266. mp_rshd(r1, i);
  27267. if (mp_cmp(a, r1) != MP_EQ)
  27268. return -12682;
  27269. }
  27270. for (i = 0; i < DIGIT_BIT+1; i++) {
  27271. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  27272. return -12683;
  27273. mp_rshb(r1, i);
  27274. if (mp_cmp(a, r1) != MP_EQ)
  27275. return -12684;
  27276. }
  27277. return 0;
  27278. }
  27279. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  27280. {
  27281. int i, j;
  27282. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  27283. mp_zero(a);
  27284. mp_set_bit(a, i);
  27285. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  27286. return -12690;
  27287. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  27288. mp_zero(r1);
  27289. mp_set_bit(r1, i);
  27290. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  27291. return -12691;
  27292. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  27293. return -12692;
  27294. if (mp_cmp(a, r1) != MP_EQ)
  27295. return -12693;
  27296. }
  27297. }
  27298. mp_zero(r1);
  27299. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  27300. return -12694;
  27301. if (r1->used != 1)
  27302. return -12695;
  27303. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  27304. return -12696;
  27305. if (r1->used != 0)
  27306. return -12697;
  27307. #ifdef WOLFSSL_SP_MATH
  27308. if (mp_set(r1, 1) != MP_OKAY)
  27309. return -12698;
  27310. if (mp_mul_2d(r1, SP_INT_MAX_BITS - 1, r1) != MP_OKAY)
  27311. return -12699;
  27312. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  27313. return -12700;
  27314. if (mp_mul_2d(r1, 1, r1) != MP_OKAY)
  27315. return -12701;
  27316. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  27317. return -12702;
  27318. if (mp_add_d(r1, 1, r1) == MP_OKAY)
  27319. return -12703;
  27320. #endif
  27321. return 0;
  27322. }
  27323. static int mp_test_read_to_bin(mp_int* a)
  27324. {
  27325. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  27326. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  27327. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  27328. };
  27329. byte out[24];
  27330. int i, j, k;
  27331. const byte* p;
  27332. int ret;
  27333. for (i = 0; i < (int)sizeof(in); i++) {
  27334. p = in + sizeof(in) - i;
  27335. ret = mp_read_unsigned_bin(a, p, i);
  27336. if (ret != 0)
  27337. return -12710;
  27338. for (j = i; j < (int)sizeof(out); j++) {
  27339. XMEMSET(out, 0xff, sizeof(out));
  27340. ret = mp_to_unsigned_bin_len(a, out, j);
  27341. if (ret != 0)
  27342. return -12711;
  27343. for (k = 0; k < j - i; k++) {
  27344. if (out[k] != 0)
  27345. return -12712;
  27346. }
  27347. for (; k < j; k++) {
  27348. if (out[k] != p[k - (j - i)])
  27349. return -12713;
  27350. }
  27351. }
  27352. }
  27353. ret = mp_read_unsigned_bin(a, NULL, 0);
  27354. if (ret != 0)
  27355. return -12714;
  27356. if (!mp_iszero(a))
  27357. return -12715;
  27358. return 0;
  27359. }
  27360. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  27361. static int mp_test_set_int(mp_int* a)
  27362. {
  27363. #if SP_ULONG_BITS == 64
  27364. unsigned long n = 0xfedcba9876543210UL;
  27365. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  27366. byte out[8] = { 0 };
  27367. #elif SP_ULONG_BITS == 32
  27368. unsigned long n = 0xfedcba98UL;
  27369. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  27370. byte out[4] = { 0 };
  27371. #elif SP_ULONG_BITS == 16
  27372. unsigned long n = 0xfedc;
  27373. byte exp[2] = { 0xfe, 0xdc };
  27374. byte out[2] = { 0 };
  27375. #elif SP_ULONG_BITS == 8
  27376. unsigned long n = 0xfe;
  27377. byte exp[1] = { 0xfe };
  27378. byte out[1] = { 0 };
  27379. #endif
  27380. int ret;
  27381. ret = mp_set_int(a, n);
  27382. if (ret != 0)
  27383. return -12720;
  27384. ret = mp_unsigned_bin_size(a);
  27385. if (ret != sizeof(exp))
  27386. return -12721;
  27387. ret = mp_to_unsigned_bin(a, out);
  27388. if (ret != 0)
  27389. return -12722;
  27390. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  27391. return -12723;
  27392. return 0;
  27393. }
  27394. #endif
  27395. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  27396. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  27397. {
  27398. byte buffer[16];
  27399. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  27400. char hexStr[] = "abcdef0123456789";
  27401. #ifndef WOLFSSL_SP_INT_NEGATIVE
  27402. char negStr[] = "-1234";
  27403. #endif
  27404. #endif
  27405. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  27406. defined(HAVE_COMP_KEY)
  27407. char decStr[] = "0987654321";
  27408. #endif
  27409. int ret;
  27410. #ifdef WOLFSSL_SP_MATH_ALL
  27411. mp_digit rho;
  27412. int size;
  27413. #endif
  27414. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27415. int result;
  27416. #endif
  27417. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  27418. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  27419. mp_digit rd;
  27420. #endif
  27421. (void)rng;
  27422. (void)r;
  27423. ret = mp_init(NULL);
  27424. if (ret != MP_VAL)
  27425. return -12730;
  27426. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  27427. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  27428. if (ret != MP_OKAY)
  27429. return -12731;
  27430. #endif
  27431. mp_free(NULL);
  27432. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  27433. ret = mp_grow(NULL, 1);
  27434. if (ret != MP_VAL)
  27435. return -12732;
  27436. #ifdef WOLFSSL_SP_MATH
  27437. ret = mp_grow(a, SP_INT_DIGITS + 1);
  27438. if (ret != MP_MEM)
  27439. return -12733;
  27440. #endif
  27441. #endif
  27442. mp_clear(NULL);
  27443. ret = mp_abs(NULL, NULL);
  27444. if (ret != MP_VAL)
  27445. return -12734;
  27446. ret = mp_abs(a, NULL);
  27447. if (ret != MP_VAL)
  27448. return -12735;
  27449. ret = mp_abs(NULL, b);
  27450. if (ret != MP_VAL)
  27451. return -12736;
  27452. ret = mp_unsigned_bin_size(NULL);
  27453. if (ret != 0)
  27454. return -12737;
  27455. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  27456. if (ret != MP_VAL)
  27457. return -12738;
  27458. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  27459. if (ret != MP_VAL)
  27460. return -12739;
  27461. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  27462. if (ret != MP_VAL)
  27463. return -12740;
  27464. ret = mp_read_unsigned_bin(a, buffer,
  27465. (SP_INT_DIGITS - 1) * SP_WORD_SIZEOF + 1);
  27466. if (ret != MP_VAL)
  27467. return -12741;
  27468. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  27469. ret = mp_read_radix(NULL, NULL, 16);
  27470. if (ret != MP_VAL)
  27471. return -12742;
  27472. ret = mp_read_radix(a, NULL, 16);
  27473. if (ret != MP_VAL)
  27474. return -12743;
  27475. ret = mp_read_radix(NULL, hexStr, 16);
  27476. if (ret != MP_VAL)
  27477. return -12744;
  27478. #ifndef WOLFSSL_SP_INT_NEGATIVE
  27479. ret = mp_read_radix(a, negStr, 16);
  27480. if (ret != MP_VAL)
  27481. return -12745;
  27482. #ifdef WOLFSSL_SP_MATH_ALL
  27483. ret = mp_read_radix(a, negStr, 10);
  27484. if (ret != MP_VAL)
  27485. return -12746;
  27486. #endif /* WOLFSSL_SP_MATH_ALL */
  27487. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  27488. #endif
  27489. #ifndef WOLFSSL_SP_MATH_ALL
  27490. /* Radix 10 only supported with ALL. */
  27491. ret = mp_read_radix(a, decStr, 10);
  27492. if (ret != MP_VAL)
  27493. return -12747;
  27494. #endif
  27495. /* Radix 8 not supported SP_INT. */
  27496. ret = mp_read_radix(a, "0123", 8);
  27497. if (ret != MP_VAL)
  27498. return -12748;
  27499. ret = mp_count_bits(NULL);
  27500. if (ret != 0)
  27501. return -12749;
  27502. ret = mp_is_bit_set(NULL, 0);
  27503. if (ret != 0)
  27504. return -12750;
  27505. ret = mp_leading_bit(NULL);
  27506. if (ret != 0)
  27507. return -12751;
  27508. mp_zero(a);
  27509. ret = mp_leading_bit(a);
  27510. if (ret != 0)
  27511. return -12752;
  27512. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27513. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  27514. !defined(NO_RSA)
  27515. ret = mp_set_bit(NULL, 1);
  27516. if (ret != MP_VAL)
  27517. return -12753;
  27518. #endif
  27519. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  27520. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27521. ret = mp_to_unsigned_bin(NULL, NULL);
  27522. if (ret != MP_VAL)
  27523. return -12754;
  27524. ret = mp_to_unsigned_bin(a, NULL);
  27525. if (ret != MP_VAL)
  27526. return -12755;
  27527. ret = mp_to_unsigned_bin(NULL, buffer);
  27528. if (ret != MP_VAL)
  27529. return -12756;
  27530. #endif
  27531. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  27532. if (ret != MP_VAL)
  27533. return -12757;
  27534. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  27535. if (ret != MP_VAL)
  27536. return -12758;
  27537. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  27538. if (ret != MP_VAL)
  27539. return -12759;
  27540. #ifdef WOLFSSL_SP_MATH_ALL
  27541. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  27542. if (ret != MP_VAL)
  27543. return -12760;
  27544. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  27545. if (ret != MP_VAL)
  27546. return -12761;
  27547. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  27548. if (ret != MP_VAL)
  27549. return -12762;
  27550. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  27551. if (ret != MP_OKAY)
  27552. return -12763;
  27553. #endif
  27554. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  27555. ret = mp_copy(NULL, NULL);
  27556. if (ret != MP_VAL)
  27557. return -12764;
  27558. ret = mp_copy(a, NULL);
  27559. if (ret != MP_VAL)
  27560. return -12765;
  27561. ret = mp_copy(NULL, b);
  27562. if (ret != MP_VAL)
  27563. return -12766;
  27564. #endif
  27565. #if defined(WOLFSSL_KEY_GEN)
  27566. ret = sp_2expt(NULL, 1);
  27567. if (ret != MP_VAL)
  27568. return -12767;
  27569. #endif
  27570. ret = mp_set(NULL, 0);
  27571. if (ret != MP_VAL)
  27572. return -12768;
  27573. ret = mp_cmp_d(NULL, 0);
  27574. if (ret != MP_LT)
  27575. return -12769;
  27576. ret = mp_cmp(NULL, NULL);
  27577. if (ret != MP_EQ)
  27578. return -12770;
  27579. ret = mp_cmp(a, NULL);
  27580. if (ret != MP_GT)
  27581. return -12771;
  27582. ret = mp_cmp(NULL, b);
  27583. if (ret != MP_LT)
  27584. return -12772;
  27585. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27586. mp_rshd(NULL, 1);
  27587. #endif
  27588. mp_zero(NULL);
  27589. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  27590. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27591. ret = mp_lshd(NULL, 0);
  27592. if (ret != MP_VAL)
  27593. return -12773;
  27594. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  27595. if (ret != MP_VAL)
  27596. return -12774;
  27597. #endif
  27598. #if defined(WOLFSSL_SP_MATH_ALL)
  27599. ret = mp_div(NULL, NULL, a, b);
  27600. if (ret != MP_VAL)
  27601. return -12775;
  27602. ret = mp_div(a, NULL, a, b);
  27603. if (ret != MP_VAL)
  27604. return -12776;
  27605. ret = mp_div(NULL, b, a, b);
  27606. if (ret != MP_VAL)
  27607. return -12777;
  27608. ret = mp_div(a, b, NULL, NULL);
  27609. if (ret != MP_VAL)
  27610. return -12778;
  27611. #endif
  27612. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  27613. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27614. ret = mp_mod(NULL, NULL, NULL);
  27615. if (ret != MP_VAL)
  27616. return -12779;
  27617. ret = mp_mod(a, NULL, NULL);
  27618. if (ret != MP_VAL)
  27619. return -12780;
  27620. ret = mp_mod(NULL, b, NULL);
  27621. if (ret != MP_VAL)
  27622. return -12781;
  27623. ret = mp_mod(NULL, NULL, r);
  27624. if (ret != MP_VAL)
  27625. return -12782;
  27626. ret = mp_mod(a, b, NULL);
  27627. if (ret != MP_VAL)
  27628. return -12783;
  27629. ret = mp_mod(a, NULL, r);
  27630. if (ret != MP_VAL)
  27631. return -12784;
  27632. ret = mp_mod(NULL, b, r);
  27633. if (ret != MP_VAL)
  27634. return -12785;
  27635. #endif
  27636. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  27637. ret = mp_set_int(NULL, 0);
  27638. if (ret != MP_VAL)
  27639. return -12786;
  27640. #endif
  27641. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  27642. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  27643. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  27644. if (ret != MP_VAL)
  27645. return 9950;
  27646. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  27647. if (ret != MP_VAL)
  27648. return 9951;
  27649. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  27650. if (ret != MP_VAL)
  27651. return 9952;
  27652. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  27653. if (ret != MP_VAL)
  27654. return 9953;
  27655. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  27656. if (ret != MP_VAL)
  27657. return 9954;
  27658. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  27659. if (ret != MP_VAL)
  27660. return 9955;
  27661. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  27662. if (ret != MP_VAL)
  27663. return 9956;
  27664. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  27665. if (ret != MP_VAL)
  27666. return 9957;
  27667. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  27668. if (ret != MP_VAL)
  27669. return 9958;
  27670. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  27671. if (ret != MP_VAL)
  27672. return 9960;
  27673. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  27674. if (ret != MP_VAL)
  27675. return 9961;
  27676. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  27677. if (ret != MP_VAL)
  27678. return 9962;
  27679. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  27680. if (ret != MP_VAL)
  27681. return 9963;
  27682. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  27683. if (ret != MP_VAL)
  27684. return 9964;
  27685. ret = mp_exptmod_nct(a, a, a, NULL);
  27686. if (ret != MP_VAL)
  27687. return 9965;
  27688. ret = mp_exptmod_nct(a, a, NULL, a);
  27689. if (ret != MP_VAL)
  27690. return 9966;
  27691. ret = mp_exptmod_nct(a, NULL, a, a);
  27692. if (ret != MP_VAL)
  27693. return 9967;
  27694. ret = mp_exptmod_nct(NULL, a, a, a);
  27695. if (ret != MP_VAL)
  27696. return 9968;
  27697. #endif
  27698. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  27699. !defined(WC_NO_RNG)
  27700. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  27701. if (ret != MP_VAL)
  27702. return -12787;
  27703. ret = mp_rand_prime(a, 32, NULL, NULL);
  27704. if (ret != MP_VAL)
  27705. return -12788;
  27706. ret = mp_rand_prime(NULL, 32, rng, NULL);
  27707. if (ret != MP_VAL)
  27708. return -12789;
  27709. ret = mp_rand_prime(a, 0, rng, NULL);
  27710. if (ret != MP_VAL)
  27711. return -9969;
  27712. #endif
  27713. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27714. ret = mp_mul(NULL, NULL, NULL);
  27715. if (ret != MP_VAL)
  27716. return -12790;
  27717. ret = mp_mul(a, NULL, NULL);
  27718. if (ret != MP_VAL)
  27719. return -12791;
  27720. ret = mp_mul(NULL, b, NULL);
  27721. if (ret != MP_VAL)
  27722. return -12792;
  27723. ret = mp_mul(NULL, NULL, r);
  27724. if (ret != MP_VAL)
  27725. return -12793;
  27726. ret = mp_mul(a, b, NULL);
  27727. if (ret != MP_VAL)
  27728. return -12794;
  27729. ret = mp_mul(a, NULL, r);
  27730. if (ret != MP_VAL)
  27731. return -12795;
  27732. ret = mp_mul(NULL, b, r);
  27733. if (ret != MP_VAL)
  27734. return -12796;
  27735. #endif
  27736. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27737. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  27738. ret = mp_sqr(NULL, NULL);
  27739. if (ret != MP_VAL)
  27740. return -12797;
  27741. ret = mp_sqr(a, NULL);
  27742. if (ret != MP_VAL)
  27743. return -12798;
  27744. ret = mp_sqr(NULL, r);
  27745. if (ret != MP_VAL)
  27746. return -12799;
  27747. #endif
  27748. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  27749. ret = mp_sqrmod(NULL, NULL, NULL);
  27750. if (ret != MP_VAL)
  27751. return -12800;
  27752. ret = mp_sqrmod(a, NULL, NULL);
  27753. if (ret != MP_VAL)
  27754. return -12801;
  27755. ret = mp_sqrmod(NULL, a, NULL);
  27756. if (ret != MP_VAL)
  27757. return -12802;
  27758. ret = mp_sqrmod(NULL, NULL, a);
  27759. if (ret != MP_VAL)
  27760. return -12803;
  27761. ret = mp_sqrmod(a, b, NULL);
  27762. if (ret != MP_VAL)
  27763. return -12804;
  27764. ret = mp_sqrmod(a, NULL, b);
  27765. if (ret != MP_VAL)
  27766. return -12805;
  27767. ret = mp_sqrmod(NULL, a, b);
  27768. if (ret != MP_VAL)
  27769. return -12806;
  27770. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  27771. if (ret != MP_VAL)
  27772. return -12807;
  27773. ret = mp_mulmod(a, NULL, NULL, NULL);
  27774. if (ret != MP_VAL)
  27775. return -12808;
  27776. ret = mp_mulmod(NULL, a, NULL, NULL);
  27777. if (ret != MP_VAL)
  27778. return -12809;
  27779. ret = mp_mulmod(NULL, NULL, a, NULL);
  27780. if (ret != MP_VAL)
  27781. return -12810;
  27782. ret = mp_mulmod(NULL, NULL, NULL, a);
  27783. if (ret != MP_VAL)
  27784. return -12811;
  27785. ret = mp_mulmod(a, b, b, NULL);
  27786. if (ret != MP_VAL)
  27787. return -12812;
  27788. ret = mp_mulmod(a, b, NULL, a);
  27789. if (ret != MP_VAL)
  27790. return -12813;
  27791. ret = mp_mulmod(a, NULL, b, a);
  27792. if (ret != MP_VAL)
  27793. return -12814;
  27794. ret = mp_mulmod(NULL, b, b, a);
  27795. if (ret != MP_VAL)
  27796. return -12815;
  27797. #endif
  27798. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  27799. !defined(NO_RSA) || !defined(NO_DSA)
  27800. ret = mp_add_d(NULL, 1, NULL);
  27801. if (ret != MP_VAL)
  27802. return -12816;
  27803. ret = mp_add_d(a, 1, NULL);
  27804. if (ret != MP_VAL)
  27805. return -12817;
  27806. ret = mp_add_d(NULL, 1, b);
  27807. if (ret != MP_VAL)
  27808. return -12818;
  27809. #endif
  27810. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  27811. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  27812. ret = mp_sub_d(NULL, 1, NULL);
  27813. if (ret != MP_VAL)
  27814. return -12819;
  27815. ret = mp_sub_d(a, 1, NULL);
  27816. if (ret != MP_VAL)
  27817. return -12820;
  27818. ret = mp_sub_d(NULL, 1, b);
  27819. if (ret != MP_VAL)
  27820. return -12821;
  27821. #endif
  27822. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  27823. (defined(HAVE_ECC) && defined(FP_ECC))
  27824. ret = mp_div_d(NULL, 0, NULL, NULL);
  27825. if (ret != MP_VAL)
  27826. return -12822;
  27827. ret = mp_div_d(a, 0, NULL, NULL);
  27828. if (ret != MP_VAL)
  27829. return -12823;
  27830. ret = mp_div_d(NULL, 1, NULL, NULL);
  27831. if (ret != MP_VAL)
  27832. return -12824;
  27833. #endif
  27834. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  27835. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  27836. ret = mp_mod_d(NULL, 0, NULL);
  27837. if (ret != MP_VAL)
  27838. return -12825;
  27839. ret = mp_mod_d(a, 0, NULL);
  27840. if (ret != MP_VAL)
  27841. return -12826;
  27842. ret = mp_mod_d(NULL, 0, &rd);
  27843. if (ret != MP_VAL)
  27844. return -12827;
  27845. #endif
  27846. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  27847. ret = mp_gcd(NULL, NULL, NULL);
  27848. if (ret != MP_VAL)
  27849. return -12828;
  27850. ret = mp_gcd(a, NULL, NULL);
  27851. if (ret != MP_VAL)
  27852. return -12829;
  27853. ret = mp_gcd(NULL, a, NULL);
  27854. if (ret != MP_VAL)
  27855. return -12830;
  27856. ret = mp_gcd(NULL, NULL, a);
  27857. if (ret != MP_VAL)
  27858. return -12831;
  27859. ret = mp_gcd(a, b, NULL);
  27860. if (ret != MP_VAL)
  27861. return -12832;
  27862. ret = mp_gcd(a, NULL, b);
  27863. if (ret != MP_VAL)
  27864. return -12833;
  27865. ret = mp_gcd(NULL, a, b);
  27866. if (ret != MP_VAL)
  27867. return -12834;
  27868. #endif
  27869. #ifdef HAVE_ECC
  27870. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  27871. if (ret != MP_VAL)
  27872. return -12835;
  27873. ret = mp_div_2_mod_ct(a, NULL, NULL);
  27874. if (ret != MP_VAL)
  27875. return -12836;
  27876. ret = mp_div_2_mod_ct(NULL, b, NULL);
  27877. if (ret != MP_VAL)
  27878. return -12837;
  27879. ret = mp_div_2_mod_ct(NULL, NULL, a);
  27880. if (ret != MP_VAL)
  27881. return -12838;
  27882. ret = mp_div_2_mod_ct(a, b, NULL);
  27883. if (ret != MP_VAL)
  27884. return -12839;
  27885. ret = mp_div_2_mod_ct(a, b, NULL);
  27886. if (ret != MP_VAL)
  27887. return -12840;
  27888. ret = mp_div_2_mod_ct(NULL, b, a);
  27889. if (ret != MP_VAL)
  27890. return -12841;
  27891. ret = mp_div_2(NULL, NULL);
  27892. if (ret != MP_VAL)
  27893. return -12842;
  27894. ret = mp_div_2(a, NULL);
  27895. if (ret != MP_VAL)
  27896. return -12843;
  27897. ret = mp_div_2(NULL, a);
  27898. if (ret != MP_VAL)
  27899. return -12844;
  27900. #endif
  27901. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  27902. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  27903. ret = mp_invmod(NULL, NULL, NULL);
  27904. if (ret != MP_VAL)
  27905. return -12845;
  27906. ret = mp_invmod(a, NULL, NULL);
  27907. if (ret != MP_VAL)
  27908. return -12846;
  27909. ret = mp_invmod(NULL, b, NULL);
  27910. if (ret != MP_VAL)
  27911. return -12847;
  27912. ret = mp_invmod(NULL, NULL, a);
  27913. if (ret != MP_VAL)
  27914. return -12848;
  27915. ret = mp_invmod(a, b, NULL);
  27916. if (ret != MP_VAL)
  27917. return -12849;
  27918. ret = mp_invmod(a, NULL, a);
  27919. if (ret != MP_VAL)
  27920. return -12850;
  27921. ret = mp_invmod(NULL, b, a);
  27922. if (ret != MP_VAL)
  27923. return -12851;
  27924. #endif
  27925. #ifdef HAVE_ECC
  27926. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  27927. if (ret != MP_VAL)
  27928. return -12852;
  27929. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  27930. if (ret != MP_VAL)
  27931. return -12853;
  27932. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  27933. if (ret != MP_VAL)
  27934. return -12854;
  27935. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  27936. if (ret != MP_VAL)
  27937. return -12855;
  27938. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  27939. if (ret != MP_VAL)
  27940. return -12856;
  27941. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  27942. if (ret != MP_VAL)
  27943. return -12857;
  27944. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  27945. if (ret != MP_VAL)
  27946. return -12858;
  27947. #endif
  27948. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  27949. ret = mp_lcm(NULL, NULL, NULL);
  27950. if (ret != MP_VAL)
  27951. return -12859;
  27952. ret = mp_lcm(a, NULL, NULL);
  27953. if (ret != MP_VAL)
  27954. return -12860;
  27955. ret = mp_lcm(NULL, b, NULL);
  27956. if (ret != MP_VAL)
  27957. return -12861;
  27958. ret = mp_lcm(NULL, NULL, a);
  27959. if (ret != MP_VAL)
  27960. return -12862;
  27961. ret = mp_lcm(a, b, NULL);
  27962. if (ret != MP_VAL)
  27963. return -12863;
  27964. ret = mp_lcm(a, NULL, a);
  27965. if (ret != MP_VAL)
  27966. return -12864;
  27967. ret = mp_lcm(NULL, b, a);
  27968. if (ret != MP_VAL)
  27969. return -12865;
  27970. #endif
  27971. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  27972. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  27973. if (ret != MP_VAL)
  27974. return -12866;
  27975. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  27976. if (ret != MP_VAL)
  27977. return -12867;
  27978. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  27979. if (ret != MP_VAL)
  27980. return -12868;
  27981. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  27982. if (ret != MP_VAL)
  27983. return -12869;
  27984. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  27985. if (ret != MP_VAL)
  27986. return -12870;
  27987. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  27988. if (ret != MP_VAL)
  27989. return -12871;
  27990. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  27991. if (ret != MP_VAL)
  27992. return -12872;
  27993. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  27994. if (ret != MP_VAL)
  27995. return -12873;
  27996. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  27997. if (ret != MP_VAL)
  27998. return -12874;
  27999. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  28000. if (ret != MP_VAL)
  28001. return -12875;
  28002. ret = mp_exptmod(a, NULL, NULL, NULL);
  28003. if (ret != MP_VAL)
  28004. return -12876;
  28005. ret = mp_exptmod(NULL, b, NULL, NULL);
  28006. if (ret != MP_VAL)
  28007. return -12877;
  28008. ret = mp_exptmod(NULL, NULL, b, NULL);
  28009. if (ret != MP_VAL)
  28010. return -12878;
  28011. ret = mp_exptmod(NULL, NULL, NULL, a);
  28012. if (ret != MP_VAL)
  28013. return -12879;
  28014. ret = mp_exptmod(a, b, b, NULL);
  28015. if (ret != MP_VAL)
  28016. return -12880;
  28017. ret = mp_exptmod(a, b, NULL, a);
  28018. if (ret != MP_VAL)
  28019. return -12881;
  28020. ret = mp_exptmod(a, NULL, b, a);
  28021. if (ret != MP_VAL)
  28022. return -12882;
  28023. ret = mp_exptmod(NULL, b, b, a);
  28024. if (ret != MP_VAL)
  28025. return -12883;
  28026. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  28027. if (ret != MP_VAL)
  28028. return -12884;
  28029. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  28030. if (ret != MP_VAL)
  28031. return -12885;
  28032. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  28033. if (ret != MP_VAL)
  28034. return -12886;
  28035. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  28036. if (ret != MP_VAL)
  28037. return -12887;
  28038. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  28039. if (ret != MP_VAL)
  28040. return -12888;
  28041. ret = mp_exptmod_nct(a, b, b, NULL);
  28042. if (ret != MP_VAL)
  28043. return -12889;
  28044. ret = mp_exptmod_nct(a, b, NULL, a);
  28045. if (ret != MP_VAL)
  28046. return -12890;
  28047. ret = mp_exptmod_nct(a, NULL, b, a);
  28048. if (ret != MP_VAL)
  28049. return -12891;
  28050. ret = mp_exptmod_nct(NULL, b, b, a);
  28051. if (ret != MP_VAL)
  28052. return -12892;
  28053. #endif
  28054. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  28055. ret = mp_cnt_lsb(NULL);
  28056. if (ret != 0)
  28057. return -12893;
  28058. #endif
  28059. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  28060. ret = mp_prime_is_prime(NULL, 1, NULL);
  28061. if (ret != MP_VAL)
  28062. return -12894;
  28063. ret = mp_prime_is_prime(a, 1, NULL);
  28064. if (ret != MP_VAL)
  28065. return -12895;
  28066. ret = mp_prime_is_prime(NULL, 1, &result);
  28067. if (ret != MP_VAL)
  28068. return -12896;
  28069. ret = mp_prime_is_prime(a, 0, &result);
  28070. if (ret != MP_VAL)
  28071. return -12897;
  28072. ret = mp_prime_is_prime(a, 1024, &result);
  28073. if (ret != MP_VAL)
  28074. return -12898;
  28075. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  28076. if (ret != MP_VAL)
  28077. return -12899;
  28078. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  28079. if (ret != MP_VAL)
  28080. return -12900;
  28081. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  28082. if (ret != MP_VAL)
  28083. return -12901;
  28084. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  28085. if (ret != MP_VAL)
  28086. return -12902;
  28087. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  28088. if (ret != MP_VAL)
  28089. return -12903;
  28090. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  28091. if (ret != MP_VAL)
  28092. return -12904;
  28093. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  28094. if (ret != MP_VAL)
  28095. return -12905;
  28096. #endif
  28097. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  28098. ret = mp_exch(NULL, NULL);
  28099. if (ret != MP_VAL)
  28100. return -12906;
  28101. ret = mp_exch(a, NULL);
  28102. if (ret != MP_VAL)
  28103. return -12907;
  28104. ret = mp_exch(NULL, b);
  28105. if (ret != MP_VAL)
  28106. return -12908;
  28107. #endif
  28108. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  28109. defined(WOLFSSL_SP_MATH_ALL)
  28110. ret = mp_mul_d(NULL, 1, NULL);
  28111. if (ret != MP_VAL)
  28112. return -12909;
  28113. ret = mp_mul_d(a, 1, NULL);
  28114. if (ret != MP_VAL)
  28115. return -12910;
  28116. ret = mp_mul_d(NULL, 1, b);
  28117. if (ret != MP_VAL)
  28118. return -12911;
  28119. #endif
  28120. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  28121. ret = mp_add(NULL, NULL, NULL);
  28122. if (ret != MP_VAL)
  28123. return -12912;
  28124. ret = mp_add(a, NULL, NULL);
  28125. if (ret != MP_VAL)
  28126. return -12913;
  28127. ret = mp_add(NULL, b, NULL);
  28128. if (ret != MP_VAL)
  28129. return -12914;
  28130. ret = mp_add(NULL, NULL, r);
  28131. if (ret != MP_VAL)
  28132. return -12915;
  28133. ret = mp_add(a, b, NULL);
  28134. if (ret != MP_VAL)
  28135. return -12916;
  28136. ret = mp_add(a, NULL, r);
  28137. if (ret != MP_VAL)
  28138. return -12917;
  28139. ret = mp_add(NULL, b, r);
  28140. if (ret != MP_VAL)
  28141. return -12918;
  28142. #endif
  28143. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  28144. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  28145. ret = mp_sub(NULL, NULL, NULL);
  28146. if (ret != MP_VAL)
  28147. return -12919;
  28148. ret = mp_sub(a, NULL, NULL);
  28149. if (ret != MP_VAL)
  28150. return -12920;
  28151. ret = mp_sub(NULL, b, NULL);
  28152. if (ret != MP_VAL)
  28153. return -12921;
  28154. ret = mp_sub(NULL, NULL, r);
  28155. if (ret != MP_VAL)
  28156. return -12922;
  28157. ret = mp_sub(a, b, NULL);
  28158. if (ret != MP_VAL)
  28159. return -12923;
  28160. ret = mp_sub(a, NULL, r);
  28161. if (ret != MP_VAL)
  28162. return -12924;
  28163. ret = mp_sub(NULL, b, r);
  28164. if (ret != MP_VAL)
  28165. return -12925;
  28166. #endif
  28167. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  28168. defined(WOLFSSL_CUSTOM_CURVES))
  28169. ret = mp_addmod(NULL, NULL, NULL, NULL);
  28170. if (ret != MP_VAL)
  28171. return -12926;
  28172. ret = mp_addmod(a, NULL, NULL, NULL);
  28173. if (ret != MP_VAL)
  28174. return -12927;
  28175. ret = mp_addmod(NULL, b, NULL, NULL);
  28176. if (ret != MP_VAL)
  28177. return -12928;
  28178. ret = mp_addmod(NULL, NULL, b, NULL);
  28179. if (ret != MP_VAL)
  28180. return -12929;
  28181. ret = mp_addmod(NULL, NULL, NULL, a);
  28182. if (ret != MP_VAL)
  28183. return -12930;
  28184. ret = mp_addmod(a, b, b, NULL);
  28185. if (ret != MP_VAL)
  28186. return -12931;
  28187. ret = mp_addmod(a, b, NULL, a);
  28188. if (ret != MP_VAL)
  28189. return -12932;
  28190. ret = mp_addmod(a, NULL, b, a);
  28191. if (ret != MP_VAL)
  28192. return -12933;
  28193. ret = mp_addmod(NULL, b, b, a);
  28194. if (ret != MP_VAL)
  28195. return -12934;
  28196. #endif
  28197. #ifdef WOLFSSL_SP_MATH_ALL
  28198. ret = mp_submod(NULL, NULL, NULL, NULL);
  28199. if (ret != MP_VAL)
  28200. return -12935;
  28201. ret = mp_submod(a, NULL, NULL, NULL);
  28202. if (ret != MP_VAL)
  28203. return -12936;
  28204. ret = mp_submod(NULL, b, NULL, NULL);
  28205. if (ret != MP_VAL)
  28206. return -12937;
  28207. ret = mp_submod(NULL, NULL, b, NULL);
  28208. if (ret != MP_VAL)
  28209. return -12938;
  28210. ret = mp_submod(NULL, NULL, NULL, a);
  28211. if (ret != MP_VAL)
  28212. return -12939;
  28213. ret = mp_submod(a, b, b, NULL);
  28214. if (ret != MP_VAL)
  28215. return -12940;
  28216. ret = mp_submod(a, b, NULL, a);
  28217. if (ret != MP_VAL)
  28218. return -12941;
  28219. ret = mp_submod(a, NULL, b, a);
  28220. if (ret != MP_VAL)
  28221. return -12942;
  28222. ret = mp_submod(NULL, b, b, a);
  28223. if (ret != MP_VAL)
  28224. return -12943;
  28225. #endif
  28226. #ifdef WOLFSSL_SP_MATH_ALL
  28227. ret = mp_div_2d(NULL, 1, a, b);
  28228. if (ret != MP_VAL)
  28229. return -12944;
  28230. ret = mp_mod_2d(NULL, 1, NULL);
  28231. if (ret != MP_VAL)
  28232. return -12945;
  28233. ret = mp_mod_2d(a, 1, NULL);
  28234. if (ret != MP_VAL)
  28235. return -12946;
  28236. ret = mp_mod_2d(NULL, 1, b);
  28237. if (ret != MP_VAL)
  28238. return -12947;
  28239. ret = mp_mul_2d(NULL, 1, NULL);
  28240. if (ret != MP_VAL)
  28241. return -12948;
  28242. ret = mp_mul_2d(a, 1, NULL);
  28243. if (ret != MP_VAL)
  28244. return -12949;
  28245. ret = mp_mul_2d(NULL, 1, b);
  28246. if (ret != MP_VAL)
  28247. return -12950;
  28248. #endif
  28249. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28250. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  28251. ret = mp_montgomery_reduce(NULL, NULL, 1);
  28252. if (ret != MP_VAL)
  28253. return -12951;
  28254. ret = mp_montgomery_reduce(a, NULL, 1);
  28255. if (ret != MP_VAL)
  28256. return -12952;
  28257. ret = mp_montgomery_reduce(NULL, b, 1);
  28258. if (ret != MP_VAL)
  28259. return -12953;
  28260. mp_zero(b);
  28261. ret = mp_montgomery_reduce(a, b, 1);
  28262. if (ret != MP_VAL)
  28263. return -12954;
  28264. #endif
  28265. #ifdef WOLFSSL_SP_MATH_ALL
  28266. ret = mp_montgomery_setup(NULL, NULL);
  28267. if (ret != MP_VAL)
  28268. return -12955;
  28269. ret = mp_montgomery_setup(a, NULL);
  28270. if (ret != MP_VAL)
  28271. return -12956;
  28272. ret = mp_montgomery_setup(NULL, &rho);
  28273. if (ret != MP_VAL)
  28274. return -12957;
  28275. ret = mp_montgomery_calc_normalization(NULL, NULL);
  28276. if (ret != MP_VAL)
  28277. return -12958;
  28278. ret = mp_montgomery_calc_normalization(a, NULL);
  28279. if (ret != MP_VAL)
  28280. return -12959;
  28281. ret = mp_montgomery_calc_normalization(NULL, b);
  28282. if (ret != MP_VAL)
  28283. return -12960;
  28284. #endif
  28285. ret = mp_unsigned_bin_size(NULL);
  28286. if (ret != 0)
  28287. return -12961;
  28288. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  28289. ret = mp_tohex(NULL, NULL);
  28290. if (ret != MP_VAL)
  28291. return -12962;
  28292. ret = mp_tohex(a, NULL);
  28293. if (ret != MP_VAL)
  28294. return -12963;
  28295. ret = mp_tohex(NULL, hexStr);
  28296. if (ret != MP_VAL)
  28297. return -12964;
  28298. #endif
  28299. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  28300. ret = mp_todecimal(NULL, NULL);
  28301. if (ret != MP_VAL)
  28302. return -12965;
  28303. ret = mp_todecimal(a, NULL);
  28304. if (ret != MP_VAL)
  28305. return -12966;
  28306. ret = mp_todecimal(NULL, decStr);
  28307. if (ret != MP_VAL)
  28308. return -12967;
  28309. #endif
  28310. #ifdef WOLFSSL_SP_MATH_ALL
  28311. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  28312. if (ret != MP_VAL)
  28313. return -12968;
  28314. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  28315. if (ret != MP_VAL)
  28316. return -12969;
  28317. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  28318. if (ret != MP_VAL)
  28319. return -12970;
  28320. ret = mp_toradix(a, hexStr, 3);
  28321. if (ret != MP_VAL)
  28322. return -12971;
  28323. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  28324. if (ret != MP_VAL)
  28325. return -12972;
  28326. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  28327. if (ret != MP_VAL)
  28328. return -12973;
  28329. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  28330. if (ret != MP_VAL)
  28331. return -12974;
  28332. ret = mp_radix_size(a, 3, &size);
  28333. if (ret != MP_VAL)
  28334. return -12975;
  28335. #endif
  28336. return 0;
  28337. }
  28338. #endif
  28339. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28340. static int mp_test_set_is_bit(mp_int* a)
  28341. {
  28342. int i, j;
  28343. mp_zero(a);
  28344. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28345. if (mp_is_bit_set(a, i))
  28346. return -12980;
  28347. for (j = 0; j < i; j++) {
  28348. if (!mp_is_bit_set(a, j))
  28349. return -12981;
  28350. }
  28351. if (mp_set_bit(a, i) != 0)
  28352. return -12982;
  28353. if (!mp_is_bit_set(a, i))
  28354. return -12983;
  28355. }
  28356. mp_zero(a);
  28357. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28358. if (mp_is_bit_set(a, i))
  28359. return -12984;
  28360. }
  28361. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  28362. mp_zero(a);
  28363. if (mp_set_bit(a, i) != 0)
  28364. return -12985;
  28365. for (j = 0; j < i; j++) {
  28366. if (mp_is_bit_set(a, j))
  28367. return -12986;
  28368. }
  28369. if (!mp_is_bit_set(a, i))
  28370. return -12987;
  28371. }
  28372. #ifdef WOLFSSL_KEY_GEN
  28373. for (i = 0; i < DIGIT_BIT * 2; i++) {
  28374. mp_set(a, 1);
  28375. if (mp_2expt(a, i) != 0)
  28376. return -12988;
  28377. for (j = 0; j < i; j++) {
  28378. if (mp_is_bit_set(a, j))
  28379. return -12989;
  28380. }
  28381. if (!mp_is_bit_set(a, i))
  28382. return -12990;
  28383. }
  28384. #endif
  28385. #ifdef WOLFSSL_SP_MATH
  28386. mp_zero(a);
  28387. for (j = 1; j <= 3; j++) {
  28388. i = SP_INT_MAX_BITS - j;
  28389. if (mp_is_bit_set(a, i))
  28390. return -12991;
  28391. if (mp_set_bit(a, i) != 0)
  28392. return -12992;
  28393. if (!mp_is_bit_set(a, i))
  28394. return -12993;
  28395. #ifdef WOLFSSL_KEY_GEN
  28396. if (mp_2expt(a, i) != 0)
  28397. return -12994;
  28398. if (!mp_is_bit_set(a, i))
  28399. return -12995;
  28400. #endif
  28401. }
  28402. mp_zero(a);
  28403. for (j = 0; j <= 3; j++) {
  28404. i = SP_INT_MAX_BITS + j;
  28405. if (mp_is_bit_set(a, i))
  28406. return -12996;
  28407. if (mp_set_bit(a, i) != MP_VAL)
  28408. return -12997;
  28409. #ifdef WOLFSSL_KEY_GEN
  28410. if (mp_2expt(a, i) != MP_VAL)
  28411. return -12998;
  28412. #endif
  28413. }
  28414. #endif
  28415. return 0;
  28416. }
  28417. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  28418. static int mp_test_cmp(mp_int* a, mp_int* b)
  28419. {
  28420. int ret;
  28421. mp_zero(a);
  28422. mp_zero(b);
  28423. ret = mp_cmp_d(a, 0);
  28424. if (ret != MP_EQ)
  28425. return -13000;
  28426. ret = mp_cmp_d(a, 1);
  28427. if (ret != MP_LT)
  28428. return -13001;
  28429. ret = mp_cmp(a, b);
  28430. if (ret != MP_EQ)
  28431. return -13002;
  28432. mp_set(a, 1);
  28433. ret = mp_cmp_d(a, 0);
  28434. if (ret != MP_GT)
  28435. return -13003;
  28436. ret = mp_cmp_d(a, 1);
  28437. if (ret != MP_EQ)
  28438. return -13004;
  28439. ret = mp_cmp_d(a, 2);
  28440. if (ret != MP_LT)
  28441. return -13005;
  28442. ret = mp_cmp(a, b);
  28443. if (ret != MP_GT)
  28444. return -13006;
  28445. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  28446. ret = mp_cmp_d(b, -1);
  28447. if (ret != MP_GT)
  28448. return -13007;
  28449. ret = mp_cmp(a, b);
  28450. if (ret != MP_LT)
  28451. return -13008;
  28452. ret = mp_cmp(b, a);
  28453. if (ret != MP_GT)
  28454. return -13009;
  28455. ret = mp_cmp(b, b);
  28456. if (ret != MP_EQ)
  28457. return -13010;
  28458. return 0;
  28459. }
  28460. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  28461. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  28462. {
  28463. int ret;
  28464. int i, j, k;
  28465. for (i = 0; i < 10; i++) {
  28466. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  28467. ret = randNum(a, j, rng, NULL);
  28468. if (ret != MP_OKAY)
  28469. return -13020;
  28470. mp_copy(a, b);
  28471. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  28472. ret = mp_mul_2d(a, k, a);
  28473. if (ret != MP_OKAY)
  28474. return -13021;
  28475. mp_rshb(a, k);
  28476. if (mp_cmp(a, b) != MP_EQ)
  28477. return -13022;
  28478. }
  28479. }
  28480. }
  28481. for (i = 0; i < 10; i++) {
  28482. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  28483. ret = randNum(a, j, rng, NULL);
  28484. if (ret != MP_OKAY)
  28485. return -13023;
  28486. mp_copy(a, b);
  28487. for (k = 0; k < 10; k++) {
  28488. ret = mp_lshd(a, k);
  28489. if (ret != MP_OKAY)
  28490. return -13024;
  28491. mp_rshd(a, k);
  28492. if (mp_cmp(a, b) != MP_EQ)
  28493. return -13025;
  28494. }
  28495. }
  28496. }
  28497. mp_zero(a);
  28498. mp_rshd(a, 1);
  28499. if (!mp_iszero(a))
  28500. return -13026;
  28501. mp_set(a, 1);
  28502. mp_rshd(a, 1);
  28503. if (!mp_iszero(a))
  28504. return -13027;
  28505. mp_set(a, 1);
  28506. mp_rshd(a, 2);
  28507. if (!mp_iszero(a))
  28508. return -13028;
  28509. return 0;
  28510. }
  28511. #endif
  28512. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  28513. WC_RNG* rng)
  28514. {
  28515. int ret;
  28516. int i, j, k;
  28517. mp_zero(a);
  28518. mp_zero(d);
  28519. ret = mp_div(a, d, r, rem);
  28520. if (ret != MP_VAL)
  28521. return -13030;
  28522. mp_set(d, 1);
  28523. ret = mp_div(a, d, r, rem);
  28524. if (ret != MP_OKAY)
  28525. return -13031;
  28526. if (!mp_iszero(r))
  28527. return -13032;
  28528. if (!mp_iszero(rem))
  28529. return -13033;
  28530. mp_set(a, 1);
  28531. ret = mp_div(a, d, r, rem);
  28532. if (ret != MP_OKAY)
  28533. return -13034;
  28534. if (!mp_isone(r))
  28535. return -13035;
  28536. if (!mp_iszero(rem))
  28537. return -13036;
  28538. for (i = 0; i < 100; i++) {
  28539. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  28540. ret = randNum(d, j, rng, NULL);
  28541. if (ret != MP_OKAY)
  28542. return -13037;
  28543. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  28544. ret = randNum(a, k, rng, NULL);
  28545. if (ret != MP_OKAY)
  28546. return -13038;
  28547. ret = mp_div(a, d, NULL, rem);
  28548. if (ret != MP_OKAY)
  28549. return -13039;
  28550. ret = mp_div(a, d, r, NULL);
  28551. if (ret != MP_OKAY)
  28552. return -13040;
  28553. ret = mp_div(a, d, r, rem);
  28554. if (ret != MP_OKAY)
  28555. return -13041;
  28556. mp_mul(r, d, r);
  28557. mp_add(r, rem, r);
  28558. if (mp_cmp(r, a) != MP_EQ)
  28559. return -13042;
  28560. }
  28561. }
  28562. }
  28563. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  28564. if (ret != MP_OKAY)
  28565. return -13043;
  28566. mp_add(d, d, a);
  28567. mp_set(rem, 1);
  28568. mp_div(a, d, NULL, rem);
  28569. if (ret != MP_OKAY)
  28570. return -13044;
  28571. if (!mp_iszero(rem))
  28572. return -13045;
  28573. mp_set(r, 1);
  28574. mp_div(a, d, r, NULL);
  28575. if (ret != MP_OKAY)
  28576. return -13046;
  28577. if (mp_cmp_d(r, 2) != MP_EQ)
  28578. return -13047;
  28579. mp_set(r, 1);
  28580. mp_set(rem, 1);
  28581. mp_div(a, d, r, rem);
  28582. if (ret != MP_OKAY)
  28583. return -13048;
  28584. if (mp_cmp_d(r, 2) != MP_EQ)
  28585. return -13049;
  28586. if (!mp_iszero(rem))
  28587. return -13050;
  28588. mp_set(a, 0xfe);
  28589. mp_lshd(a, 3);
  28590. mp_add_d(a, 0xff, a);
  28591. mp_set(d, 0xfe);
  28592. mp_lshd(d, 2);
  28593. ret = mp_div(a, d, r, rem);
  28594. if (ret != MP_OKAY)
  28595. return -13051;
  28596. mp_mul(r, d, d);
  28597. mp_add(rem, d, d);
  28598. if (mp_cmp(a, d) != MP_EQ)
  28599. return -13052;
  28600. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  28601. * a digit. So mp_div must detect and handle.
  28602. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  28603. */
  28604. mp_set(a, 1);
  28605. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  28606. mp_set(d, 1);
  28607. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  28608. mp_add_d(d, 1, d);
  28609. ret = mp_div(a, d, r, rem);
  28610. if (ret != MP_OKAY)
  28611. return -13053;
  28612. return 0;
  28613. }
  28614. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  28615. !defined(WC_NO_RNG)
  28616. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  28617. {
  28618. int ret;
  28619. int res;
  28620. ret = mp_rand_prime(a, 1, rng, NULL);
  28621. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28622. if (ret != 0)
  28623. #else
  28624. if (ret != MP_VAL)
  28625. #endif
  28626. return -13060;
  28627. ret = mp_rand_prime(a, -5, rng, NULL);
  28628. if (ret != 0)
  28629. return -13061;
  28630. ret = mp_prime_is_prime(a, 1, &res);
  28631. if (ret != MP_OKAY)
  28632. return -13062;
  28633. if (res != MP_YES)
  28634. return -13063;
  28635. ret = mp_prime_is_prime(a, 0, &res);
  28636. if (ret != MP_VAL)
  28637. return -13064;
  28638. ret = mp_prime_is_prime(a, -1, &res);
  28639. if (ret != MP_VAL)
  28640. return -13065;
  28641. ret = mp_prime_is_prime(a, 257, &res);
  28642. if (ret != MP_VAL)
  28643. return -13066;
  28644. mp_set(a, 1);
  28645. ret = mp_prime_is_prime(a, 1, &res);
  28646. if (ret != MP_OKAY)
  28647. return -13067;
  28648. if (res != MP_NO)
  28649. return -13068;
  28650. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28651. if (ret != MP_OKAY)
  28652. return -13069;
  28653. if (res != MP_NO)
  28654. return -13070;
  28655. mp_set(a, 2);
  28656. ret = mp_prime_is_prime(a, 1, &res);
  28657. if (ret != MP_OKAY)
  28658. return -13071;
  28659. if (res != MP_YES)
  28660. return -13072;
  28661. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28662. if (ret != MP_OKAY)
  28663. return -13073;
  28664. if (res != MP_YES)
  28665. return -13074;
  28666. mp_set(a, 0xfb);
  28667. ret = mp_prime_is_prime(a, 1, &res);
  28668. if (ret != MP_OKAY)
  28669. return -13075;
  28670. if (res != MP_YES)
  28671. return -13076;
  28672. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28673. if (ret != MP_OKAY)
  28674. return -13077;
  28675. if (res != MP_YES)
  28676. return -13078;
  28677. mp_set(a, 0x6);
  28678. ret = mp_prime_is_prime(a, 1, &res);
  28679. if (ret != MP_OKAY)
  28680. return -13079;
  28681. if (res != MP_NO)
  28682. return -13080;
  28683. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  28684. if (ret != MP_OKAY)
  28685. return -13081;
  28686. if (res != MP_NO)
  28687. return -13082;
  28688. mp_set_int(a, 0x655 * 0x65b);
  28689. ret = mp_prime_is_prime(a, 10, &res);
  28690. if (ret != MP_OKAY)
  28691. return -13083;
  28692. if (res != MP_NO)
  28693. return -13084;
  28694. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  28695. if (ret != MP_OKAY)
  28696. return -13085;
  28697. if (res != MP_NO)
  28698. return -13086;
  28699. return 0;
  28700. }
  28701. #endif
  28702. #if defined(WOLFSSL_KEY_GEN) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  28703. !defined(NO_RSA))
  28704. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  28705. WC_RNG* rng)
  28706. {
  28707. int ret;
  28708. int i;
  28709. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  28710. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  28711. { 12, 56, 168 }, { 56, 12, 168 }
  28712. };
  28713. (void)exp;
  28714. mp_set(a, 0);
  28715. mp_set(b, 1);
  28716. ret = mp_lcm(a, a, r);
  28717. if (ret != MP_VAL)
  28718. return -13090;
  28719. ret = mp_lcm(a, b, r);
  28720. if (ret != MP_VAL)
  28721. return -13091;
  28722. ret = mp_lcm(b, a, r);
  28723. if (ret != MP_VAL)
  28724. return -13092;
  28725. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  28726. mp_set(a, kat[i][0]);
  28727. mp_set(b, kat[i][1]);
  28728. ret = mp_lcm(a, b, r);
  28729. if (ret != MP_OKAY)
  28730. return -13093;
  28731. mp_set(exp, kat[i][2]);
  28732. if (mp_cmp(r, exp) != MP_EQ)
  28733. return -13094;
  28734. }
  28735. (void)rng;
  28736. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  28737. !defined(WC_NO_RNG)
  28738. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  28739. return -13095;
  28740. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  28741. return -13096;
  28742. if (mp_mul(a, b, exp) != MP_OKAY)
  28743. return -13097;
  28744. ret = mp_lcm(a, b, r);
  28745. if (ret != MP_OKAY)
  28746. return -13098;
  28747. if (mp_cmp(r, exp) != MP_EQ)
  28748. return -13099;
  28749. ret = mp_lcm(b, a, r);
  28750. if (ret != MP_OKAY)
  28751. return -13100;
  28752. if (mp_cmp(r, exp) != MP_EQ)
  28753. return -13101;
  28754. #endif
  28755. mp_set(a, 11);
  28756. mp_zero(b);
  28757. ret = mp_gcd(a, b, r);
  28758. if (ret != MP_OKAY)
  28759. return -13102;
  28760. if (mp_cmp_d(r, 11) != MP_EQ)
  28761. return -13103;
  28762. ret = mp_gcd(b, a, r);
  28763. if (ret != MP_OKAY)
  28764. return -13104;
  28765. if (mp_cmp_d(r, 11) != MP_EQ)
  28766. return -13105;
  28767. ret = mp_gcd(b, b, r);
  28768. if (ret != MP_VAL)
  28769. return -13106;
  28770. return 0;
  28771. }
  28772. #endif
  28773. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  28774. defined(WOLFSSL_SP_MATH_ALL)
  28775. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  28776. {
  28777. int ret;
  28778. int i;
  28779. int j;
  28780. mp_set(a, 10);
  28781. ret = mp_mod_2d(a, 0, r);
  28782. if (ret != MP_OKAY)
  28783. return -13110;
  28784. if (!mp_iszero(r))
  28785. return -13111;
  28786. ret = mp_mod_2d(a, 1, r);
  28787. if (ret != MP_OKAY)
  28788. return -13112;
  28789. if (!mp_iszero(r))
  28790. return -13113;
  28791. ret = mp_mod_2d(a, 2, r);
  28792. if (ret != MP_OKAY)
  28793. return -13114;
  28794. if (mp_cmp_d(r, 2))
  28795. return -13115;
  28796. for (i = 2; i < 20; i++) {
  28797. ret = randNum(a, i, rng, NULL);
  28798. if (ret != 0)
  28799. return -13116;
  28800. for (j = 1; j <= mp_count_bits(a); j++) {
  28801. /* Get top part */
  28802. ret = mp_div_2d(a, j, t, NULL);
  28803. if (ret != 0)
  28804. return -13117;
  28805. ret = mp_mul_2d(t, j, t);
  28806. if (ret != 0)
  28807. return -13118;
  28808. /* Get bottom part */
  28809. ret = mp_mod_2d(a, j, r);
  28810. if (ret != 0)
  28811. return -13119;
  28812. /* Reassemble */
  28813. ret = mp_add(t, r, r);
  28814. if (ret != 0)
  28815. return -13120;
  28816. if (mp_cmp(a, r) != MP_EQ)
  28817. return -13121;
  28818. }
  28819. }
  28820. return 0;
  28821. }
  28822. #endif
  28823. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  28824. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  28825. static int mp_test_mod_d(mp_int* a)
  28826. {
  28827. int ret;
  28828. mp_digit r;
  28829. if (mp_set(a, 1) != MP_OKAY)
  28830. return -13130;
  28831. ret = mp_mod_d(a, 0, &r);
  28832. if (ret != MP_VAL)
  28833. return -13131;
  28834. mp_zero(a);
  28835. ret = mp_mod_d(a, 1, &r);
  28836. if (ret != MP_OKAY)
  28837. return -13132;
  28838. ret = mp_mod_d(a, 3, &r);
  28839. if (ret != MP_OKAY)
  28840. return -13133;
  28841. ret = mp_mod_d(a, 5, &r);
  28842. if (ret != MP_OKAY)
  28843. return -13134;
  28844. return 0;
  28845. }
  28846. #endif
  28847. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  28848. WC_RNG* rng)
  28849. {
  28850. int ret;
  28851. int i;
  28852. for (i = 1; i < 16; i++) {
  28853. ret = randNum(a, i, rng, NULL);
  28854. if (ret != 0)
  28855. return -13140;
  28856. ret = mp_mul(a, a, r1);
  28857. if (ret != 0)
  28858. return -13141;
  28859. ret = mp_sqr(a, r2);
  28860. if (ret != 0)
  28861. return -13142;
  28862. if (mp_cmp(r1, r2) != MP_EQ)
  28863. return -13143;
  28864. }
  28865. ret = mp_set(b, 0);
  28866. if (ret != MP_OKAY)
  28867. return -13144;
  28868. ret = mp_mul(a, b, r1);
  28869. if (ret != MP_OKAY)
  28870. return -13145;
  28871. if (!mp_iszero(r1))
  28872. return -13146;
  28873. ret = mp_sqr(b, r1);
  28874. if (ret != MP_OKAY)
  28875. return -13147;
  28876. if (!mp_iszero(r1))
  28877. return -13148;
  28878. #ifdef WOLFSSL_SP_MATH
  28879. ret = mp_set(a, 1);
  28880. if (ret != MP_OKAY)
  28881. return -13149;
  28882. i = (SP_INT_DIGITS + 1) / 2;
  28883. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  28884. if (ret != MP_OKAY)
  28885. return -13150;
  28886. ret = mp_set(b, 1);
  28887. if (ret != MP_OKAY)
  28888. return -13151;
  28889. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  28890. if (ret != MP_OKAY)
  28891. return -13152;
  28892. ret = mp_mul(a, b, r1);
  28893. if (ret != MP_OKAY)
  28894. return -13153;
  28895. ret = mp_mul(a, a, r1);
  28896. if (ret == MP_OKAY)
  28897. return -13154;
  28898. ret = mp_sqr(a, r1);
  28899. if (ret == MP_OKAY)
  28900. return -13155;
  28901. ret = mp_sqr(b, r1);
  28902. if (ret != MP_OKAY)
  28903. return -13156;
  28904. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  28905. (defined(HAVE_ECC) && defined(FP_ECC))
  28906. ret = mp_mulmod(a, b, b, r1);
  28907. if (ret != MP_OKAY)
  28908. return -13157;
  28909. ret = mp_mulmod(a, a, b, r1);
  28910. if (ret == MP_OKAY)
  28911. return -13158;
  28912. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  28913. ret = mp_sqrmod(a, b, r1);
  28914. if (ret == MP_OKAY)
  28915. return -13159;
  28916. ret = mp_sqrmod(b, a, r1);
  28917. if (ret != MP_OKAY)
  28918. return -13160;
  28919. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  28920. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  28921. #endif /* WOLFSSL_SP_MATH */
  28922. return 0;
  28923. }
  28924. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  28925. defined(OPENSSL_EXTRA)
  28926. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  28927. {
  28928. int ret;
  28929. mp_set(a, 0);
  28930. mp_set(m, 1);
  28931. ret = mp_invmod(a, m, r);
  28932. if (ret != MP_VAL)
  28933. return -13170;
  28934. ret = mp_invmod(m, a, r);
  28935. if (ret != MP_VAL)
  28936. return -13171;
  28937. mp_set(a, 2);
  28938. mp_set(m, 4);
  28939. ret = mp_invmod(a, m, r);
  28940. if (ret != MP_VAL)
  28941. return -13172;
  28942. mp_set(a, 1);
  28943. mp_set(m, 4);
  28944. ret = mp_invmod(a, m, r);
  28945. if (ret != MP_OKAY)
  28946. return -13173;
  28947. if (!mp_isone(r))
  28948. return -13174;
  28949. mp_set(a, 3);
  28950. mp_set(m, 4);
  28951. ret = mp_invmod(a, m, r);
  28952. if (ret != MP_OKAY)
  28953. return -13175;
  28954. mp_set(a, 3);
  28955. mp_set(m, 5);
  28956. ret = mp_invmod(a, m, r);
  28957. if (ret != MP_OKAY)
  28958. return -13176;
  28959. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  28960. mp_read_radix(a, "-3", 16);
  28961. ret = mp_invmod(a, m, r);
  28962. if (ret != MP_OKAY)
  28963. return -13177;
  28964. #endif
  28965. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  28966. #ifdef HAVE_ECC
  28967. mp_set(a, 0);
  28968. mp_set(m, 3);
  28969. ret = mp_invmod_mont_ct(a, m, r, 1);
  28970. if (ret != MP_VAL)
  28971. return -13178;
  28972. mp_set(a, 1);
  28973. mp_set(m, 0);
  28974. ret = mp_invmod_mont_ct(a, m, r, 1);
  28975. if (ret != MP_VAL)
  28976. return -13179;
  28977. mp_set(a, 1);
  28978. mp_set(m, 1);
  28979. ret = mp_invmod_mont_ct(a, m, r, 1);
  28980. if (ret != MP_VAL)
  28981. return -13180;
  28982. mp_set(a, 1);
  28983. mp_set(m, 2);
  28984. ret = mp_invmod_mont_ct(a, m, r, 1);
  28985. if (ret != MP_VAL)
  28986. return -13181;
  28987. mp_set(a, 1);
  28988. mp_set(m, 3);
  28989. ret = mp_invmod_mont_ct(a, m, r, 1);
  28990. if (ret != MP_OKAY)
  28991. return -13182;
  28992. #endif
  28993. #endif
  28994. return 0;
  28995. }
  28996. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  28997. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  28998. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  28999. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  29000. {
  29001. int ret;
  29002. mp_set(b, 0x2);
  29003. mp_set(e, 0x3);
  29004. mp_set(m, 0x0);
  29005. ret = mp_exptmod_ex(b, e, 1, m, r);
  29006. if (ret != MP_VAL)
  29007. return -13190;
  29008. ret = mp_exptmod_nct(b, e, m, r);
  29009. if (ret != MP_VAL)
  29010. return -13191;
  29011. mp_set(b, 0x2);
  29012. mp_set(e, 0x3);
  29013. mp_set(m, 0x1);
  29014. ret = mp_exptmod_ex(b, e, 1, m, r);
  29015. if (ret != MP_OKAY)
  29016. return -13192;
  29017. if (!mp_iszero(r))
  29018. return -13193;
  29019. ret = mp_exptmod_nct(b, e, m, r);
  29020. if (ret != MP_OKAY)
  29021. return -13194;
  29022. if (!mp_iszero(r))
  29023. return -13195;
  29024. mp_set(b, 0x2);
  29025. mp_set(e, 0x0);
  29026. mp_set(m, 0x7);
  29027. ret = mp_exptmod_ex(b, e, 1, m, r);
  29028. if (ret != MP_OKAY)
  29029. return -13196;
  29030. if (!mp_isone(r))
  29031. return -13197;
  29032. ret = mp_exptmod_nct(b, e, m, r);
  29033. if (ret != MP_OKAY)
  29034. return -13198;
  29035. if (!mp_isone(r))
  29036. return -13199;
  29037. mp_set(b, 0x0);
  29038. mp_set(e, 0x3);
  29039. mp_set(m, 0x7);
  29040. ret = mp_exptmod_ex(b, e, 1, m, r);
  29041. if (ret != MP_OKAY)
  29042. return -13200;
  29043. if (!mp_iszero(r))
  29044. return -13201;
  29045. ret = mp_exptmod_nct(b, e, m, r);
  29046. if (ret != MP_OKAY)
  29047. return -13202;
  29048. if (!mp_iszero(r))
  29049. return -13203;
  29050. mp_set(b, 0x10);
  29051. mp_set(e, 0x3);
  29052. mp_set(m, 0x7);
  29053. ret = mp_exptmod_ex(b, e, 1, m, r);
  29054. if (ret != MP_OKAY)
  29055. return -13204;
  29056. ret = mp_exptmod_nct(b, e, m, r);
  29057. if (ret != MP_OKAY)
  29058. return -13205;
  29059. mp_set(b, 0x7);
  29060. mp_set(e, 0x3);
  29061. mp_set(m, 0x7);
  29062. ret = mp_exptmod_ex(b, e, 1, m, r);
  29063. if (ret != MP_OKAY)
  29064. return -13206;
  29065. if (!mp_iszero(r))
  29066. return -13207;
  29067. ret = mp_exptmod_nct(b, e, m, r);
  29068. if (ret != MP_OKAY)
  29069. return -13208;
  29070. if (!mp_iszero(r))
  29071. return -13209;
  29072. mp_set(b, 0x01);
  29073. mp_mul_2d(b, DIGIT_BIT, b);
  29074. mp_add_d(b, 1, b);
  29075. mp_set(e, 0x3);
  29076. mp_copy(b, m);
  29077. ret = mp_exptmod_ex(b, e, 1, m, r);
  29078. if (ret != MP_OKAY)
  29079. return -13210;
  29080. if (!mp_iszero(r))
  29081. return -13211;
  29082. ret = mp_exptmod_nct(b, e, m, r);
  29083. if (ret != MP_OKAY)
  29084. return -13212;
  29085. if (!mp_iszero(r))
  29086. return -13213;
  29087. mp_set(b, 0x2);
  29088. mp_set(e, 0x3);
  29089. mp_set(m, 0x7);
  29090. ret = mp_exptmod_ex(b, e, 1, m, r);
  29091. if (ret != MP_OKAY)
  29092. return -13214;
  29093. ret = mp_exptmod_nct(b, e, m, r);
  29094. if (ret != MP_OKAY)
  29095. return -13215;
  29096. #ifdef WOLFSSL_SP_MATH
  29097. mp_set(b, 0x2);
  29098. mp_set(e, 0x3);
  29099. mp_set(m, 0x01);
  29100. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  29101. mp_add_d(m, 0x01, m);
  29102. ret = mp_exptmod_ex(b, e, 1, m, r);
  29103. if (ret != MP_VAL)
  29104. return -13216;
  29105. ret = mp_exptmod_nct(b, e, m, r);
  29106. if (ret != MP_VAL)
  29107. return -13217;
  29108. #endif
  29109. return 0;
  29110. }
  29111. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  29112. * OPENSSL_EXTRA */
  29113. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  29114. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  29115. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  29116. {
  29117. int ret;
  29118. mp_digit mp;
  29119. static int exp[] = { 7, 8, 16, 27, 32, 64,
  29120. 127, 128, 255, 256,
  29121. 383, 384, 2033, 2048
  29122. };
  29123. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  29124. 0x01, 0x9f, 0x13, 0xbd,
  29125. 0x1f, 0x13d, 0x45, 0x615
  29126. };
  29127. int i;
  29128. int j;
  29129. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  29130. if (exp[i] >= DIGIT_BIT)
  29131. continue;
  29132. mp_zero(m);
  29133. ret = mp_set_bit(m, exp[i]);
  29134. if (ret != MP_OKAY)
  29135. return -13220;
  29136. ret = mp_sub_d(m, sub[i], m);
  29137. if (ret != MP_OKAY)
  29138. return -13221;
  29139. ret = mp_montgomery_setup(m, &mp);
  29140. if (ret != MP_OKAY)
  29141. return -13222;
  29142. ret = mp_montgomery_calc_normalization(n, m);
  29143. if (ret != MP_OKAY)
  29144. return -13223;
  29145. for (j = 0; j < 10; j++) {
  29146. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  29147. if (ret != 0)
  29148. return -13224;
  29149. ret = mp_mod(a, m, a);
  29150. if (ret != 0)
  29151. return -13225;
  29152. /* r = a * a */
  29153. ret = mp_sqrmod(a, m, r);
  29154. if (ret != MP_OKAY)
  29155. return -13226;
  29156. /* Convert to Montgomery form = a*n */
  29157. ret = mp_mulmod(a, n, m, a);
  29158. if (ret != MP_OKAY)
  29159. return -13227;
  29160. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  29161. ret = mp_sqr(a, a);
  29162. if (ret != MP_OKAY)
  29163. return -13228;
  29164. ret = mp_montgomery_reduce(a, m, mp);
  29165. if (ret != MP_OKAY)
  29166. return -13229;
  29167. ret = mp_montgomery_reduce(a, m, mp);
  29168. if (ret != MP_OKAY)
  29169. return -13230;
  29170. if (mp_cmp(a, r) != MP_EQ)
  29171. return -13231;
  29172. }
  29173. }
  29174. return 0;
  29175. }
  29176. #endif
  29177. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  29178. {
  29179. WC_RNG rng;
  29180. int ret;
  29181. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  29182. int i, j, k;
  29183. mp_digit d;
  29184. #endif
  29185. mp_int a, b, r1, r2, p;
  29186. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  29187. if (ret != 0)
  29188. return -13300;
  29189. #ifdef WOLSSL_SP_MATH_ALL
  29190. mp_init_copy(&p, &a);
  29191. #else
  29192. ret = mp_init(&p);
  29193. if (ret != 0)
  29194. return -13301;
  29195. #endif
  29196. #ifndef HAVE_FIPS
  29197. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29198. #else
  29199. ret = wc_InitRng(&rng);
  29200. #endif
  29201. if (ret != 0)
  29202. goto done;
  29203. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  29204. mp_set_int(&a, 0);
  29205. if (a.used != 0 || a.dp[0] != 0)
  29206. return -13302;
  29207. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  29208. for (i = 0; i < 4 * j; i++) {
  29209. /* New values to use. */
  29210. ret = randNum(&p, j, &rng, NULL);
  29211. if (ret != 0)
  29212. return -13303;
  29213. ret = randNum(&a, j, &rng, NULL);
  29214. if (ret != 0)
  29215. return -13304;
  29216. ret = randNum(&b, j, &rng, NULL);
  29217. if (ret != 0)
  29218. return -13305;
  29219. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  29220. if (ret != 0)
  29221. return -13306;
  29222. d &= MP_MASK;
  29223. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  29224. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  29225. /* Ensure sqrmod produce same result as mulmod. */
  29226. ret = mp_sqrmod(&a, &p, &r1);
  29227. if (ret != 0)
  29228. return -13307;
  29229. ret = mp_mulmod(&a, &a, &p, &r2);
  29230. if (ret != 0)
  29231. return -13308;
  29232. if (mp_cmp(&r1, &r2) != 0)
  29233. return -13309;
  29234. #endif
  29235. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  29236. !defined(WOLFSSL_SP_INT_NEGATIVE))
  29237. ret = mp_addmod(&a, &b, &p, &r1);
  29238. if (ret != 0)
  29239. return -13310;
  29240. ret = mp_submod(&r1, &b, &p, &r2);
  29241. if (ret != 0)
  29242. return -13311;
  29243. ret = mp_mod(&a, &p, &r1);
  29244. if (ret != 0)
  29245. return -13312;
  29246. if (mp_cmp(&r1, &r2) != MP_EQ)
  29247. return -13313;
  29248. #else
  29249. /* Ensure add with mod produce same result as sub with mod. */
  29250. ret = mp_addmod(&a, &b, &p, &r1);
  29251. if (ret != 0)
  29252. return -13314;
  29253. b.sign ^= 1;
  29254. ret = mp_submod(&a, &b, &p, &r2);
  29255. if (ret != 0)
  29256. return -13315;
  29257. if (mp_cmp(&r1, &r2) != 0)
  29258. return -13316;
  29259. #endif
  29260. /* Ensure add digit produce same result as sub digit. */
  29261. ret = mp_add_d(&a, d, &r1);
  29262. if (ret != 0)
  29263. return -13317;
  29264. ret = mp_sub_d(&r1, d, &r2);
  29265. if (ret != 0)
  29266. return -13318;
  29267. if (mp_cmp(&a, &r2) != 0)
  29268. return -13319;
  29269. /* Invert - if p is even it will use the slow impl.
  29270. * - if p and a are even it will fail.
  29271. */
  29272. ret = mp_invmod(&a, &p, &r1);
  29273. if (ret != 0 && ret != MP_VAL)
  29274. return -13320;
  29275. ret = 0;
  29276. /* Shift up and down number all bits in a digit. */
  29277. for (k = 0; k < DIGIT_BIT; k++) {
  29278. mp_mul_2d(&a, k, &r1);
  29279. mp_div_2d(&r1, k, &r2, &p);
  29280. if (mp_cmp(&a, &r2) != 0)
  29281. return -13321;
  29282. if (!mp_iszero(&p))
  29283. return -13322;
  29284. mp_rshb(&r1, k);
  29285. if (mp_cmp(&a, &r1) != 0)
  29286. return -13323;
  29287. }
  29288. }
  29289. }
  29290. #if DIGIT_BIT >= 32
  29291. /* Check that setting a 32-bit digit works. */
  29292. d &= 0xffffffffU;
  29293. mp_set_int(&a, d);
  29294. if (a.used != 1 || a.dp[0] != d)
  29295. return -13324;
  29296. #endif
  29297. /* Check setting a bit and testing a bit works. */
  29298. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  29299. mp_zero(&a);
  29300. mp_set_bit(&a, i);
  29301. if (!mp_is_bit_set(&a, i))
  29302. return -13325;
  29303. }
  29304. #endif
  29305. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  29306. mp_zero(&a);
  29307. i = mp_cnt_lsb(&a);
  29308. if (i != 0)
  29309. return -13326;
  29310. mp_set(&a, 1);
  29311. i = mp_cnt_lsb(&a);
  29312. if (i != 0)
  29313. return -13327;
  29314. #endif
  29315. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29316. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  29317. return ret;
  29318. #endif
  29319. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  29320. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  29321. return ret;
  29322. #endif
  29323. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  29324. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  29325. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  29326. return ret;
  29327. #endif
  29328. #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC)
  29329. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  29330. return ret;
  29331. #endif
  29332. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  29333. return ret;
  29334. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  29335. return ret;
  29336. if ((ret = mp_test_read_to_bin(&a)) != 0)
  29337. return ret;
  29338. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29339. if ((ret = mp_test_set_int(&a)) != 0)
  29340. return ret;
  29341. #endif
  29342. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  29343. return ret;
  29344. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  29345. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  29346. return ret;
  29347. #endif
  29348. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  29349. if ((ret = mp_test_set_is_bit(&a)) != 0)
  29350. return ret;
  29351. #endif
  29352. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  29353. return ret;
  29354. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  29355. !defined(WC_NO_RNG)
  29356. if ((ret = mp_test_prime(&a, &rng)) != 0)
  29357. return ret;
  29358. #endif
  29359. #if defined(WOLFSSL_KEY_GEN) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  29360. !defined(NO_RSA))
  29361. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  29362. return ret;
  29363. #endif
  29364. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  29365. defined(WOLFSSL_SP_MATH_ALL)
  29366. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  29367. return ret;
  29368. #endif
  29369. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  29370. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  29371. if ((ret = mp_test_mod_d(&a)) != 0)
  29372. return ret;
  29373. #endif
  29374. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  29375. return ret;
  29376. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  29377. defined(OPENSSL_EXTRA)
  29378. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  29379. return ret;
  29380. #endif
  29381. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  29382. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  29383. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  29384. return ret;
  29385. #endif
  29386. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  29387. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  29388. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  29389. return ret;
  29390. #endif
  29391. done:
  29392. mp_clear(&p);
  29393. mp_clear(&r2);
  29394. mp_clear(&r1);
  29395. mp_clear(&b);
  29396. mp_clear(&a);
  29397. wc_FreeRng(&rng);
  29398. return ret;
  29399. }
  29400. #endif
  29401. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  29402. typedef struct pairs_t {
  29403. const unsigned char* coeff;
  29404. int coeffSz;
  29405. int exp;
  29406. } pairs_t;
  29407. /*
  29408. n =p1p2p3, where pi = ki(p1−1)+1 with (k2,k3) = (173,293)
  29409. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  29410. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  29411. */
  29412. static const unsigned char c192a[] =
  29413. {
  29414. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  29415. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  29416. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  29417. };
  29418. static const unsigned char c0a[] =
  29419. {
  29420. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  29421. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  29422. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  29423. };
  29424. static const pairs_t ecPairsA[] =
  29425. {
  29426. {c192a, sizeof(c192a), 192},
  29427. {c0a, sizeof(c0a), 0}
  29428. };
  29429. static const int kA[] = {173, 293};
  29430. static const unsigned char controlPrime[] = {
  29431. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  29432. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  29433. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  29434. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  29435. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  29436. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  29437. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  29438. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  29439. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  29440. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  29441. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  29442. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  29443. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  29444. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  29445. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  29446. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  29447. };
  29448. static const unsigned char testOne[] = { 1 };
  29449. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  29450. {
  29451. int ret;
  29452. mp_int ki;
  29453. ret = mp_init(&ki);
  29454. if (ret == 0)
  29455. ret = mp_set(&ki, k);
  29456. if (ret == 0)
  29457. ret = mp_sub_d(p1, 1, p2);
  29458. if (ret == 0)
  29459. ret = mp_mul(p2, &ki, p2);
  29460. if (ret == 0)
  29461. ret = mp_add_d(p2, 1, p2);
  29462. mp_clear(&ki);
  29463. return ret;
  29464. }
  29465. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  29466. const pairs_t* ecPairs, int ecPairsSz,
  29467. const int* k)
  29468. {
  29469. mp_int x,y;
  29470. int ret, i;
  29471. ret = mp_init(&x);
  29472. if (ret == 0) {
  29473. ret = mp_init(&y);
  29474. if (ret != 0) {
  29475. mp_clear(&x);
  29476. return MP_MEM;
  29477. }
  29478. }
  29479. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  29480. ret = mp_read_unsigned_bin(&x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  29481. /* p1 = 2^exp */
  29482. if (ret == 0)
  29483. ret = mp_2expt(&y, ecPairs[i].exp);
  29484. /* p1 = p1 * m */
  29485. if (ret == 0)
  29486. ret = mp_mul(&x, &y, &x);
  29487. /* p1 += */
  29488. if (ret == 0)
  29489. ret = mp_add(p1, &x, p1);
  29490. mp_zero(&x);
  29491. mp_zero(&y);
  29492. }
  29493. mp_clear(&x);
  29494. mp_clear(&y);
  29495. if (ret == 0)
  29496. ret = GenerateNextP(p1, p2, k[0]);
  29497. if (ret == 0)
  29498. ret = GenerateNextP(p1, p3, k[1]);
  29499. return ret;
  29500. }
  29501. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  29502. {
  29503. #ifdef WOLFSSL_SMALL_STACK
  29504. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29505. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29506. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  29507. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29508. #else
  29509. mp_int n[1],
  29510. p1[1],
  29511. p2[1],
  29512. p3[1];
  29513. #endif
  29514. int ret, isPrime = 0;
  29515. WC_RNG rng;
  29516. #ifdef WOLFSSL_SMALL_STACK
  29517. if ((n == NULL) ||
  29518. (p1 == NULL) ||
  29519. (p2 == NULL) ||
  29520. (p3 == NULL))
  29521. ERROR_OUT(MEMORY_E, out);
  29522. #endif
  29523. ret = wc_InitRng(&rng);
  29524. if (ret == 0)
  29525. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  29526. if (ret == 0)
  29527. ret = GenerateP(p1, p2, p3,
  29528. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  29529. if (ret == 0)
  29530. ret = mp_mul(p1, p2, n);
  29531. if (ret == 0)
  29532. ret = mp_mul(n, p3, n);
  29533. if (ret != 0)
  29534. ERROR_OUT(-13400, out);
  29535. /* Check the old prime test using the number that false positives.
  29536. * This test result should indicate as not prime. */
  29537. ret = mp_prime_is_prime(n, 40, &isPrime);
  29538. if (ret != 0)
  29539. ERROR_OUT(-13401, out);
  29540. if (isPrime)
  29541. ERROR_OUT(-13402, out);
  29542. /* This test result should fail. It should indicate the value as prime. */
  29543. ret = mp_prime_is_prime(n, 8, &isPrime);
  29544. if (ret != 0)
  29545. ERROR_OUT(-13403, out);
  29546. if (!isPrime)
  29547. ERROR_OUT(-13404, out);
  29548. /* This test result should indicate the value as not prime. */
  29549. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29550. if (ret != 0)
  29551. ERROR_OUT(-13405, out);
  29552. if (isPrime)
  29553. ERROR_OUT(-13406, out);
  29554. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  29555. if (ret != 0)
  29556. ERROR_OUT(-13407, out);
  29557. /* This test result should indicate the value as prime. */
  29558. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29559. if (ret != 0)
  29560. ERROR_OUT(-13408, out);
  29561. if (!isPrime)
  29562. ERROR_OUT(-13409, out);
  29563. /* This test result should indicate the value as prime. */
  29564. isPrime = -1;
  29565. ret = mp_prime_is_prime(n, 8, &isPrime);
  29566. if (ret != 0)
  29567. ERROR_OUT(-13410, out);
  29568. if (!isPrime)
  29569. ERROR_OUT(-13411, out);
  29570. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  29571. if (ret != 0)
  29572. ERROR_OUT(-13412, out);
  29573. /* This test result should indicate the value as not prime. */
  29574. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  29575. if (ret != 0)
  29576. ERROR_OUT(-13413, out);
  29577. if (isPrime)
  29578. ERROR_OUT(-13414, out);
  29579. ret = mp_prime_is_prime(n, 8, &isPrime);
  29580. if (ret != 0)
  29581. ERROR_OUT(-13415, out);
  29582. if (isPrime)
  29583. ERROR_OUT(-13416, out);
  29584. ret = 0;
  29585. out:
  29586. #ifdef WOLFSSL_SMALL_STACK
  29587. if (n != NULL) {
  29588. mp_clear(n);
  29589. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29590. }
  29591. if (p1 != NULL) {
  29592. mp_clear(p1);
  29593. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29594. }
  29595. if (p2 != NULL) {
  29596. mp_clear(p2);
  29597. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29598. }
  29599. if (p3 != NULL) {
  29600. mp_clear(p3);
  29601. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29602. }
  29603. #else
  29604. mp_clear(p3);
  29605. mp_clear(p2);
  29606. mp_clear(p1);
  29607. mp_clear(n);
  29608. #endif
  29609. wc_FreeRng(&rng);
  29610. return ret;
  29611. }
  29612. #endif /* WOLFSSL_PUBLIC_MP */
  29613. #if defined(ASN_BER_TO_DER) && \
  29614. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  29615. defined(OPENSSL_EXTRA_X509_SMALL))
  29616. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  29617. typedef struct berDerTestData {
  29618. const byte *in;
  29619. word32 inSz;
  29620. const byte *out;
  29621. word32 outSz;
  29622. } berDerTestData;
  29623. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  29624. {
  29625. int ret;
  29626. int i;
  29627. word32 len = 0, l;
  29628. byte out[32];
  29629. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  29630. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  29631. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  29632. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  29633. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  29634. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  29635. };
  29636. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  29637. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  29638. 0x30, 0x80,
  29639. 0x02, 0x01, 0x01,
  29640. 0x30, 0x80,
  29641. 0x24, 0x80,
  29642. 0x04, 0x01, 0x01,
  29643. 0x04, 0x02, 0x02, 0x03,
  29644. 0x00, 0x00,
  29645. 0x06, 0x01, 0x01,
  29646. 0x00, 0x00,
  29647. 0x31, 0x80,
  29648. 0x06, 0x01, 0x01,
  29649. 0x00, 0x00,
  29650. 0x00, 0x00,
  29651. };
  29652. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  29653. 0x30, 0x12,
  29654. 0x02, 0x01, 0x01,
  29655. 0x30, 0x08,
  29656. 0x04, 0x03, 0x01, 0x02, 0x03,
  29657. 0x06, 0x01, 0x01,
  29658. 0x31, 0x03,
  29659. 0x06, 0x01, 0x01
  29660. };
  29661. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  29662. berDerTestData testData[] = {
  29663. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  29664. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  29665. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  29666. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  29667. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  29668. };
  29669. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  29670. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  29671. if (ret != LENGTH_ONLY_E)
  29672. return -13500 - i;
  29673. if (len != testData[i].outSz)
  29674. return -13510 - i;
  29675. len = testData[i].outSz;
  29676. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  29677. if (ret != 0)
  29678. return -13520 - i;
  29679. if (XMEMCMP(out, testData[i].out, len) != 0)
  29680. return -13530 - i;
  29681. for (l = 1; l < testData[i].inSz; l++) {
  29682. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  29683. if (ret != ASN_PARSE_E)
  29684. return -13540;
  29685. len = testData[i].outSz;
  29686. ret = wc_BerToDer(testData[i].in, l, out, &len);
  29687. if (ret != ASN_PARSE_E)
  29688. return -13541;
  29689. }
  29690. for (l = 0; l < testData[i].outSz-1; l++) {
  29691. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  29692. if (ret != BUFFER_E)
  29693. return -13542;
  29694. }
  29695. }
  29696. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  29697. if (ret != BAD_FUNC_ARG)
  29698. return -13543;
  29699. ret = wc_BerToDer(out, 4, NULL, NULL);
  29700. if (ret != BAD_FUNC_ARG)
  29701. return -13544;
  29702. ret = wc_BerToDer(NULL, 4, NULL, &len);
  29703. if (ret != BAD_FUNC_ARG)
  29704. return -13545;
  29705. ret = wc_BerToDer(NULL, 4, out, NULL);
  29706. if (ret != BAD_FUNC_ARG)
  29707. return -13546;
  29708. ret = wc_BerToDer(out, 4, out, NULL);
  29709. if (ret != BAD_FUNC_ARG)
  29710. return -13547;
  29711. ret = wc_BerToDer(NULL, 4, out, &len);
  29712. if (ret != BAD_FUNC_ARG)
  29713. return -13548;
  29714. for (l = 1; l < sizeof(good4_out); l++) {
  29715. len = l;
  29716. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  29717. if (ret != BUFFER_E)
  29718. return -13549;
  29719. }
  29720. return 0;
  29721. }
  29722. #endif
  29723. #ifdef DEBUG_WOLFSSL
  29724. static THREAD_LS_T int log_cnt = 0;
  29725. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  29726. {
  29727. (void)logLevel;
  29728. (void)logMessage;
  29729. log_cnt++;
  29730. }
  29731. #endif /* DEBUG_WOLFSSL */
  29732. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  29733. {
  29734. #ifdef DEBUG_WOLFSSL
  29735. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  29736. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  29737. byte b[256];
  29738. int i;
  29739. for (i = 0; i < (int)sizeof(b); i++)
  29740. b[i] = i;
  29741. if (wolfSSL_Debugging_ON() != 0)
  29742. return -13600;
  29743. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  29744. return -13601;
  29745. WOLFSSL_MSG(msg);
  29746. WOLFSSL_BUFFER(a, sizeof(a));
  29747. WOLFSSL_BUFFER(b, sizeof(b));
  29748. WOLFSSL_BUFFER(NULL, 0);
  29749. WOLFSSL_ERROR(MEMORY_E);
  29750. WOLFSSL_ERROR_MSG(msg);
  29751. /* turn off logs */
  29752. wolfSSL_Debugging_OFF();
  29753. /* capture log count */
  29754. i = log_cnt;
  29755. /* validate no logs are output when disabled */
  29756. WOLFSSL_MSG(msg);
  29757. WOLFSSL_BUFFER(a, sizeof(a));
  29758. WOLFSSL_BUFFER(b, sizeof(b));
  29759. WOLFSSL_BUFFER(NULL, 0);
  29760. WOLFSSL_ERROR(MEMORY_E);
  29761. WOLFSSL_ERROR_MSG(msg);
  29762. /* check the logs were disabled */
  29763. if (i != log_cnt)
  29764. return -13602;
  29765. /* restore callback and leave logging enabled */
  29766. wolfSSL_SetLoggingCb(NULL);
  29767. wolfSSL_Debugging_ON();
  29768. /* suppress unused args */
  29769. (void)a;
  29770. (void)b;
  29771. #else
  29772. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  29773. return -13603;
  29774. wolfSSL_Debugging_OFF();
  29775. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  29776. return -13604;
  29777. #endif /* DEBUG_WOLFSSL */
  29778. return 0;
  29779. }
  29780. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  29781. {
  29782. #ifdef WOLFSSL_PTHREADS
  29783. wolfSSL_Mutex m;
  29784. #endif
  29785. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  29786. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  29787. if (mm == NULL)
  29788. return -13700;
  29789. wc_FreeMutex(mm);
  29790. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  29791. #endif
  29792. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  29793. #ifdef WOLFSSL_PTHREADS
  29794. if (wc_InitMutex(&m) != 0)
  29795. return -13701;
  29796. if (wc_LockMutex(&m) != 0)
  29797. return -13702;
  29798. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  29799. /* trying to free a locked mutex is not portable behavior with pthread */
  29800. /* Attempting to destroy a locked mutex results in undefined behavior */
  29801. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  29802. return -13703;
  29803. #endif
  29804. if (wc_UnLockMutex(&m) != 0)
  29805. return -13704;
  29806. if (wc_FreeMutex(&m) != 0)
  29807. return -13705;
  29808. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  29809. /* Trying to use a pthread after free'ing is not portable behavior */
  29810. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  29811. return -13706;
  29812. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  29813. return -13707;
  29814. #endif
  29815. #endif
  29816. return 0;
  29817. }
  29818. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  29819. #ifndef WOLFSSL_NO_MALLOC
  29820. static int malloc_cnt = 0;
  29821. static int realloc_cnt = 0;
  29822. static int free_cnt = 0;
  29823. static void *my_Malloc_cb(size_t size)
  29824. {
  29825. malloc_cnt++;
  29826. #ifndef WOLFSSL_NO_MALLOC
  29827. return malloc(size);
  29828. #else
  29829. WOLFSSL_MSG("No malloc available");
  29830. (void)size;
  29831. return NULL;
  29832. #endif
  29833. }
  29834. static void my_Free_cb(void *ptr)
  29835. {
  29836. free_cnt++;
  29837. #ifndef WOLFSSL_NO_MALLOC
  29838. free(ptr);
  29839. #else
  29840. WOLFSSL_MSG("No free available");
  29841. (void)ptr;
  29842. #endif
  29843. }
  29844. static void *my_Realloc_cb(void *ptr, size_t size)
  29845. {
  29846. realloc_cnt++;
  29847. #ifndef WOLFSSL_NO_MALLOC
  29848. return realloc(ptr, size);
  29849. #else
  29850. WOLFSSL_MSG("No realloc available");
  29851. (void)ptr;
  29852. (void)size;
  29853. return NULL;
  29854. #endif
  29855. }
  29856. #endif /* !WOLFSSL_NO_MALLOC */
  29857. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  29858. {
  29859. int ret = 0;
  29860. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29861. byte* b = NULL;
  29862. #endif
  29863. wolfSSL_Malloc_cb mc;
  29864. wolfSSL_Free_cb fc;
  29865. wolfSSL_Realloc_cb rc;
  29866. /* Save existing memory callbacks */
  29867. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  29868. return -13800;
  29869. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29870. /* test realloc */
  29871. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29872. if (b == NULL) {
  29873. ERROR_OUT(-13801, exit_memcb);
  29874. }
  29875. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29876. b = NULL;
  29877. /* Use API. */
  29878. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)(void*)&my_Malloc_cb,
  29879. (wolfSSL_Free_cb)(void*)&my_Free_cb,
  29880. (wolfSSL_Realloc_cb)(void*)&my_Realloc_cb) != 0) {
  29881. ERROR_OUT(-13802, exit_memcb);
  29882. }
  29883. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29884. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29885. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29886. #ifndef WOLFSSL_STATIC_MEMORY
  29887. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  29888. #else
  29889. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  29890. #endif
  29891. ret = -13803;
  29892. #endif /* !WOLFSSL_NO_MALLOC */
  29893. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM)
  29894. exit_memcb:
  29895. #endif
  29896. /* restore memory callbacks */
  29897. wolfSSL_SetAllocators(mc, fc, rc);
  29898. return ret;
  29899. }
  29900. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  29901. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  29902. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  29903. {
  29904. int ret = 0;
  29905. byte out[112];
  29906. byte blob[112];
  29907. word32 outSz;
  29908. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  29909. {
  29910. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  29911. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  29912. };
  29913. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  29914. {
  29915. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  29916. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  29917. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  29918. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  29919. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  29920. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  29921. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  29922. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  29923. };
  29924. XMEMSET(blob, 0, sizeof(blob));
  29925. XMEMSET(out, 0, sizeof(out));
  29926. outSz = sizeof(blob);
  29927. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  29928. if (ret != 0) {
  29929. ERROR_OUT(-13900, exit_blob);
  29930. }
  29931. blob[outSz - 2] += 1;
  29932. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29933. if (ret == 0) { /* should fail with altered blob */
  29934. ERROR_OUT(-13901, exit_blob);
  29935. }
  29936. XMEMSET(blob, 0, sizeof(blob));
  29937. outSz = sizeof(blob);
  29938. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  29939. if (ret != 0) {
  29940. ERROR_OUT(-13902, exit_blob);
  29941. }
  29942. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29943. if (ret != 0) {
  29944. ERROR_OUT(-13903, exit_blob);
  29945. }
  29946. if (XMEMCMP(out, iv, sizeof(iv))) {
  29947. ERROR_OUT(-13904, exit_blob);
  29948. }
  29949. XMEMSET(blob, 0, sizeof(blob));
  29950. outSz = sizeof(blob);
  29951. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  29952. if (ret != 0) {
  29953. ERROR_OUT(-13905, exit_blob);
  29954. }
  29955. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  29956. if (ret != 0) {
  29957. ERROR_OUT(-13906, exit_blob);
  29958. }
  29959. if (XMEMCMP(out, text, sizeof(text))) {
  29960. ERROR_OUT(-13907, exit_blob);
  29961. }
  29962. exit_blob:
  29963. return ret;
  29964. }
  29965. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  29966. #ifdef WOLF_CRYPTO_CB
  29967. /* Example custom context for crypto callback */
  29968. typedef struct {
  29969. int exampleVar; /* example, not used */
  29970. } myCryptoDevCtx;
  29971. /* Example crypto dev callback function that calls software version */
  29972. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  29973. {
  29974. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  29975. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  29976. if (info == NULL)
  29977. return BAD_FUNC_ARG;
  29978. #ifdef DEBUG_WOLFSSL
  29979. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  29980. #endif
  29981. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  29982. #ifndef WC_NO_RNG
  29983. /* set devId to invalid, so software is used */
  29984. info->rng.rng->devId = INVALID_DEVID;
  29985. ret = wc_RNG_GenerateBlock(info->rng.rng,
  29986. info->rng.out, info->rng.sz);
  29987. /* reset devId */
  29988. info->rng.rng->devId = devIdArg;
  29989. #endif
  29990. }
  29991. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  29992. #ifndef WC_NO_RNG
  29993. static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  29994. word32* seedWord32 = (word32*)seed;
  29995. word32 len;
  29996. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  29997. while (info->seed.sz > 0) {
  29998. len = (word32)sizeof(seed);
  29999. if (info->seed.sz < len)
  30000. len = info->seed.sz;
  30001. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  30002. info->seed.seed += len;
  30003. info->seed.sz -= len;
  30004. (*seedWord32)++;
  30005. }
  30006. ret = 0;
  30007. #endif
  30008. }
  30009. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  30010. #ifdef DEBUG_WOLFSSL
  30011. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  30012. #endif
  30013. #ifndef NO_RSA
  30014. if (info->pk.type == WC_PK_TYPE_RSA) {
  30015. /* set devId to invalid, so software is used */
  30016. info->pk.rsa.key->devId = INVALID_DEVID;
  30017. switch (info->pk.rsa.type) {
  30018. case RSA_PUBLIC_ENCRYPT:
  30019. case RSA_PUBLIC_DECRYPT:
  30020. /* perform software based RSA public op */
  30021. ret = wc_RsaFunction(
  30022. info->pk.rsa.in, info->pk.rsa.inLen,
  30023. info->pk.rsa.out, info->pk.rsa.outLen,
  30024. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  30025. break;
  30026. case RSA_PRIVATE_ENCRYPT:
  30027. case RSA_PRIVATE_DECRYPT:
  30028. /* perform software based RSA private op */
  30029. ret = wc_RsaFunction(
  30030. info->pk.rsa.in, info->pk.rsa.inLen,
  30031. info->pk.rsa.out, info->pk.rsa.outLen,
  30032. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  30033. break;
  30034. }
  30035. /* reset devId */
  30036. info->pk.rsa.key->devId = devIdArg;
  30037. }
  30038. #ifdef WOLFSSL_KEY_GEN
  30039. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  30040. info->pk.rsakg.key->devId = INVALID_DEVID;
  30041. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  30042. info->pk.rsakg.e, info->pk.rsakg.rng);
  30043. /* reset devId */
  30044. info->pk.rsakg.key->devId = devIdArg;
  30045. }
  30046. #endif
  30047. #endif /* !NO_RSA */
  30048. #ifdef HAVE_ECC
  30049. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  30050. /* set devId to invalid, so software is used */
  30051. info->pk.eckg.key->devId = INVALID_DEVID;
  30052. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  30053. info->pk.eckg.key, info->pk.eckg.curveId);
  30054. /* reset devId */
  30055. info->pk.eckg.key->devId = devIdArg;
  30056. }
  30057. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  30058. /* set devId to invalid, so software is used */
  30059. info->pk.eccsign.key->devId = INVALID_DEVID;
  30060. ret = wc_ecc_sign_hash(
  30061. info->pk.eccsign.in, info->pk.eccsign.inlen,
  30062. info->pk.eccsign.out, info->pk.eccsign.outlen,
  30063. info->pk.eccsign.rng, info->pk.eccsign.key);
  30064. /* reset devId */
  30065. info->pk.eccsign.key->devId = devIdArg;
  30066. }
  30067. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  30068. /* set devId to invalid, so software is used */
  30069. info->pk.eccverify.key->devId = INVALID_DEVID;
  30070. ret = wc_ecc_verify_hash(
  30071. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  30072. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  30073. info->pk.eccverify.res, info->pk.eccverify.key);
  30074. /* reset devId */
  30075. info->pk.eccverify.key->devId = devIdArg;
  30076. }
  30077. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  30078. /* set devId to invalid, so software is used */
  30079. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  30080. ret = wc_ecc_shared_secret(
  30081. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  30082. info->pk.ecdh.out, info->pk.ecdh.outlen);
  30083. /* reset devId */
  30084. info->pk.ecdh.private_key->devId = devIdArg;
  30085. }
  30086. #endif /* HAVE_ECC */
  30087. }
  30088. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  30089. #if !defined(NO_AES) || !defined(NO_DES3)
  30090. #ifdef HAVE_AESGCM
  30091. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  30092. if (info->cipher.enc) {
  30093. /* set devId to invalid, so software is used */
  30094. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  30095. ret = wc_AesGcmEncrypt(
  30096. info->cipher.aesgcm_enc.aes,
  30097. info->cipher.aesgcm_enc.out,
  30098. info->cipher.aesgcm_enc.in,
  30099. info->cipher.aesgcm_enc.sz,
  30100. info->cipher.aesgcm_enc.iv,
  30101. info->cipher.aesgcm_enc.ivSz,
  30102. info->cipher.aesgcm_enc.authTag,
  30103. info->cipher.aesgcm_enc.authTagSz,
  30104. info->cipher.aesgcm_enc.authIn,
  30105. info->cipher.aesgcm_enc.authInSz);
  30106. /* reset devId */
  30107. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  30108. }
  30109. else {
  30110. /* set devId to invalid, so software is used */
  30111. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  30112. ret = wc_AesGcmDecrypt(
  30113. info->cipher.aesgcm_dec.aes,
  30114. info->cipher.aesgcm_dec.out,
  30115. info->cipher.aesgcm_dec.in,
  30116. info->cipher.aesgcm_dec.sz,
  30117. info->cipher.aesgcm_dec.iv,
  30118. info->cipher.aesgcm_dec.ivSz,
  30119. info->cipher.aesgcm_dec.authTag,
  30120. info->cipher.aesgcm_dec.authTagSz,
  30121. info->cipher.aesgcm_dec.authIn,
  30122. info->cipher.aesgcm_dec.authInSz);
  30123. /* reset devId */
  30124. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  30125. }
  30126. }
  30127. #endif /* HAVE_AESGCM */
  30128. #ifdef HAVE_AES_CBC
  30129. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  30130. if (info->cipher.enc) {
  30131. /* set devId to invalid, so software is used */
  30132. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  30133. ret = wc_AesCbcEncrypt(
  30134. info->cipher.aescbc.aes,
  30135. info->cipher.aescbc.out,
  30136. info->cipher.aescbc.in,
  30137. info->cipher.aescbc.sz);
  30138. /* reset devId */
  30139. info->cipher.aescbc.aes->devId = devIdArg;
  30140. }
  30141. else {
  30142. /* set devId to invalid, so software is used */
  30143. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  30144. ret = wc_AesCbcDecrypt(
  30145. info->cipher.aescbc.aes,
  30146. info->cipher.aescbc.out,
  30147. info->cipher.aescbc.in,
  30148. info->cipher.aescbc.sz);
  30149. /* reset devId */
  30150. info->cipher.aescbc.aes->devId = devIdArg;
  30151. }
  30152. }
  30153. #endif /* HAVE_AES_CBC */
  30154. #ifndef NO_DES3
  30155. if (info->cipher.type == WC_CIPHER_DES3) {
  30156. if (info->cipher.enc) {
  30157. /* set devId to invalid, so software is used */
  30158. info->cipher.des3.des->devId = INVALID_DEVID;
  30159. ret = wc_Des3_CbcEncrypt(
  30160. info->cipher.des3.des,
  30161. info->cipher.des3.out,
  30162. info->cipher.des3.in,
  30163. info->cipher.des3.sz);
  30164. /* reset devId */
  30165. info->cipher.des3.des->devId = devIdArg;
  30166. }
  30167. else {
  30168. /* set devId to invalid, so software is used */
  30169. info->cipher.des3.des->devId = INVALID_DEVID;
  30170. ret = wc_Des3_CbcDecrypt(
  30171. info->cipher.des3.des,
  30172. info->cipher.des3.out,
  30173. info->cipher.des3.in,
  30174. info->cipher.des3.sz);
  30175. /* reset devId */
  30176. info->cipher.des3.des->devId = devIdArg;
  30177. }
  30178. }
  30179. #endif /* !NO_DES3 */
  30180. #endif /* !NO_AES || !NO_DES3 */
  30181. }
  30182. #if !defined(NO_SHA) || !defined(NO_SHA256)
  30183. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  30184. #if !defined(NO_SHA)
  30185. if (info->hash.type == WC_HASH_TYPE_SHA) {
  30186. if (info->hash.sha1 == NULL)
  30187. return NOT_COMPILED_IN;
  30188. /* set devId to invalid, so software is used */
  30189. info->hash.sha1->devId = INVALID_DEVID;
  30190. if (info->hash.in != NULL) {
  30191. ret = wc_ShaUpdate(
  30192. info->hash.sha1,
  30193. info->hash.in,
  30194. info->hash.inSz);
  30195. }
  30196. if (info->hash.digest != NULL) {
  30197. ret = wc_ShaFinal(
  30198. info->hash.sha1,
  30199. info->hash.digest);
  30200. }
  30201. /* reset devId */
  30202. info->hash.sha1->devId = devIdArg;
  30203. }
  30204. else
  30205. #endif
  30206. #if !defined(NO_SHA256)
  30207. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  30208. if (info->hash.sha256 == NULL)
  30209. return NOT_COMPILED_IN;
  30210. /* set devId to invalid, so software is used */
  30211. info->hash.sha256->devId = INVALID_DEVID;
  30212. if (info->hash.in != NULL) {
  30213. ret = wc_Sha256Update(
  30214. info->hash.sha256,
  30215. info->hash.in,
  30216. info->hash.inSz);
  30217. }
  30218. if (info->hash.digest != NULL) {
  30219. ret = wc_Sha256Final(
  30220. info->hash.sha256,
  30221. info->hash.digest);
  30222. }
  30223. /* reset devId */
  30224. info->hash.sha256->devId = devIdArg;
  30225. }
  30226. else
  30227. #endif
  30228. {
  30229. }
  30230. }
  30231. #endif /* !NO_SHA || !NO_SHA256 */
  30232. #ifndef NO_HMAC
  30233. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  30234. if (info->hmac.hmac == NULL)
  30235. return NOT_COMPILED_IN;
  30236. /* set devId to invalid, so software is used */
  30237. info->hmac.hmac->devId = INVALID_DEVID;
  30238. if (info->hash.in != NULL) {
  30239. ret = wc_HmacUpdate(
  30240. info->hmac.hmac,
  30241. info->hmac.in,
  30242. info->hmac.inSz);
  30243. }
  30244. else if (info->hash.digest != NULL) {
  30245. ret = wc_HmacFinal(
  30246. info->hmac.hmac,
  30247. info->hmac.digest);
  30248. }
  30249. /* reset devId */
  30250. info->hmac.hmac->devId = devIdArg;
  30251. }
  30252. #endif
  30253. (void)devIdArg;
  30254. (void)myCtx;
  30255. return ret;
  30256. }
  30257. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  30258. {
  30259. int ret = 0;
  30260. myCryptoDevCtx myCtx;
  30261. /* example data for callback */
  30262. myCtx.exampleVar = 1;
  30263. /* set devId to something other than INVALID_DEVID */
  30264. devId = 1;
  30265. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  30266. #ifndef WC_NO_RNG
  30267. if (ret == 0)
  30268. ret = random_test();
  30269. #endif /* WC_NO_RNG */
  30270. #ifndef NO_RSA
  30271. if (ret == 0)
  30272. ret = rsa_test();
  30273. #endif
  30274. #ifdef HAVE_ECC
  30275. if (ret == 0)
  30276. ret = ecc_test();
  30277. #endif
  30278. #ifndef NO_AES
  30279. #ifdef HAVE_AESGCM
  30280. if (ret == 0)
  30281. ret = aesgcm_test();
  30282. #endif
  30283. #ifdef HAVE_AES_CBC
  30284. if (ret == 0)
  30285. ret = aes_test();
  30286. #endif
  30287. #endif /* !NO_AES */
  30288. #ifndef NO_DES3
  30289. if (ret == 0)
  30290. ret = des3_test();
  30291. #endif /* !NO_DES3 */
  30292. #if !defined(NO_SHA) || !defined(NO_SHA256)
  30293. #ifndef NO_SHA
  30294. if (ret == 0)
  30295. ret = sha_test();
  30296. #endif
  30297. #ifndef NO_SHA256
  30298. if (ret == 0)
  30299. ret = sha256_test();
  30300. #endif
  30301. #endif
  30302. #ifndef NO_HMAC
  30303. #ifndef NO_SHA
  30304. if (ret == 0)
  30305. ret = hmac_sha_test();
  30306. #endif
  30307. #ifndef NO_SHA256
  30308. if (ret == 0)
  30309. ret = hmac_sha256_test();
  30310. #endif
  30311. #endif
  30312. #ifndef NO_PWDBASED
  30313. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  30314. if (ret == 0)
  30315. ret = pbkdf2_test();
  30316. #endif
  30317. #endif
  30318. /* reset devId */
  30319. devId = INVALID_DEVID;
  30320. return ret;
  30321. }
  30322. #endif /* WOLF_CRYPTO_CB */
  30323. #ifdef WOLFSSL_CERT_PIV
  30324. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  30325. {
  30326. int ret;
  30327. wc_CertPIV piv;
  30328. /* Template for Identiv PIV cert, nonce and signature */
  30329. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  30330. 0x0A, 0x0D,
  30331. 0x53, 0x04, /* NIST PIV Cert */
  30332. 0x70, 0x02, /* Certificate */
  30333. 0x30, 0x00,
  30334. 0x71, 0x01, 0x00, /* Cert Info */
  30335. 0xFE, 0x00, /* Error Detection */
  30336. 0x0B, 0x01, 0x00, /* Nonce */
  30337. 0x0C, 0x01, 0x00, /* Signed Nonce */
  30338. };
  30339. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  30340. 0x53, 0x04, /* NIST PIV Cert */
  30341. 0x70, 0x02, /* Certificate */
  30342. 0x30, 0x00,
  30343. 0x71, 0x01, 0x00, /* Cert Info */
  30344. 0xFE, 0x00, /* Error Detection */
  30345. };
  30346. /* Test with identiv 0x0A, 0x0B and 0x0C markers */
  30347. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  30348. if (ret == 0) {
  30349. /* Test with NIST PIV format */
  30350. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  30351. }
  30352. return ret;
  30353. }
  30354. #endif /* WOLFSSL_CERT_PIV */
  30355. #undef ERROR_OUT
  30356. #else
  30357. #ifndef NO_MAIN_DRIVER
  30358. int main() { return 0; }
  30359. #endif
  30360. #endif /* NO_CRYPT_TEST */