test.c 612 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2017 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. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef XMALLOC_USER
  26. #include <stdlib.h> /* we're using malloc / free direct here */
  27. #endif
  28. #ifndef NO_CRYPT_TEST
  29. #include <wolfssl/wolfcrypt/memory.h>
  30. #ifdef WOLFSSL_STATIC_MEMORY
  31. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  32. #else
  33. #define HEAP_HINT NULL
  34. #endif /* WOLFSSL_STATIC_MEMORY */
  35. #include <wolfssl/wolfcrypt/wc_port.h>
  36. #include <wolfssl/wolfcrypt/logging.h>
  37. #include <wolfssl/wolfcrypt/types.h>
  38. #if defined(WOLFSSL_TEST_CERT) || defined(ASN_BER_TO_DER)
  39. #include <wolfssl/wolfcrypt/asn.h>
  40. #else
  41. #include <wolfssl/wolfcrypt/asn_public.h>
  42. #endif
  43. #include <wolfssl/wolfcrypt/md2.h>
  44. #include <wolfssl/wolfcrypt/md5.h>
  45. #include <wolfssl/wolfcrypt/md4.h>
  46. #include <wolfssl/wolfcrypt/sha.h>
  47. #include <wolfssl/wolfcrypt/sha256.h>
  48. #include <wolfssl/wolfcrypt/sha512.h>
  49. #include <wolfssl/wolfcrypt/arc4.h>
  50. #if defined(WC_NO_RNG) && defined(USE_FAST_MATH)
  51. #include <wolfssl/wolfcrypt/tfm.h>
  52. #else
  53. #include <wolfssl/wolfcrypt/random.h>
  54. #endif
  55. #include <wolfssl/wolfcrypt/coding.h>
  56. #include <wolfssl/wolfcrypt/signature.h>
  57. #include <wolfssl/wolfcrypt/rsa.h>
  58. #include <wolfssl/wolfcrypt/des3.h>
  59. #include <wolfssl/wolfcrypt/aes.h>
  60. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  61. #include <wolfssl/wolfcrypt/cmac.h>
  62. #include <wolfssl/wolfcrypt/poly1305.h>
  63. #include <wolfssl/wolfcrypt/camellia.h>
  64. #include <wolfssl/wolfcrypt/hmac.h>
  65. #include <wolfssl/wolfcrypt/dh.h>
  66. #include <wolfssl/wolfcrypt/dsa.h>
  67. #include <wolfssl/wolfcrypt/srp.h>
  68. #include <wolfssl/wolfcrypt/idea.h>
  69. #include <wolfssl/wolfcrypt/hc128.h>
  70. #include <wolfssl/wolfcrypt/rabbit.h>
  71. #include <wolfssl/wolfcrypt/chacha.h>
  72. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  73. #include <wolfssl/wolfcrypt/pwdbased.h>
  74. #include <wolfssl/wolfcrypt/ripemd.h>
  75. #include <wolfssl/wolfcrypt/error-crypt.h>
  76. #ifdef HAVE_ECC
  77. #include <wolfssl/wolfcrypt/ecc.h>
  78. #endif
  79. #ifdef HAVE_CURVE25519
  80. #include <wolfssl/wolfcrypt/curve25519.h>
  81. #endif
  82. #ifdef HAVE_ED25519
  83. #include <wolfssl/wolfcrypt/ed25519.h>
  84. #endif
  85. #ifdef HAVE_BLAKE2
  86. #include <wolfssl/wolfcrypt/blake2.h>
  87. #endif
  88. #ifdef WOLFSSL_SHA3
  89. #include <wolfssl/wolfcrypt/sha3.h>
  90. #endif
  91. #ifdef HAVE_LIBZ
  92. #include <wolfssl/wolfcrypt/compress.h>
  93. #endif
  94. #ifdef HAVE_PKCS7
  95. #include <wolfssl/wolfcrypt/pkcs7.h>
  96. #endif
  97. #ifdef HAVE_FIPS
  98. #include <wolfssl/wolfcrypt/fips_test.h>
  99. #endif
  100. #ifdef HAVE_SELFTEST
  101. #include <wolfssl/wolfcrypt/selftest.h>
  102. #endif
  103. #ifdef WOLFSSL_ASYNC_CRYPT
  104. #include <wolfssl/wolfcrypt/async.h>
  105. #endif
  106. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  107. #include <wolfssl/wolfcrypt/logging.h>
  108. #endif
  109. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  110. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  111. #endif
  112. #ifdef WOLF_CRYPTO_DEV
  113. #include <wolfssl/wolfcrypt/cryptodev.h>
  114. #endif
  115. /* only for stack size check */
  116. #ifdef HAVE_STACK_SIZE
  117. #include <wolfssl/ssl.h>
  118. #define err_sys err_sys_remap /* remap err_sys */
  119. #include <wolfssl/test.h>
  120. #undef err_sys
  121. #endif
  122. #ifdef _MSC_VER
  123. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  124. #pragma warning(disable: 4996)
  125. #endif
  126. #ifdef OPENSSL_EXTRA
  127. #ifndef WOLFCRYPT_ONLY
  128. #include <wolfssl/openssl/evp.h>
  129. #endif
  130. #include <wolfssl/openssl/rand.h>
  131. #include <wolfssl/openssl/hmac.h>
  132. #include <wolfssl/openssl/aes.h>
  133. #include <wolfssl/openssl/des.h>
  134. #endif
  135. #if defined(NO_FILESYSTEM)
  136. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  137. !defined(USE_CERT_BUFFERS_4096)
  138. #define USE_CERT_BUFFERS_2048
  139. #endif
  140. #if !defined(USE_CERT_BUFFERS_256)
  141. #define USE_CERT_BUFFERS_256
  142. #endif
  143. #endif
  144. #include <wolfssl/certs_test.h>
  145. #ifdef HAVE_NTRU
  146. #include "libntruencrypt/ntru_crypto.h"
  147. #endif
  148. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  149. #include <mqx.h>
  150. #include <stdlib.h>
  151. #if MQX_USE_IO_OLD
  152. #include <fio.h>
  153. #else
  154. #include <nio.h>
  155. #endif
  156. #elif defined(FREESCALE_KSDK_BM)
  157. #include "fsl_debug_console.h"
  158. #undef printf
  159. #define printf PRINTF
  160. #elif defined(WOLFSSL_APACHE_MYNEWT)
  161. #include <assert.h>
  162. #include <string.h>
  163. #include "sysinit/sysinit.h"
  164. #include "os/os.h"
  165. #ifdef ARCH_sim
  166. #include "mcu/mcu_sim.h"
  167. #endif
  168. #include "os/os_time.h"
  169. #else
  170. #include <stdio.h>
  171. #endif
  172. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  173. #define ENABLE_ECC384_CERT_GEN_TEST
  174. #endif
  175. #ifdef THREADX
  176. /* since just testing, use THREADX log printf instead */
  177. int dc_log_printf(char*, ...);
  178. #undef printf
  179. #define printf dc_log_printf
  180. #endif
  181. #ifdef MICRIUM
  182. #include <bsp_ser.h>
  183. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  184. #undef printf
  185. #define printf BSP_Ser_Printf
  186. #elif defined(WOLFSSL_PB)
  187. #include <stdarg.h>
  188. int wolfssl_pb_print(const char*, ...);
  189. #undef printf
  190. #define printf wolfssl_pb_print
  191. #endif
  192. #include "wolfcrypt/test/test.h"
  193. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  194. static void initDefaultName(void);
  195. #endif
  196. /* for async devices */
  197. static int devId = INVALID_DEVID;
  198. #ifdef HAVE_WNR
  199. const char* wnrConfigFile = "wnr-example.conf";
  200. #endif
  201. typedef struct testVector {
  202. const char* input;
  203. const char* output;
  204. size_t inLen;
  205. size_t outLen;
  206. } testVector;
  207. int error_test(void);
  208. int base64_test(void);
  209. int base16_test(void);
  210. int asn_test(void);
  211. int md2_test(void);
  212. int md5_test(void);
  213. int md4_test(void);
  214. int sha_test(void);
  215. int sha224_test(void);
  216. int sha256_test(void);
  217. int sha512_test(void);
  218. int sha384_test(void);
  219. int sha3_test(void);
  220. int hash_test(void);
  221. int hmac_md5_test(void);
  222. int hmac_sha_test(void);
  223. int hmac_sha224_test(void);
  224. int hmac_sha256_test(void);
  225. int hmac_sha384_test(void);
  226. int hmac_sha512_test(void);
  227. int hmac_blake2b_test(void);
  228. int hmac_sha3_test(void);
  229. int hkdf_test(void);
  230. int x963kdf_test(void);
  231. int arc4_test(void);
  232. int hc128_test(void);
  233. int rabbit_test(void);
  234. int chacha_test(void);
  235. int chacha20_poly1305_aead_test(void);
  236. int des_test(void);
  237. int des3_test(void);
  238. int aes_test(void);
  239. int aes192_test(void);
  240. int aes256_test(void);
  241. int cmac_test(void);
  242. int poly1305_test(void);
  243. int aesgcm_test(void);
  244. int aesgcm_default_test(void);
  245. int gmac_test(void);
  246. int aesccm_test(void);
  247. int aeskeywrap_test(void);
  248. int camellia_test(void);
  249. int rsa_no_pad_test(void);
  250. int rsa_test(void);
  251. int dh_test(void);
  252. int dsa_test(void);
  253. int srp_test(void);
  254. #ifndef WC_NO_RNG
  255. int random_test(void);
  256. #endif /* WC_NO_RNG */
  257. int pwdbased_test(void);
  258. int ripemd_test(void);
  259. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  260. int openssl_test(void); /* test mini api */
  261. int openssl_pkey_test(void);
  262. int openssl_pkey0_test(void);
  263. int openssl_pkey1_test(void);
  264. int openSSL_evpMD_test(void);
  265. int openssl_evpSig_test(void);
  266. #endif
  267. int pbkdf1_test(void);
  268. int pkcs12_test(void);
  269. int pbkdf2_test(void);
  270. int scrypt_test(void);
  271. #ifdef HAVE_ECC
  272. int ecc_test(void);
  273. #ifdef HAVE_ECC_ENCRYPT
  274. int ecc_encrypt_test(void);
  275. #endif
  276. #ifdef USE_CERT_BUFFERS_256
  277. int ecc_test_buffers(void);
  278. #endif
  279. #endif
  280. #ifdef HAVE_CURVE25519
  281. int curve25519_test(void);
  282. #endif
  283. #ifdef HAVE_ED25519
  284. int ed25519_test(void);
  285. #endif
  286. #ifdef HAVE_BLAKE2
  287. int blake2b_test(void);
  288. #endif
  289. #ifdef HAVE_LIBZ
  290. int compress_test(void);
  291. #endif
  292. #ifdef HAVE_PKCS7
  293. int pkcs7enveloped_test(void);
  294. int pkcs7signed_test(void);
  295. #ifndef NO_PKCS7_ENCRYPTED_DATA
  296. int pkcs7encrypted_test(void);
  297. #endif
  298. #endif
  299. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT)
  300. int cert_test(void);
  301. #endif
  302. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  303. int certext_test(void);
  304. #endif
  305. #ifdef HAVE_IDEA
  306. int idea_test(void);
  307. #endif
  308. #ifdef WOLFSSL_STATIC_MEMORY
  309. int memory_test(void);
  310. #endif
  311. #ifdef HAVE_VALGRIND
  312. int mp_test(void);
  313. #endif
  314. #ifdef WOLFSSL_PUBLIC_MP
  315. int prime_test(void);
  316. #endif
  317. #ifdef ASN_BER_TO_DER
  318. int berder_test(void);
  319. #endif
  320. int logging_test(void);
  321. int mutex_test(void);
  322. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  323. int memcb_test(void);
  324. #endif
  325. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  326. int blob_test(void);
  327. #endif
  328. #ifdef WOLF_CRYPTO_DEV
  329. int cryptodev_test(void);
  330. #endif
  331. /* General big buffer size for many tests. */
  332. #define FOURK_BUF 4096
  333. #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
  334. #ifdef HAVE_STACK_SIZE
  335. static THREAD_RETURN err_sys(const char* msg, int es)
  336. #else
  337. static int err_sys(const char* msg, int es)
  338. #endif
  339. {
  340. printf("%s error = %d\n", msg, es);
  341. EXIT_TEST(-1);
  342. }
  343. #ifndef HAVE_STACK_SIZE
  344. /* func_args from test.h, so don't have to pull in other stuff */
  345. typedef struct func_args {
  346. int argc;
  347. char** argv;
  348. int return_code;
  349. } func_args;
  350. #endif /* !HAVE_STACK_SIZE */
  351. #ifdef HAVE_FIPS
  352. static void myFipsCb(int ok, int err, const char* hash)
  353. {
  354. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  355. printf("message = %s\n", wc_GetErrorString(err));
  356. printf("hash = %s\n", hash);
  357. if (err == IN_CORE_FIPS_E) {
  358. printf("In core integrity hash check failure, copy above hash\n");
  359. printf("into verifyCore[] in fips_test.c and rebuild\n");
  360. }
  361. }
  362. #endif /* HAVE_FIPS */
  363. #ifdef WOLFSSL_STATIC_MEMORY
  364. #ifdef BENCH_EMBEDDED
  365. static byte gTestMemory[10000];
  366. #elif defined(WOLFSSL_CERT_EXT)
  367. static byte gTestMemory[140000];
  368. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  369. static byte gTestMemory[130000];
  370. #else
  371. static byte gTestMemory[80000];
  372. #endif
  373. #endif
  374. #ifdef WOLFSSL_PB
  375. int wolfssl_pb_print(const char* msg, ...)
  376. {
  377. int ret;
  378. va_list args;
  379. char tmpBuf[80];
  380. va_start(args, msg);
  381. ret = vsprint(tmpBuf, msg, args);
  382. va_end(args);
  383. fnDumpStringToSystemLog(tmpBuf);
  384. return ret;
  385. }
  386. #endif /* WOLFSSL_PB */
  387. #ifdef HAVE_STACK_SIZE
  388. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  389. #else
  390. int wolfcrypt_test(void* args)
  391. #endif
  392. {
  393. int ret;
  394. if (args)
  395. ((func_args*)args)->return_code = -1; /* error state */
  396. #ifdef WOLFSSL_STATIC_MEMORY
  397. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  398. WOLFMEM_GENERAL, 1) != 0) {
  399. printf("unable to load static memory");
  400. exit(EXIT_FAILURE);
  401. }
  402. #endif
  403. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  404. wolfSSL_Debugging_ON();
  405. #endif
  406. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  407. wc_SetLoggingHeap(HEAP_HINT);
  408. #endif
  409. #ifdef HAVE_FIPS
  410. wolfCrypt_SetCb_fips(myFipsCb);
  411. #endif
  412. #if !defined(NO_BIG_INT)
  413. if (CheckCtcSettings() != 1)
  414. return err_sys("Build vs runtime math mismatch\n", -1000);
  415. #if defined(USE_FAST_MATH) && \
  416. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  417. if (CheckFastMathSettings() != 1)
  418. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  419. -1001);
  420. #endif /* USE_FAST_MATH */
  421. #endif /* !NO_BIG_INT */
  422. #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_MULTI_ATTRIB)
  423. initDefaultName();
  424. #endif
  425. #ifdef WOLFSSL_ASYNC_CRYPT
  426. ret = wolfAsync_DevOpen(&devId);
  427. if (ret < 0) {
  428. printf("Async device open failed\nRunning without async\n");
  429. }
  430. #else
  431. (void)devId;
  432. #endif /* WOLFSSL_ASYNC_CRYPT */
  433. #ifdef HAVE_SELFTEST
  434. if ( (ret = wolfCrypt_SelfTest()) != 0)
  435. return err_sys("CAVP selftest failed!\n", ret);
  436. else
  437. printf("CAVP selftest passed!\n");
  438. #endif
  439. if ( (ret = error_test()) != 0)
  440. return err_sys("error test failed!\n", ret);
  441. else
  442. printf( "error test passed!\n");
  443. #ifndef NO_CODING
  444. if ( (ret = base64_test()) != 0)
  445. return err_sys("base64 test failed!\n", ret);
  446. else
  447. printf( "base64 test passed!\n");
  448. #ifdef WOLFSSL_BASE16
  449. if ( (ret = base16_test()) != 0)
  450. return err_sys("base16 test failed!\n", ret);
  451. else
  452. printf( "base16 test passed!\n");
  453. #endif
  454. #endif /* !NO_CODING */
  455. #ifndef NO_ASN
  456. if ( (ret = asn_test()) != 0)
  457. return err_sys("asn test failed!\n", ret);
  458. else
  459. printf( "asn test passed!\n");
  460. #endif
  461. #ifndef NO_MD5
  462. if ( (ret = md5_test()) != 0)
  463. return err_sys("MD5 test failed!\n", ret);
  464. else
  465. printf( "MD5 test passed!\n");
  466. #endif
  467. #ifdef WOLFSSL_MD2
  468. if ( (ret = md2_test()) != 0)
  469. return err_sys("MD2 test failed!\n", ret);
  470. else
  471. printf( "MD2 test passed!\n");
  472. #endif
  473. #ifndef NO_MD4
  474. if ( (ret = md4_test()) != 0)
  475. return err_sys("MD4 test failed!\n", ret);
  476. else
  477. printf( "MD4 test passed!\n");
  478. #endif
  479. #ifndef NO_SHA
  480. if ( (ret = sha_test()) != 0)
  481. return err_sys("SHA test failed!\n", ret);
  482. else
  483. printf( "SHA test passed!\n");
  484. #endif
  485. #ifdef WOLFSSL_SHA224
  486. if ( (ret = sha224_test()) != 0)
  487. return err_sys("SHA-224 test failed!\n", ret);
  488. else
  489. printf( "SHA-224 test passed!\n");
  490. #endif
  491. #ifndef NO_SHA256
  492. if ( (ret = sha256_test()) != 0)
  493. return err_sys("SHA-256 test failed!\n", ret);
  494. else
  495. printf( "SHA-256 test passed!\n");
  496. #endif
  497. #ifdef WOLFSSL_SHA384
  498. if ( (ret = sha384_test()) != 0)
  499. return err_sys("SHA-384 test failed!\n", ret);
  500. else
  501. printf( "SHA-384 test passed!\n");
  502. #endif
  503. #ifdef WOLFSSL_SHA512
  504. if ( (ret = sha512_test()) != 0)
  505. return err_sys("SHA-512 test failed!\n", ret);
  506. else
  507. printf( "SHA-512 test passed!\n");
  508. #endif
  509. #ifdef WOLFSSL_SHA3
  510. if ( (ret = sha3_test()) != 0)
  511. return err_sys("SHA-3 test failed!\n", ret);
  512. else
  513. printf( "SHA-3 test passed!\n");
  514. #endif
  515. if ( (ret = hash_test()) != 0)
  516. return err_sys("Hash test failed!\n", ret);
  517. else
  518. printf( "Hash test passed!\n");
  519. #ifdef WOLFSSL_RIPEMD
  520. if ( (ret = ripemd_test()) != 0)
  521. return err_sys("RIPEMD test failed!\n", ret);
  522. else
  523. printf( "RIPEMD test passed!\n");
  524. #endif
  525. #ifdef HAVE_BLAKE2
  526. if ( (ret = blake2b_test()) != 0)
  527. return err_sys("BLAKE2b test failed!\n", ret);
  528. else
  529. printf( "BLAKE2b test passed!\n");
  530. #endif
  531. #ifndef NO_HMAC
  532. #ifndef NO_MD5
  533. if ( (ret = hmac_md5_test()) != 0)
  534. return err_sys("HMAC-MD5 test failed!\n", ret);
  535. else
  536. printf( "HMAC-MD5 test passed!\n");
  537. #endif
  538. #ifndef NO_SHA
  539. if ( (ret = hmac_sha_test()) != 0)
  540. return err_sys("HMAC-SHA test failed!\n", ret);
  541. else
  542. printf( "HMAC-SHA test passed!\n");
  543. #endif
  544. #ifdef WOLFSSL_SHA224
  545. if ( (ret = hmac_sha224_test()) != 0)
  546. return err_sys("HMAC-SHA224 test failed!\n", ret);
  547. else
  548. printf( "HMAC-SHA224 test passed!\n");
  549. #endif
  550. #ifndef NO_SHA256
  551. if ( (ret = hmac_sha256_test()) != 0)
  552. return err_sys("HMAC-SHA256 test failed!\n", ret);
  553. else
  554. printf( "HMAC-SHA256 test passed!\n");
  555. #endif
  556. #ifdef WOLFSSL_SHA384
  557. if ( (ret = hmac_sha384_test()) != 0)
  558. return err_sys("HMAC-SHA384 test failed!\n", ret);
  559. else
  560. printf( "HMAC-SHA384 test passed!\n");
  561. #endif
  562. #ifdef WOLFSSL_SHA512
  563. if ( (ret = hmac_sha512_test()) != 0)
  564. return err_sys("HMAC-SHA512 test failed!\n", ret);
  565. else
  566. printf( "HMAC-SHA512 test passed!\n");
  567. #endif
  568. #ifdef HAVE_BLAKE2
  569. if ( (ret = hmac_blake2b_test()) != 0)
  570. return err_sys("HMAC-BLAKE2 test failed!\n", ret);
  571. else
  572. printf( "HMAC-BLAKE2 test passed!\n");
  573. #endif
  574. #ifdef WOLFSSL_SHA3
  575. if ( (ret = hmac_sha3_test()) != 0)
  576. return err_sys("HMAC-SHA3 test failed!\n", ret);
  577. else
  578. printf( "HMAC-SHA3 test passed!\n");
  579. #endif
  580. #ifdef HAVE_HKDF
  581. if ( (ret = hkdf_test()) != 0)
  582. return err_sys("HMAC-KDF test failed!\n", ret);
  583. else
  584. printf( "HMAC-KDF test passed!\n");
  585. #endif
  586. #endif /* !NO_HMAC */
  587. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  588. if ( (ret = x963kdf_test()) != 0)
  589. return err_sys("X963-KDF test failed!\n", ret);
  590. else
  591. printf( "X963-KDF test passed!\n");
  592. #endif
  593. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  594. if ( (ret = gmac_test()) != 0)
  595. return err_sys("GMAC test failed!\n", ret);
  596. else
  597. printf( "GMAC test passed!\n");
  598. #endif
  599. #ifndef NO_RC4
  600. if ( (ret = arc4_test()) != 0)
  601. return err_sys("ARC4 test failed!\n", ret);
  602. else
  603. printf( "ARC4 test passed!\n");
  604. #endif
  605. #ifndef NO_HC128
  606. if ( (ret = hc128_test()) != 0)
  607. return err_sys("HC-128 test failed!\n", ret);
  608. else
  609. printf( "HC-128 test passed!\n");
  610. #endif
  611. #ifndef NO_RABBIT
  612. if ( (ret = rabbit_test()) != 0)
  613. return err_sys("Rabbit test failed!\n", ret);
  614. else
  615. printf( "Rabbit test passed!\n");
  616. #endif
  617. #ifdef HAVE_CHACHA
  618. if ( (ret = chacha_test()) != 0)
  619. return err_sys("Chacha test failed!\n", ret);
  620. else
  621. printf( "Chacha test passed!\n");
  622. #endif
  623. #ifdef HAVE_POLY1305
  624. if ( (ret = poly1305_test()) != 0)
  625. return err_sys("POLY1305 test failed!\n", ret);
  626. else
  627. printf( "POLY1305 test passed!\n");
  628. #endif
  629. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  630. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  631. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  632. else
  633. printf( "ChaCha20-Poly1305 AEAD test passed!\n");
  634. #endif
  635. #ifndef NO_DES3
  636. if ( (ret = des_test()) != 0)
  637. return err_sys("DES test failed!\n", ret);
  638. else
  639. printf( "DES test passed!\n");
  640. #endif
  641. #ifndef NO_DES3
  642. if ( (ret = des3_test()) != 0)
  643. return err_sys("DES3 test failed!\n", ret);
  644. else
  645. printf( "DES3 test passed!\n");
  646. #endif
  647. #ifndef NO_AES
  648. if ( (ret = aes_test()) != 0)
  649. return err_sys("AES test failed!\n", ret);
  650. else
  651. printf( "AES test passed!\n");
  652. #ifdef WOLFSSL_AES_192
  653. if ( (ret = aes192_test()) != 0)
  654. return err_sys("AES192 test failed!\n", ret);
  655. else
  656. printf( "AES192 test passed!\n");
  657. #endif
  658. #ifdef WOLFSSL_AES_256
  659. if ( (ret = aes256_test()) != 0)
  660. return err_sys("AES256 test failed!\n", ret);
  661. else
  662. printf( "AES256 test passed!\n");
  663. #endif
  664. #ifdef HAVE_AESGCM
  665. #ifndef WOLFSSL_AFALG
  666. if ( (ret = aesgcm_test()) != 0)
  667. return err_sys("AES-GCM test failed!\n", ret);
  668. else
  669. #endif
  670. {
  671. if ((ret = aesgcm_default_test()) != 0) {
  672. return err_sys("AES-GCM test failed!\n", ret);
  673. }
  674. else {
  675. printf( "AES-GCM test passed!\n");
  676. }
  677. }
  678. #endif
  679. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  680. if ( (ret = aesccm_test()) != 0)
  681. return err_sys("AES-CCM test failed!\n", ret);
  682. else
  683. printf( "AES-CCM test passed!\n");
  684. #endif
  685. #ifdef HAVE_AES_KEYWRAP
  686. if ( (ret = aeskeywrap_test()) != 0)
  687. return err_sys("AES Key Wrap test failed!\n", ret);
  688. else
  689. printf( "AES Key Wrap test passed!\n");
  690. #endif
  691. #endif
  692. #ifdef HAVE_CAMELLIA
  693. if ( (ret = camellia_test()) != 0)
  694. return err_sys("CAMELLIA test failed!\n", ret);
  695. else
  696. printf( "CAMELLIA test passed!\n");
  697. #endif
  698. #ifdef HAVE_IDEA
  699. if ( (ret = idea_test()) != 0)
  700. return err_sys("IDEA test failed!\n", ret);
  701. else
  702. printf( "IDEA test passed!\n");
  703. #endif
  704. #ifndef WC_NO_RNG
  705. if ( (ret = random_test()) != 0)
  706. return err_sys("RANDOM test failed!\n", ret);
  707. else
  708. printf( "RANDOM test passed!\n");
  709. #endif /* WC_NO_RNG */
  710. #ifdef WOLFSSL_STATIC_MEMORY
  711. if ( (ret = memory_test()) != 0)
  712. return err_sys("MEMORY test failed!\n", ret);
  713. else
  714. printf( "MEMORY test passed!\n");
  715. #endif
  716. #ifndef NO_RSA
  717. #ifdef WC_RSA_NO_PADDING
  718. if ( (ret = rsa_no_pad_test()) != 0)
  719. return err_sys("RSA NOPAD test failed!\n", ret);
  720. else
  721. printf( "RSA NOPAD test passed!\n");
  722. #endif
  723. if ( (ret = rsa_test()) != 0)
  724. return err_sys("RSA test failed!\n", ret);
  725. else
  726. printf( "RSA test passed!\n");
  727. #endif
  728. #ifndef NO_DH
  729. if ( (ret = dh_test()) != 0)
  730. return err_sys("DH test failed!\n", ret);
  731. else
  732. printf( "DH test passed!\n");
  733. #endif
  734. #ifndef NO_DSA
  735. if ( (ret = dsa_test()) != 0)
  736. return err_sys("DSA test failed!\n", ret);
  737. else
  738. printf( "DSA test passed!\n");
  739. #endif
  740. #ifdef WOLFCRYPT_HAVE_SRP
  741. if ( (ret = srp_test()) != 0)
  742. return err_sys("SRP test failed!\n", ret);
  743. else
  744. printf( "SRP test passed!\n");
  745. #endif
  746. #ifndef NO_PWDBASED
  747. if ( (ret = pwdbased_test()) != 0)
  748. return err_sys("PWDBASED test failed!\n", ret);
  749. else
  750. printf( "PWDBASED test passed!\n");
  751. #endif
  752. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  753. if ( (ret = openssl_test()) != 0)
  754. return err_sys("OPENSSL test failed!\n", ret);
  755. else
  756. printf( "OPENSSL test passed!\n");
  757. if ( (ret = openSSL_evpMD_test()) != 0)
  758. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  759. else
  760. printf( "OPENSSL (EVP MD) passed!\n");
  761. if ( (ret = openssl_pkey0_test()) != 0)
  762. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  763. else
  764. printf( "OPENSSL (PKEY0) passed!\n");
  765. if ( (ret = openssl_pkey1_test()) != 0)
  766. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  767. else
  768. printf( "OPENSSL (PKEY1) passed!\n");
  769. if ( (ret = openssl_evpSig_test()) != 0)
  770. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  771. else
  772. printf( "OPENSSL (EVP Sign/Verify) passed!\n");
  773. #endif
  774. #ifdef HAVE_ECC
  775. if ( (ret = ecc_test()) != 0)
  776. return err_sys("ECC test failed!\n", ret);
  777. else
  778. printf( "ECC test passed!\n");
  779. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  780. if ( (ret = ecc_encrypt_test()) != 0)
  781. return err_sys("ECC Enc test failed!\n", ret);
  782. else
  783. printf( "ECC Enc test passed!\n");
  784. #endif
  785. #ifdef USE_CERT_BUFFERS_256
  786. if ( (ret = ecc_test_buffers()) != 0)
  787. return err_sys("ECC buffer test failed!\n", ret);
  788. else
  789. printf( "ECC buffer test passed!\n");
  790. #endif
  791. #endif
  792. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT)
  793. if ( (ret = cert_test()) != 0)
  794. return err_sys("CERT test failed!\n", ret);
  795. else
  796. printf( "CERT test passed!\n");
  797. #endif
  798. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  799. if ( (ret = certext_test()) != 0)
  800. return err_sys("CERT EXT test failed!\n", ret);
  801. else
  802. printf( "CERT EXT test passed!\n");
  803. #endif
  804. #ifdef HAVE_CURVE25519
  805. if ( (ret = curve25519_test()) != 0)
  806. return err_sys("CURVE25519 test failed!\n", ret);
  807. else
  808. printf( "CURVE25519 test passed!\n");
  809. #endif
  810. #ifdef HAVE_ED25519
  811. if ( (ret = ed25519_test()) != 0)
  812. return err_sys("ED25519 test failed!\n", ret);
  813. else
  814. printf( "ED25519 test passed!\n");
  815. #endif
  816. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  817. if ( (ret = cmac_test()) != 0)
  818. return err_sys("CMAC test failed!\n", ret);
  819. else
  820. printf( "CMAC test passed!\n");
  821. #endif
  822. #ifdef HAVE_LIBZ
  823. if ( (ret = compress_test()) != 0)
  824. return err_sys("COMPRESS test failed!\n", ret);
  825. else
  826. printf( "COMPRESS test passed!\n");
  827. #endif
  828. #ifdef HAVE_PKCS7
  829. if ( (ret = pkcs7enveloped_test()) != 0)
  830. return err_sys("PKCS7enveloped test failed!\n", ret);
  831. else
  832. printf( "PKCS7enveloped test passed!\n");
  833. if ( (ret = pkcs7signed_test()) != 0)
  834. return err_sys("PKCS7signed test failed!\n", ret);
  835. else
  836. printf( "PKCS7signed test passed!\n");
  837. #ifndef NO_PKCS7_ENCRYPTED_DATA
  838. if ( (ret = pkcs7encrypted_test()) != 0)
  839. return err_sys("PKCS7encrypted test failed!\n", ret);
  840. else
  841. printf( "PKCS7encrypted test passed!\n");
  842. #endif
  843. #endif
  844. #ifdef HAVE_VALGRIND
  845. if ( (ret = mp_test()) != 0)
  846. return err_sys("mp test failed!\n", ret);
  847. else
  848. printf( "mp test passed!\n");
  849. #endif
  850. #ifdef WOLFSSL_PUBLIC_MP
  851. if ( (ret = prime_test()) != 0)
  852. return err_sys("prime test failed!\n", ret);
  853. else
  854. printf( "prime test passed!\n");
  855. #endif
  856. #if defined(ASN_BER_TO_DER) && \
  857. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  858. defined(OPENSSL_EXTRA_X509_SMALL))
  859. if ( (ret = berder_test()) != 0)
  860. return err_sys("ber-der test failed!\n", ret);
  861. else
  862. printf( "ber-der test passed!\n");
  863. #endif
  864. if ( (ret = logging_test()) != 0)
  865. return err_sys("logging test failed!\n", ret);
  866. else
  867. printf( "logging test passed!\n");
  868. if ( (ret = mutex_test()) != 0)
  869. return err_sys("mutex test failed!\n", ret);
  870. else
  871. printf( "mutex test passed!\n");
  872. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  873. if ( (ret = memcb_test()) != 0)
  874. return err_sys("memcb test failed!\n", ret);
  875. else
  876. printf( "memcb test passed!\n");
  877. #endif
  878. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  879. if ( (ret = blob_test()) != 0)
  880. return err_sys("blob test failed!\n", ret);
  881. else
  882. printf( "blob test passed!\n");
  883. #endif
  884. #ifdef WOLF_CRYPTO_DEV
  885. if ( (ret = cryptodev_test()) != 0)
  886. return err_sys("crypto dev test failed!\n", ret);
  887. else
  888. printf( "crypto dev test passed!\n");
  889. #endif
  890. #ifdef WOLFSSL_ASYNC_CRYPT
  891. wolfAsync_DevClose(&devId);
  892. #endif
  893. /* cleanup the thread if fixed point cache is enabled and have thread local */
  894. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  895. wc_ecc_fp_free();
  896. #endif
  897. if (args)
  898. ((func_args*)args)->return_code = ret;
  899. EXIT_TEST(ret);
  900. }
  901. #ifndef NO_MAIN_DRIVER
  902. /* so overall tests can pull in test function */
  903. int main(int argc, char** argv)
  904. {
  905. int ret;
  906. func_args args;
  907. #ifdef WOLFSSL_APACHE_MYNEWT
  908. #ifdef ARCH_sim
  909. mcu_sim_parse_args(argc, argv);
  910. #endif
  911. sysinit();
  912. /* set dummy wallclock time. */
  913. struct os_timeval utctime;
  914. struct os_timezone tz;
  915. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  916. utctime.tv_usec = 0;
  917. tz.tz_minuteswest = 0;
  918. tz.tz_dsttime = 0;
  919. os_settimeofday(&utctime, &tz);
  920. #endif
  921. #ifdef HAVE_WNR
  922. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  923. err_sys("Whitewood netRandom global config failed", -1002);
  924. return -1001;
  925. }
  926. #endif
  927. args.argc = argc;
  928. args.argv = argv;
  929. if ((ret = wolfCrypt_Init()) != 0) {
  930. printf("wolfCrypt_Init failed %d\n", ret);
  931. err_sys("Error with wolfCrypt_Init!\n", -1003);
  932. }
  933. #ifdef HAVE_STACK_SIZE
  934. StackSizeCheck(&args, wolfcrypt_test);
  935. #else
  936. wolfcrypt_test(&args);
  937. #endif
  938. if ((ret = wolfCrypt_Cleanup()) != 0) {
  939. printf("wolfCrypt_Cleanup failed %d\n", ret);
  940. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  941. }
  942. #ifdef HAVE_WNR
  943. if (wc_FreeNetRandom() < 0)
  944. err_sys("Failed to free netRandom context", -1005);
  945. #endif /* HAVE_WNR */
  946. return args.return_code;
  947. }
  948. #endif /* NO_MAIN_DRIVER */
  949. /* helper to save DER, convert to PEM and save PEM */
  950. #if !defined(NO_ASN) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  951. (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  952. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  953. #define SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB)
  954. #else
  955. #define SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, p, pSz, NULL, NULL, pT, eB)
  956. #endif
  957. static int _SaveDerAndPem(const byte* der, int derSz,
  958. byte* pem, int pemSz, const char* fileDer,
  959. const char* filePem, int pemType, int errBase)
  960. {
  961. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  962. int ret;
  963. FILE* derFile;
  964. derFile = fopen(fileDer, "wb");
  965. if (!derFile) {
  966. return errBase + 0;
  967. }
  968. ret = (int)fwrite(der, 1, derSz, derFile);
  969. fclose(derFile);
  970. if (ret != derSz) {
  971. return errBase + 1;
  972. }
  973. #endif
  974. if (pem && filePem) {
  975. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  976. FILE* pemFile;
  977. #endif
  978. #ifdef WOLFSSL_DER_TO_PEM
  979. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  980. if (pemSz < 0) {
  981. return errBase + 2;
  982. }
  983. #endif
  984. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  985. pemFile = fopen(filePem, "wb");
  986. if (!pemFile) {
  987. return errBase + 3;
  988. }
  989. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  990. fclose(pemFile);
  991. if (ret != pemSz) {
  992. return errBase + 4;
  993. }
  994. #endif
  995. }
  996. /* suppress unused variable warnings */
  997. (void)filePem;
  998. (void)fileDer;
  999. return 0;
  1000. }
  1001. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1002. int error_test(void)
  1003. {
  1004. const char* errStr;
  1005. char out[WOLFSSL_MAX_ERROR_SZ];
  1006. const char* unknownStr = wc_GetErrorString(0);
  1007. #ifdef NO_ERROR_STRINGS
  1008. /* Ensure a valid error code's string matches an invalid code's.
  1009. * The string is that error strings are not available.
  1010. */
  1011. errStr = wc_GetErrorString(OPEN_RAN_E);
  1012. wc_ErrorString(OPEN_RAN_E, out);
  1013. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1014. return -1100;
  1015. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1016. return -1101;
  1017. #else
  1018. int i;
  1019. int j = 0;
  1020. /* Values that are not or no longer error codes. */
  1021. int missing[] = { -122, -123, -124, -127, -128, -129,
  1022. -163, -164, -165, -166, -167, -168, -169,
  1023. -179, -233,
  1024. 0 };
  1025. /* Check that all errors have a string and it's the same through the two
  1026. * APIs. Check that the values that are not errors map to the unknown
  1027. * string.
  1028. */
  1029. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1030. errStr = wc_GetErrorString(i);
  1031. wc_ErrorString(i, out);
  1032. if (i != missing[j]) {
  1033. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  1034. return -1102;
  1035. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  1036. return -1103;
  1037. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  1038. return -1104;
  1039. }
  1040. else {
  1041. j++;
  1042. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1043. return -1105;
  1044. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1045. return -1106;
  1046. }
  1047. }
  1048. /* Check if the next possible value has been given a string. */
  1049. errStr = wc_GetErrorString(i);
  1050. wc_ErrorString(i, out);
  1051. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  1052. return -1107;
  1053. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  1054. return -1108;
  1055. #endif
  1056. return 0;
  1057. }
  1058. #ifndef NO_CODING
  1059. int base64_test(void)
  1060. {
  1061. int ret;
  1062. const byte good[] = "A+Gd\0\0\0";
  1063. const byte goodEnd[] = "A+Gd \r\n";
  1064. byte out[128];
  1065. word32 outLen;
  1066. #ifdef WOLFSSL_BASE64_ENCODE
  1067. byte data[3];
  1068. word32 dataLen;
  1069. byte longData[79] = { 0 };
  1070. const byte symbols[] = "+/A=";
  1071. #endif
  1072. const byte badSmall[] = "AAA Gdj=";
  1073. const byte badLarge[] = "AAA~Gdj=";
  1074. const byte badEOL[] = "A+Gd ";
  1075. int i;
  1076. /* Good Base64 encodings. */
  1077. outLen = sizeof(out);
  1078. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1079. if (ret != 0)
  1080. return -1200;
  1081. outLen = sizeof(out);
  1082. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1083. if (ret != 0)
  1084. return -1201;
  1085. /* Bad parameters. */
  1086. outLen = 1;
  1087. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1088. if (ret != BAD_FUNC_ARG)
  1089. return -1202;
  1090. outLen = sizeof(out);
  1091. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1092. if (ret != ASN_INPUT_E)
  1093. return -1203;
  1094. /* Bad character at each offset 0-3. */
  1095. for (i = 0; i < 4; i++) {
  1096. outLen = sizeof(out);
  1097. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1098. if (ret != ASN_INPUT_E)
  1099. return -1204 - i;
  1100. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1101. if (ret != ASN_INPUT_E)
  1102. return -1214 - i;
  1103. }
  1104. #ifdef WOLFSSL_BASE64_ENCODE
  1105. /* Decode and encode all symbols - non-alphanumeric. */
  1106. dataLen = sizeof(data);
  1107. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1108. if (ret != 0)
  1109. return -1224;
  1110. outLen = sizeof(out);
  1111. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1112. if (ret != LENGTH_ONLY_E)
  1113. return -1225;
  1114. outLen = sizeof(out);
  1115. ret = Base64_Encode(data, dataLen, out, &outLen);
  1116. if (ret != 0)
  1117. return -1226;
  1118. outLen = 7;
  1119. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1120. if (ret != BUFFER_E)
  1121. return -1227;
  1122. outLen = sizeof(out);
  1123. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1124. if (ret != LENGTH_ONLY_E)
  1125. return -1228;
  1126. outLen = sizeof(out);
  1127. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1128. if (ret != 0)
  1129. return -1229;
  1130. outLen = sizeof(out);
  1131. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1132. if (ret != 0)
  1133. return -1230;
  1134. /* Data that results in an encoding longer than one line. */
  1135. outLen = sizeof(out);
  1136. dataLen = sizeof(longData);
  1137. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1138. if (ret != 0)
  1139. return -1231;
  1140. outLen = sizeof(out);
  1141. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1142. if (ret != 0)
  1143. return -1232;
  1144. outLen = sizeof(out);
  1145. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1146. if (ret != 0)
  1147. return -1233;
  1148. #endif
  1149. return 0;
  1150. }
  1151. #ifdef WOLFSSL_BASE16
  1152. int base16_test(void)
  1153. {
  1154. int ret;
  1155. const byte testData[] = "SomeDataToEncode\n";
  1156. const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1157. byte encoded[40];
  1158. word32 encodedLen;
  1159. byte plain[40];
  1160. word32 len;
  1161. /* length returned includes null termination */
  1162. encodedLen = sizeof(encoded);
  1163. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1164. if (ret != 0)
  1165. return -1300;
  1166. len = (word32)XSTRLEN((char*)encoded);
  1167. if (len != encodedLen - 1)
  1168. return -1301;
  1169. len = sizeof(plain);
  1170. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1171. if (ret != 0)
  1172. return -1302;
  1173. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1174. return -1303;
  1175. if (encodedLen != sizeof(encodedTestData) ||
  1176. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1177. return -1304;
  1178. }
  1179. return 0;
  1180. }
  1181. #endif /* WOLFSSL_BASE16 */
  1182. #endif /* !NO_CODING */
  1183. #ifndef NO_ASN
  1184. int asn_test(void)
  1185. {
  1186. int ret;
  1187. /* ASN1 encoded date buffer */
  1188. const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1189. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1190. byte format;
  1191. int length;
  1192. const byte* datePart;
  1193. #ifndef NO_ASN_TIME
  1194. struct tm timearg;
  1195. #ifdef WORD64_AVAILABLE
  1196. word64 now;
  1197. #else
  1198. word32 now;
  1199. #endif
  1200. #endif
  1201. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1202. &length);
  1203. if (ret != 0)
  1204. return -1400;
  1205. #ifndef NO_ASN_TIME
  1206. /* Parameter Validation tests. */
  1207. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1208. return -1401;
  1209. if (wc_GetTime(&now, 0) != BUFFER_E)
  1210. return -1402;
  1211. now = 0;
  1212. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1213. return -1403;
  1214. }
  1215. if (now == 0) {
  1216. printf("RTC/Time not set!\n");
  1217. return -1404;
  1218. }
  1219. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1220. if (ret != 0)
  1221. return -1405;
  1222. #endif /* !NO_ASN_TIME */
  1223. return 0;
  1224. }
  1225. #endif /* !NO_ASN */
  1226. #ifdef WOLFSSL_MD2
  1227. int md2_test(void)
  1228. {
  1229. Md2 md2;
  1230. byte hash[MD2_DIGEST_SIZE];
  1231. testVector a, b, c, d, e, f, g;
  1232. testVector test_md2[7];
  1233. int times = sizeof(test_md2) / sizeof(testVector), i;
  1234. a.input = "";
  1235. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1236. "\x27\x73";
  1237. a.inLen = XSTRLEN(a.input);
  1238. a.outLen = MD2_DIGEST_SIZE;
  1239. b.input = "a";
  1240. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1241. "\xb5\xd1";
  1242. b.inLen = XSTRLEN(b.input);
  1243. b.outLen = MD2_DIGEST_SIZE;
  1244. c.input = "abc";
  1245. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1246. "\xd6\xbb";
  1247. c.inLen = XSTRLEN(c.input);
  1248. c.outLen = MD2_DIGEST_SIZE;
  1249. d.input = "message digest";
  1250. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1251. "\x06\xb0";
  1252. d.inLen = XSTRLEN(d.input);
  1253. d.outLen = MD2_DIGEST_SIZE;
  1254. e.input = "abcdefghijklmnopqrstuvwxyz";
  1255. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1256. "\x94\x0b";
  1257. e.inLen = XSTRLEN(e.input);
  1258. e.outLen = MD2_DIGEST_SIZE;
  1259. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1260. "6789";
  1261. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1262. "\x38\xcd";
  1263. f.inLen = XSTRLEN(f.input);
  1264. f.outLen = MD2_DIGEST_SIZE;
  1265. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1266. "9012345678901234567890";
  1267. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1268. "\xef\xd8";
  1269. g.inLen = XSTRLEN(g.input);
  1270. g.outLen = MD2_DIGEST_SIZE;
  1271. test_md2[0] = a;
  1272. test_md2[1] = b;
  1273. test_md2[2] = c;
  1274. test_md2[3] = d;
  1275. test_md2[4] = e;
  1276. test_md2[5] = f;
  1277. test_md2[6] = g;
  1278. wc_InitMd2(&md2);
  1279. for (i = 0; i < times; ++i) {
  1280. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1281. wc_Md2Final(&md2, hash);
  1282. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1283. return -1500 - i;
  1284. }
  1285. return 0;
  1286. }
  1287. #endif
  1288. #ifndef NO_MD5
  1289. int md5_test(void)
  1290. {
  1291. int ret = 0;
  1292. wc_Md5 md5, md5Copy;
  1293. byte hash[WC_MD5_DIGEST_SIZE];
  1294. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1295. testVector a, b, c, d, e, f;
  1296. testVector test_md5[6];
  1297. int times = sizeof(test_md5) / sizeof(testVector), i;
  1298. a.input = "";
  1299. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1300. "\x7e";
  1301. a.inLen = XSTRLEN(a.input);
  1302. a.outLen = WC_MD5_DIGEST_SIZE;
  1303. b.input = "abc";
  1304. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1305. "\x72";
  1306. b.inLen = XSTRLEN(b.input);
  1307. b.outLen = WC_MD5_DIGEST_SIZE;
  1308. c.input = "message digest";
  1309. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1310. "\xd0";
  1311. c.inLen = XSTRLEN(c.input);
  1312. c.outLen = WC_MD5_DIGEST_SIZE;
  1313. d.input = "abcdefghijklmnopqrstuvwxyz";
  1314. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1315. "\x3b";
  1316. d.inLen = XSTRLEN(d.input);
  1317. d.outLen = WC_MD5_DIGEST_SIZE;
  1318. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1319. "6789";
  1320. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1321. "\x9f";
  1322. e.inLen = XSTRLEN(e.input);
  1323. e.outLen = WC_MD5_DIGEST_SIZE;
  1324. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1325. "9012345678901234567890";
  1326. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1327. "\x7a";
  1328. f.inLen = XSTRLEN(f.input);
  1329. f.outLen = WC_MD5_DIGEST_SIZE;
  1330. test_md5[0] = a;
  1331. test_md5[1] = b;
  1332. test_md5[2] = c;
  1333. test_md5[3] = d;
  1334. test_md5[4] = e;
  1335. test_md5[5] = f;
  1336. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1337. if (ret != 0)
  1338. return -1600;
  1339. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1340. if (ret != 0) {
  1341. wc_Md5Free(&md5);
  1342. return -1601;
  1343. }
  1344. for (i = 0; i < times; ++i) {
  1345. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1346. (word32)test_md5[i].inLen);
  1347. if (ret != 0)
  1348. ERROR_OUT(-1602 - i, exit);
  1349. ret = wc_Md5GetHash(&md5, hashcopy);
  1350. if (ret != 0)
  1351. ERROR_OUT(-1603 - i, exit);
  1352. ret = wc_Md5Copy(&md5, &md5Copy);
  1353. if (ret != 0)
  1354. ERROR_OUT(-1604 - i, exit);
  1355. ret = wc_Md5Final(&md5, hash);
  1356. if (ret != 0)
  1357. ERROR_OUT(-1605 - i, exit);
  1358. wc_Md5Free(&md5Copy);
  1359. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1360. ERROR_OUT(-1606 - i, exit);
  1361. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1362. ERROR_OUT(-1607 - i, exit);
  1363. }
  1364. /* BEGIN LARGE HASH TEST */ {
  1365. byte large_input[1024];
  1366. const char* large_digest =
  1367. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1368. for (i = 0; i < (int)sizeof(large_input); i++) {
  1369. large_input[i] = (byte)(i & 0xFF);
  1370. }
  1371. times = 100;
  1372. #ifdef WOLFSSL_PIC32MZ_HASH
  1373. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1374. #endif
  1375. for (i = 0; i < times; ++i) {
  1376. ret = wc_Md5Update(&md5, (byte*)large_input,
  1377. (word32)sizeof(large_input));
  1378. if (ret != 0)
  1379. ERROR_OUT(-1608, exit);
  1380. }
  1381. ret = wc_Md5Final(&md5, hash);
  1382. if (ret != 0)
  1383. ERROR_OUT(-1609, exit);
  1384. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1385. ERROR_OUT(-1610, exit);
  1386. } /* END LARGE HASH TEST */
  1387. exit:
  1388. wc_Md5Free(&md5);
  1389. wc_Md5Free(&md5Copy);
  1390. return ret;
  1391. }
  1392. #endif /* NO_MD5 */
  1393. #ifndef NO_MD4
  1394. int md4_test(void)
  1395. {
  1396. Md4 md4;
  1397. byte hash[MD4_DIGEST_SIZE];
  1398. testVector a, b, c, d, e, f, g;
  1399. testVector test_md4[7];
  1400. int times = sizeof(test_md4) / sizeof(testVector), i;
  1401. a.input = "";
  1402. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1403. "\xc0";
  1404. a.inLen = XSTRLEN(a.input);
  1405. a.outLen = MD4_DIGEST_SIZE;
  1406. b.input = "a";
  1407. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1408. "\x24";
  1409. b.inLen = XSTRLEN(b.input);
  1410. b.outLen = MD4_DIGEST_SIZE;
  1411. c.input = "abc";
  1412. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1413. "\x9d";
  1414. c.inLen = XSTRLEN(c.input);
  1415. c.outLen = MD4_DIGEST_SIZE;
  1416. d.input = "message digest";
  1417. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1418. "\x4b";
  1419. d.inLen = XSTRLEN(d.input);
  1420. d.outLen = MD4_DIGEST_SIZE;
  1421. e.input = "abcdefghijklmnopqrstuvwxyz";
  1422. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1423. "\xa9";
  1424. e.inLen = XSTRLEN(e.input);
  1425. e.outLen = MD4_DIGEST_SIZE;
  1426. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1427. "6789";
  1428. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1429. "\xe4";
  1430. f.inLen = XSTRLEN(f.input);
  1431. f.outLen = MD4_DIGEST_SIZE;
  1432. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1433. "9012345678901234567890";
  1434. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1435. "\x36";
  1436. g.inLen = XSTRLEN(g.input);
  1437. g.outLen = MD4_DIGEST_SIZE;
  1438. test_md4[0] = a;
  1439. test_md4[1] = b;
  1440. test_md4[2] = c;
  1441. test_md4[3] = d;
  1442. test_md4[4] = e;
  1443. test_md4[5] = f;
  1444. test_md4[6] = g;
  1445. wc_InitMd4(&md4);
  1446. for (i = 0; i < times; ++i) {
  1447. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1448. wc_Md4Final(&md4, hash);
  1449. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1450. return -1700 - i;
  1451. }
  1452. return 0;
  1453. }
  1454. #endif /* NO_MD4 */
  1455. #ifndef NO_SHA
  1456. int sha_test(void)
  1457. {
  1458. int ret = 0;
  1459. wc_Sha sha, shaCopy;
  1460. byte hash[WC_SHA_DIGEST_SIZE];
  1461. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1462. testVector a, b, c, d, e;
  1463. testVector test_sha[5];
  1464. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1465. a.input = "";
  1466. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1467. "\x90\xaf\xd8\x07\x09";
  1468. a.inLen = XSTRLEN(a.input);
  1469. a.outLen = WC_SHA_DIGEST_SIZE;
  1470. b.input = "abc";
  1471. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1472. "\x6C\x9C\xD0\xD8\x9D";
  1473. b.inLen = XSTRLEN(b.input);
  1474. b.outLen = WC_SHA_DIGEST_SIZE;
  1475. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1476. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1477. "\xE5\xE5\x46\x70\xF1";
  1478. c.inLen = XSTRLEN(c.input);
  1479. c.outLen = WC_SHA_DIGEST_SIZE;
  1480. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1481. "aaaaaa";
  1482. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1483. "\x2A\x25\xEC\x64\x4D";
  1484. d.inLen = XSTRLEN(d.input);
  1485. d.outLen = WC_SHA_DIGEST_SIZE;
  1486. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1487. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1488. "aaaaaaaaaa";
  1489. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1490. "\x53\x99\x5E\x26\xA0";
  1491. e.inLen = XSTRLEN(e.input);
  1492. e.outLen = WC_SHA_DIGEST_SIZE;
  1493. test_sha[0] = a;
  1494. test_sha[1] = b;
  1495. test_sha[2] = c;
  1496. test_sha[3] = d;
  1497. test_sha[4] = e;
  1498. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  1499. if (ret != 0)
  1500. return -1800;
  1501. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  1502. if (ret != 0) {
  1503. wc_ShaFree(&sha);
  1504. return -1801;
  1505. }
  1506. for (i = 0; i < times; ++i) {
  1507. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  1508. (word32)test_sha[i].inLen);
  1509. if (ret != 0)
  1510. ERROR_OUT(-1802 - i, exit);
  1511. ret = wc_ShaGetHash(&sha, hashcopy);
  1512. if (ret != 0)
  1513. ERROR_OUT(-1803 - i, exit);
  1514. ret = wc_ShaCopy(&sha, &shaCopy);
  1515. if (ret != 0)
  1516. ERROR_OUT(-1804 - i, exit);
  1517. ret = wc_ShaFinal(&sha, hash);
  1518. if (ret != 0)
  1519. ERROR_OUT(-1805 - i, exit);
  1520. wc_ShaFree(&shaCopy);
  1521. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  1522. ERROR_OUT(-1806 - i, exit);
  1523. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  1524. ERROR_OUT(-1807 - i, exit);
  1525. }
  1526. /* BEGIN LARGE HASH TEST */ {
  1527. byte large_input[1024];
  1528. const char* large_digest =
  1529. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  1530. "\xc5\xd9\x0a\xac";
  1531. for (i = 0; i < (int)sizeof(large_input); i++) {
  1532. large_input[i] = (byte)(i & 0xFF);
  1533. }
  1534. times = 100;
  1535. #ifdef WOLFSSL_PIC32MZ_HASH
  1536. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  1537. #endif
  1538. for (i = 0; i < times; ++i) {
  1539. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  1540. (word32)sizeof(large_input));
  1541. if (ret != 0)
  1542. ERROR_OUT(-1808, exit);
  1543. }
  1544. ret = wc_ShaFinal(&sha, hash);
  1545. if (ret != 0)
  1546. ERROR_OUT(-1809, exit);
  1547. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  1548. ERROR_OUT(-1810, exit);
  1549. } /* END LARGE HASH TEST */
  1550. exit:
  1551. wc_ShaFree(&sha);
  1552. wc_ShaFree(&shaCopy);
  1553. return ret;
  1554. }
  1555. #endif /* NO_SHA */
  1556. #ifdef WOLFSSL_RIPEMD
  1557. int ripemd_test(void)
  1558. {
  1559. RipeMd ripemd;
  1560. int ret;
  1561. byte hash[RIPEMD_DIGEST_SIZE];
  1562. testVector a, b, c, d;
  1563. testVector test_ripemd[4];
  1564. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  1565. a.input = "abc";
  1566. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  1567. "\xb0\x87\xf1\x5a\x0b\xfc";
  1568. a.inLen = XSTRLEN(a.input);
  1569. a.outLen = RIPEMD_DIGEST_SIZE;
  1570. b.input = "message digest";
  1571. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  1572. "\x5f\xfa\x21\x59\x5f\x36";
  1573. b.inLen = XSTRLEN(b.input);
  1574. b.outLen = RIPEMD_DIGEST_SIZE;
  1575. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1576. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  1577. "\xf4\x9a\xda\x62\xeb\x2b";
  1578. c.inLen = XSTRLEN(c.input);
  1579. c.outLen = RIPEMD_DIGEST_SIZE;
  1580. d.input = "12345678901234567890123456789012345678901234567890123456"
  1581. "789012345678901234567890";
  1582. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  1583. "\x82\xbf\x63\x32\x6b\xfb";
  1584. d.inLen = XSTRLEN(d.input);
  1585. d.outLen = RIPEMD_DIGEST_SIZE;
  1586. test_ripemd[0] = a;
  1587. test_ripemd[1] = b;
  1588. test_ripemd[2] = c;
  1589. test_ripemd[3] = d;
  1590. ret = wc_InitRipeMd(&ripemd);
  1591. if (ret != 0) {
  1592. return -1900;
  1593. }
  1594. for (i = 0; i < times; ++i) {
  1595. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  1596. (word32)test_ripemd[i].inLen);
  1597. if (ret != 0) {
  1598. return -1901 - i;
  1599. }
  1600. ret = wc_RipeMdFinal(&ripemd, hash);
  1601. if (ret != 0) {
  1602. return -1911 - i;
  1603. }
  1604. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  1605. return -1921 - i;
  1606. }
  1607. return 0;
  1608. }
  1609. #endif /* WOLFSSL_RIPEMD */
  1610. #ifdef HAVE_BLAKE2
  1611. #define BLAKE2_TESTS 3
  1612. static const byte blake2b_vec[BLAKE2_TESTS][BLAKE2B_OUTBYTES] =
  1613. {
  1614. {
  1615. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  1616. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  1617. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  1618. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  1619. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  1620. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  1621. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  1622. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  1623. },
  1624. {
  1625. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  1626. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  1627. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  1628. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  1629. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  1630. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  1631. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  1632. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  1633. },
  1634. {
  1635. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  1636. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  1637. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  1638. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  1639. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  1640. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  1641. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  1642. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  1643. }
  1644. };
  1645. int blake2b_test(void)
  1646. {
  1647. Blake2b b2b;
  1648. byte digest[64];
  1649. byte input[64];
  1650. int i, ret;
  1651. for (i = 0; i < (int)sizeof(input); i++)
  1652. input[i] = (byte)i;
  1653. for (i = 0; i < BLAKE2_TESTS; i++) {
  1654. ret = wc_InitBlake2b(&b2b, 64);
  1655. if (ret != 0)
  1656. return -2000 - i;
  1657. ret = wc_Blake2bUpdate(&b2b, input, i);
  1658. if (ret != 0)
  1659. return -2010 - 1;
  1660. ret = wc_Blake2bFinal(&b2b, digest, 64);
  1661. if (ret != 0)
  1662. return -2020 - i;
  1663. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  1664. return -2030 - i;
  1665. }
  1666. }
  1667. return 0;
  1668. }
  1669. #endif /* HAVE_BLAKE2 */
  1670. #ifdef WOLFSSL_SHA224
  1671. int sha224_test(void)
  1672. {
  1673. wc_Sha224 sha, shaCopy;
  1674. byte hash[WC_SHA224_DIGEST_SIZE];
  1675. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  1676. int ret = 0;
  1677. testVector a, b, c;
  1678. testVector test_sha[3];
  1679. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1680. a.input = "";
  1681. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  1682. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  1683. a.inLen = XSTRLEN(a.input);
  1684. a.outLen = WC_SHA224_DIGEST_SIZE;
  1685. b.input = "abc";
  1686. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  1687. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  1688. b.inLen = XSTRLEN(b.input);
  1689. b.outLen = WC_SHA224_DIGEST_SIZE;
  1690. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1691. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  1692. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  1693. c.inLen = XSTRLEN(c.input);
  1694. c.outLen = WC_SHA224_DIGEST_SIZE;
  1695. test_sha[0] = a;
  1696. test_sha[1] = b;
  1697. test_sha[2] = c;
  1698. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  1699. if (ret != 0)
  1700. return -2100;
  1701. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  1702. if (ret != 0) {
  1703. wc_Sha224Free(&sha);
  1704. return -2101;
  1705. }
  1706. for (i = 0; i < times; ++i) {
  1707. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  1708. (word32)test_sha[i].inLen);
  1709. if (ret != 0)
  1710. ERROR_OUT(-2102 - i, exit);
  1711. ret = wc_Sha224GetHash(&sha, hashcopy);
  1712. if (ret != 0)
  1713. ERROR_OUT(-2103 - i, exit);
  1714. ret = wc_Sha224Copy(&sha, &shaCopy);
  1715. if (ret != 0)
  1716. ERROR_OUT(-2104 - i, exit);
  1717. ret = wc_Sha224Final(&sha, hash);
  1718. if (ret != 0)
  1719. ERROR_OUT(-2105 - i, exit);
  1720. wc_Sha224Free(&shaCopy);
  1721. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  1722. ERROR_OUT(-2106 - i, exit);
  1723. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  1724. ERROR_OUT(-2107 - i, exit);
  1725. }
  1726. exit:
  1727. wc_Sha224Free(&sha);
  1728. wc_Sha224Free(&shaCopy);
  1729. return ret;
  1730. }
  1731. #endif
  1732. #ifndef NO_SHA256
  1733. int sha256_test(void)
  1734. {
  1735. wc_Sha256 sha, shaCopy;
  1736. byte hash[WC_SHA256_DIGEST_SIZE];
  1737. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  1738. int ret = 0;
  1739. testVector a, b, c;
  1740. testVector test_sha[3];
  1741. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1742. a.input = "";
  1743. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  1744. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  1745. "\xb8\x55";
  1746. a.inLen = XSTRLEN(a.input);
  1747. a.outLen = WC_SHA256_DIGEST_SIZE;
  1748. b.input = "abc";
  1749. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  1750. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  1751. "\x15\xAD";
  1752. b.inLen = XSTRLEN(b.input);
  1753. b.outLen = WC_SHA256_DIGEST_SIZE;
  1754. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1755. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  1756. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  1757. "\x06\xC1";
  1758. c.inLen = XSTRLEN(c.input);
  1759. c.outLen = WC_SHA256_DIGEST_SIZE;
  1760. test_sha[0] = a;
  1761. test_sha[1] = b;
  1762. test_sha[2] = c;
  1763. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  1764. if (ret != 0)
  1765. return -2200;
  1766. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  1767. if (ret != 0) {
  1768. wc_Sha256Free(&sha);
  1769. return -2201;
  1770. }
  1771. for (i = 0; i < times; ++i) {
  1772. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  1773. (word32)test_sha[i].inLen);
  1774. if (ret != 0) {
  1775. ERROR_OUT(-2202 - i, exit);
  1776. }
  1777. ret = wc_Sha256GetHash(&sha, hashcopy);
  1778. if (ret != 0)
  1779. ERROR_OUT(-2203 - i, exit);
  1780. ret = wc_Sha256Copy(&sha, &shaCopy);
  1781. if (ret != 0)
  1782. ERROR_OUT(-2204 - i, exit);
  1783. ret = wc_Sha256Final(&sha, hash);
  1784. if (ret != 0)
  1785. ERROR_OUT(-2205 - i, exit);
  1786. wc_Sha256Free(&shaCopy);
  1787. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  1788. ERROR_OUT(-2206 - i, exit);
  1789. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  1790. ERROR_OUT(-2207 - i, exit);
  1791. }
  1792. /* BEGIN LARGE HASH TEST */ {
  1793. byte large_input[1024];
  1794. const char* large_digest =
  1795. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  1796. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  1797. for (i = 0; i < (int)sizeof(large_input); i++) {
  1798. large_input[i] = (byte)(i & 0xFF);
  1799. }
  1800. times = 100;
  1801. #ifdef WOLFSSL_PIC32MZ_HASH
  1802. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  1803. #endif
  1804. for (i = 0; i < times; ++i) {
  1805. ret = wc_Sha256Update(&sha, (byte*)large_input,
  1806. (word32)sizeof(large_input));
  1807. if (ret != 0)
  1808. ERROR_OUT(-2208, exit);
  1809. }
  1810. ret = wc_Sha256Final(&sha, hash);
  1811. if (ret != 0)
  1812. ERROR_OUT(-2209, exit);
  1813. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  1814. ERROR_OUT(-2210, exit);
  1815. } /* END LARGE HASH TEST */
  1816. exit:
  1817. wc_Sha256Free(&sha);
  1818. wc_Sha256Free(&shaCopy);
  1819. return ret;
  1820. }
  1821. #endif
  1822. #ifdef WOLFSSL_SHA512
  1823. int sha512_test(void)
  1824. {
  1825. wc_Sha512 sha, shaCopy;
  1826. byte hash[WC_SHA512_DIGEST_SIZE];
  1827. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  1828. int ret = 0;
  1829. testVector a, b, c;
  1830. testVector test_sha[3];
  1831. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1832. a.input = "";
  1833. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  1834. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  1835. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  1836. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  1837. "\xf9\x27\xda\x3e";
  1838. a.inLen = XSTRLEN(a.input);
  1839. a.outLen = WC_SHA512_DIGEST_SIZE;
  1840. b.input = "abc";
  1841. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  1842. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  1843. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  1844. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  1845. "\xa5\x4c\xa4\x9f";
  1846. b.inLen = XSTRLEN(b.input);
  1847. b.outLen = WC_SHA512_DIGEST_SIZE;
  1848. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  1849. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  1850. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  1851. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  1852. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  1853. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  1854. "\x87\x4b\xe9\x09";
  1855. c.inLen = XSTRLEN(c.input);
  1856. c.outLen = WC_SHA512_DIGEST_SIZE;
  1857. test_sha[0] = a;
  1858. test_sha[1] = b;
  1859. test_sha[2] = c;
  1860. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  1861. if (ret != 0)
  1862. return -2300;
  1863. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  1864. if (ret != 0) {
  1865. wc_Sha512Free(&sha);
  1866. return -2301;
  1867. }
  1868. for (i = 0; i < times; ++i) {
  1869. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  1870. (word32)test_sha[i].inLen);
  1871. if (ret != 0)
  1872. ERROR_OUT(-2302 - i, exit);
  1873. ret = wc_Sha512GetHash(&sha, hashcopy);
  1874. if (ret != 0)
  1875. ERROR_OUT(-2303 - i, exit);
  1876. ret = wc_Sha512Copy(&sha, &shaCopy);
  1877. if (ret != 0)
  1878. ERROR_OUT(-2304 - i, exit);
  1879. ret = wc_Sha512Final(&sha, hash);
  1880. if (ret != 0)
  1881. ERROR_OUT(-2305 - i, exit);
  1882. wc_Sha512Free(&shaCopy);
  1883. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  1884. ERROR_OUT(-2306 - i, exit);
  1885. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  1886. ERROR_OUT(-2307 - i, exit);
  1887. }
  1888. /* BEGIN LARGE HASH TEST */ {
  1889. byte large_input[1024];
  1890. const char* large_digest =
  1891. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  1892. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  1893. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  1894. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  1895. for (i = 0; i < (int)sizeof(large_input); i++) {
  1896. large_input[i] = (byte)(i & 0xFF);
  1897. }
  1898. times = 100;
  1899. for (i = 0; i < times; ++i) {
  1900. ret = wc_Sha512Update(&sha, (byte*)large_input,
  1901. (word32)sizeof(large_input));
  1902. if (ret != 0)
  1903. ERROR_OUT(-2308, exit);
  1904. }
  1905. ret = wc_Sha512Final(&sha, hash);
  1906. if (ret != 0)
  1907. ERROR_OUT(-2309, exit);
  1908. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  1909. ERROR_OUT(-2310, exit);
  1910. } /* END LARGE HASH TEST */
  1911. exit:
  1912. wc_Sha512Free(&sha);
  1913. wc_Sha512Free(&shaCopy);
  1914. return ret;
  1915. }
  1916. #endif
  1917. #ifdef WOLFSSL_SHA384
  1918. int sha384_test(void)
  1919. {
  1920. wc_Sha384 sha, shaCopy;
  1921. byte hash[WC_SHA384_DIGEST_SIZE];
  1922. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  1923. int ret = 0;
  1924. testVector a, b, c;
  1925. testVector test_sha[3];
  1926. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1927. a.input = "";
  1928. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  1929. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  1930. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  1931. "\x98\xb9\x5b";
  1932. a.inLen = XSTRLEN(a.input);
  1933. a.outLen = WC_SHA384_DIGEST_SIZE;
  1934. b.input = "abc";
  1935. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  1936. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  1937. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  1938. "\xc8\x25\xa7";
  1939. b.inLen = XSTRLEN(b.input);
  1940. b.outLen = WC_SHA384_DIGEST_SIZE;
  1941. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  1942. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  1943. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  1944. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  1945. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  1946. "\x74\x60\x39";
  1947. c.inLen = XSTRLEN(c.input);
  1948. c.outLen = WC_SHA384_DIGEST_SIZE;
  1949. test_sha[0] = a;
  1950. test_sha[1] = b;
  1951. test_sha[2] = c;
  1952. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  1953. if (ret != 0)
  1954. return -2400;
  1955. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  1956. if (ret != 0) {
  1957. wc_Sha384Free(&sha);
  1958. return -2401;
  1959. }
  1960. for (i = 0; i < times; ++i) {
  1961. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  1962. (word32)test_sha[i].inLen);
  1963. if (ret != 0)
  1964. ERROR_OUT(-2402 - i, exit);
  1965. ret = wc_Sha384GetHash(&sha, hashcopy);
  1966. if (ret != 0)
  1967. ERROR_OUT(-2403 - i, exit);
  1968. ret = wc_Sha384Copy(&sha, &shaCopy);
  1969. if (ret != 0)
  1970. ERROR_OUT(-2404 - i, exit);
  1971. ret = wc_Sha384Final(&sha, hash);
  1972. if (ret != 0)
  1973. ERROR_OUT(-2405 - i, exit);
  1974. wc_Sha384Free(&shaCopy);
  1975. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  1976. ERROR_OUT(-2406 - i, exit);
  1977. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  1978. ERROR_OUT(-2407 - i, exit);
  1979. }
  1980. /* BEGIN LARGE HASH TEST */ {
  1981. byte large_input[1024];
  1982. const char* large_digest =
  1983. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  1984. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  1985. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  1986. for (i = 0; i < (int)sizeof(large_input); i++) {
  1987. large_input[i] = (byte)(i & 0xFF);
  1988. }
  1989. times = 100;
  1990. for (i = 0; i < times; ++i) {
  1991. ret = wc_Sha384Update(&sha, (byte*)large_input,
  1992. (word32)sizeof(large_input));
  1993. if (ret != 0)
  1994. ERROR_OUT(-2408, exit);
  1995. }
  1996. ret = wc_Sha384Final(&sha, hash);
  1997. if (ret != 0)
  1998. ERROR_OUT(-2409, exit);
  1999. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2000. ERROR_OUT(-2410, exit);
  2001. } /* END LARGE HASH TEST */
  2002. exit:
  2003. wc_Sha384Free(&sha);
  2004. wc_Sha384Free(&shaCopy);
  2005. return ret;
  2006. }
  2007. #endif /* WOLFSSL_SHA384 */
  2008. #ifdef WOLFSSL_SHA3
  2009. #ifndef WOLFSSL_NOSHA3_224
  2010. static int sha3_224_test(void)
  2011. {
  2012. wc_Sha3 sha;
  2013. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2014. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2015. testVector a, b, c;
  2016. testVector test_sha[3];
  2017. int ret = 0;
  2018. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2019. a.input = "";
  2020. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2021. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2022. a.inLen = XSTRLEN(a.input);
  2023. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2024. b.input = "abc";
  2025. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2026. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2027. b.inLen = XSTRLEN(b.input);
  2028. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2029. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2030. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2031. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2032. c.inLen = XSTRLEN(c.input);
  2033. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2034. test_sha[0] = a;
  2035. test_sha[1] = b;
  2036. test_sha[2] = c;
  2037. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2038. if (ret != 0)
  2039. return -2500;
  2040. for (i = 0; i < times; ++i) {
  2041. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2042. (word32)test_sha[i].inLen);
  2043. if (ret != 0)
  2044. ERROR_OUT(-2501 - i, exit);
  2045. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2046. if (ret != 0)
  2047. ERROR_OUT(-2502 - i, exit);
  2048. ret = wc_Sha3_224_Final(&sha, hash);
  2049. if (ret != 0)
  2050. ERROR_OUT(-2503 - i, exit);
  2051. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2052. ERROR_OUT(-2504 - i, exit);
  2053. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2054. ERROR_OUT(-2505 - i, exit);
  2055. }
  2056. /* BEGIN LARGE HASH TEST */ {
  2057. byte large_input[1024];
  2058. const char* large_digest =
  2059. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2060. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2061. for (i = 0; i < (int)sizeof(large_input); i++) {
  2062. large_input[i] = (byte)(i & 0xFF);
  2063. }
  2064. times = 100;
  2065. for (i = 0; i < times; ++i) {
  2066. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2067. (word32)sizeof(large_input));
  2068. if (ret != 0)
  2069. ERROR_OUT(-2506, exit);
  2070. }
  2071. ret = wc_Sha3_224_Final(&sha, hash);
  2072. if (ret != 0)
  2073. ERROR_OUT(-2507, exit);
  2074. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2075. ERROR_OUT(-2508, exit);
  2076. } /* END LARGE HASH TEST */
  2077. exit:
  2078. wc_Sha3_224_Free(&sha);
  2079. return ret;
  2080. }
  2081. #endif /* WOLFSSL_NOSHA3_224 */
  2082. #ifndef WOLFSSL_NOSHA3_256
  2083. static int sha3_256_test(void)
  2084. {
  2085. wc_Sha3 sha;
  2086. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2087. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2088. testVector a, b, c;
  2089. testVector test_sha[3];
  2090. int ret = 0;
  2091. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2092. a.input = "";
  2093. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2094. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2095. "\x43\x4a";
  2096. a.inLen = XSTRLEN(a.input);
  2097. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2098. b.input = "abc";
  2099. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2100. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2101. "\x15\x32";
  2102. b.inLen = XSTRLEN(b.input);
  2103. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2104. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2105. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2106. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2107. "\x33\x76";
  2108. c.inLen = XSTRLEN(c.input);
  2109. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2110. test_sha[0] = a;
  2111. test_sha[1] = b;
  2112. test_sha[2] = c;
  2113. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2114. if (ret != 0)
  2115. return -2600;
  2116. for (i = 0; i < times; ++i) {
  2117. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2118. (word32)test_sha[i].inLen);
  2119. if (ret != 0)
  2120. ERROR_OUT(-2601 - i, exit);
  2121. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2122. if (ret != 0)
  2123. ERROR_OUT(-2602 - i, exit);
  2124. ret = wc_Sha3_256_Final(&sha, hash);
  2125. if (ret != 0)
  2126. ERROR_OUT(-2603 - i, exit);
  2127. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2128. ERROR_OUT(-2604 - i, exit);
  2129. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2130. ERROR_OUT(-2605 - i, exit);
  2131. }
  2132. /* BEGIN LARGE HASH TEST */ {
  2133. byte large_input[1024];
  2134. const char* large_digest =
  2135. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2136. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2137. for (i = 0; i < (int)sizeof(large_input); i++) {
  2138. large_input[i] = (byte)(i & 0xFF);
  2139. }
  2140. times = 100;
  2141. for (i = 0; i < times; ++i) {
  2142. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2143. (word32)sizeof(large_input));
  2144. if (ret != 0)
  2145. ERROR_OUT(-2606, exit);
  2146. }
  2147. ret = wc_Sha3_256_Final(&sha, hash);
  2148. if (ret != 0)
  2149. ERROR_OUT(-2607, exit);
  2150. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2151. ERROR_OUT(-2608, exit);
  2152. } /* END LARGE HASH TEST */
  2153. exit:
  2154. wc_Sha3_256_Free(&sha);
  2155. return ret;
  2156. }
  2157. #endif /* WOLFSSL_NOSHA3_256 */
  2158. #ifndef WOLFSSL_NOSHA3_384
  2159. static int sha3_384_test(void)
  2160. {
  2161. wc_Sha3 sha;
  2162. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2163. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2164. testVector a, b, c;
  2165. testVector test_sha[3];
  2166. int ret;
  2167. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2168. a.input = "";
  2169. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2170. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2171. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2172. "\xd5\xf0\x04";
  2173. a.inLen = XSTRLEN(a.input);
  2174. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2175. b.input = "abc";
  2176. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2177. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2178. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2179. "\x37\x6d\x25";
  2180. b.inLen = XSTRLEN(b.input);
  2181. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2182. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2183. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2184. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2185. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2186. "\x65\x7c\x22";
  2187. c.inLen = XSTRLEN(c.input);
  2188. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2189. test_sha[0] = a;
  2190. test_sha[1] = b;
  2191. test_sha[2] = c;
  2192. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2193. if (ret != 0)
  2194. return -2700;
  2195. for (i = 0; i < times; ++i) {
  2196. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2197. (word32)test_sha[i].inLen);
  2198. if (ret != 0)
  2199. ERROR_OUT(-2701 - i, exit);
  2200. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2201. if (ret != 0)
  2202. ERROR_OUT(-2702 - i, exit);
  2203. ret = wc_Sha3_384_Final(&sha, hash);
  2204. if (ret != 0)
  2205. ERROR_OUT(-2703 - i, exit);
  2206. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2207. ERROR_OUT(-2704 - i, exit);
  2208. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2209. ERROR_OUT(-2705 - i, exit);
  2210. }
  2211. /* BEGIN LARGE HASH TEST */ {
  2212. byte large_input[1024];
  2213. const char* large_digest =
  2214. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2215. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2216. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2217. for (i = 0; i < (int)sizeof(large_input); i++) {
  2218. large_input[i] = (byte)(i & 0xFF);
  2219. }
  2220. times = 100;
  2221. for (i = 0; i < times; ++i) {
  2222. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2223. (word32)sizeof(large_input));
  2224. if (ret != 0)
  2225. ERROR_OUT(-2706, exit);
  2226. }
  2227. ret = wc_Sha3_384_Final(&sha, hash);
  2228. if (ret != 0)
  2229. ERROR_OUT(-2707, exit);
  2230. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2231. ERROR_OUT(-2708, exit);
  2232. } /* END LARGE HASH TEST */
  2233. exit:
  2234. wc_Sha3_384_Free(&sha);
  2235. return ret;
  2236. }
  2237. #endif /* WOLFSSL_NOSHA3_384 */
  2238. #ifndef WOLFSSL_NOSHA3_512
  2239. static int sha3_512_test(void)
  2240. {
  2241. wc_Sha3 sha;
  2242. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2243. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  2244. testVector a, b, c;
  2245. testVector test_sha[3];
  2246. int ret;
  2247. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2248. a.input = "";
  2249. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2250. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2251. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2252. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2253. "\x28\x1d\xcd\x26";
  2254. a.inLen = XSTRLEN(a.input);
  2255. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2256. b.input = "abc";
  2257. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2258. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2259. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2260. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2261. "\x4e\xec\x53\xf0";
  2262. b.inLen = XSTRLEN(b.input);
  2263. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2264. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2265. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2266. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2267. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2268. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2269. "\x39\xd6\xe7\x5e";
  2270. c.inLen = XSTRLEN(c.input);
  2271. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  2272. test_sha[0] = a;
  2273. test_sha[1] = b;
  2274. test_sha[2] = c;
  2275. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2276. if (ret != 0)
  2277. return -2800;
  2278. for (i = 0; i < times; ++i) {
  2279. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2280. (word32)test_sha[i].inLen);
  2281. if (ret != 0)
  2282. ERROR_OUT(-2801 - i, exit);
  2283. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2284. if (ret != 0)
  2285. ERROR_OUT(-2802 - i, exit);
  2286. ret = wc_Sha3_512_Final(&sha, hash);
  2287. if (ret != 0)
  2288. ERROR_OUT(-2803 - i, exit);
  2289. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  2290. ERROR_OUT(-2804 - i, exit);
  2291. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  2292. ERROR_OUT(-2805 - i, exit);
  2293. }
  2294. /* BEGIN LARGE HASH TEST */ {
  2295. byte large_input[1024];
  2296. const char* large_digest =
  2297. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2298. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2299. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2300. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2301. for (i = 0; i < (int)sizeof(large_input); i++) {
  2302. large_input[i] = (byte)(i & 0xFF);
  2303. }
  2304. times = 100;
  2305. for (i = 0; i < times; ++i) {
  2306. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2307. (word32)sizeof(large_input));
  2308. if (ret != 0)
  2309. ERROR_OUT(-2806, exit);
  2310. }
  2311. ret = wc_Sha3_512_Final(&sha, hash);
  2312. if (ret != 0)
  2313. ERROR_OUT(-2807, exit);
  2314. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  2315. ERROR_OUT(-2808, exit);
  2316. } /* END LARGE HASH TEST */
  2317. exit:
  2318. wc_Sha3_512_Free(&sha);
  2319. return ret;
  2320. }
  2321. #endif /* WOLFSSL_NOSHA3_512 */
  2322. int sha3_test(void)
  2323. {
  2324. int ret;
  2325. #ifndef WOLFSSL_NOSHA3_224
  2326. if ((ret = sha3_224_test()) != 0)
  2327. return ret;
  2328. #endif
  2329. #ifndef WOLFSSL_NOSHA3_256
  2330. if ((ret = sha3_256_test()) != 0)
  2331. return ret;
  2332. #endif
  2333. #ifndef WOLFSSL_NOSHA3_384
  2334. if ((ret = sha3_384_test()) != 0)
  2335. return ret;
  2336. #endif
  2337. #ifndef WOLFSSL_NOSHA3_512
  2338. if ((ret = sha3_512_test()) != 0)
  2339. return ret;
  2340. #endif
  2341. return 0;
  2342. }
  2343. #endif
  2344. int hash_test(void)
  2345. {
  2346. wc_HashAlg hash;
  2347. int ret, exp_ret;
  2348. int i, j;
  2349. int digestSz;
  2350. byte data[] = "0123456789abcdef0123456789abcdef012345";
  2351. byte out[WC_MAX_DIGEST_SIZE];
  2352. byte hashOut[WC_MAX_DIGEST_SIZE];
  2353. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2354. enum wc_HashType hashType;
  2355. #endif
  2356. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  2357. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  2358. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512 };
  2359. enum wc_HashType typesNoImpl[] = {
  2360. #ifdef NO_MD5
  2361. WC_HASH_TYPE_MD5,
  2362. #endif
  2363. #ifdef NO_SHA
  2364. WC_HASH_TYPE_SHA,
  2365. #endif
  2366. #ifndef WOLFSSL_SHA224
  2367. WC_HASH_TYPE_SHA224,
  2368. #endif
  2369. #ifdef NO_SHA256
  2370. WC_HASH_TYPE_SHA256,
  2371. #endif
  2372. #ifndef WOLFSSL_SHA384
  2373. WC_HASH_TYPE_SHA384,
  2374. #endif
  2375. #ifndef WOLFSSL_SHA512
  2376. WC_HASH_TYPE_SHA512,
  2377. #endif
  2378. WC_HASH_TYPE_NONE
  2379. };
  2380. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  2381. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  2382. enum wc_HashType typesSha3[] = { WC_HASH_TYPE_SHA3_224,
  2383. WC_HASH_TYPE_SHA3_256,
  2384. WC_HASH_TYPE_SHA3_384,
  2385. WC_HASH_TYPE_SHA3_512 };
  2386. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  2387. WC_HASH_TYPE_SHA3_224,
  2388. WC_HASH_TYPE_SHA3_256,
  2389. WC_HASH_TYPE_SHA3_384,
  2390. WC_HASH_TYPE_SHA3_512,
  2391. WC_HASH_TYPE_BLAKE2B,
  2392. WC_HASH_TYPE_NONE };
  2393. /* Parameter Validation testing. */
  2394. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  2395. if (ret != BAD_FUNC_ARG)
  2396. return -2900;
  2397. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  2398. if (ret != BAD_FUNC_ARG)
  2399. return -2901;
  2400. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  2401. if (ret != BAD_FUNC_ARG)
  2402. return -2902;
  2403. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  2404. if (ret != BAD_FUNC_ARG)
  2405. return -2903;
  2406. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  2407. if (ret != BAD_FUNC_ARG)
  2408. return -2904;
  2409. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  2410. if (ret != BAD_FUNC_ARG)
  2411. return -2905;
  2412. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  2413. if (ret != BAD_FUNC_ARG)
  2414. return -2906;
  2415. /* Try invalid hash algorithms. */
  2416. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  2417. ret = wc_HashInit(&hash, typesBad[i]);
  2418. if (ret != BAD_FUNC_ARG)
  2419. return -2907 - i;
  2420. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  2421. if (ret != BAD_FUNC_ARG)
  2422. return -2917 - i;
  2423. ret = wc_HashFinal(&hash, typesBad[i], out);
  2424. if (ret != BAD_FUNC_ARG)
  2425. return -2927 - i;
  2426. wc_HashFree(&hash, typesBad[i]);
  2427. }
  2428. /* Try valid hash algorithms. */
  2429. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  2430. exp_ret = 0;
  2431. if (typesGood[i] == typesNoImpl[j]) {
  2432. /* Recognized but no implementation compiled in. */
  2433. exp_ret = HASH_TYPE_E;
  2434. j++;
  2435. }
  2436. ret = wc_HashInit(&hash, typesGood[i]);
  2437. if (ret != exp_ret)
  2438. return -2937 - i;
  2439. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  2440. if (ret != exp_ret)
  2441. return -2947 - i;
  2442. ret = wc_HashFinal(&hash, typesGood[i], out);
  2443. if (ret != exp_ret)
  2444. return -2957 - i;
  2445. wc_HashFree(&hash, typesGood[i]);
  2446. digestSz = wc_HashGetDigestSize(typesGood[i]);
  2447. if (exp_ret < 0 && digestSz != exp_ret)
  2448. return -2967 - i;
  2449. if (exp_ret == 0 && digestSz < 0)
  2450. return -2977 - i;
  2451. if (exp_ret == 0) {
  2452. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  2453. digestSz - 1);
  2454. if (ret != BUFFER_E)
  2455. return -2987 - i;
  2456. }
  2457. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  2458. if (ret != exp_ret)
  2459. return -2997 - i;
  2460. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  2461. return -3007 -i;
  2462. ret = wc_HashGetBlockSize(typesGood[i]);
  2463. if (exp_ret < 0 && ret != exp_ret)
  2464. return -3008 - i;
  2465. if (exp_ret == 0 && ret < 0)
  2466. return -3018 - i;
  2467. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2468. ret = wc_HashGetOID(typesGood[i]);
  2469. if (ret == BAD_FUNC_ARG ||
  2470. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  2471. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  2472. return -3028 - i;
  2473. }
  2474. hashType = wc_OidGetHash(ret);
  2475. if (exp_ret < 0 && ret != exp_ret)
  2476. return -3038 - i;
  2477. if (exp_ret == 0 && hashType != typesGood[i])
  2478. return -3048 - i;
  2479. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  2480. }
  2481. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  2482. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  2483. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  2484. return -3058 - i;
  2485. }
  2486. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2487. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  2488. #ifdef WOLFSSL_MD2
  2489. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2490. return -3068;
  2491. #else
  2492. if (ret != HASH_TYPE_E)
  2493. return -3069;
  2494. #endif
  2495. hashType = wc_OidGetHash(646); /* Md2h */
  2496. #ifdef WOLFSSL_MD2
  2497. if (hashType != WC_HASH_TYPE_MD2)
  2498. return -3070;
  2499. #else
  2500. if (hashType != WC_HASH_TYPE_NONE)
  2501. return -3071;
  2502. #endif
  2503. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  2504. #ifndef NO_MD5
  2505. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2506. return -3072;
  2507. #else
  2508. if (ret != HASH_TYPE_E)
  2509. return -3073;
  2510. #endif
  2511. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  2512. if (ret != BAD_FUNC_ARG)
  2513. return -3074;
  2514. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  2515. if (ret != BAD_FUNC_ARG)
  2516. return -3075;
  2517. hashType = wc_OidGetHash(0);
  2518. if (hashType != WC_HASH_TYPE_NONE)
  2519. return -3076;
  2520. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  2521. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  2522. #ifdef WOLFSSL_MD2
  2523. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2524. return -3077;
  2525. #else
  2526. if (ret != HASH_TYPE_E)
  2527. return -3078;
  2528. #endif
  2529. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  2530. #ifdef WOLFSSL_MD2
  2531. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2532. return -3079;
  2533. #else
  2534. if (ret != HASH_TYPE_E)
  2535. return -3080;
  2536. #endif
  2537. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  2538. #ifndef NO_MD4
  2539. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2540. return -3081;
  2541. #else
  2542. if (ret != HASH_TYPE_E)
  2543. return -3082;
  2544. #endif
  2545. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  2546. #ifndef NO_MD4
  2547. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2548. return -3083;
  2549. #else
  2550. if (ret != HASH_TYPE_E)
  2551. return -3084;
  2552. #endif
  2553. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  2554. #if !defined(NO_MD5) && !defined(NO_SHA)
  2555. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2556. return -3085;
  2557. #else
  2558. if (ret != HASH_TYPE_E)
  2559. return -3086;
  2560. #endif
  2561. for (i = 0; i < (int)(sizeof(typesSha3)/sizeof(*typesSha3)); i++) {
  2562. ret = wc_HashGetBlockSize(typesSha3[i]);
  2563. #ifdef WOLFSSL_SHA3
  2564. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2565. return -3087;
  2566. #else
  2567. if (ret != HASH_TYPE_E)
  2568. return -3088;
  2569. #endif
  2570. ret = wc_HashGetDigestSize(typesSha3[i]);
  2571. #ifdef WOLFSSL_SHA3
  2572. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2573. return -3089;
  2574. #else
  2575. if (ret != HASH_TYPE_E)
  2576. return -3090;
  2577. #endif
  2578. }
  2579. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  2580. if (ret != BAD_FUNC_ARG)
  2581. return -3091;
  2582. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  2583. if (ret != BAD_FUNC_ARG)
  2584. return -3092;
  2585. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  2586. if (ret != BAD_FUNC_ARG)
  2587. return -3093;
  2588. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  2589. if (ret != BAD_FUNC_ARG)
  2590. return -3094;
  2591. #ifndef NO_ASN
  2592. #ifdef WOLFSSL_MD2
  2593. ret = wc_GetCTC_HashOID(MD2);
  2594. if (ret == 0)
  2595. return -3095;
  2596. #endif
  2597. #ifndef NO_MD5
  2598. ret = wc_GetCTC_HashOID(WC_MD5);
  2599. if (ret == 0)
  2600. return -3096;
  2601. #endif
  2602. #ifndef NO_SHA
  2603. ret = wc_GetCTC_HashOID(WC_SHA);
  2604. if (ret == 0)
  2605. return -3097;
  2606. #endif
  2607. #ifdef WOLFSSL_SHA224
  2608. ret = wc_GetCTC_HashOID(WC_SHA224);
  2609. if (ret == 0)
  2610. return -3098;
  2611. #endif
  2612. #ifndef NO_SHA256
  2613. ret = wc_GetCTC_HashOID(WC_SHA256);
  2614. if (ret == 0)
  2615. return -3099;
  2616. #endif
  2617. #ifdef WOLFSSL_SHA384
  2618. ret = wc_GetCTC_HashOID(WC_SHA384);
  2619. if (ret == 0)
  2620. return -3100;
  2621. #endif
  2622. #ifdef WOLFSSL_SHA512
  2623. ret = wc_GetCTC_HashOID(WC_SHA512);
  2624. if (ret == 0)
  2625. return -3101;
  2626. #endif
  2627. ret = wc_GetCTC_HashOID(-1);
  2628. if (ret != 0)
  2629. return -3102;
  2630. #endif
  2631. return 0;
  2632. }
  2633. #if !defined(NO_HMAC) && !defined(NO_MD5)
  2634. int hmac_md5_test(void)
  2635. {
  2636. Hmac hmac;
  2637. byte hash[WC_MD5_DIGEST_SIZE];
  2638. const char* keys[]=
  2639. {
  2640. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  2641. "Jefe",
  2642. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2643. };
  2644. testVector a, b, c;
  2645. testVector test_hmac[3];
  2646. int ret;
  2647. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2648. a.input = "Hi There";
  2649. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  2650. "\x9d";
  2651. a.inLen = XSTRLEN(a.input);
  2652. a.outLen = WC_MD5_DIGEST_SIZE;
  2653. b.input = "what do ya want for nothing?";
  2654. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  2655. "\x38";
  2656. b.inLen = XSTRLEN(b.input);
  2657. b.outLen = WC_MD5_DIGEST_SIZE;
  2658. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2659. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2660. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2661. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2662. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  2663. "\xf6";
  2664. c.inLen = XSTRLEN(c.input);
  2665. c.outLen = WC_MD5_DIGEST_SIZE;
  2666. test_hmac[0] = a;
  2667. test_hmac[1] = b;
  2668. test_hmac[2] = c;
  2669. for (i = 0; i < times; ++i) {
  2670. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2671. if (i == 1) {
  2672. continue; /* cavium can't handle short keys, fips not allowed */
  2673. }
  2674. #endif
  2675. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  2676. return -3200;
  2677. }
  2678. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  2679. (word32)XSTRLEN(keys[i]));
  2680. if (ret != 0)
  2681. return -3201;
  2682. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2683. (word32)test_hmac[i].inLen);
  2684. if (ret != 0)
  2685. return -3202;
  2686. ret = wc_HmacFinal(&hmac, hash);
  2687. if (ret != 0)
  2688. return -3203;
  2689. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2690. return -3204 - i;
  2691. wc_HmacFree(&hmac);
  2692. }
  2693. #ifndef HAVE_FIPS
  2694. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  2695. return -3214;
  2696. #endif
  2697. return 0;
  2698. }
  2699. #endif /* NO_HMAC && NO_MD5 */
  2700. #if !defined(NO_HMAC) && !defined(NO_SHA)
  2701. int hmac_sha_test(void)
  2702. {
  2703. Hmac hmac;
  2704. byte hash[WC_SHA_DIGEST_SIZE];
  2705. const char* keys[]=
  2706. {
  2707. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2708. "\x0b\x0b\x0b",
  2709. "Jefe",
  2710. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2711. "\xAA\xAA\xAA"
  2712. };
  2713. testVector a, b, c;
  2714. testVector test_hmac[3];
  2715. int ret;
  2716. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2717. a.input = "Hi There";
  2718. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  2719. "\x8e\xf1\x46\xbe\x00";
  2720. a.inLen = XSTRLEN(a.input);
  2721. a.outLen = WC_SHA_DIGEST_SIZE;
  2722. b.input = "what do ya want for nothing?";
  2723. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  2724. "\x9c\x25\x9a\x7c\x79";
  2725. b.inLen = XSTRLEN(b.input);
  2726. b.outLen = WC_SHA_DIGEST_SIZE;
  2727. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2728. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2729. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2730. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2731. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  2732. "\x4f\x63\xf1\x75\xd3";
  2733. c.inLen = XSTRLEN(c.input);
  2734. c.outLen = WC_SHA_DIGEST_SIZE;
  2735. test_hmac[0] = a;
  2736. test_hmac[1] = b;
  2737. test_hmac[2] = c;
  2738. for (i = 0; i < times; ++i) {
  2739. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2740. if (i == 1)
  2741. continue; /* cavium can't handle short keys, fips not allowed */
  2742. #endif
  2743. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2744. return -3300;
  2745. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  2746. (word32)XSTRLEN(keys[i]));
  2747. if (ret != 0)
  2748. return -3301;
  2749. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2750. (word32)test_hmac[i].inLen);
  2751. if (ret != 0)
  2752. return -3302;
  2753. ret = wc_HmacFinal(&hmac, hash);
  2754. if (ret != 0)
  2755. return -3303;
  2756. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2757. return -3304 - i;
  2758. wc_HmacFree(&hmac);
  2759. }
  2760. #ifndef HAVE_FIPS
  2761. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  2762. return -3314;
  2763. #endif
  2764. return 0;
  2765. }
  2766. #endif
  2767. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  2768. int hmac_sha224_test(void)
  2769. {
  2770. Hmac hmac;
  2771. byte hash[WC_SHA224_DIGEST_SIZE];
  2772. const char* keys[]=
  2773. {
  2774. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2775. "\x0b\x0b\x0b",
  2776. "Jefe",
  2777. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2778. "\xAA\xAA\xAA",
  2779. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  2780. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  2781. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  2782. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  2783. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  2784. };
  2785. testVector a, b, c, d;
  2786. testVector test_hmac[4];
  2787. int ret;
  2788. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2789. a.input = "Hi There";
  2790. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  2791. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  2792. a.inLen = XSTRLEN(a.input);
  2793. a.outLen = WC_SHA224_DIGEST_SIZE;
  2794. b.input = "what do ya want for nothing?";
  2795. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  2796. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  2797. b.inLen = XSTRLEN(b.input);
  2798. b.outLen = WC_SHA224_DIGEST_SIZE;
  2799. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2800. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2801. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2802. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2803. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  2804. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  2805. c.inLen = XSTRLEN(c.input);
  2806. c.outLen = WC_SHA224_DIGEST_SIZE;
  2807. d.input = "Big Key Input";
  2808. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  2809. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  2810. d.inLen = XSTRLEN(d.input);
  2811. d.outLen = WC_SHA224_DIGEST_SIZE;
  2812. test_hmac[0] = a;
  2813. test_hmac[1] = b;
  2814. test_hmac[2] = c;
  2815. test_hmac[3] = d;
  2816. for (i = 0; i < times; ++i) {
  2817. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2818. if (i == 1)
  2819. continue; /* cavium can't handle short keys, fips not allowed */
  2820. #endif
  2821. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2822. return -3400;
  2823. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  2824. (word32)XSTRLEN(keys[i]));
  2825. if (ret != 0)
  2826. return -3401;
  2827. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2828. (word32)test_hmac[i].inLen);
  2829. if (ret != 0)
  2830. return -3402;
  2831. ret = wc_HmacFinal(&hmac, hash);
  2832. if (ret != 0)
  2833. return -3403;
  2834. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2835. return -3404 - i;
  2836. wc_HmacFree(&hmac);
  2837. }
  2838. #ifndef HAVE_FIPS
  2839. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  2840. return -3414;
  2841. #endif
  2842. return 0;
  2843. }
  2844. #endif
  2845. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  2846. int hmac_sha256_test(void)
  2847. {
  2848. Hmac hmac;
  2849. byte hash[WC_SHA256_DIGEST_SIZE];
  2850. const char* keys[]=
  2851. {
  2852. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2853. "\x0b\x0b\x0b",
  2854. "Jefe",
  2855. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2856. "\xAA\xAA\xAA",
  2857. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2858. "\xAA\xAA\xAA",
  2859. };
  2860. testVector a, b, c, d;
  2861. testVector test_hmac[4];
  2862. int ret;
  2863. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2864. a.input = "Hi There";
  2865. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  2866. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  2867. "\xcf\xf7";
  2868. a.inLen = XSTRLEN(a.input);
  2869. a.outLen = WC_SHA256_DIGEST_SIZE;
  2870. b.input = "what do ya want for nothing?";
  2871. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  2872. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  2873. "\x38\x43";
  2874. b.inLen = XSTRLEN(b.input);
  2875. b.outLen = WC_SHA256_DIGEST_SIZE;
  2876. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2877. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2878. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2879. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2880. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  2881. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  2882. "\x65\xfe";
  2883. c.inLen = XSTRLEN(c.input);
  2884. c.outLen = WC_SHA256_DIGEST_SIZE;
  2885. d.input = 0;
  2886. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  2887. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  2888. "\x3e\x46";
  2889. d.inLen = 0;
  2890. d.outLen = WC_SHA256_DIGEST_SIZE;
  2891. test_hmac[0] = a;
  2892. test_hmac[1] = b;
  2893. test_hmac[2] = c;
  2894. test_hmac[3] = d;
  2895. for (i = 0; i < times; ++i) {
  2896. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2897. if (i == 1)
  2898. continue; /* cavium can't handle short keys, fips not allowed */
  2899. #endif
  2900. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  2901. if (i == 3)
  2902. continue; /* QuickAssist can't handle empty HMAC */
  2903. #endif
  2904. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2905. return -3500 - i;
  2906. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  2907. (word32)XSTRLEN(keys[i]));
  2908. if (ret != 0)
  2909. return -3510 - i;
  2910. if (test_hmac[i].input != NULL) {
  2911. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2912. (word32)test_hmac[i].inLen);
  2913. if (ret != 0)
  2914. return -3520 - i;
  2915. }
  2916. ret = wc_HmacFinal(&hmac, hash);
  2917. if (ret != 0)
  2918. return -3530 - i;
  2919. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2920. return -3540 - i;
  2921. wc_HmacFree(&hmac);
  2922. }
  2923. #ifndef HAVE_FIPS
  2924. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  2925. return -3550;
  2926. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  2927. return -3551;
  2928. #endif
  2929. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  2930. return -3552;
  2931. return 0;
  2932. }
  2933. #endif
  2934. #if !defined(NO_HMAC) && defined(HAVE_BLAKE2)
  2935. int hmac_blake2b_test(void)
  2936. {
  2937. Hmac hmac;
  2938. byte hash[BLAKE2B_256];
  2939. const char* keys[]=
  2940. {
  2941. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2942. "\x0b\x0b\x0b",
  2943. "Jefe",
  2944. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2945. "\xAA\xAA\xAA"
  2946. };
  2947. testVector a, b, c;
  2948. testVector test_hmac[3];
  2949. int ret;
  2950. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2951. a.input = "Hi There";
  2952. a.output = "\x72\x93\x0d\xdd\xf5\xf7\xe1\x78\x38\x07\x44\x18\x0b\x3f\x51"
  2953. "\x37\x25\xb5\x82\xc2\x08\x83\x2f\x1c\x99\xfd\x03\xa0\x16\x75"
  2954. "\xac\xfd";
  2955. a.inLen = XSTRLEN(a.input);
  2956. a.outLen = BLAKE2B_256;
  2957. b.input = "what do ya want for nothing?";
  2958. b.output = "\x3d\x20\x50\x71\x05\xc0\x8c\x0c\x38\x44\x1e\xf7\xf9\xd1\x67"
  2959. "\x21\xff\x64\xf5\x94\x00\xcf\xf9\x75\x41\xda\x88\x61\x9d\x7c"
  2960. "\xda\x2b";
  2961. b.inLen = XSTRLEN(b.input);
  2962. b.outLen = BLAKE2B_256;
  2963. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2964. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2965. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2966. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2967. c.output = "\xda\xfe\x2a\x24\xfc\xe7\xea\x36\x34\xbe\x41\x92\xc7\x11\xa7"
  2968. "\x00\xae\x53\x9c\x11\x9c\x80\x74\x55\x22\x25\x4a\xb9\x55\xd3"
  2969. "\x0f\x87";
  2970. c.inLen = XSTRLEN(c.input);
  2971. c.outLen = BLAKE2B_256;
  2972. test_hmac[0] = a;
  2973. test_hmac[1] = b;
  2974. test_hmac[2] = c;
  2975. for (i = 0; i < times; ++i) {
  2976. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2977. if (i == 1)
  2978. continue; /* cavium can't handle short keys, fips not allowed */
  2979. #endif
  2980. #if !defined(HAVE_CAVIUM_V)
  2981. /* Blake2 only supported on Cavium Nitrox III */
  2982. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2983. return -3600;
  2984. #endif
  2985. ret = wc_HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i],
  2986. (word32)XSTRLEN(keys[i]));
  2987. if (ret != 0)
  2988. return -3601;
  2989. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2990. (word32)test_hmac[i].inLen);
  2991. if (ret != 0)
  2992. return -3602;
  2993. ret = wc_HmacFinal(&hmac, hash);
  2994. if (ret != 0)
  2995. return -3603;
  2996. if (XMEMCMP(hash, test_hmac[i].output, BLAKE2B_256) != 0)
  2997. return -3604 - i;
  2998. wc_HmacFree(&hmac);
  2999. }
  3000. #ifndef HAVE_FIPS
  3001. if (wc_HmacSizeByType(BLAKE2B_ID) != BLAKE2B_OUTBYTES)
  3002. return -3614;
  3003. #endif
  3004. return 0;
  3005. }
  3006. #endif
  3007. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  3008. int hmac_sha384_test(void)
  3009. {
  3010. Hmac hmac;
  3011. byte hash[WC_SHA384_DIGEST_SIZE];
  3012. const char* keys[]=
  3013. {
  3014. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3015. "\x0b\x0b\x0b",
  3016. "Jefe",
  3017. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3018. "\xAA\xAA\xAA",
  3019. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3020. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3021. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3022. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3023. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3024. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3025. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3026. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3027. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3028. };
  3029. testVector a, b, c, d;
  3030. testVector test_hmac[4];
  3031. int ret;
  3032. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3033. a.input = "Hi There";
  3034. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  3035. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  3036. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  3037. "\xfa\x9c\xb6";
  3038. a.inLen = XSTRLEN(a.input);
  3039. a.outLen = WC_SHA384_DIGEST_SIZE;
  3040. b.input = "what do ya want for nothing?";
  3041. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  3042. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  3043. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  3044. "\xb2\x16\x49";
  3045. b.inLen = XSTRLEN(b.input);
  3046. b.outLen = WC_SHA384_DIGEST_SIZE;
  3047. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3048. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3049. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3050. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3051. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  3052. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  3053. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  3054. "\xa3\x4f\x27";
  3055. c.inLen = XSTRLEN(c.input);
  3056. c.outLen = WC_SHA384_DIGEST_SIZE;
  3057. d.input = "Big Key Input";
  3058. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  3059. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  3060. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  3061. "\x57\x41\x69";
  3062. d.inLen = XSTRLEN(d.input);
  3063. d.outLen = WC_SHA384_DIGEST_SIZE;
  3064. test_hmac[0] = a;
  3065. test_hmac[1] = b;
  3066. test_hmac[2] = c;
  3067. test_hmac[3] = d;
  3068. for (i = 0; i < times; ++i) {
  3069. #if defined(HAVE_FIPS)
  3070. if (i == 1)
  3071. continue; /* fips not allowed */
  3072. #endif
  3073. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3074. return -3700;
  3075. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  3076. (word32)XSTRLEN(keys[i]));
  3077. if (ret != 0)
  3078. return -3701;
  3079. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3080. (word32)test_hmac[i].inLen);
  3081. if (ret != 0)
  3082. return -3702;
  3083. ret = wc_HmacFinal(&hmac, hash);
  3084. if (ret != 0)
  3085. return -3703;
  3086. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3087. return -3704 - i;
  3088. wc_HmacFree(&hmac);
  3089. }
  3090. #ifndef HAVE_FIPS
  3091. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  3092. return -3714;
  3093. #endif
  3094. return 0;
  3095. }
  3096. #endif
  3097. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  3098. int hmac_sha512_test(void)
  3099. {
  3100. Hmac hmac;
  3101. byte hash[WC_SHA512_DIGEST_SIZE];
  3102. const char* keys[]=
  3103. {
  3104. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3105. "\x0b\x0b\x0b",
  3106. "Jefe",
  3107. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3108. "\xAA\xAA\xAA",
  3109. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3110. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3111. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3112. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3113. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3114. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3115. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3116. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3117. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3118. };
  3119. testVector a, b, c, d;
  3120. testVector test_hmac[4];
  3121. int ret;
  3122. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3123. a.input = "Hi There";
  3124. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  3125. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  3126. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  3127. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  3128. "\x3a\x12\x68\x54";
  3129. a.inLen = XSTRLEN(a.input);
  3130. a.outLen = WC_SHA512_DIGEST_SIZE;
  3131. b.input = "what do ya want for nothing?";
  3132. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  3133. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  3134. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  3135. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  3136. "\x38\xbc\xe7\x37";
  3137. b.inLen = XSTRLEN(b.input);
  3138. b.outLen = WC_SHA512_DIGEST_SIZE;
  3139. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3140. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3141. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3142. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3143. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  3144. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  3145. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  3146. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  3147. "\xe1\x32\x92\xfb";
  3148. c.inLen = XSTRLEN(c.input);
  3149. c.outLen = WC_SHA512_DIGEST_SIZE;
  3150. d.input = "Big Key Input";
  3151. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  3152. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  3153. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  3154. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  3155. "\x1e\x18\xfe\xfa";
  3156. d.inLen = XSTRLEN(d.input);
  3157. d.outLen = WC_SHA512_DIGEST_SIZE;
  3158. test_hmac[0] = a;
  3159. test_hmac[1] = b;
  3160. test_hmac[2] = c;
  3161. test_hmac[3] = d;
  3162. for (i = 0; i < times; ++i) {
  3163. #if defined(HAVE_FIPS)
  3164. if (i == 1)
  3165. continue; /* fips not allowed */
  3166. #endif
  3167. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3168. return -3800;
  3169. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  3170. (word32)XSTRLEN(keys[i]));
  3171. if (ret != 0)
  3172. return -3801;
  3173. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  3174. (word32)test_hmac[i].inLen);
  3175. if (ret != 0)
  3176. return -3802;
  3177. ret = wc_HmacFinal(&hmac, hash);
  3178. if (ret != 0)
  3179. return -3803;
  3180. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  3181. return -3804 - i;
  3182. wc_HmacFree(&hmac);
  3183. }
  3184. #ifndef HAVE_FIPS
  3185. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  3186. return -3814;
  3187. #endif
  3188. return 0;
  3189. }
  3190. #endif
  3191. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3)
  3192. int hmac_sha3_test(void)
  3193. {
  3194. Hmac hmac;
  3195. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3196. const char* key[4] =
  3197. {
  3198. "Jefe",
  3199. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  3200. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3201. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3202. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3203. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3204. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3205. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3206. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3207. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3208. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3209. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3210. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3211. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3212. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  3213. };
  3214. const char* input[4] =
  3215. {
  3216. "what do ya want for nothing?",
  3217. "Hi There",
  3218. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3219. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3220. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3221. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3222. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3223. "Big Key Input"
  3224. };
  3225. const int hashType[4] =
  3226. {
  3227. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  3228. };
  3229. const int hashSz[4] =
  3230. {
  3231. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  3232. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  3233. };
  3234. const char* output[16] =
  3235. {
  3236. /* key = jefe, input = what do ya want for nothing? */
  3237. /* HMAC-SHA3-224 */
  3238. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  3239. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  3240. /* HMAC-SHA3-256 */
  3241. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  3242. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  3243. /* HMAC-SHA3-384 */
  3244. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  3245. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  3246. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  3247. /* HMAC-SHA3-512 */
  3248. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  3249. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  3250. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  3251. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  3252. /* key = 0b..., input = Hi There */
  3253. /* HMAC-SHA3-224 */
  3254. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  3255. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  3256. /* HMAC-SHA3-256 */
  3257. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  3258. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  3259. /* HMAC-SHA3-384 */
  3260. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  3261. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  3262. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  3263. /* HMAC-SHA3-512 */
  3264. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  3265. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  3266. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  3267. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  3268. /* key = aa..., output = dd... */
  3269. /* HMAC-SHA3-224 */
  3270. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  3271. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  3272. /* HMAC-SHA3-256 */
  3273. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  3274. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  3275. /* HMAC-SHA3-384 */
  3276. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  3277. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  3278. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  3279. /* HMAC-SHA3-512 */
  3280. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  3281. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  3282. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  3283. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  3284. /* key = big key, input = Big Key Input */
  3285. /* HMAC-SHA3-224 */
  3286. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  3287. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  3288. /* HMAC-SHA3-256 */
  3289. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  3290. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  3291. /* HMAC-SHA3-384 */
  3292. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  3293. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  3294. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  3295. /* HMAC-SHA3-512 */
  3296. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  3297. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  3298. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  3299. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  3300. };
  3301. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  3302. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  3303. ret;
  3304. #ifdef HAVE_FIPS
  3305. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  3306. * short. Skip it in FIPS builds. */
  3307. i = 1;
  3308. #endif
  3309. for (; i < iMax; i++) {
  3310. for (j = 0; j < jMax; j++) {
  3311. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  3312. return -3900;
  3313. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  3314. (word32)XSTRLEN(key[i]));
  3315. if (ret != 0)
  3316. return -3901;
  3317. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  3318. (word32)XSTRLEN(input[i]));
  3319. if (ret != 0)
  3320. return -3902;
  3321. ret = wc_HmacFinal(&hmac, hash);
  3322. if (ret != 0)
  3323. return -3903;
  3324. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  3325. return -3904;
  3326. wc_HmacFree(&hmac);
  3327. if (i > 0)
  3328. continue;
  3329. #ifndef HAVE_FIPS
  3330. ret = wc_HmacSizeByType(hashType[j]);
  3331. if (ret != hashSz[j])
  3332. return -3905;
  3333. #endif
  3334. }
  3335. }
  3336. return 0;
  3337. }
  3338. #endif
  3339. #ifndef NO_RC4
  3340. int arc4_test(void)
  3341. {
  3342. byte cipher[16];
  3343. byte plain[16];
  3344. const char* keys[] =
  3345. {
  3346. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  3347. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  3348. "\x00\x00\x00\x00\x00\x00\x00\x00",
  3349. "\xef\x01\x23\x45"
  3350. };
  3351. testVector a, b, c, d;
  3352. testVector test_arc4[4];
  3353. int times = sizeof(test_arc4) / sizeof(testVector), i;
  3354. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  3355. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  3356. a.inLen = 8;
  3357. a.outLen = 8;
  3358. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3359. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  3360. b.inLen = 8;
  3361. b.outLen = 8;
  3362. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3363. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  3364. c.inLen = 8;
  3365. c.outLen = 8;
  3366. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  3367. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  3368. d.inLen = 10;
  3369. d.outLen = 10;
  3370. test_arc4[0] = a;
  3371. test_arc4[1] = b;
  3372. test_arc4[2] = c;
  3373. test_arc4[3] = d;
  3374. for (i = 0; i < times; ++i) {
  3375. Arc4 enc;
  3376. Arc4 dec;
  3377. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  3378. if (i == 3)
  3379. keylen = 4;
  3380. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  3381. return -4000;
  3382. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  3383. return -4001;
  3384. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  3385. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  3386. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  3387. (word32)test_arc4[i].outLen);
  3388. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  3389. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  3390. return -4002 - i;
  3391. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  3392. return -4012 - i;
  3393. wc_Arc4Free(&enc);
  3394. wc_Arc4Free(&dec);
  3395. }
  3396. return 0;
  3397. }
  3398. #endif
  3399. int hc128_test(void)
  3400. {
  3401. #ifdef HAVE_HC128
  3402. byte cipher[16];
  3403. byte plain[16];
  3404. const char* keys[] =
  3405. {
  3406. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3407. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3408. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  3409. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  3410. };
  3411. const char* ivs[] =
  3412. {
  3413. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3414. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3415. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  3416. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  3417. };
  3418. testVector a, b, c, d;
  3419. testVector test_hc128[4];
  3420. int times = sizeof(test_hc128) / sizeof(testVector), i;
  3421. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3422. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  3423. a.inLen = 8;
  3424. a.outLen = 8;
  3425. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3426. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  3427. b.inLen = 8;
  3428. b.outLen = 8;
  3429. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3430. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  3431. c.inLen = 8;
  3432. c.outLen = 8;
  3433. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  3434. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  3435. d.inLen = 15;
  3436. d.outLen = 15;
  3437. test_hc128[0] = a;
  3438. test_hc128[1] = b;
  3439. test_hc128[2] = c;
  3440. test_hc128[3] = d;
  3441. for (i = 0; i < times; ++i) {
  3442. HC128 enc;
  3443. HC128 dec;
  3444. /* align keys/ivs in plain/cipher buffers */
  3445. XMEMCPY(plain, keys[i], 16);
  3446. XMEMCPY(cipher, ivs[i], 16);
  3447. wc_Hc128_SetKey(&enc, plain, cipher);
  3448. wc_Hc128_SetKey(&dec, plain, cipher);
  3449. /* align input */
  3450. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  3451. if (wc_Hc128_Process(&enc, cipher, plain,
  3452. (word32)test_hc128[i].outLen) != 0) {
  3453. return -4100;
  3454. }
  3455. if (wc_Hc128_Process(&dec, plain, cipher,
  3456. (word32)test_hc128[i].outLen) != 0) {
  3457. return -4101;
  3458. }
  3459. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen))
  3460. return -4102 - i;
  3461. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen))
  3462. return -4112 - i;
  3463. }
  3464. #endif /* HAVE_HC128 */
  3465. return 0;
  3466. }
  3467. #ifndef NO_RABBIT
  3468. int rabbit_test(void)
  3469. {
  3470. byte cipher[16];
  3471. byte plain[16];
  3472. const char* keys[] =
  3473. {
  3474. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3475. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  3476. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  3477. };
  3478. const char* ivs[] =
  3479. {
  3480. "\x00\x00\x00\x00\x00\x00\x00\x00",
  3481. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  3482. 0
  3483. };
  3484. testVector a, b, c;
  3485. testVector test_rabbit[3];
  3486. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  3487. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3488. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  3489. a.inLen = 8;
  3490. a.outLen = 8;
  3491. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3492. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  3493. b.inLen = 8;
  3494. b.outLen = 8;
  3495. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  3496. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  3497. c.inLen = 8;
  3498. c.outLen = 8;
  3499. test_rabbit[0] = a;
  3500. test_rabbit[1] = b;
  3501. test_rabbit[2] = c;
  3502. for (i = 0; i < times; ++i) {
  3503. Rabbit enc;
  3504. Rabbit dec;
  3505. byte* iv;
  3506. /* align keys/ivs in plain/cipher buffers */
  3507. XMEMCPY(plain, keys[i], 16);
  3508. if (ivs[i]) {
  3509. XMEMCPY(cipher, ivs[i], 8);
  3510. iv = cipher;
  3511. } else
  3512. iv = NULL;
  3513. wc_RabbitSetKey(&enc, plain, iv);
  3514. wc_RabbitSetKey(&dec, plain, iv);
  3515. /* align input */
  3516. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  3517. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  3518. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  3519. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  3520. return -4200 - i;
  3521. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  3522. return -4210 - i;
  3523. }
  3524. return 0;
  3525. }
  3526. #endif /* NO_RABBIT */
  3527. #ifdef HAVE_CHACHA
  3528. int chacha_test(void)
  3529. {
  3530. ChaCha enc;
  3531. ChaCha dec;
  3532. byte cipher[128];
  3533. byte plain[128];
  3534. byte sliver[64];
  3535. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  3536. word32 keySz = 32;
  3537. int ret = 0;
  3538. int i;
  3539. int times = 4;
  3540. static const byte key1[] =
  3541. {
  3542. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3543. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3544. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3545. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3546. };
  3547. static const byte key2[] =
  3548. {
  3549. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3550. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3551. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3552. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  3553. };
  3554. static const byte key3[] =
  3555. {
  3556. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3558. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3559. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3560. };
  3561. /* 128 bit key */
  3562. static const byte key4[] =
  3563. {
  3564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3566. };
  3567. const byte* keys[] = {key1, key2, key3, key4};
  3568. static const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  3569. static const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  3570. static const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
  3571. static const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  3572. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  3573. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  3574. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  3575. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  3576. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  3577. byte* test_chacha[4];
  3578. test_chacha[0] = a;
  3579. test_chacha[1] = b;
  3580. test_chacha[2] = c;
  3581. test_chacha[3] = d;
  3582. for (i = 0; i < times; ++i) {
  3583. if (i < 3) {
  3584. keySz = 32;
  3585. }
  3586. else {
  3587. keySz = 16;
  3588. }
  3589. XMEMCPY(plain, keys[i], keySz);
  3590. XMEMSET(cipher, 0, 32);
  3591. XMEMCPY(cipher + 4, ivs[i], 8);
  3592. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  3593. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  3594. if (ret != 0)
  3595. return ret;
  3596. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  3597. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  3598. if (ret != 0)
  3599. return ret;
  3600. XMEMCPY(plain, input, 8);
  3601. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  3602. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  3603. if (ret != 0)
  3604. return ret;
  3605. if (XMEMCMP(test_chacha[i], cipher, 8))
  3606. return -4300 - i;
  3607. if (XMEMCMP(plain, input, 8))
  3608. return -4310 - i;
  3609. }
  3610. /* test of starting at a different counter
  3611. encrypts all of the information and decrypts starting at 2nd chunk */
  3612. XMEMSET(plain, 0, sizeof(plain));
  3613. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  3614. XMEMSET(cipher, 0, sizeof(cipher));
  3615. XMEMCPY(cipher + 4, ivs[0], 8);
  3616. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  3617. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  3618. if (ret != 0)
  3619. return ret;
  3620. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  3621. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  3622. if (ret != 0)
  3623. return ret;
  3624. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  3625. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  3626. if (ret != 0)
  3627. return ret;
  3628. if (XMEMCMP(plain + 64, sliver, 64))
  3629. return -4320;
  3630. return 0;
  3631. }
  3632. #endif /* HAVE_CHACHA */
  3633. #ifdef HAVE_POLY1305
  3634. int poly1305_test(void)
  3635. {
  3636. int ret = 0;
  3637. int i;
  3638. byte tag[16];
  3639. Poly1305 enc;
  3640. static const byte msg1[] =
  3641. {
  3642. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  3643. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  3644. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  3645. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  3646. 0x75,0x70
  3647. };
  3648. static const byte msg2[] =
  3649. {
  3650. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  3651. 0x6c,0x64,0x21
  3652. };
  3653. static const byte msg3[] =
  3654. {
  3655. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3656. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3657. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3658. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3659. };
  3660. static const byte msg4[] =
  3661. {
  3662. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  3663. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  3664. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  3665. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  3666. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  3667. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  3668. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  3669. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  3670. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  3671. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  3672. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  3673. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  3674. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  3675. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  3676. 0x61,0x16
  3677. };
  3678. static const byte msg5[] =
  3679. {
  3680. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  3681. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  3682. };
  3683. static const byte msg6[] =
  3684. {
  3685. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  3686. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  3687. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  3688. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  3689. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  3690. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  3691. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  3692. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  3693. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  3694. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  3695. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  3696. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  3697. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  3698. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  3699. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  3700. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  3701. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  3702. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  3703. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  3704. 0x61,0x16
  3705. };
  3706. byte additional[] =
  3707. {
  3708. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  3709. 0xc4,0xc5,0xc6,0xc7
  3710. };
  3711. static const byte correct0[] =
  3712. {
  3713. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  3714. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  3715. };
  3716. static const byte correct1[] =
  3717. {
  3718. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  3719. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  3720. };
  3721. static const byte correct2[] =
  3722. {
  3723. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  3724. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  3725. };
  3726. static const byte correct3[] =
  3727. {
  3728. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  3729. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  3730. };
  3731. static const byte correct4[] =
  3732. {
  3733. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  3734. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  3735. };
  3736. static const byte correct5[] =
  3737. {
  3738. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3739. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3740. };
  3741. static const byte correct6[] =
  3742. {
  3743. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  3744. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  3745. };
  3746. static const byte key[] = {
  3747. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  3748. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  3749. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  3750. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  3751. };
  3752. static const byte key2[] = {
  3753. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  3754. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  3755. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  3756. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  3757. };
  3758. static const byte key4[] = {
  3759. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  3760. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  3761. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  3762. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  3763. };
  3764. static const byte key5[] = {
  3765. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3766. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3767. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3768. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3769. };
  3770. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  3771. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  3772. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  3773. const byte* keys[] = {key, key, key2, key2, key5, key};
  3774. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  3775. correct6};
  3776. for (i = 0; i < 6; i++) {
  3777. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  3778. if (ret != 0)
  3779. return -4400 - i;
  3780. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  3781. if (ret != 0)
  3782. return -4410 - i;
  3783. ret = wc_Poly1305Final(&enc, tag);
  3784. if (ret != 0)
  3785. return -4420 - i;
  3786. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  3787. return -4430 - i;
  3788. }
  3789. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  3790. XMEMSET(tag, 0, sizeof(tag));
  3791. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  3792. if (ret != 0)
  3793. return -4440;
  3794. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  3795. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  3796. if (ret != 0)
  3797. return -4441;
  3798. if (XMEMCMP(tag, correct4, sizeof(tag)))
  3799. return -4442;
  3800. /* Check fail of TLS MAC function if altering additional data */
  3801. XMEMSET(tag, 0, sizeof(tag));
  3802. additional[0]++;
  3803. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  3804. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  3805. if (ret != 0)
  3806. return -4443;
  3807. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  3808. return -4444;
  3809. return 0;
  3810. }
  3811. #endif /* HAVE_POLY1305 */
  3812. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3813. int chacha20_poly1305_aead_test(void)
  3814. {
  3815. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  3816. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  3817. const byte key1[] = {
  3818. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  3819. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  3820. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  3821. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  3822. };
  3823. const byte plaintext1[] = {
  3824. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  3825. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  3826. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  3827. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  3828. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  3829. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  3830. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  3831. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  3832. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  3833. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  3834. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  3835. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  3836. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  3837. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  3838. 0x74, 0x2e
  3839. };
  3840. const byte iv1[] = {
  3841. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  3842. 0x44, 0x45, 0x46, 0x47
  3843. };
  3844. const byte aad1[] = { /* additional data */
  3845. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  3846. 0xc4, 0xc5, 0xc6, 0xc7
  3847. };
  3848. const byte cipher1[] = { /* expected output from operation */
  3849. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  3850. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  3851. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  3852. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  3853. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  3854. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  3855. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  3856. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  3857. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  3858. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  3859. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  3860. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  3861. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  3862. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  3863. 0x61, 0x16
  3864. };
  3865. const byte authTag1[] = { /* expected output from operation */
  3866. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  3867. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  3868. };
  3869. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  3870. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  3871. const byte key2[] = {
  3872. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  3873. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  3874. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  3875. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  3876. };
  3877. const byte plaintext2[] = {
  3878. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  3879. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  3880. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  3881. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  3882. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  3883. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  3884. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  3885. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  3886. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  3887. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  3888. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  3889. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  3890. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  3891. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  3892. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  3893. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  3894. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  3895. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  3896. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  3897. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  3898. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  3899. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  3900. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  3901. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  3902. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  3903. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  3904. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  3905. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  3906. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  3907. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  3908. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  3909. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  3910. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  3911. 0x9d
  3912. };
  3913. const byte iv2[] = {
  3914. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  3915. 0x05, 0x06, 0x07, 0x08
  3916. };
  3917. const byte aad2[] = { /* additional data */
  3918. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  3919. 0x00, 0x00, 0x4e, 0x91
  3920. };
  3921. const byte cipher2[] = { /* expected output from operation */
  3922. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  3923. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  3924. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  3925. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  3926. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  3927. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  3928. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  3929. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  3930. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  3931. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  3932. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  3933. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  3934. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  3935. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  3936. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  3937. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  3938. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  3939. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  3940. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  3941. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  3942. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  3943. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  3944. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  3945. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  3946. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  3947. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  3948. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  3949. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  3950. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  3951. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  3952. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  3953. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  3954. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  3955. 0x9b
  3956. };
  3957. const byte authTag2[] = { /* expected output from operation */
  3958. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  3959. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  3960. };
  3961. byte generatedCiphertext[272];
  3962. byte generatedPlaintext[272];
  3963. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  3964. int err;
  3965. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  3966. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  3967. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  3968. /* Parameter Validation testing */
  3969. /* Encrypt */
  3970. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  3971. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  3972. if (err != BAD_FUNC_ARG)
  3973. return -4500;
  3974. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  3975. plaintext1, sizeof(plaintext1), generatedCiphertext,
  3976. generatedAuthTag);
  3977. if (err != BAD_FUNC_ARG)
  3978. return -4501;
  3979. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  3980. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  3981. if (err != BAD_FUNC_ARG)
  3982. return -4502;
  3983. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  3984. sizeof(plaintext1), NULL, generatedAuthTag);
  3985. if (err != BAD_FUNC_ARG)
  3986. return -4503;
  3987. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  3988. sizeof(plaintext1), generatedCiphertext, NULL);
  3989. if (err != BAD_FUNC_ARG)
  3990. return -4504;
  3991. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  3992. 0, generatedCiphertext, generatedAuthTag);
  3993. if (err != BAD_FUNC_ARG)
  3994. return -4505;
  3995. /* Decrypt */
  3996. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  3997. sizeof(cipher2), authTag2, generatedPlaintext);
  3998. if (err != BAD_FUNC_ARG)
  3999. return -4506;
  4000. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  4001. sizeof(cipher2), authTag2, generatedPlaintext);
  4002. if (err != BAD_FUNC_ARG)
  4003. return -4507;
  4004. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  4005. sizeof(cipher2), authTag2, generatedPlaintext);
  4006. if (err != BAD_FUNC_ARG)
  4007. return -4508;
  4008. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  4009. sizeof(cipher2), NULL, generatedPlaintext);
  4010. if (err != BAD_FUNC_ARG)
  4011. return -4509;
  4012. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  4013. sizeof(cipher2), authTag2, NULL);
  4014. if (err != BAD_FUNC_ARG)
  4015. return -4510;
  4016. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  4017. 0, authTag2, generatedPlaintext);
  4018. if (err != BAD_FUNC_ARG)
  4019. return -4511;
  4020. /* Test #1 */
  4021. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  4022. aad1, sizeof(aad1),
  4023. plaintext1, sizeof(plaintext1),
  4024. generatedCiphertext, generatedAuthTag);
  4025. if (err) {
  4026. return err;
  4027. }
  4028. /* -- Check the ciphertext and authtag */
  4029. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  4030. return -4512;
  4031. }
  4032. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  4033. return -4513;
  4034. }
  4035. /* -- Verify decryption works */
  4036. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  4037. aad1, sizeof(aad1),
  4038. cipher1, sizeof(cipher1),
  4039. authTag1, generatedPlaintext);
  4040. if (err) {
  4041. return err;
  4042. }
  4043. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof( plaintext1))) {
  4044. return -4514;
  4045. }
  4046. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  4047. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  4048. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  4049. /* Test #2 */
  4050. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  4051. aad2, sizeof(aad2),
  4052. plaintext2, sizeof(plaintext2),
  4053. generatedCiphertext, generatedAuthTag);
  4054. if (err) {
  4055. return err;
  4056. }
  4057. /* -- Check the ciphertext and authtag */
  4058. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  4059. return -4515;
  4060. }
  4061. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  4062. return -4516;
  4063. }
  4064. /* -- Verify decryption works */
  4065. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  4066. aad2, sizeof(aad2),
  4067. cipher2, sizeof(cipher2),
  4068. authTag2, generatedPlaintext);
  4069. if (err) {
  4070. return err;
  4071. }
  4072. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  4073. return -4517;
  4074. }
  4075. return err;
  4076. }
  4077. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  4078. #ifndef NO_DES3
  4079. int des_test(void)
  4080. {
  4081. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  4082. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4083. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4084. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4085. };
  4086. byte plain[24];
  4087. byte cipher[24];
  4088. Des enc;
  4089. Des dec;
  4090. const byte key[] =
  4091. {
  4092. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  4093. };
  4094. const byte iv[] =
  4095. {
  4096. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  4097. };
  4098. const byte verify[] =
  4099. {
  4100. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  4101. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  4102. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  4103. };
  4104. int ret;
  4105. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  4106. if (ret != 0)
  4107. return -4600;
  4108. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  4109. if (ret != 0)
  4110. return -4601;
  4111. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  4112. if (ret != 0)
  4113. return -4602;
  4114. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  4115. if (ret != 0)
  4116. return -4603;
  4117. if (XMEMCMP(plain, vector, sizeof(plain)))
  4118. return -4604;
  4119. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  4120. return -4605;
  4121. return 0;
  4122. }
  4123. #endif /* NO_DES3 */
  4124. #ifndef NO_DES3
  4125. int des3_test(void)
  4126. {
  4127. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  4128. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4129. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4130. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4131. };
  4132. byte plain[24];
  4133. byte cipher[24];
  4134. Des3 enc;
  4135. Des3 dec;
  4136. const byte key3[] =
  4137. {
  4138. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  4139. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  4140. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  4141. };
  4142. const byte iv3[] =
  4143. {
  4144. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  4145. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  4146. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  4147. };
  4148. const byte verify3[] =
  4149. {
  4150. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  4151. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  4152. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  4153. };
  4154. int ret;
  4155. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  4156. return -4700;
  4157. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  4158. return -4701;
  4159. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  4160. if (ret != 0)
  4161. return -4702;
  4162. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  4163. if (ret != 0)
  4164. return -4703;
  4165. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  4166. #if defined(WOLFSSL_ASYNC_CRYPT)
  4167. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4168. #endif
  4169. if (ret != 0)
  4170. return -4704;
  4171. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  4172. #if defined(WOLFSSL_ASYNC_CRYPT)
  4173. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  4174. #endif
  4175. if (ret != 0)
  4176. return -4705;
  4177. if (XMEMCMP(plain, vector, sizeof(plain)))
  4178. return -4706;
  4179. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  4180. return -4707;
  4181. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  4182. /* test the same vectors with using compatibility layer */
  4183. {
  4184. DES_key_schedule ks1;
  4185. DES_key_schedule ks2;
  4186. DES_key_schedule ks3;
  4187. DES_cblock iv4;
  4188. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  4189. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  4190. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  4191. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  4192. XMEMSET(plain, 0, sizeof(plain));
  4193. XMEMSET(cipher, 0, sizeof(cipher));
  4194. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  4195. &iv4, DES_ENCRYPT);
  4196. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  4197. &iv4, DES_DECRYPT);
  4198. if (XMEMCMP(plain, vector, sizeof(plain)))
  4199. return -4708;
  4200. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  4201. return -4709;
  4202. }
  4203. #endif /* OPENSSL_EXTRA */
  4204. wc_Des3Free(&enc);
  4205. wc_Des3Free(&dec);
  4206. return 0;
  4207. }
  4208. #endif /* NO_DES */
  4209. #ifndef NO_AES
  4210. #ifdef WOLFSSL_AES_CFB
  4211. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  4212. static int aescfb_test(void)
  4213. {
  4214. Aes enc;
  4215. byte cipher[AES_BLOCK_SIZE * 4];
  4216. #ifdef HAVE_AES_DECRYPT
  4217. Aes dec;
  4218. byte plain [AES_BLOCK_SIZE * 4];
  4219. #endif
  4220. int ret = 0;
  4221. const byte iv[] = {
  4222. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  4223. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  4224. };
  4225. #ifdef WOLFSSL_AES_128
  4226. const byte key1[] =
  4227. {
  4228. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  4229. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  4230. };
  4231. const byte cipher1[] =
  4232. {
  4233. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  4234. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  4235. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  4236. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  4237. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  4238. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  4239. };
  4240. const byte msg1[] =
  4241. {
  4242. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4243. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  4244. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  4245. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  4246. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  4247. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  4248. };
  4249. #endif /* WOLFSSL_AES_128 */
  4250. #ifdef WOLFSSL_AES_192
  4251. /* 192 size key test */
  4252. const byte key2[] =
  4253. {
  4254. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  4255. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  4256. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  4257. };
  4258. const byte cipher2[] =
  4259. {
  4260. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  4261. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  4262. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  4263. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  4264. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  4265. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  4266. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  4267. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  4268. };
  4269. const byte msg2[] =
  4270. {
  4271. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4272. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  4273. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  4274. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  4275. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  4276. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  4277. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  4278. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  4279. };
  4280. #endif /* WOLFSSL_AES_192 */
  4281. #ifdef WOLFSSL_AES_256
  4282. /* 256 size key simple test */
  4283. const byte key3[] =
  4284. {
  4285. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  4286. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  4287. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  4288. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  4289. };
  4290. const byte cipher3[] =
  4291. {
  4292. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  4293. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  4294. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  4295. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  4296. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  4297. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  4298. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  4299. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  4300. };
  4301. const byte msg3[] =
  4302. {
  4303. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4304. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  4305. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  4306. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  4307. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  4308. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  4309. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  4310. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  4311. };
  4312. #endif /* WOLFSSL_AES_256 */
  4313. #ifdef WOLFSSL_AES_128
  4314. /* 128 key tests */
  4315. ret = wc_AesSetKey(&enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  4316. if (ret != 0)
  4317. return -4710;
  4318. #ifdef HAVE_AES_DECRYPT
  4319. /* decrypt uses AES_ENCRYPTION */
  4320. ret = wc_AesSetKey(&dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  4321. if (ret != 0)
  4322. return -4711;
  4323. #endif
  4324. XMEMSET(cipher, 0, sizeof(cipher));
  4325. ret = wc_AesCfbEncrypt(&enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  4326. if (ret != 0)
  4327. return -4712;
  4328. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  4329. return -4713;
  4330. /* test restarting encryption process */
  4331. ret = wc_AesCfbEncrypt(&enc, cipher + (AES_BLOCK_SIZE * 2),
  4332. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  4333. if (ret != 0)
  4334. return -4714;
  4335. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  4336. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  4337. return -4715;
  4338. #ifdef HAVE_AES_DECRYPT
  4339. ret = wc_AesCfbDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE * 3);
  4340. if (ret != 0)
  4341. return -4716;
  4342. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  4343. return -4717;
  4344. #endif /* HAVE_AES_DECRYPT */
  4345. #endif /* WOLFSSL_AES_128 */
  4346. #ifdef WOLFSSL_AES_192
  4347. /* 192 key size test */
  4348. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  4349. if (ret != 0)
  4350. return -4718;
  4351. #ifdef HAVE_AES_DECRYPT
  4352. /* decrypt uses AES_ENCRYPTION */
  4353. ret = wc_AesSetKey(&dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  4354. if (ret != 0)
  4355. return -4719;
  4356. #endif
  4357. XMEMSET(cipher, 0, sizeof(cipher));
  4358. ret = wc_AesCfbEncrypt(&enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  4359. if (ret != 0)
  4360. return -4720;
  4361. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  4362. return -4721;
  4363. #ifdef HAVE_AES_DECRYPT
  4364. ret = wc_AesCfbDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE * 4);
  4365. if (ret != 0)
  4366. return -4722;
  4367. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  4368. return -4723;
  4369. #endif /* HAVE_AES_DECRYPT */
  4370. #endif /* WOLFSSL_AES_192 */
  4371. #ifdef WOLFSSL_AES_256
  4372. /* 256 key size test */
  4373. ret = wc_AesSetKey(&enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  4374. if (ret != 0)
  4375. return -4724;
  4376. #ifdef HAVE_AES_DECRYPT
  4377. /* decrypt uses AES_ENCRYPTION */
  4378. ret = wc_AesSetKey(&dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  4379. if (ret != 0)
  4380. return -4725;
  4381. #endif
  4382. /* test with data left overs, magic lengths are checking near edges */
  4383. XMEMSET(cipher, 0, sizeof(cipher));
  4384. ret = wc_AesCfbEncrypt(&enc, cipher, msg3, 4);
  4385. if (ret != 0)
  4386. return -4726;
  4387. if (XMEMCMP(cipher, cipher3, 4))
  4388. return -4727;
  4389. ret = wc_AesCfbEncrypt(&enc, cipher + 4, msg3 + 4, 27);
  4390. if (ret != 0)
  4391. return -4728;
  4392. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  4393. return -4729;
  4394. ret = wc_AesCfbEncrypt(&enc, cipher + 31, msg3 + 31,
  4395. (AES_BLOCK_SIZE * 4) - 31);
  4396. if (ret != 0)
  4397. return -4730;
  4398. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  4399. return -4731;
  4400. #ifdef HAVE_AES_DECRYPT
  4401. ret = wc_AesCfbDecrypt(&dec, plain, cipher, 4);
  4402. if (ret != 0)
  4403. return -4732;
  4404. if (XMEMCMP(plain, msg3, 4))
  4405. return -4733;
  4406. ret = wc_AesCfbDecrypt(&dec, plain + 4, cipher + 4, 4);
  4407. if (ret != 0)
  4408. return -4734;
  4409. ret = wc_AesCfbDecrypt(&dec, plain + 8, cipher + 8, 23);
  4410. if (ret != 0)
  4411. return -4735;
  4412. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  4413. return -4736;
  4414. ret = wc_AesCfbDecrypt(&dec, plain + 31, cipher + 31,
  4415. (AES_BLOCK_SIZE * 4) - 31);
  4416. if (ret != 0)
  4417. return -4737;
  4418. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  4419. return -4738;
  4420. #endif /* HAVE_AES_DECRYPT */
  4421. #endif /* WOLFSSL_AES_256 */
  4422. return ret;
  4423. }
  4424. #endif /* WOLFSSL_AES_CFB */
  4425. static int aes_key_size_test(void)
  4426. {
  4427. int ret;
  4428. Aes aes;
  4429. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  4430. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  4431. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  4432. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  4433. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  4434. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  4435. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  4436. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  4437. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  4438. byte iv[] = "1234567890abcdef";
  4439. #ifndef HAVE_FIPS
  4440. word32 keySize;
  4441. #endif
  4442. #ifdef WC_INITAES_H
  4443. ret = wc_InitAes_h(NULL, NULL);
  4444. if (ret != BAD_FUNC_ARG)
  4445. return -4800;
  4446. ret = wc_InitAes_h(&aes, NULL);
  4447. if (ret != 0)
  4448. return -4801;
  4449. #endif
  4450. #ifndef HAVE_FIPS
  4451. /* Parameter Validation testing. */
  4452. ret = wc_AesGetKeySize(NULL, NULL);
  4453. if (ret != BAD_FUNC_ARG)
  4454. return -4802;
  4455. ret = wc_AesGetKeySize(&aes, NULL);
  4456. if (ret != BAD_FUNC_ARG)
  4457. return -4803;
  4458. ret = wc_AesGetKeySize(NULL, &keySize);
  4459. if (ret != BAD_FUNC_ARG)
  4460. return -4804;
  4461. /* Crashes in FIPS */
  4462. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  4463. if (ret != BAD_FUNC_ARG)
  4464. return -4805;
  4465. #endif
  4466. /* NULL IV indicates to use all zeros IV. */
  4467. ret = wc_AesSetKey(&aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  4468. #ifdef WOLFSSL_AES_128
  4469. if (ret != 0)
  4470. #else
  4471. if (ret != BAD_FUNC_ARG)
  4472. #endif
  4473. return -4806;
  4474. ret = wc_AesSetKey(&aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  4475. if (ret != BAD_FUNC_ARG)
  4476. return -4807;
  4477. #ifndef HAVE_FIPS
  4478. /* Force invalid rounds */
  4479. aes.rounds = 16;
  4480. ret = wc_AesGetKeySize(&aes, &keySize);
  4481. if (ret != BAD_FUNC_ARG)
  4482. return -4808;
  4483. #endif
  4484. ret = wc_AesSetKey(&aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  4485. #ifdef WOLFSSL_AES_128
  4486. if (ret != 0)
  4487. #else
  4488. if (ret != BAD_FUNC_ARG)
  4489. #endif
  4490. return -4809;
  4491. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  4492. ret = wc_AesGetKeySize(&aes, &keySize);
  4493. if (ret != 0 || keySize != sizeof(key16))
  4494. return -4810;
  4495. #endif
  4496. ret = wc_AesSetKey(&aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  4497. #ifdef WOLFSSL_AES_192
  4498. if (ret != 0)
  4499. #else
  4500. if (ret != BAD_FUNC_ARG)
  4501. #endif
  4502. return -4811;
  4503. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  4504. ret = wc_AesGetKeySize(&aes, &keySize);
  4505. if (ret != 0 || keySize != sizeof(key24))
  4506. return -4812;
  4507. #endif
  4508. ret = wc_AesSetKey(&aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  4509. #ifdef WOLFSSL_AES_256
  4510. if (ret != 0)
  4511. #else
  4512. if (ret != BAD_FUNC_ARG)
  4513. #endif
  4514. return -4813;
  4515. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  4516. ret = wc_AesGetKeySize(&aes, &keySize);
  4517. if (ret != 0 || keySize != sizeof(key32))
  4518. return -4814;
  4519. #endif
  4520. return 0;
  4521. }
  4522. #if defined(WOLFSSL_AES_XTS)
  4523. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  4524. #ifdef WOLFSSL_AES_128
  4525. static int aes_xts_128_test(void)
  4526. {
  4527. XtsAes aes;
  4528. int ret = 0;
  4529. unsigned char buf[AES_BLOCK_SIZE * 2];
  4530. unsigned char cipher[AES_BLOCK_SIZE * 2];
  4531. /* 128 key tests */
  4532. static unsigned char k1[] = {
  4533. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  4534. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  4535. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  4536. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  4537. };
  4538. static unsigned char i1[] = {
  4539. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  4540. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  4541. };
  4542. static unsigned char p1[] = {
  4543. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  4544. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  4545. };
  4546. /* plain text test of partial block is not from NIST test vector list */
  4547. static unsigned char pp[] = {
  4548. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  4549. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  4550. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  4551. };
  4552. static unsigned char c1[] = {
  4553. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  4554. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  4555. };
  4556. static unsigned char k2[] = {
  4557. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  4558. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  4559. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  4560. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  4561. };
  4562. static unsigned char i2[] = {
  4563. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  4564. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  4565. };
  4566. static unsigned char p2[] = {
  4567. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  4568. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  4569. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  4570. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  4571. };
  4572. static unsigned char c2[] = {
  4573. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  4574. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  4575. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  4576. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  4577. };
  4578. XMEMSET(buf, 0, sizeof(buf));
  4579. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  4580. HEAP_HINT, devId) != 0)
  4581. return -4900;
  4582. ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  4583. #if defined(WOLFSSL_ASYNC_CRYPT)
  4584. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4585. #endif
  4586. if (ret != 0)
  4587. return -4901;
  4588. if (XMEMCMP(c2, buf, sizeof(c2)))
  4589. return -4902;
  4590. XMEMSET(buf, 0, sizeof(buf));
  4591. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  4592. HEAP_HINT, devId) != 0)
  4593. return -4903;
  4594. ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  4595. #if defined(WOLFSSL_ASYNC_CRYPT)
  4596. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4597. #endif
  4598. if (ret != 0)
  4599. return -4904;
  4600. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  4601. return -4905;
  4602. /* partial block encryption test */
  4603. XMEMSET(cipher, 0, sizeof(cipher));
  4604. ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  4605. #if defined(WOLFSSL_ASYNC_CRYPT)
  4606. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4607. #endif
  4608. if (ret != 0)
  4609. return -4906;
  4610. wc_AesXtsFree(&aes);
  4611. /* partial block decrypt test */
  4612. XMEMSET(buf, 0, sizeof(buf));
  4613. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  4614. HEAP_HINT, devId) != 0)
  4615. return -4907;
  4616. ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  4617. #if defined(WOLFSSL_ASYNC_CRYPT)
  4618. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4619. #endif
  4620. if (ret != 0)
  4621. return -4908;
  4622. if (XMEMCMP(pp, buf, sizeof(pp)))
  4623. return -4909;
  4624. /* NIST decrypt test vector */
  4625. XMEMSET(buf, 0, sizeof(buf));
  4626. ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  4627. #if defined(WOLFSSL_ASYNC_CRYPT)
  4628. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4629. #endif
  4630. if (ret != 0)
  4631. return -4910;
  4632. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  4633. return -4911;
  4634. /* fail case with decrypting using wrong key */
  4635. XMEMSET(buf, 0, sizeof(buf));
  4636. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  4637. #if defined(WOLFSSL_ASYNC_CRYPT)
  4638. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4639. #endif
  4640. if (ret != 0)
  4641. return -4912;
  4642. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  4643. return -4913;
  4644. /* set correct key and retest */
  4645. XMEMSET(buf, 0, sizeof(buf));
  4646. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  4647. HEAP_HINT, devId) != 0)
  4648. return -4914;
  4649. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  4650. #if defined(WOLFSSL_ASYNC_CRYPT)
  4651. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4652. #endif
  4653. if (ret != 0)
  4654. return -4915;
  4655. if (XMEMCMP(p2, buf, sizeof(p2)))
  4656. return -4916;
  4657. wc_AesXtsFree(&aes);
  4658. return ret;
  4659. }
  4660. #endif /* WOLFSSL_AES_128 */
  4661. #ifdef WOLFSSL_AES_256
  4662. static int aes_xts_256_test(void)
  4663. {
  4664. XtsAes aes;
  4665. int ret = 0;
  4666. unsigned char buf[AES_BLOCK_SIZE * 3];
  4667. unsigned char cipher[AES_BLOCK_SIZE * 3];
  4668. /* 256 key tests */
  4669. static unsigned char k1[] = {
  4670. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  4671. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  4672. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  4673. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  4674. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  4675. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  4676. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  4677. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  4678. };
  4679. static unsigned char i1[] = {
  4680. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  4681. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  4682. };
  4683. static unsigned char p1[] = {
  4684. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  4685. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  4686. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  4687. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  4688. };
  4689. /* plain text test of partial block is not from NIST test vector list */
  4690. static unsigned char pp[] = {
  4691. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  4692. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  4693. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  4694. };
  4695. static unsigned char c1[] = {
  4696. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  4697. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  4698. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  4699. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  4700. };
  4701. static unsigned char k2[] = {
  4702. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  4703. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  4704. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  4705. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  4706. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  4707. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  4708. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  4709. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  4710. };
  4711. static unsigned char i2[] = {
  4712. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  4713. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  4714. };
  4715. static unsigned char p2[] = {
  4716. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  4717. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  4718. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  4719. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  4720. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  4721. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  4722. };
  4723. static unsigned char c2[] = {
  4724. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  4725. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  4726. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  4727. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  4728. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  4729. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  4730. };
  4731. XMEMSET(buf, 0, sizeof(buf));
  4732. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  4733. HEAP_HINT, devId) != 0)
  4734. return -5000;
  4735. ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  4736. #if defined(WOLFSSL_ASYNC_CRYPT)
  4737. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4738. #endif
  4739. if (ret != 0)
  4740. return -5001;
  4741. if (XMEMCMP(c2, buf, sizeof(c2)))
  4742. return -5002;
  4743. XMEMSET(buf, 0, sizeof(buf));
  4744. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  4745. HEAP_HINT, devId) != 0)
  4746. return -5003;
  4747. ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  4748. #if defined(WOLFSSL_ASYNC_CRYPT)
  4749. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4750. #endif
  4751. if (ret != 0)
  4752. return -5004;
  4753. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  4754. return -5005;
  4755. /* partial block encryption test */
  4756. XMEMSET(cipher, 0, sizeof(cipher));
  4757. ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  4758. #if defined(WOLFSSL_ASYNC_CRYPT)
  4759. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4760. #endif
  4761. if (ret != 0)
  4762. return -5006;
  4763. wc_AesXtsFree(&aes);
  4764. /* partial block decrypt test */
  4765. XMEMSET(buf, 0, sizeof(buf));
  4766. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  4767. HEAP_HINT, devId) != 0)
  4768. return -5007;
  4769. ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  4770. #if defined(WOLFSSL_ASYNC_CRYPT)
  4771. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4772. #endif
  4773. if (ret != 0)
  4774. return -5008;
  4775. if (XMEMCMP(pp, buf, sizeof(pp)))
  4776. return -5009;
  4777. /* NIST decrypt test vector */
  4778. XMEMSET(buf, 0, sizeof(buf));
  4779. ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  4780. #if defined(WOLFSSL_ASYNC_CRYPT)
  4781. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4782. #endif
  4783. if (ret != 0)
  4784. return -5010;
  4785. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  4786. return -5011;
  4787. XMEMSET(buf, 0, sizeof(buf));
  4788. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  4789. HEAP_HINT, devId) != 0)
  4790. return -5012;
  4791. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  4792. #if defined(WOLFSSL_ASYNC_CRYPT)
  4793. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4794. #endif
  4795. if (ret != 0)
  4796. return -5013;
  4797. if (XMEMCMP(p2, buf, sizeof(p2)))
  4798. return -5014;
  4799. wc_AesXtsFree(&aes);
  4800. return ret;
  4801. }
  4802. #endif /* WOLFSSL_AES_256 */
  4803. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  4804. /* both 128 and 256 bit key test */
  4805. static int aes_xts_sector_test(void)
  4806. {
  4807. XtsAes aes;
  4808. int ret = 0;
  4809. unsigned char buf[AES_BLOCK_SIZE * 2];
  4810. /* 128 key tests */
  4811. static unsigned char k1[] = {
  4812. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  4813. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  4814. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  4815. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  4816. };
  4817. static unsigned char p1[] = {
  4818. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  4819. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  4820. };
  4821. static unsigned char c1[] = {
  4822. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  4823. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  4824. };
  4825. word64 s1 = 141;
  4826. /* 256 key tests */
  4827. static unsigned char k2[] = {
  4828. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  4829. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  4830. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  4831. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  4832. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  4833. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  4834. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  4835. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  4836. };
  4837. static unsigned char p2[] = {
  4838. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  4839. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  4840. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  4841. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  4842. };
  4843. static unsigned char c2[] = {
  4844. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  4845. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  4846. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  4847. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  4848. };
  4849. word64 s2 = 187;
  4850. XMEMSET(buf, 0, sizeof(buf));
  4851. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  4852. HEAP_HINT, devId) != 0)
  4853. return -5100;
  4854. ret = wc_AesXtsEncryptSector(&aes, buf, p1, sizeof(p1), s1);
  4855. #if defined(WOLFSSL_ASYNC_CRYPT)
  4856. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4857. #endif
  4858. if (ret != 0)
  4859. return -5101;
  4860. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  4861. return -5102;
  4862. /* decrypt test */
  4863. XMEMSET(buf, 0, sizeof(buf));
  4864. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  4865. HEAP_HINT, devId) != 0)
  4866. return -5103;
  4867. ret = wc_AesXtsDecryptSector(&aes, buf, c1, sizeof(c1), s1);
  4868. #if defined(WOLFSSL_ASYNC_CRYPT)
  4869. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4870. #endif
  4871. if (ret != 0)
  4872. return -5104;
  4873. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  4874. return -5105;
  4875. wc_AesXtsFree(&aes);
  4876. /* 256 bit key tests */
  4877. XMEMSET(buf, 0, sizeof(buf));
  4878. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  4879. HEAP_HINT, devId) != 0)
  4880. return -5106;
  4881. ret = wc_AesXtsEncryptSector(&aes, buf, p2, sizeof(p2), s2);
  4882. #if defined(WOLFSSL_ASYNC_CRYPT)
  4883. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4884. #endif
  4885. if (ret != 0)
  4886. return -5107;
  4887. if (XMEMCMP(c2, buf, sizeof(c2)))
  4888. return -5108;
  4889. /* decrypt test */
  4890. XMEMSET(buf, 0, sizeof(buf));
  4891. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  4892. HEAP_HINT, devId) != 0)
  4893. return -5109;
  4894. ret = wc_AesXtsDecryptSector(&aes, buf, c2, sizeof(c2), s2);
  4895. #if defined(WOLFSSL_ASYNC_CRYPT)
  4896. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4897. #endif
  4898. if (ret != 0)
  4899. return -5110;
  4900. if (XMEMCMP(p2, buf, sizeof(p2)))
  4901. return -5111;
  4902. wc_AesXtsFree(&aes);
  4903. return ret;
  4904. }
  4905. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  4906. #ifdef WOLFSSL_AES_128
  4907. /* testing of bad arguments */
  4908. static int aes_xts_args_test(void)
  4909. {
  4910. XtsAes aes;
  4911. int ret = 0;
  4912. unsigned char buf[AES_BLOCK_SIZE * 2];
  4913. /* 128 key tests */
  4914. static unsigned char k1[] = {
  4915. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  4916. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  4917. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  4918. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  4919. };
  4920. static unsigned char p1[] = {
  4921. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  4922. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  4923. };
  4924. static unsigned char c1[] = {
  4925. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  4926. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  4927. };
  4928. word64 s1 = 141;
  4929. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  4930. HEAP_HINT, devId) == 0)
  4931. return -5200;
  4932. if (wc_AesXtsSetKey(&aes, NULL, sizeof(k1), AES_ENCRYPTION,
  4933. HEAP_HINT, devId) == 0)
  4934. return -5201;
  4935. /* encryption operations */
  4936. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  4937. HEAP_HINT, devId) != 0)
  4938. return -5202;
  4939. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  4940. #if defined(WOLFSSL_ASYNC_CRYPT)
  4941. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4942. #endif
  4943. if (ret == 0)
  4944. return -5203;
  4945. ret = wc_AesXtsEncryptSector(&aes, NULL, p1, sizeof(p1), s1);
  4946. #if defined(WOLFSSL_ASYNC_CRYPT)
  4947. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4948. #endif
  4949. if (ret == 0)
  4950. return -5204;
  4951. wc_AesXtsFree(&aes);
  4952. /* decryption operations */
  4953. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  4954. HEAP_HINT, devId) != 0)
  4955. return -5205;
  4956. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  4957. #if defined(WOLFSSL_ASYNC_CRYPT)
  4958. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4959. #endif
  4960. if (ret == 0)
  4961. return -5206;
  4962. ret = wc_AesXtsDecryptSector(&aes, NULL, c1, sizeof(c1), s1);
  4963. #if defined(WOLFSSL_ASYNC_CRYPT)
  4964. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4965. #endif
  4966. if (ret == 0)
  4967. return -5207;
  4968. wc_AesXtsFree(&aes);
  4969. return 0;
  4970. }
  4971. #endif /* WOLFSSL_AES_128 */
  4972. #endif /* WOLFSSL_AES_XTS */
  4973. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4974. static int aes_cbc_test(void)
  4975. {
  4976. byte cipher[AES_BLOCK_SIZE];
  4977. byte plain[AES_BLOCK_SIZE];
  4978. int ret;
  4979. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  4980. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4981. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4982. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4983. };
  4984. byte key[] = "0123456789abcdef "; /* align */
  4985. byte iv[] = "1234567890abcdef "; /* align */
  4986. /* Parameter Validation testing. */
  4987. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  4988. if (ret != BAD_FUNC_ARG)
  4989. return -5300;
  4990. #ifdef HAVE_AES_DECRYPT
  4991. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  4992. if (ret != BAD_FUNC_ARG)
  4993. return -5301;
  4994. #endif
  4995. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  4996. AES_BLOCK_SIZE, iv);
  4997. if (ret != 0)
  4998. return -5302;
  4999. #ifdef HAVE_AES_DECRYPT
  5000. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  5001. AES_BLOCK_SIZE, iv);
  5002. if (ret != 0)
  5003. return -5303;
  5004. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  5005. return -5304;
  5006. #endif /* HAVE_AES_DECRYPT */
  5007. (void)plain;
  5008. return 0;
  5009. }
  5010. #endif
  5011. int aes_test(void)
  5012. {
  5013. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  5014. Aes enc;
  5015. byte cipher[AES_BLOCK_SIZE * 4];
  5016. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  5017. Aes dec;
  5018. byte plain [AES_BLOCK_SIZE * 4];
  5019. #endif
  5020. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER */
  5021. int ret = 0;
  5022. #ifdef HAVE_AES_CBC
  5023. #ifdef WOLFSSL_AES_128
  5024. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  5025. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5026. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5027. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5028. };
  5029. const byte verify[] =
  5030. {
  5031. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  5032. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  5033. };
  5034. byte key[] = "0123456789abcdef "; /* align */
  5035. byte iv[] = "1234567890abcdef "; /* align */
  5036. #ifdef WOLFSSL_ASYNC_CRYPT
  5037. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  5038. return -5400;
  5039. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  5040. return -5401;
  5041. #endif
  5042. ret = wc_AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  5043. if (ret != 0)
  5044. return -5402;
  5045. #ifdef HAVE_AES_DECRYPT
  5046. ret = wc_AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  5047. if (ret != 0)
  5048. return -5403;
  5049. #endif
  5050. ret = wc_AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  5051. #if defined(WOLFSSL_ASYNC_CRYPT)
  5052. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5053. #endif
  5054. if (ret != 0)
  5055. return -5404;
  5056. #ifdef HAVE_AES_DECRYPT
  5057. ret = wc_AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  5058. #if defined(WOLFSSL_ASYNC_CRYPT)
  5059. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5060. #endif
  5061. if (ret != 0)
  5062. return -5405;
  5063. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  5064. return -5406;
  5065. #endif /* HAVE_AES_DECRYPT */
  5066. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  5067. return -5407;
  5068. #endif /* WOLFSSL_AES_128 */
  5069. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  5070. {
  5071. const byte bigMsg[] = {
  5072. /* "All work and no play makes Jack a dull boy. " */
  5073. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  5074. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  5075. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  5076. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  5077. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  5078. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  5079. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  5080. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  5081. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  5082. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  5083. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  5084. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  5085. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  5086. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  5087. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  5088. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  5089. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  5090. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  5091. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  5092. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  5093. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  5094. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  5095. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  5096. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  5097. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  5098. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  5099. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  5100. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  5101. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  5102. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  5103. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  5104. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  5105. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  5106. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  5107. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  5108. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  5109. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  5110. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  5111. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  5112. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  5113. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  5114. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  5115. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  5116. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  5117. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  5118. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  5119. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  5120. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  5121. };
  5122. const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  5123. byte bigCipher[sizeof(bigMsg)];
  5124. byte bigPlain[sizeof(bigMsg)];
  5125. word32 keySz, msgSz;
  5126. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  5127. * message by AES_BLOCK_SIZE for each size of AES key. */
  5128. for (keySz = 16; keySz <= 32; keySz += 8) {
  5129. for (msgSz = AES_BLOCK_SIZE;
  5130. msgSz <= sizeof(bigMsg);
  5131. msgSz += AES_BLOCK_SIZE) {
  5132. XMEMSET(bigCipher, 0, sizeof(bigCipher));
  5133. XMEMSET(bigPlain, 0, sizeof(bigPlain));
  5134. ret = wc_AesSetKey(&enc, bigKey, keySz, iv, AES_ENCRYPTION);
  5135. if (ret != 0)
  5136. return -5408;
  5137. ret = wc_AesSetKey(&dec, bigKey, keySz, iv, AES_DECRYPTION);
  5138. if (ret != 0)
  5139. return -5409;
  5140. ret = wc_AesCbcEncrypt(&enc, bigCipher, bigMsg, msgSz);
  5141. #if defined(WOLFSSL_ASYNC_CRYPT)
  5142. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5143. #endif
  5144. if (ret != 0)
  5145. return -5410;
  5146. ret = wc_AesCbcDecrypt(&dec, bigPlain, bigCipher, msgSz);
  5147. #if defined(WOLFSSL_ASYNC_CRYPT)
  5148. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5149. #endif
  5150. if (ret != 0)
  5151. return -5411;
  5152. if (XMEMCMP(bigPlain, bigMsg, msgSz))
  5153. return -5412;
  5154. }
  5155. }
  5156. }
  5157. #endif /* WOLFSSL_AESNI HAVE_AES_DECRYPT */
  5158. /* Test of AES IV state with encrypt/decrypt */
  5159. #ifdef WOLFSSL_AES_128
  5160. {
  5161. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  5162. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  5163. */
  5164. const byte msg2[] =
  5165. {
  5166. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  5167. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  5168. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  5169. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  5170. };
  5171. const byte verify2[] =
  5172. {
  5173. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  5174. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  5175. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  5176. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  5177. };
  5178. byte key2[] = {
  5179. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  5180. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  5181. };
  5182. byte iv2[] = {
  5183. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5184. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  5185. };
  5186. ret = wc_AesSetKey(&enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  5187. if (ret != 0)
  5188. return -5413;
  5189. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  5190. ret = wc_AesCbcEncrypt(&enc, cipher, msg2, AES_BLOCK_SIZE);
  5191. #if defined(WOLFSSL_ASYNC_CRYPT)
  5192. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5193. #endif
  5194. if (ret != 0)
  5195. return -5414;
  5196. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  5197. return -5415;
  5198. ret = wc_AesCbcEncrypt(&enc, cipher + AES_BLOCK_SIZE,
  5199. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5200. #if defined(WOLFSSL_ASYNC_CRYPT)
  5201. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5202. #endif
  5203. if (ret != 0)
  5204. return -5416;
  5205. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  5206. AES_BLOCK_SIZE))
  5207. return -5417;
  5208. #if defined(HAVE_AES_DECRYPT)
  5209. ret = wc_AesSetKey(&dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  5210. if (ret != 0)
  5211. return -5418;
  5212. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  5213. ret = wc_AesCbcDecrypt(&dec, plain, verify2, AES_BLOCK_SIZE);
  5214. #if defined(WOLFSSL_ASYNC_CRYPT)
  5215. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5216. #endif
  5217. if (ret != 0)
  5218. return -5419;
  5219. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  5220. return -5420;
  5221. ret = wc_AesCbcDecrypt(&dec, plain + AES_BLOCK_SIZE,
  5222. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  5223. #if defined(WOLFSSL_ASYNC_CRYPT)
  5224. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5225. #endif
  5226. if (ret != 0)
  5227. return -5421;
  5228. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  5229. AES_BLOCK_SIZE))
  5230. return -5422;
  5231. #endif /* HAVE_AES_DECRYPT */
  5232. }
  5233. #endif /* WOLFSSL_AES_128 */
  5234. #endif /* HAVE_AES_CBC */
  5235. #ifdef WOLFSSL_AES_COUNTER
  5236. {
  5237. /* test vectors from "Recommendation for Block Cipher Modes of
  5238. * Operation" NIST Special Publication 800-38A */
  5239. const byte ctrIv[] =
  5240. {
  5241. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  5242. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  5243. };
  5244. const byte ctrPlain[] =
  5245. {
  5246. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5247. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  5248. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  5249. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  5250. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  5251. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  5252. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  5253. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  5254. };
  5255. #ifdef WOLFSSL_AES_128
  5256. const byte oddCipher[] =
  5257. {
  5258. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  5259. 0xc2
  5260. };
  5261. const byte ctr128Key[] =
  5262. {
  5263. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  5264. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  5265. };
  5266. const byte ctr128Cipher[] =
  5267. {
  5268. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  5269. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  5270. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  5271. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  5272. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  5273. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  5274. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  5275. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  5276. };
  5277. #endif /* WOLFSSL_AES_128 */
  5278. #ifdef WOLFSSL_AES_192
  5279. const byte ctr192Key[] =
  5280. {
  5281. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  5282. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  5283. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  5284. };
  5285. const byte ctr192Cipher[] =
  5286. {
  5287. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  5288. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  5289. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  5290. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  5291. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  5292. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  5293. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  5294. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  5295. };
  5296. #endif
  5297. #ifdef WOLFSSL_AES_256
  5298. const byte ctr256Key[] =
  5299. {
  5300. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  5301. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  5302. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  5303. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  5304. };
  5305. const byte ctr256Cipher[] =
  5306. {
  5307. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  5308. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  5309. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  5310. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  5311. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  5312. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  5313. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  5314. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  5315. };
  5316. #endif
  5317. #ifdef WOLFSSL_AES_128
  5318. wc_AesSetKeyDirect(&enc, ctr128Key, sizeof(ctr128Key),
  5319. ctrIv, AES_ENCRYPTION);
  5320. /* Ctr only uses encrypt, even on key setup */
  5321. wc_AesSetKeyDirect(&dec, ctr128Key, sizeof(ctr128Key),
  5322. ctrIv, AES_ENCRYPTION);
  5323. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(ctrPlain));
  5324. if (ret != 0) {
  5325. return -5423;
  5326. }
  5327. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(ctrPlain));
  5328. if (ret != 0) {
  5329. return -5424;
  5330. }
  5331. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  5332. return -5425;
  5333. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  5334. return -5426;
  5335. /* let's try with just 9 bytes, non block size test */
  5336. wc_AesSetKeyDirect(&enc, ctr128Key, AES_BLOCK_SIZE,
  5337. ctrIv, AES_ENCRYPTION);
  5338. /* Ctr only uses encrypt, even on key setup */
  5339. wc_AesSetKeyDirect(&dec, ctr128Key, AES_BLOCK_SIZE,
  5340. ctrIv, AES_ENCRYPTION);
  5341. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(oddCipher));
  5342. if (ret != 0) {
  5343. return -5427;
  5344. }
  5345. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(oddCipher));
  5346. if (ret != 0) {
  5347. return -5428;
  5348. }
  5349. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  5350. return -5429;
  5351. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  5352. return -5430;
  5353. /* and an additional 9 bytes to reuse tmp left buffer */
  5354. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(oddCipher));
  5355. if (ret != 0) {
  5356. return -5431;
  5357. }
  5358. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(oddCipher));
  5359. if (ret != 0) {
  5360. return -5432;
  5361. }
  5362. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  5363. return -5433;
  5364. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  5365. return -5434;
  5366. #endif /* WOLFSSL_AES_128 */
  5367. #ifdef WOLFSSL_AES_192
  5368. /* 192 bit key */
  5369. wc_AesSetKeyDirect(&enc, ctr192Key, sizeof(ctr192Key),
  5370. ctrIv, AES_ENCRYPTION);
  5371. /* Ctr only uses encrypt, even on key setup */
  5372. wc_AesSetKeyDirect(&dec, ctr192Key, sizeof(ctr192Key),
  5373. ctrIv, AES_ENCRYPTION);
  5374. XMEMSET(plain, 0, sizeof(plain));
  5375. ret = wc_AesCtrEncrypt(&enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  5376. if (ret != 0) {
  5377. return -5435;
  5378. }
  5379. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  5380. return -5436;
  5381. ret = wc_AesCtrEncrypt(&dec, cipher, ctrPlain, sizeof(ctrPlain));
  5382. if (ret != 0) {
  5383. return -5437;
  5384. }
  5385. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  5386. return -5438;
  5387. #endif /* WOLFSSL_AES_192 */
  5388. #ifdef WOLFSSL_AES_256
  5389. /* 256 bit key */
  5390. wc_AesSetKeyDirect(&enc, ctr256Key, sizeof(ctr256Key),
  5391. ctrIv, AES_ENCRYPTION);
  5392. /* Ctr only uses encrypt, even on key setup */
  5393. wc_AesSetKeyDirect(&dec, ctr256Key, sizeof(ctr256Key),
  5394. ctrIv, AES_ENCRYPTION);
  5395. XMEMSET(plain, 0, sizeof(plain));
  5396. ret = wc_AesCtrEncrypt(&enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  5397. if (ret != 0) {
  5398. return -5439;
  5399. }
  5400. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  5401. return -5440;
  5402. ret = wc_AesCtrEncrypt(&dec, cipher, ctrPlain, sizeof(ctrPlain));
  5403. if (ret != 0) {
  5404. return -5441;
  5405. }
  5406. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  5407. return -5442;
  5408. #endif /* WOLFSSL_AES_256 */
  5409. }
  5410. #endif /* WOLFSSL_AES_COUNTER */
  5411. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  5412. {
  5413. const byte niPlain[] =
  5414. {
  5415. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5416. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  5417. };
  5418. const byte niCipher[] =
  5419. {
  5420. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  5421. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  5422. };
  5423. const byte niKey[] =
  5424. {
  5425. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  5426. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  5427. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  5428. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  5429. };
  5430. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  5431. ret = wc_AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  5432. if (ret != 0)
  5433. return -5443;
  5434. wc_AesEncryptDirect(&enc, cipher, niPlain);
  5435. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  5436. return -5444;
  5437. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  5438. ret = wc_AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  5439. if (ret != 0)
  5440. return -5445;
  5441. wc_AesDecryptDirect(&dec, plain, niCipher);
  5442. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  5443. return -5446;
  5444. }
  5445. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  5446. ret = aes_key_size_test();
  5447. if (ret != 0)
  5448. return ret;
  5449. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5450. ret = aes_cbc_test();
  5451. if (ret != 0)
  5452. return ret;
  5453. #endif
  5454. #if defined(WOLFSSL_AES_XTS)
  5455. #ifdef WOLFSSL_AES_128
  5456. ret = aes_xts_128_test();
  5457. if (ret != 0)
  5458. return ret;
  5459. #endif
  5460. #ifdef WOLFSSL_AES_256
  5461. ret = aes_xts_256_test();
  5462. if (ret != 0)
  5463. return ret;
  5464. #endif
  5465. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  5466. ret = aes_xts_sector_test();
  5467. if (ret != 0)
  5468. return ret;
  5469. #endif
  5470. #ifdef WOLFSSL_AES_128
  5471. ret = aes_xts_args_test();
  5472. if (ret != 0)
  5473. return ret;
  5474. #endif
  5475. #endif
  5476. #if defined(WOLFSSL_AES_CFB)
  5477. ret = aescfb_test();
  5478. if (ret != 0)
  5479. return ret;
  5480. #endif
  5481. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  5482. wc_AesFree(&enc);
  5483. (void)cipher;
  5484. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  5485. wc_AesFree(&dec);
  5486. (void)plain;
  5487. #endif
  5488. #endif
  5489. return ret;
  5490. }
  5491. #ifdef WOLFSSL_AES_192
  5492. int aes192_test(void)
  5493. {
  5494. #ifdef HAVE_AES_CBC
  5495. Aes enc;
  5496. byte cipher[AES_BLOCK_SIZE];
  5497. #ifdef HAVE_AES_DECRYPT
  5498. Aes dec;
  5499. byte plain[AES_BLOCK_SIZE];
  5500. #endif
  5501. #endif /* HAVE_AES_CBC */
  5502. int ret = 0;
  5503. #ifdef HAVE_AES_CBC
  5504. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  5505. * Appendix F.2.3 */
  5506. const byte msg[] = {
  5507. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5508. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  5509. };
  5510. const byte verify[] =
  5511. {
  5512. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  5513. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  5514. };
  5515. byte key[] = {
  5516. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  5517. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  5518. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  5519. };
  5520. byte iv[] = {
  5521. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  5522. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  5523. };
  5524. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  5525. return -5500;
  5526. #ifdef HAVE_AES_DECRYPT
  5527. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  5528. return -5501;
  5529. #endif
  5530. ret = wc_AesSetKey(&enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  5531. if (ret != 0)
  5532. return -5502;
  5533. #ifdef HAVE_AES_DECRYPT
  5534. ret = wc_AesSetKey(&dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  5535. if (ret != 0)
  5536. return -5503;
  5537. #endif
  5538. ret = wc_AesCbcEncrypt(&enc, cipher, msg, (int) sizeof(msg));
  5539. #if defined(WOLFSSL_ASYNC_CRYPT)
  5540. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5541. #endif
  5542. if (ret != 0)
  5543. return -5504;
  5544. #ifdef HAVE_AES_DECRYPT
  5545. ret = wc_AesCbcDecrypt(&dec, plain, cipher, (int) sizeof(cipher));
  5546. #if defined(WOLFSSL_ASYNC_CRYPT)
  5547. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5548. #endif
  5549. if (ret != 0)
  5550. return -5505;
  5551. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  5552. return -5506;
  5553. }
  5554. #endif
  5555. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  5556. return -5507;
  5557. wc_AesFree(&enc);
  5558. #ifdef HAVE_AES_DECRYPT
  5559. wc_AesFree(&dec);
  5560. #endif
  5561. #endif /* HAVE_AES_CBC */
  5562. return ret;
  5563. }
  5564. #endif /* WOLFSSL_AES_192 */
  5565. #ifdef WOLFSSL_AES_256
  5566. int aes256_test(void)
  5567. {
  5568. #ifdef HAVE_AES_CBC
  5569. Aes enc;
  5570. byte cipher[AES_BLOCK_SIZE];
  5571. #ifdef HAVE_AES_DECRYPT
  5572. Aes dec;
  5573. byte plain[AES_BLOCK_SIZE];
  5574. #endif
  5575. #endif /* HAVE_AES_CBC */
  5576. int ret = 0;
  5577. #ifdef HAVE_AES_CBC
  5578. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  5579. * Appendix F.2.5 */
  5580. const byte msg[] = {
  5581. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  5582. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  5583. };
  5584. const byte verify[] =
  5585. {
  5586. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  5587. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  5588. };
  5589. byte key[] = {
  5590. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  5591. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  5592. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  5593. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  5594. };
  5595. byte iv[] = {
  5596. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  5597. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  5598. };
  5599. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  5600. return -5600;
  5601. #ifdef HAVE_AES_DECRYPT
  5602. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  5603. return -5601;
  5604. #endif
  5605. ret = wc_AesSetKey(&enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  5606. if (ret != 0)
  5607. return -5602;
  5608. #ifdef HAVE_AES_DECRYPT
  5609. ret = wc_AesSetKey(&dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  5610. if (ret != 0)
  5611. return -5603;
  5612. #endif
  5613. ret = wc_AesCbcEncrypt(&enc, cipher, msg, (int) sizeof(msg));
  5614. #if defined(WOLFSSL_ASYNC_CRYPT)
  5615. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5616. #endif
  5617. if (ret != 0)
  5618. return -5604;
  5619. #ifdef HAVE_AES_DECRYPT
  5620. ret = wc_AesCbcDecrypt(&dec, plain, cipher, (int) sizeof(cipher));
  5621. #if defined(WOLFSSL_ASYNC_CRYPT)
  5622. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5623. #endif
  5624. if (ret != 0)
  5625. return -5605;
  5626. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  5627. return -5606;
  5628. }
  5629. #endif
  5630. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  5631. return -5607;
  5632. wc_AesFree(&enc);
  5633. #ifdef HAVE_AES_DECRYPT
  5634. wc_AesFree(&dec);
  5635. #endif
  5636. #endif /* HAVE_AES_CBC */
  5637. return ret;
  5638. }
  5639. #endif /* WOLFSSL_AES_256 */
  5640. #ifdef HAVE_AESGCM
  5641. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  5642. byte* plain, int plainSz, byte* cipher, int cipherSz,
  5643. byte* aad, int aadSz, byte* tag, int tagSz)
  5644. {
  5645. Aes enc;
  5646. Aes dec;
  5647. byte resultT[AES_BLOCK_SIZE];
  5648. byte resultP[AES_BLOCK_SIZE * 3];
  5649. byte resultC[AES_BLOCK_SIZE * 3];
  5650. int result;
  5651. XMEMSET(resultT, 0, sizeof(resultT));
  5652. XMEMSET(resultC, 0, sizeof(resultC));
  5653. XMEMSET(resultP, 0, sizeof(resultP));
  5654. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0) {
  5655. return -5700;
  5656. }
  5657. result = wc_AesGcmSetKey(&enc, key, keySz);
  5658. if (result != 0)
  5659. return -4701;
  5660. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  5661. result = wc_AesGcmEncrypt(&enc, resultC, plain, plainSz, iv, ivSz,
  5662. resultT, tagSz, aad, aadSz);
  5663. #if defined(WOLFSSL_ASYNC_CRYPT)
  5664. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5665. #endif
  5666. if (result != 0)
  5667. return -4702;
  5668. if (cipher != NULL) {
  5669. if (XMEMCMP(cipher, resultC, cipherSz))
  5670. return -4703;
  5671. }
  5672. if (XMEMCMP(tag, resultT, tagSz))
  5673. return -4704;
  5674. wc_AesFree(&enc);
  5675. #ifdef HAVE_AES_DECRYPT
  5676. result = wc_AesGcmSetKey(&dec, key, keySz);
  5677. if (result != 0)
  5678. return -4705;
  5679. result = wc_AesGcmDecrypt(&dec, resultP, resultC, cipherSz,
  5680. iv, ivSz, resultT, tagSz, aad, aadSz);
  5681. #if defined(WOLFSSL_ASYNC_CRYPT)
  5682. result = wc_AsyncWait(result, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  5683. #endif
  5684. if (result != 0)
  5685. return -4706;
  5686. if (plain != NULL) {
  5687. if (XMEMCMP(plain, resultP, plainSz))
  5688. return -4707;
  5689. }
  5690. wc_AesFree(&dec);
  5691. #endif /* HAVE_AES_DECRYPT */
  5692. return 0;
  5693. }
  5694. /* tests that only use 12 byte IV and 16 or less byte AAD
  5695. * test vectors are from NIST SP 800-38D
  5696. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  5697. int aesgcm_default_test(void)
  5698. {
  5699. byte key1[] = {
  5700. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  5701. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  5702. };
  5703. byte iv1[] = {
  5704. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  5705. 0xe4, 0xed, 0x2f, 0x6d
  5706. };
  5707. byte plain1[] = {
  5708. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  5709. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  5710. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  5711. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  5712. };
  5713. byte aad1[] = {
  5714. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  5715. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  5716. };
  5717. byte cipher1[] = {
  5718. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  5719. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  5720. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  5721. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  5722. };
  5723. byte tag1[] = {
  5724. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  5725. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  5726. };
  5727. byte key2[] = {
  5728. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  5729. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  5730. };
  5731. byte iv2[] = {
  5732. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  5733. 0xc9, 0x8a, 0xff, 0xe3
  5734. };
  5735. byte plain2[] = {
  5736. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  5737. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  5738. };
  5739. byte cipher2[] = {
  5740. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  5741. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  5742. };
  5743. byte tag2[] = {
  5744. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  5745. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  5746. };
  5747. byte key3[] = {
  5748. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  5749. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  5750. };
  5751. byte iv3[] = {
  5752. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  5753. 0x6a, 0x4b, 0xd5, 0xe1
  5754. };
  5755. byte tag3[] = {
  5756. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  5757. 0x11, 0x64, 0xb2, 0xff
  5758. };
  5759. int ret;
  5760. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  5761. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  5762. aad1, sizeof(aad1), tag1, sizeof(tag1));
  5763. if (ret != 0) {
  5764. return ret;
  5765. }
  5766. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  5767. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  5768. NULL, 0, tag2, sizeof(tag2));
  5769. if (ret != 0) {
  5770. return ret;
  5771. }
  5772. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  5773. NULL, 0, NULL, 0,
  5774. NULL, 0, tag3, sizeof(tag3));
  5775. if (ret != 0) {
  5776. return ret;
  5777. }
  5778. return 0;
  5779. }
  5780. int aesgcm_test(void)
  5781. {
  5782. Aes enc;
  5783. /*
  5784. * This is Test Case 16 from the document Galois/
  5785. * Counter Mode of Operation (GCM) by McGrew and
  5786. * Viega.
  5787. */
  5788. const byte p[] =
  5789. {
  5790. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  5791. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  5792. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  5793. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  5794. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  5795. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  5796. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  5797. 0xba, 0x63, 0x7b, 0x39
  5798. };
  5799. const byte a[] =
  5800. {
  5801. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  5802. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  5803. 0xab, 0xad, 0xda, 0xd2
  5804. };
  5805. #ifdef WOLFSSL_AES_256
  5806. const byte k1[] =
  5807. {
  5808. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  5809. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  5810. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  5811. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  5812. };
  5813. const byte iv1[] =
  5814. {
  5815. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  5816. 0xde, 0xca, 0xf8, 0x88
  5817. };
  5818. const byte c1[] =
  5819. {
  5820. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  5821. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  5822. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  5823. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  5824. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  5825. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  5826. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  5827. 0xbc, 0xc9, 0xf6, 0x62
  5828. };
  5829. #endif /* WOLFSSL_AES_256 */
  5830. const byte t1[] =
  5831. {
  5832. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  5833. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  5834. };
  5835. /* FIPS, QAT and STM32F2/4 HW Crypto only support 12-byte IV */
  5836. #if !defined(HAVE_FIPS) && \
  5837. !defined(STM32_CRYPTO) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  5838. !defined(WOLFSSL_XILINX_CRYPT)
  5839. #define ENABLE_NON_12BYTE_IV_TEST
  5840. #ifdef WOLFSSL_AES_192
  5841. /* Test Case 12, uses same plaintext and AAD data. */
  5842. const byte k2[] =
  5843. {
  5844. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  5845. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  5846. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  5847. };
  5848. const byte iv2[] =
  5849. {
  5850. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  5851. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  5852. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  5853. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  5854. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  5855. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  5856. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  5857. 0xa6, 0x37, 0xb3, 0x9b
  5858. };
  5859. const byte c2[] =
  5860. {
  5861. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  5862. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  5863. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  5864. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  5865. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  5866. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  5867. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  5868. 0xe9, 0xb7, 0x37, 0x3b
  5869. };
  5870. const byte t2[] =
  5871. {
  5872. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  5873. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  5874. };
  5875. #endif /* WOLFSSL_AES_192 */
  5876. #ifdef WOLFSSL_AES_128
  5877. /* The following is an interesting test case from the example
  5878. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  5879. const byte p3[] =
  5880. {
  5881. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  5882. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  5883. };
  5884. const byte k3[] =
  5885. {
  5886. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  5887. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  5888. };
  5889. const byte iv3[] =
  5890. {
  5891. 0xca
  5892. };
  5893. const byte c3[] =
  5894. {
  5895. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  5896. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  5897. };
  5898. const byte a3[] =
  5899. {
  5900. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  5901. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  5902. };
  5903. const byte t3[] =
  5904. {
  5905. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  5906. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  5907. };
  5908. #endif /* WOLFSSL_AES_128 */
  5909. #ifdef WOLFSSL_AES_256
  5910. int ivlen;
  5911. #endif
  5912. #endif
  5913. byte resultT[sizeof(t1)];
  5914. byte resultP[sizeof(p)];
  5915. byte resultC[sizeof(p)];
  5916. int result;
  5917. #ifdef WOLFSSL_AES_256
  5918. int alen, plen;
  5919. #endif
  5920. #if !defined(BENCH_EMBEDDED)
  5921. #ifndef BENCH_AESGCM_LARGE
  5922. #define BENCH_AESGCM_LARGE 1024
  5923. #endif
  5924. byte large_input[BENCH_AESGCM_LARGE];
  5925. byte large_output[BENCH_AESGCM_LARGE];
  5926. byte large_outdec[BENCH_AESGCM_LARGE];
  5927. XMEMSET(large_input, 0, sizeof(large_input));
  5928. XMEMSET(large_output, 0, sizeof(large_output));
  5929. XMEMSET(large_outdec, 0, sizeof(large_outdec));
  5930. #endif
  5931. XMEMSET(resultT, 0, sizeof(resultT));
  5932. XMEMSET(resultC, 0, sizeof(resultC));
  5933. XMEMSET(resultP, 0, sizeof(resultP));
  5934. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0) {
  5935. return -5700;
  5936. }
  5937. #ifdef WOLFSSL_AES_256
  5938. result = wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  5939. if (result != 0)
  5940. return -5701;
  5941. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  5942. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  5943. resultT, sizeof(resultT), a, sizeof(a));
  5944. #if defined(WOLFSSL_ASYNC_CRYPT)
  5945. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5946. #endif
  5947. if (result != 0)
  5948. return -5702;
  5949. if (XMEMCMP(c1, resultC, sizeof(resultC)))
  5950. return -5703;
  5951. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  5952. return -5704;
  5953. #ifdef HAVE_AES_DECRYPT
  5954. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC),
  5955. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  5956. #if defined(WOLFSSL_ASYNC_CRYPT)
  5957. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5958. #endif
  5959. if (result != 0)
  5960. return -5705;
  5961. if (XMEMCMP(p, resultP, sizeof(resultP)))
  5962. return -5706;
  5963. #endif /* HAVE_AES_DECRYPT */
  5964. /* Large buffer test */
  5965. #ifdef BENCH_AESGCM_LARGE
  5966. /* setup test buffer */
  5967. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  5968. large_input[alen] = (byte)alen;
  5969. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  5970. result = wc_AesGcmEncrypt(&enc, large_output, large_input,
  5971. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  5972. resultT, sizeof(resultT), a, sizeof(a));
  5973. #if defined(WOLFSSL_ASYNC_CRYPT)
  5974. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5975. #endif
  5976. if (result != 0)
  5977. return -5707;
  5978. #ifdef HAVE_AES_DECRYPT
  5979. result = wc_AesGcmDecrypt(&enc, large_outdec, large_output,
  5980. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  5981. sizeof(resultT), a, sizeof(a));
  5982. #if defined(WOLFSSL_ASYNC_CRYPT)
  5983. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5984. #endif
  5985. if (result != 0)
  5986. return -5708;
  5987. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  5988. return -5709;
  5989. #endif /* HAVE_AES_DECRYPT */
  5990. #endif /* BENCH_AESGCM_LARGE */
  5991. #ifdef ENABLE_NON_12BYTE_IV_TEST
  5992. /* Variable IV length test */
  5993. for (ivlen=0; ivlen<(int)sizeof(k1); ivlen++) {
  5994. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  5995. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), k1,
  5996. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  5997. #if defined(WOLFSSL_ASYNC_CRYPT)
  5998. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  5999. #endif
  6000. if (result != 0)
  6001. return -5710;
  6002. #ifdef HAVE_AES_DECRYPT
  6003. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC), k1,
  6004. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  6005. #if defined(WOLFSSL_ASYNC_CRYPT)
  6006. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6007. #endif
  6008. if (result != 0)
  6009. return -5711;
  6010. #endif /* HAVE_AES_DECRYPT */
  6011. }
  6012. #endif
  6013. /* Variable authenticated data length test */
  6014. for (alen=0; alen<(int)sizeof(p); alen++) {
  6015. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  6016. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1,
  6017. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  6018. #if defined(WOLFSSL_ASYNC_CRYPT)
  6019. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6020. #endif
  6021. if (result != 0)
  6022. return -5712;
  6023. #ifdef HAVE_AES_DECRYPT
  6024. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC), iv1,
  6025. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  6026. #if defined(WOLFSSL_ASYNC_CRYPT)
  6027. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6028. #endif
  6029. if (result != 0)
  6030. return -5713;
  6031. #endif /* HAVE_AES_DECRYPT */
  6032. }
  6033. #ifdef BENCH_AESGCM_LARGE
  6034. /* Variable plain text length test */
  6035. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  6036. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  6037. result = wc_AesGcmEncrypt(&enc, large_output, large_input,
  6038. plen, iv1, sizeof(iv1), resultT,
  6039. sizeof(resultT), a, sizeof(a));
  6040. #if defined(WOLFSSL_ASYNC_CRYPT)
  6041. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6042. #endif
  6043. if (result != 0)
  6044. return -5714;
  6045. #ifdef HAVE_AES_DECRYPT
  6046. result = wc_AesGcmDecrypt(&enc, large_outdec, large_output,
  6047. plen, iv1, sizeof(iv1), resultT,
  6048. sizeof(resultT), a, sizeof(a));
  6049. #if defined(WOLFSSL_ASYNC_CRYPT)
  6050. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6051. #endif
  6052. if (result != 0)
  6053. return -5715;
  6054. #endif /* HAVE_AES_DECRYPT */
  6055. }
  6056. #else
  6057. /* Variable plain text length test */
  6058. for (plen=1; plen<(int)sizeof(p); plen++) {
  6059. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  6060. result = wc_AesGcmEncrypt(&enc, resultC, p, (word32)plen, iv1,
  6061. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  6062. #if defined(WOLFSSL_ASYNC_CRYPT)
  6063. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6064. #endif
  6065. if (result != 0)
  6066. return -5716;
  6067. #ifdef HAVE_AES_DECRYPT
  6068. result = wc_AesGcmDecrypt(&enc, resultP, resultC, (word32)plen, iv1,
  6069. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  6070. #if defined(WOLFSSL_ASYNC_CRYPT)
  6071. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6072. #endif
  6073. if (result != 0)
  6074. return -5717;
  6075. #endif /* HAVE_AES_DECRYPT */
  6076. }
  6077. #endif /* BENCH_AESGCM_LARGE */
  6078. #endif /* WOLFSSL_AES_256 */
  6079. /* test with IV != 12 bytes */
  6080. #ifdef ENABLE_NON_12BYTE_IV_TEST
  6081. XMEMSET(resultT, 0, sizeof(resultT));
  6082. XMEMSET(resultC, 0, sizeof(resultC));
  6083. XMEMSET(resultP, 0, sizeof(resultP));
  6084. #ifdef WOLFSSL_AES_192
  6085. wc_AesGcmSetKey(&enc, k2, sizeof(k2));
  6086. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  6087. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  6088. resultT, sizeof(resultT), a, sizeof(a));
  6089. #if defined(WOLFSSL_ASYNC_CRYPT)
  6090. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6091. #endif
  6092. if (result != 0)
  6093. return -5718;
  6094. if (XMEMCMP(c2, resultC, sizeof(resultC)))
  6095. return -5719;
  6096. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  6097. return -5720;
  6098. #ifdef HAVE_AES_DECRYPT
  6099. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC),
  6100. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  6101. #if defined(WOLFSSL_ASYNC_CRYPT)
  6102. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6103. #endif
  6104. if (result != 0)
  6105. return -5721;
  6106. if (XMEMCMP(p, resultP, sizeof(resultP)))
  6107. return -5722;
  6108. #endif /* HAVE_AES_DECRYPT */
  6109. XMEMSET(resultT, 0, sizeof(resultT));
  6110. XMEMSET(resultC, 0, sizeof(resultC));
  6111. XMEMSET(resultP, 0, sizeof(resultP));
  6112. #endif /* WOLFSSL_AES_192 */
  6113. #ifdef WOLFSSL_AES_128
  6114. wc_AesGcmSetKey(&enc, k3, sizeof(k3));
  6115. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  6116. result = wc_AesGcmEncrypt(&enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  6117. resultT, sizeof(t3), a3, sizeof(a3));
  6118. #if defined(WOLFSSL_ASYNC_CRYPT)
  6119. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6120. #endif
  6121. if (result != 0)
  6122. return -5723;
  6123. if (XMEMCMP(c3, resultC, sizeof(c3)))
  6124. return -5724;
  6125. if (XMEMCMP(t3, resultT, sizeof(t3)))
  6126. return -5725;
  6127. #ifdef HAVE_AES_DECRYPT
  6128. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(c3),
  6129. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  6130. #if defined(WOLFSSL_ASYNC_CRYPT)
  6131. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6132. #endif
  6133. if (result != 0)
  6134. return -5726;
  6135. if (XMEMCMP(p3, resultP, sizeof(p3)))
  6136. return -5727;
  6137. #endif /* HAVE_AES_DECRYPT */
  6138. #endif /* WOLFSSL_AES_128 */
  6139. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  6140. #ifdef WOLFSSL_AES_256
  6141. XMEMSET(resultT, 0, sizeof(resultT));
  6142. XMEMSET(resultC, 0, sizeof(resultC));
  6143. XMEMSET(resultP, 0, sizeof(resultP));
  6144. wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  6145. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  6146. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  6147. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  6148. #if defined(WOLFSSL_ASYNC_CRYPT)
  6149. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6150. #endif
  6151. if (result != 0)
  6152. return -5728;
  6153. if (XMEMCMP(c1, resultC, sizeof(resultC)))
  6154. return -5729;
  6155. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  6156. return -5730;
  6157. #ifdef HAVE_AES_DECRYPT
  6158. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC),
  6159. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  6160. #if defined(WOLFSSL_ASYNC_CRYPT)
  6161. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6162. #endif
  6163. if (result != 0)
  6164. return -5731;
  6165. if (XMEMCMP(p, resultP, sizeof(resultP)))
  6166. return -5732;
  6167. #endif /* HAVE_AES_DECRYPT */
  6168. #endif /* WOLFSSL_AES_256 */
  6169. #if !defined(HAVE_FIPS) || \
  6170. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  6171. /* Test encrypt with internally generated IV */
  6172. #if !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST))
  6173. {
  6174. WC_RNG rng;
  6175. byte randIV[12];
  6176. result = wc_InitRng(&rng);
  6177. if (result != 0)
  6178. return -8208;
  6179. XMEMSET(randIV, 0, sizeof(randIV));
  6180. XMEMSET(resultT, 0, sizeof(resultT));
  6181. XMEMSET(resultC, 0, sizeof(resultC));
  6182. XMEMSET(resultP, 0, sizeof(resultP));
  6183. wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  6184. result = wc_AesGcmSetIV(&enc, sizeof(randIV), NULL, 0, &rng);
  6185. if (result != 0)
  6186. return -8213;
  6187. result = wc_AesGcmEncrypt_ex(&enc,
  6188. resultC, p, sizeof(p),
  6189. randIV, sizeof(randIV),
  6190. resultT, sizeof(resultT),
  6191. a, sizeof(a));
  6192. #if defined(WOLFSSL_ASYNC_CRYPT)
  6193. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6194. #endif
  6195. if (result != 0)
  6196. return -8209;
  6197. /* Check the IV has been set. */
  6198. {
  6199. word32 i, ivSum = 0;
  6200. for (i = 0; i < sizeof(randIV); i++)
  6201. ivSum += randIV[i];
  6202. if (ivSum == 0)
  6203. return -8210;
  6204. }
  6205. result = wc_AesGcmDecrypt(&enc,
  6206. resultP, resultC, sizeof(resultC),
  6207. randIV, sizeof(randIV),
  6208. resultT, sizeof(resultT),
  6209. a, sizeof(a));
  6210. #if defined(WOLFSSL_ASYNC_CRYPT)
  6211. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6212. #endif
  6213. if (result != 0)
  6214. return -8211;
  6215. if (XMEMCMP(p, resultP, sizeof(resultP)))
  6216. return -8212;
  6217. wc_FreeRng(&rng);
  6218. }
  6219. #endif /* WC_NO_RNG HAVE_SELFTEST */
  6220. #endif
  6221. wc_AesFree(&enc);
  6222. return 0;
  6223. }
  6224. #ifdef WOLFSSL_AES_128
  6225. int gmac_test(void)
  6226. {
  6227. Gmac gmac;
  6228. const byte k1[] =
  6229. {
  6230. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  6231. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  6232. };
  6233. const byte iv1[] =
  6234. {
  6235. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  6236. 0xe2, 0x8c, 0x8f, 0x16
  6237. };
  6238. const byte a1[] =
  6239. {
  6240. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  6241. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  6242. };
  6243. const byte t1[] =
  6244. {
  6245. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  6246. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  6247. };
  6248. #if !defined(HAVE_FIPS) || \
  6249. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  6250. /* FIPS builds only allow 16-byte auth tags. */
  6251. /* This sample uses a 15-byte auth tag. */
  6252. const byte k2[] =
  6253. {
  6254. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  6255. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  6256. };
  6257. const byte iv2[] =
  6258. {
  6259. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  6260. 0x1a, 0x60, 0x24, 0xa7
  6261. };
  6262. const byte a2[] =
  6263. {
  6264. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  6265. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  6266. };
  6267. const byte t2[] =
  6268. {
  6269. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  6270. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  6271. };
  6272. #endif
  6273. byte tag[16];
  6274. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  6275. XMEMSET(tag, 0, sizeof(tag));
  6276. wc_GmacSetKey(&gmac, k1, sizeof(k1));
  6277. wc_GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  6278. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  6279. return -5800;
  6280. #if !defined(HAVE_FIPS) || \
  6281. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  6282. XMEMSET(tag, 0, sizeof(tag));
  6283. wc_GmacSetKey(&gmac, k2, sizeof(k2));
  6284. wc_GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  6285. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  6286. return -5801;
  6287. #if !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST))
  6288. {
  6289. const byte badT[] =
  6290. {
  6291. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  6292. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  6293. };
  6294. WC_RNG rng;
  6295. byte iv[12];
  6296. #ifndef HAVE_FIPS
  6297. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  6298. return -8214;
  6299. #else
  6300. if (wc_InitRng(&rng) != 0)
  6301. return -8214;
  6302. #endif
  6303. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  6304. t1, sizeof(t1)) != 0)
  6305. return -8215;
  6306. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  6307. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  6308. return -8216;
  6309. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  6310. t2, sizeof(t2)) != 0)
  6311. return -8217;
  6312. XMEMSET(tag, 0, sizeof(tag));
  6313. XMEMSET(iv, 0, sizeof(iv));
  6314. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  6315. tag, sizeof(tag), &rng) != 0)
  6316. return -8218;
  6317. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  6318. tag, sizeof(tag)) != 0)
  6319. return -8219;
  6320. wc_FreeRng(&rng);
  6321. }
  6322. #endif /* WC_NO_RNG HAVE_SELFTEST */
  6323. #endif /* HAVE_FIPS */
  6324. return 0;
  6325. }
  6326. #endif /* WOLFSSL_AES_128 */
  6327. #endif /* HAVE_AESGCM */
  6328. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  6329. int aesccm_test(void)
  6330. {
  6331. Aes enc;
  6332. /* key */
  6333. const byte k[] =
  6334. {
  6335. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  6336. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  6337. };
  6338. /* nonce */
  6339. const byte iv[] =
  6340. {
  6341. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  6342. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  6343. };
  6344. /* plaintext */
  6345. const byte p[] =
  6346. {
  6347. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  6348. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6349. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  6350. };
  6351. const byte a[] =
  6352. {
  6353. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  6354. };
  6355. const byte c[] =
  6356. {
  6357. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  6358. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  6359. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  6360. };
  6361. const byte t[] =
  6362. {
  6363. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  6364. };
  6365. byte t2[sizeof(t)];
  6366. byte p2[sizeof(p)];
  6367. byte c2[sizeof(c)];
  6368. byte iv2[sizeof(iv)];
  6369. int result;
  6370. XMEMSET(&enc, 0, sizeof(Aes)); /* clear context */
  6371. XMEMSET(t2, 0, sizeof(t2));
  6372. XMEMSET(c2, 0, sizeof(c2));
  6373. XMEMSET(p2, 0, sizeof(p2));
  6374. result = wc_AesCcmSetKey(&enc, k, sizeof(k));
  6375. if (result != 0)
  6376. return -5900;
  6377. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  6378. result = wc_AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  6379. t2, sizeof(t2), a, sizeof(a));
  6380. if (result != 0)
  6381. return -5901;
  6382. if (XMEMCMP(c, c2, sizeof(c2)))
  6383. return -5902;
  6384. if (XMEMCMP(t, t2, sizeof(t2)))
  6385. return -5903;
  6386. result = wc_AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  6387. t2, sizeof(t2), a, sizeof(a));
  6388. if (result != 0)
  6389. return -5904;
  6390. if (XMEMCMP(p, p2, sizeof(p2)))
  6391. return -5905;
  6392. /* Test the authentication failure */
  6393. t2[0]++; /* Corrupt the authentication tag. */
  6394. result = wc_AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
  6395. t2, sizeof(t2), a, sizeof(a));
  6396. if (result == 0)
  6397. return -5906;
  6398. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  6399. * authentication fail. */
  6400. XMEMSET(c2, 0, sizeof(c2));
  6401. if (XMEMCMP(p2, c2, sizeof(p2)))
  6402. return -5907;
  6403. XMEMSET(&enc, 0, sizeof(Aes)); /* clear context */
  6404. XMEMSET(t2, 0, sizeof(t2));
  6405. XMEMSET(c2, 0, sizeof(c2));
  6406. XMEMSET(p2, 0, sizeof(p2));
  6407. XMEMSET(iv2, 0, sizeof(iv2));
  6408. if (wc_AesCcmSetKey(&enc, k, sizeof(k)) != 0)
  6409. return -8220;
  6410. if (wc_AesCcmSetNonce(&enc, iv, sizeof(iv)) != 0)
  6411. return -8221;
  6412. if (wc_AesCcmEncrypt_ex(&enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  6413. t2, sizeof(t2), a, sizeof(a)) != 0)
  6414. return -8222;
  6415. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  6416. return -8223;
  6417. if (XMEMCMP(c, c2, sizeof(c2)))
  6418. return -8224;
  6419. if (XMEMCMP(t, t2, sizeof(t2)))
  6420. return -8225;
  6421. return 0;
  6422. }
  6423. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  6424. #ifdef HAVE_AES_KEYWRAP
  6425. #define MAX_KEYWRAP_TEST_OUTLEN 40
  6426. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  6427. typedef struct keywrapVector {
  6428. const byte* kek;
  6429. const byte* data;
  6430. const byte* verify;
  6431. word32 kekLen;
  6432. word32 dataLen;
  6433. word32 verifyLen;
  6434. } keywrapVector;
  6435. int aeskeywrap_test(void)
  6436. {
  6437. int wrapSz, plainSz, testSz, i;
  6438. /* test vectors from RFC 3394 (kek, data, verify) */
  6439. #ifdef WOLFSSL_AES_128
  6440. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  6441. const byte k1[] = {
  6442. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6443. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  6444. };
  6445. const byte d1[] = {
  6446. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6447. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  6448. };
  6449. const byte v1[] = {
  6450. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  6451. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  6452. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  6453. };
  6454. #endif /* WOLFSSL_AES_128 */
  6455. #ifdef WOLFSSL_AES_192
  6456. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  6457. const byte k2[] = {
  6458. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6459. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6460. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  6461. };
  6462. const byte d2[] = {
  6463. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6464. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  6465. };
  6466. const byte v2[] = {
  6467. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  6468. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  6469. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  6470. };
  6471. #endif
  6472. #ifdef WOLFSSL_AES_256
  6473. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  6474. const byte k3[] = {
  6475. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6476. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6477. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6478. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  6479. };
  6480. const byte d3[] = {
  6481. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6482. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  6483. };
  6484. const byte v3[] = {
  6485. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  6486. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  6487. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  6488. };
  6489. #endif
  6490. #ifdef WOLFSSL_AES_192
  6491. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  6492. const byte k4[] = {
  6493. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6494. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6495. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  6496. };
  6497. const byte d4[] = {
  6498. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6499. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  6500. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  6501. };
  6502. const byte v4[] = {
  6503. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  6504. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  6505. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  6506. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  6507. };
  6508. #endif
  6509. #ifdef WOLFSSL_AES_256
  6510. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  6511. const byte k5[] = {
  6512. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6513. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6514. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6515. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  6516. };
  6517. const byte d5[] = {
  6518. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6519. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  6520. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  6521. };
  6522. const byte v5[] = {
  6523. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  6524. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  6525. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  6526. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  6527. };
  6528. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  6529. const byte k6[] = {
  6530. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6531. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6532. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  6533. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  6534. };
  6535. const byte d6[] = {
  6536. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6537. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  6538. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6539. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  6540. };
  6541. const byte v6[] = {
  6542. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  6543. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  6544. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  6545. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  6546. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  6547. };
  6548. #endif /* WOLFSSL_AES_256 */
  6549. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  6550. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  6551. const keywrapVector test_wrap[] =
  6552. {
  6553. #ifdef WOLFSSL_AES_128
  6554. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  6555. #endif
  6556. #ifdef WOLFSSL_AES_192
  6557. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  6558. #endif
  6559. #ifdef WOLFSSL_AES_256
  6560. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  6561. #endif
  6562. #ifdef WOLFSSL_AES_192
  6563. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  6564. #endif
  6565. #ifdef WOLFSSL_AES_256
  6566. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  6567. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  6568. #endif
  6569. };
  6570. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  6571. XMEMSET(output, 0, sizeof(output));
  6572. XMEMSET(plain, 0, sizeof(plain));
  6573. for (i = 0; i < testSz; i++) {
  6574. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  6575. test_wrap[i].data, test_wrap[i].dataLen,
  6576. output, sizeof(output), NULL);
  6577. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  6578. return -6000;
  6579. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  6580. return -6001;
  6581. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  6582. output, wrapSz,
  6583. plain, sizeof(plain), NULL);
  6584. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  6585. return -6002;
  6586. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  6587. return -6003 - i;
  6588. }
  6589. return 0;
  6590. }
  6591. #endif /* HAVE_AES_KEYWRAP */
  6592. #endif /* NO_AES */
  6593. #ifdef HAVE_CAMELLIA
  6594. enum {
  6595. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  6596. };
  6597. typedef struct {
  6598. int type;
  6599. const byte* plaintext;
  6600. const byte* iv;
  6601. const byte* ciphertext;
  6602. const byte* key;
  6603. word32 keySz;
  6604. int errorCode;
  6605. } test_vector_t;
  6606. int camellia_test(void)
  6607. {
  6608. /* Camellia ECB Test Plaintext */
  6609. static const byte pte[] =
  6610. {
  6611. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6612. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  6613. };
  6614. /* Camellia ECB Test Initialization Vector */
  6615. static const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  6616. /* Test 1: Camellia ECB 128-bit key */
  6617. static const byte k1[] =
  6618. {
  6619. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6620. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  6621. };
  6622. static const byte c1[] =
  6623. {
  6624. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  6625. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  6626. };
  6627. /* Test 2: Camellia ECB 192-bit key */
  6628. static const byte k2[] =
  6629. {
  6630. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6631. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  6632. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  6633. };
  6634. static const byte c2[] =
  6635. {
  6636. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  6637. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  6638. };
  6639. /* Test 3: Camellia ECB 256-bit key */
  6640. static const byte k3[] =
  6641. {
  6642. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  6643. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  6644. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6645. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  6646. };
  6647. static const byte c3[] =
  6648. {
  6649. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  6650. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  6651. };
  6652. /* Camellia CBC Test Plaintext */
  6653. static const byte ptc[] =
  6654. {
  6655. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  6656. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  6657. };
  6658. /* Camellia CBC Test Initialization Vector */
  6659. static const byte ivc[] =
  6660. {
  6661. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6662. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  6663. };
  6664. /* Test 4: Camellia-CBC 128-bit key */
  6665. static const byte k4[] =
  6666. {
  6667. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  6668. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  6669. };
  6670. static const byte c4[] =
  6671. {
  6672. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  6673. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  6674. };
  6675. /* Test 5: Camellia-CBC 192-bit key */
  6676. static const byte k5[] =
  6677. {
  6678. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  6679. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  6680. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  6681. };
  6682. static const byte c5[] =
  6683. {
  6684. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  6685. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  6686. };
  6687. /* Test 6: CBC 256-bit key */
  6688. static const byte k6[] =
  6689. {
  6690. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  6691. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  6692. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  6693. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  6694. };
  6695. static const byte c6[] =
  6696. {
  6697. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  6698. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  6699. };
  6700. byte out[CAMELLIA_BLOCK_SIZE];
  6701. Camellia cam;
  6702. int i, testsSz, ret;
  6703. const test_vector_t testVectors[] =
  6704. {
  6705. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  6706. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  6707. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  6708. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  6709. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  6710. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  6711. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  6712. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  6713. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  6714. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  6715. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  6716. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  6717. };
  6718. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  6719. for (i = 0; i < testsSz; i++) {
  6720. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  6721. testVectors[i].iv) != 0)
  6722. return testVectors[i].errorCode;
  6723. switch (testVectors[i].type) {
  6724. case CAM_ECB_ENC:
  6725. ret = wc_CamelliaEncryptDirect(&cam, out,
  6726. testVectors[i].plaintext);
  6727. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  6728. CAMELLIA_BLOCK_SIZE))
  6729. return testVectors[i].errorCode;
  6730. break;
  6731. case CAM_ECB_DEC:
  6732. ret = wc_CamelliaDecryptDirect(&cam, out,
  6733. testVectors[i].ciphertext);
  6734. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  6735. CAMELLIA_BLOCK_SIZE))
  6736. return testVectors[i].errorCode;
  6737. break;
  6738. case CAM_CBC_ENC:
  6739. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  6740. CAMELLIA_BLOCK_SIZE);
  6741. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  6742. CAMELLIA_BLOCK_SIZE))
  6743. return testVectors[i].errorCode;
  6744. break;
  6745. case CAM_CBC_DEC:
  6746. ret = wc_CamelliaCbcDecrypt(&cam, out,
  6747. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  6748. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  6749. CAMELLIA_BLOCK_SIZE))
  6750. return testVectors[i].errorCode;
  6751. break;
  6752. default:
  6753. break;
  6754. }
  6755. }
  6756. /* Setting the IV and checking it was actually set. */
  6757. ret = wc_CamelliaSetIV(&cam, ivc);
  6758. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  6759. return -6100;
  6760. /* Setting the IV to NULL should be same as all zeros IV */
  6761. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  6762. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  6763. return -6101;
  6764. /* First parameter should never be null */
  6765. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  6766. return -6102;
  6767. /* First parameter should never be null, check it fails */
  6768. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  6769. return -6103;
  6770. /* Key should have a size of 16, 24, or 32 */
  6771. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  6772. return -6104;
  6773. return 0;
  6774. }
  6775. #endif /* HAVE_CAMELLIA */
  6776. #ifdef HAVE_IDEA
  6777. int idea_test(void)
  6778. {
  6779. int ret;
  6780. word16 i, j;
  6781. Idea idea;
  6782. byte data[IDEA_BLOCK_SIZE];
  6783. /* Project NESSIE test vectors */
  6784. #define IDEA_NB_TESTS 6
  6785. #define IDEA_NB_TESTS_EXTRA 4
  6786. const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  6787. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  6788. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  6789. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  6790. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  6791. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6792. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  6793. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  6794. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  6795. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  6796. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  6797. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  6798. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  6799. };
  6800. const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  6801. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  6802. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  6803. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  6804. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  6805. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  6806. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  6807. };
  6808. byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  6809. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  6810. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  6811. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  6812. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  6813. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  6814. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  6815. };
  6816. byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  6817. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  6818. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  6819. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  6820. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  6821. };
  6822. byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  6823. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  6824. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  6825. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  6826. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  6827. };
  6828. /* CBC test */
  6829. const char *message = "International Data Encryption Algorithm";
  6830. byte msg_enc[40], msg_dec[40];
  6831. for (i = 0; i < IDEA_NB_TESTS; i++) {
  6832. /* Set encryption key */
  6833. XMEMSET(&idea, 0, sizeof(Idea));
  6834. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  6835. NULL, IDEA_ENCRYPTION);
  6836. if (ret != 0) {
  6837. printf("wc_IdeaSetKey (enc) failed\n");
  6838. return -6200;
  6839. }
  6840. /* Data encryption */
  6841. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  6842. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  6843. printf("Bad encryption\n");
  6844. return -6201;
  6845. }
  6846. /* Set decryption key */
  6847. XMEMSET(&idea, 0, sizeof(Idea));
  6848. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  6849. NULL, IDEA_DECRYPTION);
  6850. if (ret != 0) {
  6851. printf("wc_IdeaSetKey (dec) failed\n");
  6852. return -6202;
  6853. }
  6854. /* Data decryption */
  6855. ret = wc_IdeaCipher(&idea, data, data);
  6856. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  6857. printf("Bad decryption\n");
  6858. return -6203;
  6859. }
  6860. /* Set encryption key */
  6861. XMEMSET(&idea, 0, sizeof(Idea));
  6862. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  6863. v_key[i], IDEA_ENCRYPTION);
  6864. if (ret != 0) {
  6865. printf("wc_IdeaSetKey (enc) failed\n");
  6866. return -6204;
  6867. }
  6868. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  6869. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  6870. (word32)XSTRLEN(message)+1);
  6871. if (ret != 0) {
  6872. printf("wc_IdeaCbcEncrypt failed\n");
  6873. return -6205;
  6874. }
  6875. /* Set decryption key */
  6876. XMEMSET(&idea, 0, sizeof(Idea));
  6877. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  6878. v_key[i], IDEA_DECRYPTION);
  6879. if (ret != 0) {
  6880. printf("wc_IdeaSetKey (dec) failed\n");
  6881. return -6206;
  6882. }
  6883. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  6884. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  6885. (word32)XSTRLEN(message)+1);
  6886. if (ret != 0) {
  6887. printf("wc_IdeaCbcDecrypt failed\n");
  6888. return -6207;
  6889. }
  6890. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  6891. printf("Bad CBC decryption\n");
  6892. return -6208;
  6893. }
  6894. }
  6895. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  6896. /* Set encryption key */
  6897. XMEMSET(&idea, 0, sizeof(Idea));
  6898. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  6899. NULL, IDEA_ENCRYPTION);
  6900. if (ret != 0) {
  6901. printf("wc_IdeaSetKey (enc) failed\n");
  6902. return -6209;
  6903. }
  6904. /* 100 times data encryption */
  6905. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  6906. for (j = 0; j < 100; j++) {
  6907. ret = wc_IdeaCipher(&idea, data, data);
  6908. if (ret != 0) {
  6909. return -6210;
  6910. }
  6911. }
  6912. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  6913. printf("Bad encryption (100 times)\n");
  6914. return -6211;
  6915. }
  6916. /* 1000 times data encryption */
  6917. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  6918. for (j = 0; j < 1000; j++) {
  6919. ret = wc_IdeaCipher(&idea, data, data);
  6920. if (ret != 0) {
  6921. return -6212;
  6922. }
  6923. }
  6924. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  6925. printf("Bad encryption (100 times)\n");
  6926. return -6213;
  6927. }
  6928. }
  6929. #ifndef WC_NO_RNG
  6930. /* random test for CBC */
  6931. {
  6932. WC_RNG rng;
  6933. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  6934. rnd[1000], enc[1000], dec[1000];
  6935. /* random values */
  6936. #ifndef HAVE_FIPS
  6937. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  6938. #else
  6939. ret = wc_InitRng(&rng);
  6940. #endif
  6941. if (ret != 0)
  6942. return -6214;
  6943. for (i = 0; i < 1000; i++) {
  6944. /* random key */
  6945. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  6946. if (ret != 0)
  6947. return -6215;
  6948. /* random iv */
  6949. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  6950. if (ret != 0)
  6951. return -6216;
  6952. /* random data */
  6953. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  6954. if (ret != 0)
  6955. return -6217;
  6956. /* Set encryption key */
  6957. XMEMSET(&idea, 0, sizeof(Idea));
  6958. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  6959. if (ret != 0) {
  6960. printf("wc_IdeaSetKey (enc) failed\n");
  6961. return -6218;
  6962. }
  6963. /* Data encryption */
  6964. XMEMSET(enc, 0, sizeof(enc));
  6965. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  6966. if (ret != 0) {
  6967. printf("wc_IdeaCbcEncrypt failed\n");
  6968. return -6219;
  6969. }
  6970. /* Set decryption key */
  6971. XMEMSET(&idea, 0, sizeof(Idea));
  6972. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  6973. if (ret != 0) {
  6974. printf("wc_IdeaSetKey (enc) failed\n");
  6975. return -6220;
  6976. }
  6977. /* Data decryption */
  6978. XMEMSET(dec, 0, sizeof(dec));
  6979. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  6980. if (ret != 0) {
  6981. printf("wc_IdeaCbcDecrypt failed\n");
  6982. return -6221;
  6983. }
  6984. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  6985. printf("Bad CBC decryption\n");
  6986. return -6222;
  6987. }
  6988. }
  6989. wc_FreeRng(&rng);
  6990. }
  6991. #endif /* WC_NO_RNG */
  6992. return 0;
  6993. }
  6994. #endif /* HAVE_IDEA */
  6995. #ifndef WC_NO_RNG
  6996. static int random_rng_test(void)
  6997. {
  6998. WC_RNG rng;
  6999. byte block[32];
  7000. int ret, i;
  7001. #ifndef HAVE_FIPS
  7002. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  7003. #else
  7004. ret = wc_InitRng(&rng);
  7005. #endif
  7006. if (ret != 0) return -6300;
  7007. XMEMSET(block, 0, sizeof(block));
  7008. ret = wc_RNG_GenerateBlock(&rng, block, sizeof(block));
  7009. if (ret != 0) {
  7010. ret = -6301;
  7011. goto exit;
  7012. }
  7013. /* Check for 0's */
  7014. for (i=0; i<(int)sizeof(block); i++) {
  7015. if (block[i] == 0) {
  7016. ret++;
  7017. }
  7018. }
  7019. /* All zeros count check */
  7020. if (ret >= (int)sizeof(block)) {
  7021. ret = -6302;
  7022. goto exit;
  7023. }
  7024. ret = wc_RNG_GenerateByte(&rng, block);
  7025. if (ret != 0) {
  7026. ret = -6303;
  7027. goto exit;
  7028. }
  7029. /* Parameter validation testing. */
  7030. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  7031. if (ret != BAD_FUNC_ARG) {
  7032. ret = -6304;
  7033. goto exit;
  7034. }
  7035. ret = wc_RNG_GenerateBlock(&rng, NULL, sizeof(block));
  7036. if (ret != BAD_FUNC_ARG) {
  7037. ret = -6305;
  7038. goto exit;
  7039. }
  7040. ret = wc_RNG_GenerateByte(NULL, block);
  7041. if (ret != BAD_FUNC_ARG) {
  7042. ret = -6306;
  7043. goto exit;
  7044. }
  7045. ret = wc_RNG_GenerateByte(&rng, NULL);
  7046. if (ret != BAD_FUNC_ARG) {
  7047. ret = -6307;
  7048. goto exit;
  7049. }
  7050. ret = 0;
  7051. exit:
  7052. /* Make sure and free RNG */
  7053. wc_FreeRng(&rng);
  7054. return ret;
  7055. }
  7056. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  7057. int random_test(void)
  7058. {
  7059. const byte test1Entropy[] =
  7060. {
  7061. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  7062. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  7063. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  7064. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  7065. };
  7066. const byte test1Output[] =
  7067. {
  7068. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  7069. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  7070. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  7071. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  7072. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  7073. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  7074. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  7075. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  7076. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  7077. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  7078. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  7079. };
  7080. const byte test2EntropyA[] =
  7081. {
  7082. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  7083. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  7084. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  7085. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  7086. };
  7087. const byte test2EntropyB[] =
  7088. {
  7089. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  7090. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  7091. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  7092. };
  7093. const byte test2Output[] =
  7094. {
  7095. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  7096. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  7097. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  7098. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  7099. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  7100. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  7101. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  7102. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  7103. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  7104. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  7105. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  7106. };
  7107. byte output[WC_SHA256_DIGEST_SIZE * 4];
  7108. int ret;
  7109. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  7110. output, sizeof(output));
  7111. if (ret != 0)
  7112. return -6400;
  7113. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  7114. return -6401;
  7115. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  7116. test2EntropyB, sizeof(test2EntropyB),
  7117. output, sizeof(output));
  7118. if (ret != 0)
  7119. return -6402;
  7120. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  7121. return -6403;
  7122. /* Basic RNG generate block test */
  7123. if ((ret = random_rng_test()) != 0)
  7124. return ret;
  7125. return 0;
  7126. }
  7127. #else
  7128. int random_test(void)
  7129. {
  7130. /* Basic RNG generate block test */
  7131. return random_rng_test();
  7132. }
  7133. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  7134. #endif /* WC_NO_RNG */
  7135. #ifdef WOLFSSL_STATIC_MEMORY
  7136. int memory_test(void)
  7137. {
  7138. int ret = 0;
  7139. unsigned int i;
  7140. word32 size[] = { WOLFMEM_BUCKETS };
  7141. word32 dist[] = { WOLFMEM_DIST };
  7142. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  7143. int pad = -(int)((wolfssl_word)&(buffer[0])) & (WOLFSSL_STATIC_ALIGN - 1);
  7144. /* pad to account for if head of buffer is not at set memory
  7145. * alignment when tests are ran */
  7146. /* check macro settings */
  7147. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  7148. return -6500;
  7149. }
  7150. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  7151. return -6501;
  7152. }
  7153. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  7154. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  7155. /* each element in array should be divisable by alignment size */
  7156. return -6502;
  7157. }
  7158. }
  7159. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  7160. if (size[i - 1] >= size[i]) {
  7161. return -6503; /* sizes should be in increasing order */
  7162. }
  7163. }
  7164. /* check that padding size returned is possible */
  7165. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  7166. return -6504; /* no room for wc_Memory struct */
  7167. }
  7168. if (wolfSSL_MemoryPaddingSz() < 0) {
  7169. return -6505;
  7170. }
  7171. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  7172. return -6506; /* not aligned! */
  7173. }
  7174. /* check function to return optimum buffer size (rounded down) */
  7175. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  7176. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  7177. return -6507; /* not aligned! */
  7178. }
  7179. if (ret < 0) {
  7180. return -6508;
  7181. }
  7182. if ((unsigned int)ret > sizeof(buffer)) {
  7183. return -6509; /* did not round down as expected */
  7184. }
  7185. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  7186. return -6510; /* retrun value changed when using suggested value */
  7187. }
  7188. ret = wolfSSL_MemoryPaddingSz();
  7189. ret += pad; /* add space that is going to be needed if buffer not aligned */
  7190. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  7191. (ret + (int)size[0])) {
  7192. return -6511; /* did not round down to nearest bucket value */
  7193. }
  7194. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  7195. if ((ret - pad) < 0) {
  7196. return -6512;
  7197. }
  7198. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  7199. return -6513; /* not even chunks of memory for IO size */
  7200. }
  7201. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  7202. return -6514; /* memory not aligned */
  7203. }
  7204. /* check for passing bad or unknown argments to functions */
  7205. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  7206. return -6515;
  7207. }
  7208. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  7209. return -6516; /* should round to 0 since struct + bucket will not fit */
  7210. }
  7211. (void)dist; /* avoid static analysis warning of variable not used */
  7212. return 0;
  7213. }
  7214. #endif /* WOLFSSL_STATIC_MEMORY */
  7215. #ifdef HAVE_NTRU
  7216. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  7217. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  7218. {
  7219. static WC_RNG rng;
  7220. if (cmd == INIT)
  7221. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  7222. if (out == NULL)
  7223. return 0;
  7224. if (cmd == GET_BYTE_OF_ENTROPY)
  7225. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  7226. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  7227. *out = 1;
  7228. return 1;
  7229. }
  7230. return 0;
  7231. }
  7232. #endif /* HAVE_NTRU */
  7233. #ifndef NO_FILESYSTEM
  7234. /* Cert Paths */
  7235. #ifdef FREESCALE_MQX
  7236. #define CERT_PREFIX "a:\\"
  7237. #define CERT_PATH_SEP "\\"
  7238. #elif defined(WOLFSSL_uTKERNEL2)
  7239. #define CERT_PREFIX "/uda/"
  7240. #define CERT_PATH_SEP "/"
  7241. #else
  7242. #define CERT_PREFIX "./"
  7243. #define CERT_PATH_SEP "/"
  7244. #endif
  7245. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  7246. /* Generated Test Certs */
  7247. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  7248. !defined(NO_ASN)
  7249. #ifndef NO_RSA
  7250. static const char* clientKey = CERT_ROOT "client-key.der";
  7251. static const char* clientCert = CERT_ROOT "client-cert.der";
  7252. #ifdef WOLFSSL_CERT_EXT
  7253. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  7254. #endif
  7255. #ifdef WOLFSSL_CERT_GEN
  7256. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  7257. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  7258. #ifdef WOLFSSL_ALT_NAMES
  7259. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  7260. #endif
  7261. #endif
  7262. #endif /* !NO_RSA */
  7263. #ifndef NO_DH
  7264. static const char* dhKey = CERT_ROOT "dh2048.der";
  7265. #endif
  7266. #ifndef NO_DSA
  7267. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  7268. #endif
  7269. #endif /* !USE_CERT_BUFFER_* */
  7270. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  7271. #ifdef HAVE_ECC
  7272. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  7273. #ifdef HAVE_ECC_KEY_IMPORT
  7274. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  7275. #endif
  7276. #ifdef WOLFSSL_CERT_GEN
  7277. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  7278. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  7279. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  7280. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  7281. static const char* eccCaKey384File =
  7282. CERT_ROOT "ca-ecc384-key.der";
  7283. static const char* eccCaCert384File =
  7284. CERT_ROOT "ca-ecc384-cert.pem";
  7285. #endif
  7286. #endif
  7287. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  7288. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  7289. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  7290. #endif
  7291. #endif /* HAVE_ECC */
  7292. #ifdef HAVE_ED25519
  7293. #ifdef WOLFSSL_TEST_CERT
  7294. static const char* serverEd25519Cert =
  7295. CERT_ROOT "ed25519/server-ed25519.der";
  7296. static const char* caEd25519Cert =
  7297. CERT_ROOT "ed25519/ca-ed25519.der";
  7298. #endif
  7299. #endif
  7300. #endif /* !USE_CERT_BUFFER_* */
  7301. #ifndef NO_WRITE_TEMP_FILES
  7302. #ifdef HAVE_ECC
  7303. #ifdef WOLFSSL_CERT_GEN
  7304. static const char* certEccPemFile = CERT_PREFIX "certecc.pem";
  7305. #endif
  7306. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  7307. static const char* certEccRsaPemFile = CERT_PREFIX "certeccrsa.pem";
  7308. static const char* certEccRsaDerFile = CERT_PREFIX "certeccrsa.der";
  7309. #endif
  7310. #ifdef WOLFSSL_KEY_GEN
  7311. static const char* eccCaKeyPemFile = CERT_PREFIX "ecc-key.pem";
  7312. static const char* eccPubKeyDerFile = CERT_PREFIX "ecc-public-key.der";
  7313. static const char* eccCaKeyTempFile = CERT_PREFIX "ecc-key.der";
  7314. static const char* eccPkcs8KeyDerFile = CERT_PREFIX "ecc-key-pkcs8.der";
  7315. #endif
  7316. #if defined(WOLFSSL_CERT_GEN) || \
  7317. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  7318. static const char* certEccDerFile = CERT_PREFIX "certecc.der";
  7319. #endif
  7320. #endif /* HAVE_ECC */
  7321. #ifndef NO_RSA
  7322. #ifdef WOLFSSL_CERT_GEN
  7323. static const char* otherCertDerFile = CERT_PREFIX "othercert.der";
  7324. static const char* certDerFile = CERT_PREFIX "cert.der";
  7325. #endif
  7326. #ifdef WOLFSSL_CERT_GEN
  7327. static const char* otherCertPemFile = CERT_PREFIX "othercert.pem";
  7328. static const char* certPemFile = CERT_PREFIX "cert.pem";
  7329. #endif
  7330. #ifdef WOLFSSL_CERT_REQ
  7331. static const char* certReqDerFile = CERT_PREFIX "certreq.der";
  7332. static const char* certReqPemFile = CERT_PREFIX "certreq.pem";
  7333. #endif
  7334. #endif /* !NO_RSA */
  7335. #if !defined(NO_RSA) || !defined(NO_DSA)
  7336. #ifdef WOLFSSL_KEY_GEN
  7337. static const char* keyDerFile = CERT_PREFIX "key.der";
  7338. static const char* keyPemFile = CERT_PREFIX "key.pem";
  7339. #endif
  7340. #endif
  7341. #endif /* !NO_WRITE_TEMP_FILES */
  7342. #endif /* !NO_FILESYSTEM */
  7343. #ifdef WOLFSSL_CERT_GEN
  7344. #ifdef WOLFSSL_MULTI_ATTRIB
  7345. static CertName certDefaultName;
  7346. static void initDefaultName(void)
  7347. {
  7348. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  7349. certDefaultName.countryEnc = CTC_PRINTABLE;
  7350. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  7351. certDefaultName.stateEnc = CTC_UTF8;
  7352. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  7353. certDefaultName.localityEnc = CTC_UTF8;
  7354. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  7355. certDefaultName.surEnc = CTC_UTF8;
  7356. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  7357. certDefaultName.orgEnc = CTC_UTF8;
  7358. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  7359. certDefaultName.unitEnc = CTC_UTF8;
  7360. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  7361. certDefaultName.commonNameEnc = CTC_UTF8;
  7362. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  7363. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  7364. #ifdef WOLFSSL_CERT_EXT
  7365. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  7366. certDefaultName.busCatEnc = CTC_UTF8;
  7367. #endif
  7368. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  7369. #ifdef WOLFSSL_TEST_CERT
  7370. {
  7371. NameAttrib* n;
  7372. /* test having additional OUs and setting DC */
  7373. n = &certDefaultName.name[0];
  7374. n->id = ASN_ORGUNIT_NAME;
  7375. n->type = CTC_UTF8;
  7376. n->sz = sizeof("Development-2");
  7377. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  7378. #if CTC_MAX_ATTRIB > 3
  7379. n = &certDefaultName.name[1];
  7380. n->id = ASN_DOMAIN_COMPONENT;
  7381. n->type = CTC_UTF8;
  7382. n->sz = sizeof("com");
  7383. XMEMCPY(n->value, "com", sizeof("com"));
  7384. n = &certDefaultName.name[2];
  7385. n->id = ASN_DOMAIN_COMPONENT;
  7386. n->type = CTC_UTF8;
  7387. n->sz = sizeof("wolfssl");
  7388. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  7389. #endif
  7390. }
  7391. #endif /* WOLFSSL_TEST_CERT */
  7392. }
  7393. #else
  7394. static const CertName certDefaultName = {
  7395. "US", CTC_PRINTABLE, /* country */
  7396. "Oregon", CTC_UTF8, /* state */
  7397. "Portland", CTC_UTF8, /* locality */
  7398. "Test", CTC_UTF8, /* sur */
  7399. "wolfSSL", CTC_UTF8, /* org */
  7400. "Development", CTC_UTF8, /* unit */
  7401. "www.wolfssl.com", CTC_UTF8, /* commonName */
  7402. "wolfSSL12345", CTC_PRINTABLE, /* serial number of device */
  7403. #ifdef WOLFSSL_CERT_EXT
  7404. "Private Organization", CTC_UTF8, /* businessCategory */
  7405. "US", CTC_PRINTABLE, /* jurisdiction country */
  7406. "Oregon", CTC_PRINTABLE, /* jurisdiction state */
  7407. #endif
  7408. "info@wolfssl.com" /* email */
  7409. };
  7410. #endif /* WOLFSSL_MULTI_ATTRIB */
  7411. #ifdef WOLFSSL_CERT_EXT
  7412. #if (defined(HAVE_ED25519) && defined(WOLFSSL_TEST_CERT)) || \
  7413. defined(HAVE_ECC)
  7414. static const char certKeyUsage[] =
  7415. "digitalSignature,nonRepudiation";
  7416. #endif
  7417. #if defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)
  7418. static const char certKeyUsage2[] =
  7419. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  7420. #endif
  7421. #endif /* WOLFSSL_CERT_EXT */
  7422. #endif /* WOLFSSL_CERT_GEN */
  7423. #ifndef NO_RSA
  7424. #ifdef WOLFSSL_TEST_CERT
  7425. int cert_test(void)
  7426. {
  7427. DecodedCert cert;
  7428. byte* tmp;
  7429. size_t bytes;
  7430. FILE *file;
  7431. int ret;
  7432. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7433. if (tmp == NULL)
  7434. return -6600;
  7435. /* Certificate with Name Constraints extension. */
  7436. #ifdef FREESCALE_MQX
  7437. file = fopen(".\\certs\\test\\cert-ext-nc.der", "rb");
  7438. #else
  7439. file = fopen("./certs/test/cert-ext-nc.der", "rb");
  7440. #endif
  7441. if (!file) {
  7442. ERROR_OUT(-6601, done);
  7443. }
  7444. bytes = fread(tmp, 1, FOURK_BUF, file);
  7445. fclose(file);
  7446. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  7447. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  7448. if (ret != 0) {
  7449. ERROR_OUT(-6602, done);
  7450. }
  7451. FreeDecodedCert(&cert);
  7452. /* Certificate with Inhibit Any Policy extension. */
  7453. #ifdef FREESCALE_MQX
  7454. file = fopen(".\\certs\\test\\cert-ext-ia.der", "rb");
  7455. #else
  7456. file = fopen("./certs/test/cert-ext-ia.der", "rb");
  7457. #endif
  7458. if (!file) {
  7459. ERROR_OUT(-6603, done);
  7460. }
  7461. bytes = fread(tmp, 1, FOURK_BUF, file);
  7462. fclose(file);
  7463. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  7464. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  7465. if (ret != 0) {
  7466. ERROR_OUT(-6604, done);
  7467. }
  7468. done:
  7469. FreeDecodedCert(&cert);
  7470. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7471. return ret;
  7472. }
  7473. #endif /* WOLFSSL_TEST_CERT */
  7474. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  7475. int certext_test(void)
  7476. {
  7477. DecodedCert cert;
  7478. byte* tmp;
  7479. size_t bytes;
  7480. FILE *file;
  7481. int ret;
  7482. /* created from rsa_test : othercert.der */
  7483. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  7484. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  7485. /* created from rsa_test : othercert.der */
  7486. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  7487. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  7488. #ifdef HAVE_ECC
  7489. /* created from ecc_test_cert_gen : certecc.der */
  7490. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  7491. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  7492. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  7493. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  7494. #else
  7495. /* Authority key id from ./certs/ca-ecc-cert.pem */
  7496. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  7497. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  7498. #endif
  7499. #endif /* HAVE_ECC */
  7500. /* created from rsa_test : cert.der */
  7501. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  7502. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  7503. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7504. if (tmp == NULL)
  7505. return -6700;
  7506. /* load othercert.der (Cert signed by an authority) */
  7507. file = fopen(otherCertDerFile, "rb");
  7508. if (!file) {
  7509. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  7510. return -6701;
  7511. }
  7512. bytes = fread(tmp, 1, FOURK_BUF, file);
  7513. fclose(file);
  7514. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  7515. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  7516. if (ret != 0)
  7517. return -6702;
  7518. /* check the SKID from a RSA certificate */
  7519. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  7520. return -6703;
  7521. /* check the AKID from an RSA certificate */
  7522. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  7523. return -6704;
  7524. /* check the Key Usage from an RSA certificate */
  7525. if (!cert.extKeyUsageSet)
  7526. return -6705;
  7527. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  7528. return -6706;
  7529. /* check the CA Basic Constraints from an RSA certificate */
  7530. if (cert.isCA)
  7531. return -6707;
  7532. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  7533. /* check the Certificate Policies Id */
  7534. if (cert.extCertPoliciesNb != 1)
  7535. return -6708;
  7536. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  7537. return -6709;
  7538. #endif
  7539. FreeDecodedCert(&cert);
  7540. #ifdef HAVE_ECC
  7541. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  7542. file = fopen(certEccDerFile, "rb");
  7543. if (!file) {
  7544. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  7545. return -6710;
  7546. }
  7547. bytes = fread(tmp, 1, FOURK_BUF, file);
  7548. fclose(file);
  7549. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  7550. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  7551. if (ret != 0)
  7552. return -6711;
  7553. /* check the SKID from a ECC certificate - generated dynamically */
  7554. /* check the AKID from an ECC certificate */
  7555. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  7556. return -6712;
  7557. /* check the Key Usage from an ECC certificate */
  7558. if (!cert.extKeyUsageSet)
  7559. return -6713;
  7560. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  7561. return -6714;
  7562. /* check the CA Basic Constraints from an ECC certificate */
  7563. if (cert.isCA)
  7564. return -6715;
  7565. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  7566. /* check the Certificate Policies Id */
  7567. if (cert.extCertPoliciesNb != 2)
  7568. return -6716;
  7569. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  7570. return -6717;
  7571. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  7572. return -6718;
  7573. #endif
  7574. FreeDecodedCert(&cert);
  7575. #endif /* HAVE_ECC */
  7576. /* load cert.der (self signed certificate) */
  7577. file = fopen(certDerFile, "rb");
  7578. if (!file) {
  7579. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  7580. return -6719;
  7581. }
  7582. bytes = fread(tmp, 1, FOURK_BUF, file);
  7583. fclose(file);
  7584. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  7585. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  7586. if (ret != 0)
  7587. return -6720;
  7588. /* check the SKID from a CA certificate */
  7589. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  7590. return -6721;
  7591. /* check the AKID from an CA certificate */
  7592. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  7593. return -6722;
  7594. /* check the Key Usage from CA certificate */
  7595. if (!cert.extKeyUsageSet)
  7596. return -6723;
  7597. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  7598. return -6724;
  7599. /* check the CA Basic Constraints CA certificate */
  7600. if (!cert.isCA)
  7601. return -6725;
  7602. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  7603. /* check the Certificate Policies Id */
  7604. if (cert.extCertPoliciesNb != 2)
  7605. return -6726;
  7606. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  7607. return -6727;
  7608. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  7609. return -6728;
  7610. #endif
  7611. FreeDecodedCert(&cert);
  7612. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  7613. return 0;
  7614. }
  7615. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  7616. static int rsa_flatten_test(RsaKey* key)
  7617. {
  7618. int ret;
  7619. byte e[256];
  7620. byte n[256];
  7621. word32 eSz = sizeof(e);
  7622. word32 nSz = sizeof(n);
  7623. /* Parameter Validation testing. */
  7624. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  7625. #ifdef HAVE_USER_RSA
  7626. /* Implementation using IPP Libraries returns:
  7627. * -101 = USER_CRYPTO_ERROR
  7628. */
  7629. if (ret == 0)
  7630. #else
  7631. if (ret != BAD_FUNC_ARG)
  7632. #endif
  7633. return -6729;
  7634. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  7635. #ifdef HAVE_USER_RSA
  7636. /* Implementation using IPP Libraries returns:
  7637. * -101 = USER_CRYPTO_ERROR
  7638. */
  7639. if (ret == 0)
  7640. #else
  7641. if (ret != BAD_FUNC_ARG)
  7642. #endif
  7643. return -6730;
  7644. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  7645. #ifdef HAVE_USER_RSA
  7646. /* Implementation using IPP Libraries returns:
  7647. * -101 = USER_CRYPTO_ERROR
  7648. */
  7649. if (ret == 0)
  7650. #else
  7651. if (ret != BAD_FUNC_ARG)
  7652. #endif
  7653. return -6731;
  7654. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  7655. #ifdef HAVE_USER_RSA
  7656. /* Implementation using IPP Libraries returns:
  7657. * -101 = USER_CRYPTO_ERROR
  7658. */
  7659. if (ret == 0)
  7660. #else
  7661. if (ret != BAD_FUNC_ARG)
  7662. #endif
  7663. return -6732;
  7664. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  7665. #ifdef HAVE_USER_RSA
  7666. /* Implementation using IPP Libraries returns:
  7667. * -101 = USER_CRYPTO_ERROR
  7668. */
  7669. if (ret == 0)
  7670. #else
  7671. if (ret != BAD_FUNC_ARG)
  7672. #endif
  7673. return -6733;
  7674. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  7675. if (ret != 0)
  7676. return -6734;
  7677. eSz = 0;
  7678. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  7679. #ifdef HAVE_USER_RSA
  7680. /* Implementation using IPP Libraries returns:
  7681. * -101 = USER_CRYPTO_ERROR
  7682. */
  7683. if (ret == 0)
  7684. #elif defined(HAVE_FIPS) && \
  7685. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  7686. if (ret != 0)
  7687. #else
  7688. if (ret != RSA_BUFFER_E)
  7689. #endif
  7690. return -6735;
  7691. eSz = sizeof(e);
  7692. nSz = 0;
  7693. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  7694. #ifdef HAVE_USER_RSA
  7695. /* Implementation using IPP Libraries returns:
  7696. * -101 = USER_CRYPTO_ERROR
  7697. */
  7698. if (ret == 0)
  7699. #else
  7700. if (ret != RSA_BUFFER_E)
  7701. #endif
  7702. return -6736;
  7703. return 0;
  7704. }
  7705. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA)
  7706. static int rsa_export_key_test(RsaKey* key)
  7707. {
  7708. int ret;
  7709. byte e[3];
  7710. word32 eSz = sizeof(e);
  7711. byte n[256];
  7712. word32 nSz = sizeof(n);
  7713. byte d[256];
  7714. word32 dSz = sizeof(d);
  7715. byte p[128];
  7716. word32 pSz = sizeof(p);
  7717. byte q[128];
  7718. word32 qSz = sizeof(q);
  7719. word32 zero = 0;
  7720. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  7721. if (ret != BAD_FUNC_ARG)
  7722. return -6737;
  7723. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  7724. if (ret != BAD_FUNC_ARG)
  7725. return -6738;
  7726. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  7727. if (ret != BAD_FUNC_ARG)
  7728. return -6739;
  7729. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  7730. if (ret != BAD_FUNC_ARG)
  7731. return -6740;
  7732. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  7733. if (ret != BAD_FUNC_ARG)
  7734. return -6741;
  7735. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  7736. if (ret != BAD_FUNC_ARG)
  7737. return -6742;
  7738. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  7739. if (ret != BAD_FUNC_ARG)
  7740. return -6743;
  7741. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  7742. if (ret != BAD_FUNC_ARG)
  7743. return -6744;
  7744. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  7745. if (ret != BAD_FUNC_ARG)
  7746. return -6745;
  7747. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  7748. if (ret != BAD_FUNC_ARG)
  7749. return -6746;
  7750. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  7751. if (ret != BAD_FUNC_ARG)
  7752. return -6747;
  7753. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  7754. if (ret != RSA_BUFFER_E)
  7755. return -6748;
  7756. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  7757. if (ret != RSA_BUFFER_E)
  7758. return -6749;
  7759. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  7760. if (ret != RSA_BUFFER_E)
  7761. return -6750;
  7762. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  7763. if (ret != RSA_BUFFER_E)
  7764. return -6751;
  7765. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  7766. if (ret != RSA_BUFFER_E)
  7767. return -6752;
  7768. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  7769. if (ret != 0)
  7770. return -6753;
  7771. return 0;
  7772. }
  7773. #endif /* !HAVE_FIPS */
  7774. #ifndef NO_SIG_WRAPPER
  7775. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  7776. {
  7777. int ret;
  7778. word32 sigSz;
  7779. const byte in[] = "Everyone gets Friday off.";
  7780. const byte hash[] = {
  7781. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  7782. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  7783. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  7784. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  7785. };
  7786. const byte hashEnc[] = {
  7787. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  7788. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  7789. 0x00, 0x04, 0x20,
  7790. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  7791. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  7792. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  7793. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  7794. };
  7795. word32 inLen = (word32)XSTRLEN((char*)in);
  7796. byte out[256];
  7797. /* Parameter Validation testing. */
  7798. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  7799. if (ret != BAD_FUNC_ARG)
  7800. return -6754;
  7801. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  7802. if (ret != BAD_FUNC_ARG)
  7803. return -6755;
  7804. sigSz = (word32)modLen;
  7805. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  7806. inLen, out, &sigSz, key, keyLen, rng);
  7807. if (ret != BAD_FUNC_ARG)
  7808. return -6756;
  7809. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7810. 0, out, &sigSz, key, keyLen, rng);
  7811. if (ret != BAD_FUNC_ARG)
  7812. return -6757;
  7813. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7814. inLen, NULL, &sigSz, key, keyLen, rng);
  7815. if (ret != BAD_FUNC_ARG)
  7816. return -6758;
  7817. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7818. inLen, out, NULL, key, keyLen, rng);
  7819. if (ret != BAD_FUNC_ARG)
  7820. return -6759;
  7821. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7822. inLen, out, &sigSz, NULL, keyLen, rng);
  7823. if (ret != BAD_FUNC_ARG)
  7824. return -6760;
  7825. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7826. inLen, out, &sigSz, key, 0, rng);
  7827. if (ret != BAD_FUNC_ARG)
  7828. return -6761;
  7829. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7830. inLen, out, &sigSz, key, keyLen, NULL);
  7831. #ifdef HAVE_USER_RSA
  7832. /* Implementation using IPP Libraries returns:
  7833. * -101 = USER_CRYPTO_ERROR
  7834. */
  7835. if (ret == 0)
  7836. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_DEV)
  7837. /* async may not require RNG */
  7838. if (ret != 0 && ret != MISSING_RNG_E)
  7839. #elif defined(HAVE_FIPS) || defined(WOLFSSL_ASYNC_CRYPT) || \
  7840. !defined(WC_RSA_BLINDING)
  7841. /* FIPS140 implementation does not do blinding */
  7842. if (ret != 0)
  7843. #else
  7844. if (ret != MISSING_RNG_E)
  7845. #endif
  7846. return -6762;
  7847. sigSz = 0;
  7848. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7849. inLen, out, &sigSz, key, keyLen, rng);
  7850. if (ret != BAD_FUNC_ARG)
  7851. return -6763;
  7852. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  7853. inLen, out, (word32)modLen, key, keyLen);
  7854. if (ret != BAD_FUNC_ARG)
  7855. return -6764;
  7856. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7857. 0, out, (word32)modLen, key, keyLen);
  7858. if (ret != BAD_FUNC_ARG)
  7859. return -6765;
  7860. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7861. inLen, NULL, (word32)modLen, key, keyLen);
  7862. if (ret != BAD_FUNC_ARG)
  7863. return -6766;
  7864. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7865. inLen, out, 0, key, keyLen);
  7866. if (ret != BAD_FUNC_ARG)
  7867. return -6767;
  7868. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7869. inLen, out, (word32)modLen, NULL, keyLen);
  7870. if (ret != BAD_FUNC_ARG)
  7871. return -6768;
  7872. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7873. inLen, out, (word32)modLen, key, 0);
  7874. if (ret != BAD_FUNC_ARG)
  7875. return -6769;
  7876. #ifndef HAVE_ECC
  7877. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  7878. if (ret != SIG_TYPE_E)
  7879. return -6770;
  7880. #endif
  7881. /* Use APIs. */
  7882. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  7883. if (ret != modLen)
  7884. return -6771;
  7885. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  7886. if (ret != modLen)
  7887. return -6772;
  7888. sigSz = (word32)ret;
  7889. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7890. inLen, out, &sigSz, key, keyLen, rng);
  7891. if (ret != 0)
  7892. return -6773;
  7893. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7894. inLen, out, (word32)modLen, key, keyLen);
  7895. if (ret != 0)
  7896. return -6774;
  7897. sigSz = (word32)sizeof(out);
  7898. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  7899. in, inLen, out, &sigSz, key, keyLen, rng);
  7900. if (ret != 0)
  7901. return -6775;
  7902. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  7903. in, inLen, out, (word32)modLen, key, keyLen);
  7904. if (ret != 0)
  7905. return -6776;
  7906. /* Wrong signature type. */
  7907. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  7908. inLen, out, (word32)modLen, key, keyLen);
  7909. if (ret == 0)
  7910. return -6777;
  7911. /* check hash functions */
  7912. sigSz = (word32)sizeof(out);
  7913. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  7914. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  7915. if (ret != 0)
  7916. return -6778;
  7917. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  7918. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  7919. if (ret != 0)
  7920. return -6779;
  7921. sigSz = (word32)sizeof(out);
  7922. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  7923. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  7924. if (ret != 0)
  7925. return -6780;
  7926. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  7927. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  7928. if (ret != 0)
  7929. return -6781;
  7930. return 0;
  7931. }
  7932. #endif /* !NO_SIG_WRAPPER */
  7933. #ifndef HAVE_USER_RSA
  7934. static int rsa_decode_test(RsaKey* keyPub)
  7935. {
  7936. int ret;
  7937. word32 inSz;
  7938. word32 inOutIdx;
  7939. static const byte n[2] = { 0x00, 0x23 };
  7940. static const byte e[2] = { 0x00, 0x03 };
  7941. static const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  7942. 0x03 };
  7943. static const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  7944. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  7945. static const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00,
  7946. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  7947. 0x02, 0x1, 0x03 };
  7948. static const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00,
  7949. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  7950. 0x02, 0x1, 0x03 };
  7951. static const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  7952. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  7953. static const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  7954. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  7955. static const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03,
  7956. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  7957. static const byte badNoObj[] = {
  7958. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  7959. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  7960. static const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1,
  7961. 0x03 };
  7962. static const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1,
  7963. 0x03 };
  7964. static const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1,
  7965. 0x03 };
  7966. static const byte badBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00,
  7967. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  7968. ret = wc_InitRsaKey(keyPub, NULL);
  7969. if (ret != 0)
  7970. return -6782;
  7971. /* Parameter Validation testing. */
  7972. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  7973. if (ret != BAD_FUNC_ARG) {
  7974. ret = -6783;
  7975. goto done;
  7976. }
  7977. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  7978. if (ret != BAD_FUNC_ARG) {
  7979. ret = -6784;
  7980. goto done;
  7981. }
  7982. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  7983. if (ret != BAD_FUNC_ARG) {
  7984. ret = -6785;
  7985. goto done;
  7986. }
  7987. /* TODO: probably should fail when length is -1! */
  7988. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  7989. if (ret != 0) {
  7990. ret = -6786;
  7991. goto done;
  7992. }
  7993. wc_FreeRsaKey(keyPub);
  7994. ret = wc_InitRsaKey(keyPub, NULL);
  7995. if (ret != 0)
  7996. return -6787;
  7997. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  7998. if (ret != 0) {
  7999. ret = -6788;
  8000. goto done;
  8001. }
  8002. wc_FreeRsaKey(keyPub);
  8003. ret = wc_InitRsaKey(keyPub, NULL);
  8004. if (ret != 0)
  8005. return -6789;
  8006. /* Use API. */
  8007. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  8008. if (ret != 0) {
  8009. ret = -6790;
  8010. goto done;
  8011. }
  8012. wc_FreeRsaKey(keyPub);
  8013. ret = wc_InitRsaKey(keyPub, NULL);
  8014. if (ret != 0)
  8015. return -6791;
  8016. /* Parameter Validation testing. */
  8017. inSz = sizeof(good);
  8018. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  8019. if (ret != BAD_FUNC_ARG) {
  8020. ret = -6792;
  8021. goto done;
  8022. }
  8023. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  8024. if (ret != BAD_FUNC_ARG) {
  8025. ret = -6793;
  8026. goto done;
  8027. }
  8028. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  8029. if (ret != BAD_FUNC_ARG) {
  8030. ret = -6794;
  8031. goto done;
  8032. }
  8033. /* Use good data and offest to bad data. */
  8034. inOutIdx = 2;
  8035. inSz = sizeof(good) - inOutIdx;
  8036. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  8037. if (ret != ASN_PARSE_E) {
  8038. ret = -6795;
  8039. goto done;
  8040. }
  8041. inOutIdx = 2;
  8042. inSz = sizeof(goodAlgId) - inOutIdx;
  8043. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  8044. if (ret != ASN_PARSE_E) {
  8045. ret = -6796;
  8046. goto done;
  8047. }
  8048. inOutIdx = 2;
  8049. inSz = sizeof(goodAlgId);
  8050. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  8051. if (ret != ASN_PARSE_E) {
  8052. ret = -6797;
  8053. goto done;
  8054. }
  8055. /* Try different bad data. */
  8056. inSz = sizeof(badAlgIdNull);
  8057. inOutIdx = 0;
  8058. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  8059. if (ret != ASN_EXPECT_0_E) {
  8060. ret = -6798;
  8061. goto done;
  8062. }
  8063. inSz = sizeof(badNotBitString);
  8064. inOutIdx = 0;
  8065. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  8066. if (ret != ASN_BITSTR_E) {
  8067. ret = -6799;
  8068. goto done;
  8069. }
  8070. inSz = sizeof(badBitStringLen);
  8071. inOutIdx = 0;
  8072. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  8073. if (ret != ASN_PARSE_E) {
  8074. ret = -6800;
  8075. goto done;
  8076. }
  8077. inSz = sizeof(badNoSeq);
  8078. inOutIdx = 0;
  8079. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  8080. if (ret != ASN_PARSE_E) {
  8081. ret = -6801;
  8082. goto done;
  8083. }
  8084. inSz = sizeof(badNoObj);
  8085. inOutIdx = 0;
  8086. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  8087. if (ret != ASN_PARSE_E) {
  8088. ret = -6802;
  8089. goto done;
  8090. }
  8091. inSz = sizeof(badIntN);
  8092. inOutIdx = 0;
  8093. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  8094. if (ret != ASN_RSA_KEY_E) {
  8095. ret = -6803;
  8096. goto done;
  8097. }
  8098. inSz = sizeof(badNotIntE);
  8099. inOutIdx = 0;
  8100. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  8101. if (ret != ASN_RSA_KEY_E) {
  8102. ret = -6804;
  8103. goto done;
  8104. }
  8105. /* TODO: Shouldn't pass as the sequence length is too small. */
  8106. inSz = sizeof(badLength);
  8107. inOutIdx = 0;
  8108. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  8109. if (ret != 0) {
  8110. ret = -6805;
  8111. goto done;
  8112. }
  8113. /* TODO: Shouldn't ignore object id's data. */
  8114. wc_FreeRsaKey(keyPub);
  8115. ret = wc_InitRsaKey(keyPub, NULL);
  8116. if (ret != 0)
  8117. return -6806;
  8118. inSz = sizeof(badBitStrNoZero);
  8119. inOutIdx = 0;
  8120. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  8121. if (ret != ASN_EXPECT_0_E) {
  8122. ret = -6807;
  8123. goto done;
  8124. }
  8125. wc_FreeRsaKey(keyPub);
  8126. ret = wc_InitRsaKey(keyPub, NULL);
  8127. if (ret != 0)
  8128. return -6808;
  8129. /* Valid data cases. */
  8130. inSz = sizeof(good);
  8131. inOutIdx = 0;
  8132. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  8133. if (ret != 0) {
  8134. ret = -6809;
  8135. goto done;
  8136. }
  8137. if (inOutIdx != inSz) {
  8138. ret = -6810;
  8139. goto done;
  8140. }
  8141. wc_FreeRsaKey(keyPub);
  8142. ret = wc_InitRsaKey(keyPub, NULL);
  8143. if (ret != 0)
  8144. return -6811;
  8145. inSz = sizeof(goodAlgId);
  8146. inOutIdx = 0;
  8147. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  8148. if (ret != 0) {
  8149. ret = -6812;
  8150. goto done;
  8151. }
  8152. if (inOutIdx != inSz) {
  8153. ret = -6813;
  8154. goto done;
  8155. }
  8156. wc_FreeRsaKey(keyPub);
  8157. ret = wc_InitRsaKey(keyPub, NULL);
  8158. if (ret != 0)
  8159. return -6814;
  8160. inSz = sizeof(goodAlgIdNull);
  8161. inOutIdx = 0;
  8162. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  8163. if (ret != 0) {
  8164. ret = -6815;
  8165. goto done;
  8166. }
  8167. if (inOutIdx != inSz) {
  8168. ret = -6816;
  8169. goto done;
  8170. }
  8171. done:
  8172. wc_FreeRsaKey(keyPub);
  8173. return ret;
  8174. }
  8175. #endif
  8176. #define RSA_TEST_BYTES 256
  8177. #ifdef WC_RSA_PSS
  8178. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  8179. {
  8180. byte digest[WC_MAX_DIGEST_SIZE];
  8181. int ret = 0;
  8182. const char* inStr = "Everyone gets Friday off.";
  8183. word32 inLen = (word32)XSTRLEN((char*)inStr);
  8184. word32 outSz;
  8185. word32 plainSz;
  8186. word32 digestSz;
  8187. int i, j;
  8188. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  8189. int k, l;
  8190. #endif
  8191. byte* plain;
  8192. int mgf[] = {
  8193. #ifndef NO_SHA
  8194. WC_MGF1SHA1,
  8195. #endif
  8196. #ifdef WOLFSSL_SHA224
  8197. WC_MGF1SHA224,
  8198. #endif
  8199. WC_MGF1SHA256,
  8200. #ifdef WOLFSSL_SHA384
  8201. WC_MGF1SHA384,
  8202. #endif
  8203. #ifdef WOLFSSL_SHA512
  8204. WC_MGF1SHA512
  8205. #endif
  8206. };
  8207. enum wc_HashType hash[] = {
  8208. #ifndef NO_SHA
  8209. WC_HASH_TYPE_SHA,
  8210. #endif
  8211. #ifdef WOLFSSL_SHA224
  8212. WC_HASH_TYPE_SHA224,
  8213. #endif
  8214. WC_HASH_TYPE_SHA256,
  8215. #ifdef WOLFSSL_SHA384
  8216. WC_HASH_TYPE_SHA384,
  8217. #endif
  8218. #ifdef WOLFSSL_SHA512
  8219. WC_HASH_TYPE_SHA512,
  8220. #endif
  8221. };
  8222. DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT);
  8223. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  8224. DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  8225. /* Test all combinations of hash and MGF. */
  8226. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  8227. /* Calculate hash of message. */
  8228. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  8229. if (ret != 0)
  8230. ERROR_OUT(-6817, exit_rsa_pss);
  8231. digestSz = wc_HashGetDigestSize(hash[j]);
  8232. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  8233. outSz = RSA_TEST_BYTES;
  8234. do {
  8235. #if defined(WOLFSSL_ASYNC_CRYPT)
  8236. ret = wc_AsyncWait(ret, &key->asyncDev,
  8237. WC_ASYNC_FLAG_CALL_AGAIN);
  8238. #endif
  8239. if (ret >= 0) {
  8240. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  8241. hash[j], mgf[i], -1, key, rng);
  8242. }
  8243. } while (ret == WC_PENDING_E);
  8244. if (ret <= 0)
  8245. ERROR_OUT(-6818, exit_rsa_pss);
  8246. outSz = ret;
  8247. XMEMCPY(sig, out, outSz);
  8248. plain = NULL;
  8249. do {
  8250. #if defined(WOLFSSL_ASYNC_CRYPT)
  8251. ret = wc_AsyncWait(ret, &key->asyncDev,
  8252. WC_ASYNC_FLAG_CALL_AGAIN);
  8253. #endif
  8254. if (ret >= 0) {
  8255. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  8256. mgf[i], -1, key);
  8257. }
  8258. } while (ret == WC_PENDING_E);
  8259. if (ret <= 0)
  8260. ERROR_OUT(-6819, exit_rsa_pss);
  8261. plainSz = ret;
  8262. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  8263. hash[j], -1, wc_RsaEncryptSize(key)*8);
  8264. if (ret != 0)
  8265. ERROR_OUT(-6820, exit_rsa_pss);
  8266. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  8267. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  8268. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  8269. if (i == k && j == l)
  8270. continue;
  8271. XMEMCPY(sig, out, outSz);
  8272. do {
  8273. #if defined(WOLFSSL_ASYNC_CRYPT)
  8274. ret = wc_AsyncWait(ret, &key->asyncDev,
  8275. WC_ASYNC_FLAG_CALL_AGAIN);
  8276. #endif
  8277. if (ret >= 0) {
  8278. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  8279. (byte**)&plain, hash[l], mgf[k], -1, key);
  8280. }
  8281. } while (ret == WC_PENDING_E);
  8282. if (ret >= 0)
  8283. ERROR_OUT(-6821, exit_rsa_pss);
  8284. }
  8285. }
  8286. #endif
  8287. }
  8288. }
  8289. /* Test that a salt length of zero works. */
  8290. digestSz = wc_HashGetDigestSize(hash[0]);
  8291. outSz = RSA_TEST_BYTES;
  8292. do {
  8293. #if defined(WOLFSSL_ASYNC_CRYPT)
  8294. ret = wc_AsyncWait(ret, &key->asyncDev,
  8295. WC_ASYNC_FLAG_CALL_AGAIN);
  8296. #endif
  8297. if (ret >= 0) {
  8298. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  8299. mgf[0], 0, key, rng);
  8300. }
  8301. } while (ret == WC_PENDING_E);
  8302. if (ret <= 0)
  8303. ERROR_OUT(-6822, exit_rsa_pss);
  8304. outSz = ret;
  8305. do {
  8306. #if defined(WOLFSSL_ASYNC_CRYPT)
  8307. ret = wc_AsyncWait(ret, &key->asyncDev,
  8308. WC_ASYNC_FLAG_CALL_AGAIN);
  8309. #endif
  8310. if (ret >= 0) {
  8311. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  8312. 0, key);
  8313. }
  8314. } while (ret == WC_PENDING_E);
  8315. if (ret <= 0)
  8316. ERROR_OUT(-6823, exit_rsa_pss);
  8317. plainSz = ret;
  8318. do {
  8319. #if defined(WOLFSSL_ASYNC_CRYPT)
  8320. ret = wc_AsyncWait(ret, &key->asyncDev,
  8321. WC_ASYNC_FLAG_CALL_AGAIN);
  8322. #endif
  8323. if (ret >= 0) {
  8324. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  8325. hash[0], 0, 0);
  8326. }
  8327. } while (ret == WC_PENDING_E);
  8328. if (ret != 0)
  8329. ERROR_OUT(-6824, exit_rsa_pss);
  8330. XMEMCPY(sig, out, outSz);
  8331. plain = NULL;
  8332. do {
  8333. #if defined(WOLFSSL_ASYNC_CRYPT)
  8334. ret = wc_AsyncWait(ret, &key->asyncDev,
  8335. WC_ASYNC_FLAG_CALL_AGAIN);
  8336. #endif
  8337. if (ret >= 0) {
  8338. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  8339. 0, key);
  8340. }
  8341. } while (ret == WC_PENDING_E);
  8342. if (ret <= 0)
  8343. ERROR_OUT(-6825, exit_rsa_pss);
  8344. plainSz = ret;
  8345. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  8346. 0, 0);
  8347. if (ret != 0)
  8348. ERROR_OUT(-6826, exit_rsa_pss);
  8349. /* Test bad salt lengths in various APIs. */
  8350. digestSz = wc_HashGetDigestSize(hash[0]);
  8351. outSz = RSA_TEST_BYTES;
  8352. do {
  8353. #if defined(WOLFSSL_ASYNC_CRYPT)
  8354. ret = wc_AsyncWait(ret, &key->asyncDev,
  8355. WC_ASYNC_FLAG_CALL_AGAIN);
  8356. #endif
  8357. if (ret >= 0) {
  8358. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  8359. mgf[0], -2, key, rng);
  8360. }
  8361. } while (ret == WC_PENDING_E);
  8362. if (ret != PSS_SALTLEN_E)
  8363. ERROR_OUT(-6827, exit_rsa_pss);
  8364. do {
  8365. #if defined(WOLFSSL_ASYNC_CRYPT)
  8366. ret = wc_AsyncWait(ret, &key->asyncDev,
  8367. WC_ASYNC_FLAG_CALL_AGAIN);
  8368. #endif
  8369. if (ret >= 0) {
  8370. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  8371. mgf[0], digestSz + 1, key, rng);
  8372. }
  8373. } while (ret == WC_PENDING_E);
  8374. if (ret != PSS_SALTLEN_E)
  8375. ERROR_OUT(-6828, exit_rsa_pss);
  8376. do {
  8377. #if defined(WOLFSSL_ASYNC_CRYPT)
  8378. ret = wc_AsyncWait(ret, &key->asyncDev,
  8379. WC_ASYNC_FLAG_CALL_AGAIN);
  8380. #endif
  8381. if (ret >= 0) {
  8382. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  8383. mgf[0], -2, key);
  8384. }
  8385. } while (ret == WC_PENDING_E);
  8386. if (ret != PSS_SALTLEN_E)
  8387. ERROR_OUT(-6829, exit_rsa_pss);
  8388. do {
  8389. #if defined(WOLFSSL_ASYNC_CRYPT)
  8390. ret = wc_AsyncWait(ret, &key->asyncDev,
  8391. WC_ASYNC_FLAG_CALL_AGAIN);
  8392. #endif
  8393. if (ret >= 0) {
  8394. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  8395. digestSz + 1, key);
  8396. }
  8397. } while (ret == WC_PENDING_E);
  8398. if (ret != PSS_SALTLEN_E)
  8399. ERROR_OUT(-6830, exit_rsa_pss);
  8400. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  8401. -2, 0);
  8402. if (ret != PSS_SALTLEN_E)
  8403. ERROR_OUT(-6831, exit_rsa_pss);
  8404. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz, hash[0],
  8405. digestSz + 1, 0);
  8406. if (ret != PSS_SALTLEN_E)
  8407. ERROR_OUT(-6832, exit_rsa_pss);
  8408. ret = 0;
  8409. exit_rsa_pss:
  8410. FREE_VAR(in, HEAP_HINT);
  8411. FREE_VAR(out, HEAP_HINT);
  8412. return ret;
  8413. }
  8414. #endif
  8415. #ifdef WC_RSA_NO_PADDING
  8416. int rsa_no_pad_test(void)
  8417. {
  8418. WC_RNG rng;
  8419. RsaKey key;
  8420. byte* tmp = NULL;
  8421. size_t bytes;
  8422. int ret;
  8423. word32 inLen = 0;
  8424. word32 idx = 0;
  8425. word32 outSz = RSA_TEST_BYTES;
  8426. word32 plainSz = RSA_TEST_BYTES;
  8427. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) \
  8428. && !defined(NO_FILESYSTEM)
  8429. FILE *file;
  8430. #endif
  8431. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  8432. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  8433. /* initialize stack structures */
  8434. XMEMSET(&rng, 0, sizeof(rng));
  8435. XMEMSET(&key, 0, sizeof(key));
  8436. #ifdef USE_CERT_BUFFERS_1024
  8437. bytes = (size_t)sizeof_client_key_der_1024;
  8438. if (bytes < (size_t)sizeof_client_cert_der_1024)
  8439. bytes = (size_t)sizeof_client_cert_der_1024;
  8440. #elif defined(USE_CERT_BUFFERS_2048)
  8441. bytes = (size_t)sizeof_client_key_der_2048;
  8442. if (bytes < (size_t)sizeof_client_cert_der_2048)
  8443. bytes = (size_t)sizeof_client_cert_der_2048;
  8444. #else
  8445. bytes = FOURK_BUF;
  8446. #endif
  8447. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8448. if (tmp == NULL
  8449. #ifdef WOLFSSL_ASYNC_CRYPT
  8450. || out == NULL || plain == NULL
  8451. #endif
  8452. ) {
  8453. return -6900;
  8454. }
  8455. #ifdef USE_CERT_BUFFERS_1024
  8456. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  8457. #elif defined(USE_CERT_BUFFERS_2048)
  8458. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  8459. #elif !defined(NO_FILESYSTEM)
  8460. file = fopen(clientKey, "rb");
  8461. if (!file) {
  8462. err_sys("can't open ./certs/client-key.der, "
  8463. "Please run from wolfSSL home dir", -40);
  8464. ERROR_OUT(-6901, exit_rsa_nopadding);
  8465. }
  8466. bytes = fread(tmp, 1, FOURK_BUF, file);
  8467. fclose(file);
  8468. #else
  8469. /* No key to use. */
  8470. ERROR_OUT(-6902, exit_rsa_nopadding);
  8471. #endif /* USE_CERT_BUFFERS */
  8472. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, devId);
  8473. if (ret != 0) {
  8474. ERROR_OUT(-6903, exit_rsa_nopadding);
  8475. }
  8476. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  8477. if (ret != 0) {
  8478. ERROR_OUT(-6904, exit_rsa_nopadding);
  8479. }
  8480. /* after loading in key use tmp as the test buffer */
  8481. #ifndef HAVE_FIPS
  8482. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  8483. #else
  8484. ret = wc_InitRng(&rng);
  8485. #endif
  8486. if (ret != 0) {
  8487. ERROR_OUT(-6905, exit_rsa_nopadding);
  8488. }
  8489. inLen = wc_RsaEncryptSize(&key);
  8490. XMEMSET(tmp, 7, inLen);
  8491. do {
  8492. #if defined(WOLFSSL_ASYNC_CRYPT)
  8493. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8494. #endif
  8495. if (ret >= 0) {
  8496. ret = wc_RsaDirect(tmp, inLen, out, &outSz, &key,
  8497. RSA_PRIVATE_ENCRYPT, &rng);
  8498. }
  8499. } while (ret == WC_PENDING_E);
  8500. if (ret <= 0) {
  8501. ERROR_OUT(-6906, exit_rsa_nopadding);
  8502. }
  8503. /* encrypted result should not be the same as input */
  8504. if (XMEMCMP(out, tmp, inLen) == 0) {
  8505. ERROR_OUT(-6907, exit_rsa_nopadding);
  8506. }
  8507. /* decrypt with public key and compare result */
  8508. do {
  8509. #if defined(WOLFSSL_ASYNC_CRYPT)
  8510. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8511. #endif
  8512. if (ret >= 0) {
  8513. ret = wc_RsaDirect(out, outSz, plain, &plainSz, &key,
  8514. RSA_PUBLIC_DECRYPT, &rng);
  8515. }
  8516. } while (ret == WC_PENDING_E);
  8517. if (ret <= 0) {
  8518. ERROR_OUT(-6908, exit_rsa_nopadding);
  8519. }
  8520. if (XMEMCMP(plain, tmp, inLen) != 0) {
  8521. ERROR_OUT(-6909, exit_rsa_nopadding);
  8522. }
  8523. #ifdef WC_RSA_BLINDING
  8524. ret = wc_RsaSetRNG(NULL, &rng);
  8525. if (ret != BAD_FUNC_ARG) {
  8526. ERROR_OUT(-6910, exit_rsa_nopadding);
  8527. }
  8528. ret = wc_RsaSetRNG(&key, &rng);
  8529. if (ret < 0) {
  8530. ERROR_OUT(-6911, exit_rsa_nopadding);
  8531. }
  8532. #endif
  8533. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  8534. do {
  8535. #if defined(WOLFSSL_ASYNC_CRYPT)
  8536. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8537. #endif
  8538. if (ret >= 0) {
  8539. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, &key, &rng,
  8540. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  8541. }
  8542. } while (ret == WC_PENDING_E);
  8543. if (ret < 0) {
  8544. ERROR_OUT(-6912, exit_rsa_nopadding);
  8545. }
  8546. do {
  8547. #if defined(WOLFSSL_ASYNC_CRYPT)
  8548. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8549. #endif
  8550. if (ret >= 0) {
  8551. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, &key,
  8552. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  8553. }
  8554. } while (ret == WC_PENDING_E);
  8555. if (ret < 0) {
  8556. ERROR_OUT(-6913, exit_rsa_nopadding);
  8557. }
  8558. if (XMEMCMP(plain, tmp, inLen) != 0) {
  8559. ERROR_OUT(-6914, exit_rsa_nopadding);
  8560. }
  8561. /* test some bad arguments */
  8562. ret = wc_RsaDirect(out, outSz, plain, &plainSz, &key, -1,
  8563. &rng);
  8564. if (ret != BAD_FUNC_ARG) {
  8565. ERROR_OUT(-6915, exit_rsa_nopadding);
  8566. }
  8567. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  8568. &rng);
  8569. if (ret != BAD_FUNC_ARG) {
  8570. ERROR_OUT(-6916, exit_rsa_nopadding);
  8571. }
  8572. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, &key, RSA_PUBLIC_DECRYPT,
  8573. &rng);
  8574. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  8575. ERROR_OUT(-6917, exit_rsa_nopadding);
  8576. }
  8577. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, &key,
  8578. RSA_PUBLIC_DECRYPT, &rng);
  8579. if (ret != BAD_FUNC_ARG) {
  8580. ERROR_OUT(-6918, exit_rsa_nopadding);
  8581. }
  8582. /* if making it to this point of code without hitting an ERROR_OUT then
  8583. * all tests have passed */
  8584. ret = 0;
  8585. exit_rsa_nopadding:
  8586. wc_FreeRsaKey(&key);
  8587. wc_FreeRng(&rng);
  8588. return ret;
  8589. }
  8590. #endif /* WC_RSA_NO_PADDING */
  8591. #ifdef WOLFSSL_CERT_GEN
  8592. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  8593. {
  8594. RsaKey caKey;
  8595. byte* der = NULL;
  8596. byte* pem = NULL;
  8597. int ret;
  8598. Cert* myCert = NULL;
  8599. int certSz;
  8600. size_t bytes3;
  8601. word32 idx3 = 0;
  8602. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  8603. FILE* file3;
  8604. #endif
  8605. #ifdef WOLFSSL_TEST_CERT
  8606. DecodedCert decode;
  8607. #endif
  8608. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  8609. struct tm beforeTime;
  8610. struct tm afterTime;
  8611. #endif
  8612. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  8613. (void)keypub;
  8614. XMEMSET(&caKey, 0, sizeof(caKey));
  8615. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8616. if (der == NULL) {
  8617. ERROR_OUT(-6919, exit_rsa);
  8618. }
  8619. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  8620. if (pem == NULL) {
  8621. ERROR_OUT(-6920, exit_rsa);
  8622. }
  8623. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8624. if (myCert == NULL) {
  8625. ERROR_OUT(-6921, exit_rsa);
  8626. }
  8627. /* self signed */
  8628. if (wc_InitCert(myCert)) {
  8629. ERROR_OUT(-6922, exit_rsa);
  8630. }
  8631. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  8632. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  8633. myCert->serialSz = (int)sizeof(mySerial);
  8634. myCert->isCA = 1;
  8635. #ifndef NO_SHA256
  8636. myCert->sigType = CTC_SHA256wRSA;
  8637. #else
  8638. myCert->sigType = CTC_SHAwRSA;
  8639. #endif
  8640. #ifdef WOLFSSL_CERT_EXT
  8641. /* add Policies */
  8642. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  8643. CTC_MAX_CERTPOL_SZ);
  8644. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  8645. CTC_MAX_CERTPOL_SZ);
  8646. myCert->certPoliciesNb = 2;
  8647. /* add SKID from the Public Key */
  8648. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  8649. ERROR_OUT(-6923, exit_rsa);
  8650. }
  8651. /* add AKID from the Public Key */
  8652. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  8653. ERROR_OUT(-6924, exit_rsa);
  8654. }
  8655. /* add Key Usage */
  8656. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  8657. ERROR_OUT(-6925, exit_rsa);
  8658. }
  8659. #ifdef WOLFSSL_EKU_OID
  8660. {
  8661. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  8662. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  8663. HEAP_HINT) != 0) {
  8664. ERROR_OUT(-6926, exit_rsa);
  8665. }
  8666. }
  8667. #endif /* WOLFSSL_EKU_OID */
  8668. #endif /* WOLFSSL_CERT_EXT */
  8669. ret = 0;
  8670. do {
  8671. #if defined(WOLFSSL_ASYNC_CRYPT)
  8672. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8673. #endif
  8674. if (ret >= 0) {
  8675. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  8676. }
  8677. } while (ret == WC_PENDING_E);
  8678. if (ret < 0) {
  8679. ERROR_OUT(-6927, exit_rsa);
  8680. }
  8681. certSz = ret;
  8682. #ifdef WOLFSSL_TEST_CERT
  8683. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  8684. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  8685. if (ret != 0) {
  8686. FreeDecodedCert(&decode);
  8687. ERROR_OUT(-6928, exit_rsa);
  8688. }
  8689. FreeDecodedCert(&decode);
  8690. #endif
  8691. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, certDerFile,
  8692. certPemFile, CERT_TYPE, -5578);
  8693. if (ret != 0) {
  8694. goto exit_rsa;
  8695. }
  8696. /* Setup Certificate */
  8697. if (wc_InitCert(myCert)) {
  8698. ERROR_OUT(-6929, exit_rsa);
  8699. }
  8700. #ifdef WOLFSSL_ALT_NAMES
  8701. /* Get CA Cert for testing */
  8702. #ifdef USE_CERT_BUFFERS_1024
  8703. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  8704. bytes3 = sizeof_ca_cert_der_1024;
  8705. #elif defined(USE_CERT_BUFFERS_2048)
  8706. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  8707. bytes3 = sizeof_ca_cert_der_2048;
  8708. #else
  8709. file3 = fopen(rsaCaCertDerFile, "rb");
  8710. if (!file3) {
  8711. ERROR_OUT(-6930, exit_rsa);
  8712. }
  8713. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  8714. fclose(file3);
  8715. #endif /* USE_CERT_BUFFERS */
  8716. #ifndef NO_FILESYSTEM
  8717. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  8718. if (ret != 0) {
  8719. ERROR_OUT(-6931, exit_rsa);
  8720. }
  8721. #endif
  8722. /* get alt names from der */
  8723. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  8724. if (ret != 0) {
  8725. ERROR_OUT(-6932, exit_rsa);
  8726. }
  8727. /* get dates from der */
  8728. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  8729. if (ret != 0) {
  8730. ERROR_OUT(-6933, exit_rsa);
  8731. }
  8732. #ifndef NO_ASN_TIME
  8733. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  8734. if (ret < 0) {
  8735. ERROR_OUT(-6934, exit_rsa);
  8736. }
  8737. #endif
  8738. #endif /* WOLFSSL_ALT_NAMES */
  8739. /* Get CA Key */
  8740. #ifdef USE_CERT_BUFFERS_1024
  8741. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  8742. bytes3 = sizeof_ca_key_der_1024;
  8743. #elif defined(USE_CERT_BUFFERS_2048)
  8744. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  8745. bytes3 = sizeof_ca_key_der_2048;
  8746. #else
  8747. file3 = fopen(rsaCaKeyFile, "rb");
  8748. if (!file3) {
  8749. ERROR_OUT(-6935, exit_rsa);
  8750. }
  8751. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  8752. fclose(file3);
  8753. #endif /* USE_CERT_BUFFERS */
  8754. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  8755. if (ret != 0) {
  8756. ERROR_OUT(-6936, exit_rsa);
  8757. }
  8758. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  8759. if (ret != 0) {
  8760. ERROR_OUT(-6937, exit_rsa);
  8761. }
  8762. #ifndef NO_SHA256
  8763. myCert->sigType = CTC_SHA256wRSA;
  8764. #else
  8765. myCert->sigType = CTC_SHAwRSA;
  8766. #endif
  8767. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  8768. #ifdef WOLFSSL_CERT_EXT
  8769. /* add Policies */
  8770. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  8771. CTC_MAX_CERTPOL_SZ);
  8772. myCert->certPoliciesNb =1;
  8773. /* add SKID from the Public Key */
  8774. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  8775. ERROR_OUT(-6938, exit_rsa);
  8776. }
  8777. /* add AKID from the CA certificate */
  8778. #if defined(USE_CERT_BUFFERS_2048)
  8779. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  8780. sizeof_ca_cert_der_2048);
  8781. #elif defined(USE_CERT_BUFFERS_1024)
  8782. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  8783. sizeof_ca_cert_der_1024);
  8784. #else
  8785. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  8786. #endif
  8787. if (ret != 0) {
  8788. ERROR_OUT(-6939, exit_rsa);
  8789. }
  8790. /* add Key Usage */
  8791. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  8792. ERROR_OUT(-6940, exit_rsa);
  8793. }
  8794. #endif /* WOLFSSL_CERT_EXT */
  8795. #if defined(USE_CERT_BUFFERS_2048)
  8796. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  8797. sizeof_ca_cert_der_2048);
  8798. #elif defined(USE_CERT_BUFFERS_1024)
  8799. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  8800. sizeof_ca_cert_der_1024);
  8801. #else
  8802. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  8803. #endif
  8804. if (ret < 0) {
  8805. ERROR_OUT(-6941, exit_rsa);
  8806. }
  8807. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  8808. if (certSz < 0) {
  8809. ERROR_OUT(-6942, exit_rsa);
  8810. }
  8811. ret = 0;
  8812. do {
  8813. #if defined(WOLFSSL_ASYNC_CRYPT)
  8814. ret = wc_AsyncWait(ret, &caKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8815. #endif
  8816. if (ret >= 0) {
  8817. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  8818. &caKey, NULL, rng);
  8819. }
  8820. } while (ret == WC_PENDING_E);
  8821. if (ret < 0) {
  8822. ERROR_OUT(-6943, exit_rsa);
  8823. }
  8824. certSz = ret;
  8825. #ifdef WOLFSSL_TEST_CERT
  8826. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  8827. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  8828. if (ret != 0) {
  8829. FreeDecodedCert(&decode);
  8830. ERROR_OUT(-6944, exit_rsa);
  8831. }
  8832. FreeDecodedCert(&decode);
  8833. #endif
  8834. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, otherCertDerFile,
  8835. otherCertPemFile, CERT_TYPE, -5598);
  8836. if (ret != 0) {
  8837. goto exit_rsa;
  8838. }
  8839. exit_rsa:
  8840. wc_FreeRsaKey(&caKey);
  8841. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8842. myCert = NULL;
  8843. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8844. pem = NULL;
  8845. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8846. der = NULL;
  8847. return ret;
  8848. }
  8849. #endif
  8850. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  8851. /* Make Cert / Sign example for ECC cert and RSA CA */
  8852. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  8853. {
  8854. RsaKey caKey;
  8855. ecc_key caEccKey;
  8856. ecc_key caEccKeyPub;
  8857. byte* der = NULL;
  8858. byte* pem = NULL;
  8859. Cert* myCert = NULL;
  8860. int certSz;
  8861. size_t bytes3;
  8862. word32 idx3 = 0;
  8863. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  8864. || !defined(USE_CERT_BUFFERS_256)
  8865. FILE* file3;
  8866. #endif
  8867. #ifdef WOLFSSL_TEST_CERT
  8868. DecodedCert decode;
  8869. #endif
  8870. int ret;
  8871. XMEMSET(&caKey, 0, sizeof(caKey));
  8872. XMEMSET(&caEccKey, 0, sizeof(caEccKey));
  8873. XMEMSET(&caEccKeyPub, 0, sizeof(caEccKeyPub));
  8874. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8875. if (der == NULL) {
  8876. ERROR_OUT(-6945, exit_rsa);
  8877. }
  8878. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8879. if (pem == NULL) {
  8880. ERROR_OUT(-6946, exit_rsa);
  8881. }
  8882. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8883. if (myCert == NULL) {
  8884. ERROR_OUT(-6947, exit_rsa);
  8885. }
  8886. /* Get CA Key */
  8887. #ifdef USE_CERT_BUFFERS_1024
  8888. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  8889. bytes3 = sizeof_ca_key_der_1024;
  8890. #elif defined(USE_CERT_BUFFERS_2048)
  8891. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  8892. bytes3 = sizeof_ca_key_der_2048;
  8893. #else
  8894. file3 = fopen(rsaCaKeyFile, "rb");
  8895. if (!file3) {
  8896. ERROR_OUT(-6948, exit_rsa);
  8897. }
  8898. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  8899. fclose(file3);
  8900. #endif /* USE_CERT_BUFFERS */
  8901. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  8902. if (ret != 0) {
  8903. ERROR_OUT(-6949, exit_rsa);
  8904. }
  8905. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  8906. if (ret != 0) {
  8907. ERROR_OUT(-6950, exit_rsa);
  8908. }
  8909. /* Get Cert Key */
  8910. #ifdef USE_CERT_BUFFERS_256
  8911. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  8912. bytes3 = sizeof_ecc_key_pub_der_256;
  8913. #else
  8914. file3 = fopen(eccKeyPubFile, "rb");
  8915. if (!file3) {
  8916. ERROR_OUT(-6951, exit_rsa);
  8917. }
  8918. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  8919. fclose(file3);
  8920. #endif
  8921. ret = wc_ecc_init_ex(&caEccKeyPub, HEAP_HINT, devId);
  8922. if (ret != 0) {
  8923. ERROR_OUT(-6952, exit_rsa);
  8924. }
  8925. idx3 = 0;
  8926. ret = wc_EccPublicKeyDecode(tmp, &idx3, &caEccKeyPub, (word32)bytes3);
  8927. if (ret != 0) {
  8928. ERROR_OUT(-6953, exit_rsa);
  8929. }
  8930. /* Setup Certificate */
  8931. if (wc_InitCert(myCert)) {
  8932. ERROR_OUT(-6954, exit_rsa);
  8933. }
  8934. #ifndef NO_SHA256
  8935. myCert->sigType = CTC_SHA256wRSA;
  8936. #else
  8937. myCert->sigType = CTC_SHAwRSA;
  8938. #endif
  8939. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  8940. #ifdef WOLFSSL_CERT_EXT
  8941. /* add Policies */
  8942. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  8943. CTC_MAX_CERTPOL_SZ);
  8944. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  8945. CTC_MAX_CERTPOL_SZ);
  8946. myCert->certPoliciesNb = 2;
  8947. /* add SKID from the Public Key */
  8948. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, &caEccKeyPub) != 0) {
  8949. ERROR_OUT(-6955, exit_rsa);
  8950. }
  8951. /* add AKID from the CA certificate */
  8952. #if defined(USE_CERT_BUFFERS_2048)
  8953. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  8954. sizeof_ca_cert_der_2048);
  8955. #elif defined(USE_CERT_BUFFERS_1024)
  8956. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  8957. sizeof_ca_cert_der_1024);
  8958. #else
  8959. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  8960. #endif
  8961. if (ret != 0) {
  8962. ERROR_OUT(-6956, exit_rsa);
  8963. }
  8964. /* add Key Usage */
  8965. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  8966. ERROR_OUT(-6957, exit_rsa);
  8967. }
  8968. #endif /* WOLFSSL_CERT_EXT */
  8969. #if defined(USE_CERT_BUFFERS_2048)
  8970. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  8971. sizeof_ca_cert_der_2048);
  8972. #elif defined(USE_CERT_BUFFERS_1024)
  8973. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  8974. sizeof_ca_cert_der_1024);
  8975. #else
  8976. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  8977. #endif
  8978. if (ret < 0) {
  8979. ERROR_OUT(-6958, exit_rsa);
  8980. }
  8981. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, &caEccKeyPub, rng);
  8982. if (certSz < 0) {
  8983. ERROR_OUT(-6959, exit_rsa);
  8984. }
  8985. ret = 0;
  8986. do {
  8987. #if defined(WOLFSSL_ASYNC_CRYPT)
  8988. ret = wc_AsyncWait(ret, &caEccKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  8989. #endif
  8990. if (ret >= 0) {
  8991. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  8992. FOURK_BUF, &caKey, NULL, rng);
  8993. }
  8994. } while (ret == WC_PENDING_E);
  8995. if (ret < 0) {
  8996. ERROR_OUT(-6960, exit_rsa);
  8997. }
  8998. certSz = ret;
  8999. #ifdef WOLFSSL_TEST_CERT
  9000. InitDecodedCert(&decode, der, certSz, 0);
  9001. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  9002. if (ret != 0) {
  9003. FreeDecodedCert(&decode);
  9004. ERROR_OUT(-6961, exit_rsa);
  9005. }
  9006. FreeDecodedCert(&decode);
  9007. #endif
  9008. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, certEccRsaDerFile,
  9009. certEccRsaPemFile, CERT_TYPE, -5616);
  9010. if (ret != 0) {
  9011. goto exit_rsa;
  9012. }
  9013. exit_rsa:
  9014. wc_FreeRsaKey(&caKey);
  9015. wc_ecc_free(&caEccKey);
  9016. wc_ecc_free(&caEccKeyPub);
  9017. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9018. myCert = NULL;
  9019. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9020. pem = NULL;
  9021. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9022. der = NULL;
  9023. if (ret >= 0)
  9024. ret = 0;
  9025. return ret;
  9026. }
  9027. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  9028. #ifdef WOLFSSL_KEY_GEN
  9029. static int rsa_keygen_test(WC_RNG* rng)
  9030. {
  9031. RsaKey genKey;
  9032. int ret;
  9033. byte* der = NULL;
  9034. byte* pem = NULL;
  9035. word32 idx = 0;
  9036. int derSz = 0;
  9037. int keySz = 1024;
  9038. XMEMSET(&genKey, 0, sizeof(genKey));
  9039. #ifdef HAVE_FIPS
  9040. keySz = 2048;
  9041. #endif /* HAVE_FIPS */
  9042. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  9043. if (ret != 0) {
  9044. ERROR_OUT(-6962, exit_rsa);
  9045. }
  9046. ret = wc_MakeRsaKey(&genKey, keySz, WC_RSA_EXPONENT, rng);
  9047. if (ret != 0) {
  9048. ERROR_OUT(-6963, exit_rsa);
  9049. }
  9050. /* If not using old FIPS, or not using FAST or USER RSA... */
  9051. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  9052. (!defined(HAVE_FIPS) || \
  9053. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  9054. ret = wc_CheckRsaKey(&genKey);
  9055. if (ret != 0) {
  9056. ERROR_OUT(-8228, exit_rsa);
  9057. }
  9058. #endif
  9059. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9060. if (der == NULL) {
  9061. ERROR_OUT(-6964, exit_rsa);
  9062. }
  9063. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9064. if (pem == NULL) {
  9065. ERROR_OUT(-6965, exit_rsa);
  9066. }
  9067. derSz = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  9068. if (derSz < 0) {
  9069. ERROR_OUT(-6966, exit_rsa);
  9070. }
  9071. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, keyDerFile, keyPemFile,
  9072. PRIVATEKEY_TYPE, -5555);
  9073. if (ret != 0) {
  9074. goto exit_rsa;
  9075. }
  9076. wc_FreeRsaKey(&genKey);
  9077. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  9078. if (ret != 0) {
  9079. ERROR_OUT(-6967, exit_rsa);
  9080. }
  9081. idx = 0;
  9082. ret = wc_RsaPrivateKeyDecode(der, &idx, &genKey, derSz);
  9083. if (ret != 0) {
  9084. ERROR_OUT(-6968, exit_rsa);
  9085. }
  9086. wc_FreeRsaKey(&genKey);
  9087. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9088. pem = NULL;
  9089. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9090. der = NULL;
  9091. exit_rsa:
  9092. wc_FreeRsaKey(&genKey);
  9093. return ret;
  9094. }
  9095. #endif
  9096. int rsa_test(void)
  9097. {
  9098. int ret;
  9099. byte* tmp = NULL;
  9100. byte* der = NULL;
  9101. byte* pem = NULL;
  9102. size_t bytes;
  9103. WC_RNG rng;
  9104. RsaKey key;
  9105. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  9106. RsaKey keypub;
  9107. #endif
  9108. #if defined(HAVE_NTRU)
  9109. RsaKey caKey;
  9110. #endif
  9111. word32 idx = 0;
  9112. byte* res;
  9113. const char* inStr = "Everyone gets Friday off.";
  9114. word32 inLen = (word32)XSTRLEN((char*)inStr);
  9115. const word32 outSz = RSA_TEST_BYTES;
  9116. const word32 plainSz = RSA_TEST_BYTES;
  9117. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) \
  9118. && !defined(NO_FILESYSTEM)
  9119. FILE *file, *file2;
  9120. #endif
  9121. #ifdef WOLFSSL_TEST_CERT
  9122. DecodedCert cert;
  9123. #endif
  9124. DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT);
  9125. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  9126. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  9127. #ifdef WOLFSSL_ASYNC_CRYPT
  9128. if (in == NULL)
  9129. return MEMORY_E;
  9130. #endif
  9131. /* initialize stack structures */
  9132. XMEMSET(&rng, 0, sizeof(rng));
  9133. XMEMSET(&key, 0, sizeof(key));
  9134. #ifdef WOLFSSL_CERT_EXT
  9135. XMEMSET(&keypub, 0, sizeof(keypub));
  9136. #endif
  9137. #if defined(HAVE_NTRU)
  9138. XMEMSET(&caKey, 0, sizeof(caKey));
  9139. #endif
  9140. #ifndef HAVE_USER_RSA
  9141. ret = rsa_decode_test(&key);
  9142. if (ret != 0)
  9143. return ret;
  9144. #endif
  9145. #ifdef USE_CERT_BUFFERS_1024
  9146. bytes = (size_t)sizeof_client_key_der_1024;
  9147. if (bytes < (size_t)sizeof_client_cert_der_1024)
  9148. bytes = (size_t)sizeof_client_cert_der_1024;
  9149. #elif defined(USE_CERT_BUFFERS_2048)
  9150. bytes = (size_t)sizeof_client_key_der_2048;
  9151. if (bytes < (size_t)sizeof_client_cert_der_2048)
  9152. bytes = (size_t)sizeof_client_cert_der_2048;
  9153. #else
  9154. bytes = FOURK_BUF;
  9155. #endif
  9156. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9157. if (tmp == NULL
  9158. #ifdef WOLFSSL_ASYNC_CRYPT
  9159. || out == NULL || plain == NULL
  9160. #endif
  9161. ) {
  9162. return -7000;
  9163. }
  9164. #ifdef USE_CERT_BUFFERS_1024
  9165. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  9166. #elif defined(USE_CERT_BUFFERS_2048)
  9167. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  9168. #elif !defined(NO_FILESYSTEM)
  9169. file = fopen(clientKey, "rb");
  9170. if (!file) {
  9171. err_sys("can't open ./certs/client-key.der, "
  9172. "Please run from wolfSSL home dir", -40);
  9173. ERROR_OUT(-7001, exit_rsa);
  9174. }
  9175. bytes = fread(tmp, 1, FOURK_BUF, file);
  9176. fclose(file);
  9177. #else
  9178. /* No key to use. */
  9179. ERROR_OUT(-7002, exit_rsa);
  9180. #endif /* USE_CERT_BUFFERS */
  9181. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, devId);
  9182. if (ret != 0) {
  9183. ERROR_OUT(-7003, exit_rsa);
  9184. }
  9185. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  9186. if (ret != 0) {
  9187. ERROR_OUT(-7004, exit_rsa);
  9188. }
  9189. #ifndef HAVE_FIPS
  9190. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9191. #else
  9192. ret = wc_InitRng(&rng);
  9193. #endif
  9194. if (ret != 0) {
  9195. ERROR_OUT(-7005, exit_rsa);
  9196. }
  9197. #ifndef NO_SIG_WRAPPER
  9198. ret = rsa_sig_test(&key, sizeof(RsaKey), wc_RsaEncryptSize(&key), &rng);
  9199. if (ret != 0)
  9200. goto exit_rsa;
  9201. #endif
  9202. do {
  9203. #if defined(WOLFSSL_ASYNC_CRYPT)
  9204. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9205. #endif
  9206. if (ret >= 0) {
  9207. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, &key, &rng);
  9208. }
  9209. } while (ret == WC_PENDING_E);
  9210. if (ret < 0) {
  9211. ERROR_OUT(-7006, exit_rsa);
  9212. }
  9213. #ifdef WC_RSA_BLINDING
  9214. {
  9215. int tmpret = ret;
  9216. ret = wc_RsaSetRNG(&key, &rng);
  9217. if (ret < 0) {
  9218. ERROR_OUT(-7007, exit_rsa);
  9219. }
  9220. ret = tmpret;
  9221. }
  9222. #endif
  9223. idx = (word32)ret; /* save off encrypted length */
  9224. do {
  9225. #if defined(WOLFSSL_ASYNC_CRYPT)
  9226. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9227. #endif
  9228. if (ret >= 0) {
  9229. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, &key);
  9230. }
  9231. } while (ret == WC_PENDING_E);
  9232. if (ret < 0) {
  9233. ERROR_OUT(-7008, exit_rsa);
  9234. }
  9235. if (XMEMCMP(plain, in, inLen)) {
  9236. ERROR_OUT(-7009, exit_rsa);
  9237. }
  9238. do {
  9239. #if defined(WOLFSSL_ASYNC_CRYPT)
  9240. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9241. #endif
  9242. if (ret >= 0) {
  9243. ret = wc_RsaPrivateDecryptInline(out, idx, &res, &key);
  9244. }
  9245. } while (ret == WC_PENDING_E);
  9246. if (ret < 0) {
  9247. ERROR_OUT(-7010, exit_rsa);
  9248. }
  9249. if (ret != (int)inLen) {
  9250. ERROR_OUT(-7011, exit_rsa);
  9251. }
  9252. if (XMEMCMP(res, in, inLen)) {
  9253. ERROR_OUT(-7012, exit_rsa);
  9254. }
  9255. do {
  9256. #if defined(WOLFSSL_ASYNC_CRYPT)
  9257. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9258. #endif
  9259. if (ret >= 0) {
  9260. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  9261. }
  9262. } while (ret == WC_PENDING_E);
  9263. if (ret < 0) {
  9264. ERROR_OUT(-7013, exit_rsa);
  9265. }
  9266. idx = (word32)ret;
  9267. XMEMSET(plain, 0, plainSz);
  9268. do {
  9269. #if defined(WOLFSSL_ASYNC_CRYPT)
  9270. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9271. #endif
  9272. if (ret >= 0) {
  9273. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  9274. }
  9275. } while (ret == WC_PENDING_E);
  9276. if (ret < 0) {
  9277. ERROR_OUT(-7014, exit_rsa);
  9278. }
  9279. if (XMEMCMP(plain, in, (size_t)ret)) {
  9280. ERROR_OUT(-7015, exit_rsa);
  9281. }
  9282. #ifndef WC_NO_RSA_OAEP
  9283. /* OAEP padding testing */
  9284. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  9285. (!defined(HAVE_FIPS) || \
  9286. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  9287. #ifndef NO_SHA
  9288. XMEMSET(plain, 0, plainSz);
  9289. do {
  9290. #if defined(WOLFSSL_ASYNC_CRYPT)
  9291. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9292. #endif
  9293. if (ret >= 0) {
  9294. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9295. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  9296. }
  9297. } while (ret == WC_PENDING_E);
  9298. if (ret < 0) {
  9299. ERROR_OUT(-7016, exit_rsa);
  9300. }
  9301. idx = (word32)ret;
  9302. do {
  9303. #if defined(WOLFSSL_ASYNC_CRYPT)
  9304. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9305. #endif
  9306. if (ret >= 0) {
  9307. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9308. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  9309. }
  9310. } while (ret == WC_PENDING_E);
  9311. if (ret < 0) {
  9312. ERROR_OUT(-7017, exit_rsa);
  9313. }
  9314. if (XMEMCMP(plain, in, inLen)) {
  9315. ERROR_OUT(-7018, exit_rsa);
  9316. }
  9317. #endif /* NO_SHA */
  9318. #ifndef NO_SHA256
  9319. XMEMSET(plain, 0, plainSz);
  9320. do {
  9321. #if defined(WOLFSSL_ASYNC_CRYPT)
  9322. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9323. #endif
  9324. if (ret >= 0) {
  9325. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9326. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  9327. }
  9328. } while (ret == WC_PENDING_E);
  9329. if (ret < 0) {
  9330. ERROR_OUT(-7019, exit_rsa);
  9331. }
  9332. idx = (word32)ret;
  9333. do {
  9334. #if defined(WOLFSSL_ASYNC_CRYPT)
  9335. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9336. #endif
  9337. if (ret >= 0) {
  9338. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9339. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  9340. }
  9341. } while (ret == WC_PENDING_E);
  9342. if (ret < 0) {
  9343. ERROR_OUT(-7020, exit_rsa);
  9344. }
  9345. if (XMEMCMP(plain, in, inLen)) {
  9346. ERROR_OUT(-7021, exit_rsa);
  9347. }
  9348. do {
  9349. #if defined(WOLFSSL_ASYNC_CRYPT)
  9350. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9351. #endif
  9352. if (ret >= 0) {
  9353. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, &key,
  9354. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  9355. }
  9356. } while (ret == WC_PENDING_E);
  9357. if (ret < 0) {
  9358. ERROR_OUT(-7022, exit_rsa);
  9359. }
  9360. if (ret != (int)inLen) {
  9361. ERROR_OUT(-7023, exit_rsa);
  9362. }
  9363. if (XMEMCMP(res, in, inLen)) {
  9364. ERROR_OUT(-7024, exit_rsa);
  9365. }
  9366. /* check fails if not using the same optional label */
  9367. XMEMSET(plain, 0, plainSz);
  9368. do {
  9369. #if defined(WOLFSSL_ASYNC_CRYPT)
  9370. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9371. #endif
  9372. if (ret >= 0) {
  9373. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9374. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  9375. }
  9376. } while (ret == WC_PENDING_E);
  9377. if (ret < 0) {
  9378. ERROR_OUT(-7025, exit_rsa);
  9379. }
  9380. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  9381. #ifndef HAVE_CAVIUM
  9382. idx = (word32)ret;
  9383. do {
  9384. #if defined(WOLFSSL_ASYNC_CRYPT)
  9385. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9386. #endif
  9387. if (ret >= 0) {
  9388. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9389. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  9390. }
  9391. } while (ret == WC_PENDING_E);
  9392. if (ret > 0) { /* in this case decrypt should fail */
  9393. ERROR_OUT(-7026, exit_rsa);
  9394. }
  9395. ret = 0;
  9396. #endif /* !HAVE_CAVIUM */
  9397. /* check using optional label with encrypt/decrypt */
  9398. XMEMSET(plain, 0, plainSz);
  9399. do {
  9400. #if defined(WOLFSSL_ASYNC_CRYPT)
  9401. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9402. #endif
  9403. if (ret >= 0) {
  9404. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9405. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  9406. }
  9407. } while (ret == WC_PENDING_E);
  9408. if (ret < 0) {
  9409. ERROR_OUT(-7027, exit_rsa);
  9410. }
  9411. idx = (word32)ret;
  9412. do {
  9413. #if defined(WOLFSSL_ASYNC_CRYPT)
  9414. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9415. #endif
  9416. if (ret >= 0) {
  9417. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9418. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  9419. }
  9420. } while (ret == WC_PENDING_E);
  9421. if (ret < 0) {
  9422. ERROR_OUT(-7028, exit_rsa);
  9423. }
  9424. if (XMEMCMP(plain, in, inLen)) {
  9425. ERROR_OUT(-7029, exit_rsa);
  9426. }
  9427. #ifndef NO_SHA
  9428. /* check fail using mismatch hash algorithms */
  9429. XMEMSET(plain, 0, plainSz);
  9430. do {
  9431. #if defined(WOLFSSL_ASYNC_CRYPT)
  9432. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9433. #endif
  9434. if (ret >= 0) {
  9435. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9436. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  9437. }
  9438. } while (ret == WC_PENDING_E);
  9439. if (ret < 0) {
  9440. ERROR_OUT(-7030, exit_rsa);
  9441. }
  9442. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  9443. #ifndef HAVE_CAVIUM
  9444. idx = (word32)ret;
  9445. do {
  9446. #if defined(WOLFSSL_ASYNC_CRYPT)
  9447. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9448. #endif
  9449. if (ret >= 0) {
  9450. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9451. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  9452. in, inLen);
  9453. }
  9454. } while (ret == WC_PENDING_E);
  9455. if (ret > 0) { /* should fail */
  9456. ERROR_OUT(-7031, exit_rsa);
  9457. }
  9458. ret = 0;
  9459. #endif /* !HAVE_CAVIUM */
  9460. #endif /* NO_SHA*/
  9461. #endif /* NO_SHA256 */
  9462. #ifdef WOLFSSL_SHA512
  9463. /* Check valid RSA key size is used while using hash length of SHA512
  9464. If key size is less than (hash length * 2) + 2 then is invalid use
  9465. and test, since OAEP padding requires this.
  9466. BAD_FUNC_ARG is returned when this case is not met */
  9467. if (wc_RsaEncryptSize(&key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  9468. XMEMSET(plain, 0, plainSz);
  9469. do {
  9470. #if defined(WOLFSSL_ASYNC_CRYPT)
  9471. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9472. #endif
  9473. if (ret >= 0) {
  9474. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9475. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  9476. }
  9477. } while (ret == WC_PENDING_E);
  9478. if (ret < 0) {
  9479. ERROR_OUT(-7032, exit_rsa);
  9480. }
  9481. idx = ret;
  9482. do {
  9483. #if defined(WOLFSSL_ASYNC_CRYPT)
  9484. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9485. #endif
  9486. if (ret >= 0) {
  9487. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9488. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  9489. }
  9490. } while (ret == WC_PENDING_E);
  9491. if (ret < 0) {
  9492. ERROR_OUT(-7033, exit_rsa);
  9493. }
  9494. if (XMEMCMP(plain, in, inLen)) {
  9495. ERROR_OUT(-7034, exit_rsa);
  9496. }
  9497. }
  9498. #endif /* WOLFSSL_SHA512 */
  9499. /* check using pkcsv15 padding with _ex API */
  9500. XMEMSET(plain, 0, plainSz);
  9501. do {
  9502. #if defined(WOLFSSL_ASYNC_CRYPT)
  9503. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9504. #endif
  9505. if (ret >= 0) {
  9506. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  9507. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  9508. }
  9509. } while (ret == WC_PENDING_E);
  9510. if (ret < 0) {
  9511. ERROR_OUT(-7035, exit_rsa);
  9512. }
  9513. idx = (word32)ret;
  9514. do {
  9515. #if defined(WOLFSSL_ASYNC_CRYPT)
  9516. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9517. #endif
  9518. if (ret >= 0) {
  9519. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  9520. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  9521. }
  9522. } while (ret == WC_PENDING_E);
  9523. if (ret < 0) {
  9524. ERROR_OUT(-7036, exit_rsa);
  9525. }
  9526. if (XMEMCMP(plain, in, inLen)) {
  9527. ERROR_OUT(-7037, exit_rsa);
  9528. }
  9529. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  9530. #endif /* WC_NO_RSA_OAEP */
  9531. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA)
  9532. ret = rsa_export_key_test(&key);
  9533. if (ret != 0)
  9534. return ret;
  9535. #endif
  9536. ret = rsa_flatten_test(&key);
  9537. if (ret != 0)
  9538. return ret;
  9539. #if defined(WOLFSSL_MDK_ARM)
  9540. #define sizeof(s) XSTRLEN((char *)(s))
  9541. #endif
  9542. #ifdef USE_CERT_BUFFERS_1024
  9543. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  9544. bytes = (size_t)sizeof_client_cert_der_1024;
  9545. #elif defined(USE_CERT_BUFFERS_2048)
  9546. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  9547. bytes = (size_t)sizeof_client_cert_der_2048;
  9548. #elif !defined(NO_FILESYSTEM)
  9549. file2 = fopen(clientCert, "rb");
  9550. if (!file2) {
  9551. ERROR_OUT(-7038, exit_rsa);
  9552. }
  9553. bytes = fread(tmp, 1, FOURK_BUF, file2);
  9554. fclose(file2);
  9555. #else
  9556. /* No certificate to use. */
  9557. ERROR_OUT(-7039, exit_rsa);
  9558. #endif
  9559. #ifdef sizeof
  9560. #undef sizeof
  9561. #endif
  9562. #ifdef WOLFSSL_TEST_CERT
  9563. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  9564. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  9565. if (ret != 0) {
  9566. FreeDecodedCert(&cert);
  9567. ERROR_OUT(-7040, exit_rsa);
  9568. }
  9569. FreeDecodedCert(&cert);
  9570. #else
  9571. (void)bytes;
  9572. #endif
  9573. #ifdef WOLFSSL_CERT_EXT
  9574. #ifdef USE_CERT_BUFFERS_1024
  9575. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  9576. bytes = sizeof_client_keypub_der_1024;
  9577. #elif defined(USE_CERT_BUFFERS_2048)
  9578. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  9579. bytes = sizeof_client_keypub_der_2048;
  9580. #else
  9581. file = fopen(clientKeyPub, "rb");
  9582. if (!file) {
  9583. err_sys("can't open ./certs/client-keyPub.der, "
  9584. "Please run from wolfSSL home dir", -40);
  9585. ERROR_OUT(-7041, exit_rsa);
  9586. }
  9587. bytes = fread(tmp, 1, FOURK_BUF, file);
  9588. fclose(file);
  9589. #endif /* USE_CERT_BUFFERS */
  9590. ret = wc_InitRsaKey(&keypub, HEAP_HINT);
  9591. if (ret != 0) {
  9592. ERROR_OUT(-7042, exit_rsa);
  9593. }
  9594. idx = 0;
  9595. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keypub, (word32)bytes);
  9596. if (ret != 0) {
  9597. ERROR_OUT(-7043, exit_rsa);
  9598. }
  9599. #endif /* WOLFSSL_CERT_EXT */
  9600. #ifdef WOLFSSL_KEY_GEN
  9601. ret = rsa_keygen_test(&rng);
  9602. if (ret != 0)
  9603. goto exit_rsa;
  9604. #endif
  9605. #ifdef WOLFSSL_CERT_GEN
  9606. /* Make Cert / Sign example for RSA cert and RSA CA */
  9607. ret = rsa_certgen_test(&key, &keypub, &rng, tmp);
  9608. if (ret != 0)
  9609. goto exit_rsa;
  9610. #if !defined(NO_RSA) && defined(HAVE_ECC)
  9611. ret = rsa_ecc_certgen_test(&rng, tmp);
  9612. if (ret != 0)
  9613. goto exit_rsa;
  9614. #endif
  9615. #ifdef HAVE_NTRU
  9616. {
  9617. Cert myCert;
  9618. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  9619. FILE* caFile;
  9620. #endif
  9621. FILE* ntruPrivFile;
  9622. int certSz;
  9623. word32 idx3 = 0;
  9624. #ifdef WOLFSSL_TEST_CERT
  9625. DecodedCert decode;
  9626. #endif
  9627. byte public_key[557]; /* sized for EES401EP2 */
  9628. word16 public_key_len; /* no. of octets in public key */
  9629. byte private_key[607]; /* sized for EES401EP2 */
  9630. word16 private_key_len; /* no. of octets in private key */
  9631. DRBG_HANDLE drbg;
  9632. static uint8_t const pers_str[] = {
  9633. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  9634. };
  9635. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  9636. sizeof(pers_str), GetEntropy, &drbg);
  9637. if (rc != DRBG_OK) {
  9638. ERROR_OUT(-7044, exit_rsa);
  9639. }
  9640. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  9641. &public_key_len, NULL,
  9642. &private_key_len, NULL);
  9643. if (rc != NTRU_OK) {
  9644. ERROR_OUT(-7045, exit_rsa);
  9645. }
  9646. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  9647. &public_key_len, public_key,
  9648. &private_key_len, private_key);
  9649. if (rc != NTRU_OK) {
  9650. ERROR_OUT(-7046, exit_rsa);
  9651. }
  9652. rc = ntru_crypto_drbg_uninstantiate(drbg);
  9653. if (rc != NTRU_OK) {
  9654. ERROR_OUT(-7047, exit_rsa);
  9655. }
  9656. #ifdef USE_CERT_BUFFERS_1024
  9657. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  9658. bytes = sizeof_ca_key_der_1024;
  9659. #elif defined(USE_CERT_BUFFERS_2048)
  9660. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  9661. bytes = sizeof_ca_key_der_2048;
  9662. #else
  9663. caFile = fopen(rsaCaKeyFile, "rb");
  9664. if (!caFile) {
  9665. ERROR_OUT(-7048, exit_rsa);
  9666. }
  9667. bytes = fread(tmp, 1, FOURK_BUF, caFile);
  9668. fclose(caFile);
  9669. #endif /* USE_CERT_BUFFERS */
  9670. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  9671. if (ret != 0) {
  9672. ERROR_OUT(-7049, exit_rsa);
  9673. }
  9674. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes);
  9675. if (ret != 0) {
  9676. ERROR_OUT(-7050, exit_rsa);
  9677. }
  9678. if (wc_InitCert(&myCert)) {
  9679. ERROR_OUT(-7051, exit_rsa);
  9680. }
  9681. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  9682. myCert.daysValid = 1000;
  9683. #ifdef WOLFSSL_CERT_EXT
  9684. /* add SKID from the Public Key */
  9685. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  9686. public_key_len) != 0) {
  9687. ERROR_OUT(-7052, exit_rsa);
  9688. }
  9689. /* add AKID from the CA certificate */
  9690. #if defined(USE_CERT_BUFFERS_2048)
  9691. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  9692. sizeof_ca_cert_der_2048);
  9693. #elif defined(USE_CERT_BUFFERS_1024)
  9694. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  9695. sizeof_ca_cert_der_1024);
  9696. #else
  9697. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  9698. #endif
  9699. if (ret != 0) {
  9700. ERROR_OUT(-7053, exit_rsa);
  9701. }
  9702. /* add Key Usage */
  9703. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  9704. ERROR_OUT(-7054, exit_rsa);
  9705. }
  9706. #endif /* WOLFSSL_CERT_EXT */
  9707. #if defined(USE_CERT_BUFFERS_2048)
  9708. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  9709. sizeof_ca_cert_der_2048);
  9710. #elif defined(USE_CERT_BUFFERS_1024)
  9711. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  9712. sizeof_ca_cert_der_1024);
  9713. #else
  9714. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  9715. #endif
  9716. if (ret < 0) {
  9717. ERROR_OUT(-7055, exit_rsa);
  9718. }
  9719. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9720. if (der == NULL) {
  9721. ERROR_OUT(-7056, exit_rsa);
  9722. }
  9723. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  9724. if (pem == NULL) {
  9725. ERROR_OUT(-7057, exit_rsa);
  9726. }
  9727. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  9728. public_key_len, &rng);
  9729. if (certSz < 0) {
  9730. ERROR_OUT(-7058, exit_rsa);
  9731. }
  9732. ret = 0;
  9733. do {
  9734. #if defined(WOLFSSL_ASYNC_CRYPT)
  9735. ret = wc_AsyncWait(ret, &caKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9736. #endif
  9737. if (ret >= 0) {
  9738. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  9739. &caKey, NULL, &rng);
  9740. }
  9741. } while (ret == WC_PENDING_E);
  9742. wc_FreeRsaKey(&caKey);
  9743. if (ret < 0) {
  9744. ERROR_OUT(-7059, exit_rsa);
  9745. }
  9746. certSz = ret;
  9747. #ifdef WOLFSSL_TEST_CERT
  9748. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  9749. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  9750. if (ret != 0) {
  9751. FreeDecodedCert(&decode);
  9752. ERROR_OUT(-7060, exit_rsa);
  9753. }
  9754. FreeDecodedCert(&decode);
  9755. #endif
  9756. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, "./ntru-cert.der",
  9757. "./ntru-cert.pem", CERT_TYPE, -5637);
  9758. if (ret != 0) {
  9759. goto exit_rsa;
  9760. }
  9761. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  9762. ntruPrivFile = fopen("./ntru-key.raw", "wb");
  9763. if (!ntruPrivFile) {
  9764. ERROR_OUT(-7061, exit_rsa);
  9765. }
  9766. ret = (int)fwrite(private_key, 1, private_key_len, ntruPrivFile);
  9767. fclose(ntruPrivFile);
  9768. if (ret != private_key_len) {
  9769. ERROR_OUT(-7062, exit_rsa);
  9770. }
  9771. #endif
  9772. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9773. pem = NULL;
  9774. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9775. der = NULL;
  9776. }
  9777. #endif /* HAVE_NTRU */
  9778. #ifdef WOLFSSL_CERT_REQ
  9779. {
  9780. Cert req;
  9781. int derSz;
  9782. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  9783. if (der == NULL) {
  9784. ERROR_OUT(-7063, exit_rsa);
  9785. }
  9786. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  9787. if (pem == NULL) {
  9788. ERROR_OUT(-7064, exit_rsa);
  9789. }
  9790. if (wc_InitCert(&req)) {
  9791. ERROR_OUT(-7065, exit_rsa);
  9792. }
  9793. req.version = 0;
  9794. req.isCA = 1;
  9795. XSTRNCPY(req.challengePw, "wolf123", CTC_NAME_SIZE);
  9796. XMEMCPY(&req.subject, &certDefaultName, sizeof(CertName));
  9797. #ifndef NO_SHA256
  9798. req.sigType = CTC_SHA256wRSA;
  9799. #else
  9800. req.sigType = CTC_SHAwRSA;
  9801. #endif
  9802. #ifdef WOLFSSL_CERT_EXT
  9803. /* add SKID from the Public Key */
  9804. if (wc_SetSubjectKeyIdFromPublicKey(&req, &keypub, NULL) != 0) {
  9805. ERROR_OUT(-7066, exit_rsa);
  9806. }
  9807. /* add Key Usage */
  9808. if (wc_SetKeyUsage(&req, certKeyUsage2) != 0) {
  9809. ERROR_OUT(-7067, exit_rsa);
  9810. }
  9811. /* add Extended Key Usage */
  9812. if (wc_SetExtKeyUsage(&req, "serverAuth,clientAuth,codeSigning,"
  9813. "emailProtection,timeStamping,OCSPSigning") != 0) {
  9814. ERROR_OUT(-7068, exit_rsa);
  9815. }
  9816. #ifdef WOLFSSL_EKU_OID
  9817. {
  9818. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  9819. if (wc_SetExtKeyUsageOID(&req, unique, sizeof(unique), 0,
  9820. HEAP_HINT) != 0) {
  9821. ERROR_OUT(-7069, exit_rsa);
  9822. }
  9823. }
  9824. #endif /* WOLFSSL_EKU_OID */
  9825. #endif /* WOLFSSL_CERT_EXT */
  9826. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  9827. if (derSz < 0) {
  9828. ERROR_OUT(-7070, exit_rsa);
  9829. }
  9830. #ifdef WOLFSSL_CERT_EXT
  9831. /* Try again with "any" flag set, will override all others */
  9832. if (wc_SetExtKeyUsage(&req, "any") != 0) {
  9833. ERROR_OUT(-7071, exit_rsa);
  9834. }
  9835. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  9836. if (derSz < 0) {
  9837. ERROR_OUT(-7072, exit_rsa);
  9838. }
  9839. #endif /* WOLFSSL_CERT_EXT */
  9840. ret = 0;
  9841. do {
  9842. #if defined(WOLFSSL_ASYNC_CRYPT)
  9843. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9844. #endif
  9845. if (ret >= 0) {
  9846. ret = wc_SignCert(req.bodySz, req.sigType, der, FOURK_BUF,
  9847. &key, NULL, &rng);
  9848. }
  9849. } while (ret == WC_PENDING_E);
  9850. if (ret < 0) {
  9851. ERROR_OUT(-7073, exit_rsa);
  9852. }
  9853. derSz = ret;
  9854. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, certReqDerFile,
  9855. certReqPemFile, CERTREQ_TYPE, -5650);
  9856. if (ret != 0) {
  9857. goto exit_rsa;
  9858. }
  9859. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9860. pem = NULL;
  9861. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9862. der = NULL;
  9863. }
  9864. #endif /* WOLFSSL_CERT_REQ */
  9865. #endif /* WOLFSSL_CERT_GEN */
  9866. #ifdef WC_RSA_PSS
  9867. ret = rsa_pss_test(&rng, &key);
  9868. #endif
  9869. exit_rsa:
  9870. wc_FreeRsaKey(&key);
  9871. #ifdef WOLFSSL_CERT_EXT
  9872. wc_FreeRsaKey(&keypub);
  9873. #endif
  9874. #if defined(HAVE_NTRU)
  9875. wc_FreeRsaKey(&caKey);
  9876. #endif
  9877. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9878. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9879. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9880. wc_FreeRng(&rng);
  9881. FREE_VAR(in, HEAP_HINT);
  9882. FREE_VAR(out, HEAP_HINT);
  9883. FREE_VAR(plain, HEAP_HINT);
  9884. /* ret can be greater then 0 with certgen but all negative values should
  9885. * be returned and treated as an error */
  9886. if (ret >= 0) {
  9887. return 0;
  9888. }
  9889. else {
  9890. return ret;
  9891. }
  9892. }
  9893. #endif
  9894. #ifndef NO_DH
  9895. static int dh_fips_generate_test(WC_RNG *rng)
  9896. {
  9897. int ret = 0;
  9898. DhKey key;
  9899. static byte p[] = {
  9900. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  9901. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  9902. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  9903. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  9904. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  9905. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  9906. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  9907. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  9908. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  9909. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  9910. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  9911. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  9912. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  9913. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  9914. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  9915. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  9916. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  9917. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  9918. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  9919. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  9920. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  9921. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  9922. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  9923. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  9924. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  9925. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  9926. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  9927. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  9928. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  9929. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  9930. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  9931. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  9932. };
  9933. static byte g[] = {
  9934. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  9935. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  9936. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  9937. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  9938. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  9939. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  9940. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  9941. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  9942. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  9943. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  9944. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  9945. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  9946. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  9947. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  9948. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  9949. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  9950. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  9951. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  9952. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  9953. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  9954. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  9955. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  9956. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  9957. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  9958. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  9959. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  9960. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  9961. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  9962. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  9963. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  9964. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  9965. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  9966. };
  9967. static byte q[] = {
  9968. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  9969. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  9970. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  9971. 0x40, 0x52, 0xed, 0x41
  9972. };
  9973. static byte q0[] = {
  9974. 0x00,
  9975. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  9976. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  9977. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  9978. 0x40, 0x52, 0xed, 0x41
  9979. };
  9980. byte priv[256];
  9981. byte pub[256];
  9982. word32 privSz = sizeof(priv);
  9983. word32 pubSz = sizeof(pub);
  9984. /* Parameter Validation testing. */
  9985. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  9986. if (ret != BAD_FUNC_ARG)
  9987. return -7074;
  9988. ret = wc_DhGenerateKeyPair(&key, NULL, priv, &privSz, pub, &pubSz);
  9989. if (ret != BAD_FUNC_ARG)
  9990. return -7075;
  9991. ret = wc_DhGenerateKeyPair(&key, rng, NULL, &privSz, pub, &pubSz);
  9992. if (ret != BAD_FUNC_ARG)
  9993. return -7076;
  9994. ret = wc_DhGenerateKeyPair(&key, rng, priv, NULL, pub, &pubSz);
  9995. if (ret != BAD_FUNC_ARG)
  9996. return -7077;
  9997. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, NULL, &pubSz);
  9998. if (ret != BAD_FUNC_ARG)
  9999. return -7078;
  10000. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, NULL);
  10001. if (ret != BAD_FUNC_ARG)
  10002. return -7079;
  10003. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  10004. if (ret != 0)
  10005. return -7080;
  10006. ret = wc_DhSetKey_ex(&key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  10007. if (ret != 0) {
  10008. ERROR_OUT(-7081, exit_gen_test);
  10009. }
  10010. wc_FreeDhKey(&key);
  10011. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  10012. if (ret != 0)
  10013. return -7082;
  10014. ret = wc_DhSetKey_ex(&key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  10015. if (ret != 0) {
  10016. ERROR_OUT(-7083, exit_gen_test);
  10017. }
  10018. /* Use API. */
  10019. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, &pubSz);
  10020. #if defined(WOLFSSL_ASYNC_CRYPT)
  10021. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  10022. #endif
  10023. if (ret != 0) {
  10024. ERROR_OUT(-7084, exit_gen_test);
  10025. }
  10026. ret = wc_DhCheckPubKey_ex(&key, pub, pubSz, q0, sizeof(q0));
  10027. if (ret != 0) {
  10028. ERROR_OUT(-7085, exit_gen_test);
  10029. }
  10030. wc_FreeDhKey(&key);
  10031. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  10032. if (ret != 0)
  10033. return -7086;
  10034. ret = wc_DhSetKey(&key, p, sizeof(p), g, sizeof(g));
  10035. if (ret != 0) {
  10036. ERROR_OUT(-7087, exit_gen_test);
  10037. }
  10038. ret = wc_DhCheckPubKey_ex(&key, pub, pubSz, q, sizeof(q));
  10039. if (ret != 0) {
  10040. ERROR_OUT(-7088, exit_gen_test);
  10041. }
  10042. #ifndef HAVE_SELFTEST
  10043. ret = wc_DhCheckKeyPair(&key, pub, pubSz, priv, privSz);
  10044. if (ret != 0) {
  10045. ERROR_OUT(-8229, exit_gen_test);
  10046. }
  10047. /* Taint the public key so the check fails. */
  10048. pub[0]++;
  10049. ret = wc_DhCheckKeyPair(&key, pub, pubSz, priv, privSz);
  10050. if (ret != MP_CMP_E) {
  10051. ERROR_OUT(-8230, exit_gen_test);
  10052. }
  10053. #endif /* HAVE_SELFTEST */
  10054. #ifdef WOLFSSL_KEY_GEN
  10055. wc_FreeDhKey(&key);
  10056. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  10057. if (ret != 0)
  10058. return -8231;
  10059. ret = wc_DhGenerateParams(rng, 2048, &key);
  10060. if (ret != 0) {
  10061. ERROR_OUT(-8226, exit_gen_test);
  10062. }
  10063. privSz = sizeof(priv);
  10064. pubSz = sizeof(pub);
  10065. ret = wc_DhGenerateKeyPair(&key, rng, priv, &privSz, pub, &pubSz);
  10066. #if defined(WOLFSSL_ASYNC_CRYPT)
  10067. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  10068. #endif
  10069. if (ret != 0) {
  10070. ERROR_OUT(-8227, exit_gen_test);
  10071. }
  10072. #endif /* WOLFSSL_KEY_GEN */
  10073. ret = 0;
  10074. exit_gen_test:
  10075. wc_FreeDhKey(&key);
  10076. return ret;
  10077. }
  10078. static int dh_generate_test(WC_RNG *rng)
  10079. {
  10080. int ret = 0;
  10081. DhKey smallKey;
  10082. byte p[2] = { 0, 5 };
  10083. byte g[2] = { 0, 2 };
  10084. #ifndef WOLFSSL_SP_MATH
  10085. #ifdef WOLFSSL_DH_CONST
  10086. /* the table for constant DH lookup will round to the lowest byte size 21 */
  10087. byte priv[21];
  10088. byte pub[21];
  10089. #else
  10090. byte priv[2];
  10091. byte pub[2];
  10092. #endif
  10093. word32 privSz = sizeof(priv);
  10094. word32 pubSz = sizeof(pub);
  10095. #endif
  10096. ret = wc_InitDhKey_ex(&smallKey, HEAP_HINT, devId);
  10097. if (ret != 0)
  10098. return -7089;
  10099. /* Parameter Validation testing. */
  10100. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  10101. if (ret != BAD_FUNC_ARG)
  10102. return -7090;
  10103. wc_FreeDhKey(NULL);
  10104. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  10105. if (ret != BAD_FUNC_ARG) {
  10106. ERROR_OUT(-7091, exit_gen_test);
  10107. }
  10108. ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g));
  10109. if (ret != BAD_FUNC_ARG) {
  10110. ERROR_OUT(-7092, exit_gen_test);
  10111. }
  10112. ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g));
  10113. if (ret != BAD_FUNC_ARG) {
  10114. ERROR_OUT(-7093, exit_gen_test);
  10115. }
  10116. ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g));
  10117. if (ret != BAD_FUNC_ARG) {
  10118. ERROR_OUT(-7094, exit_gen_test);
  10119. }
  10120. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0);
  10121. if (ret != BAD_FUNC_ARG) {
  10122. ERROR_OUT(-7095, exit_gen_test);
  10123. }
  10124. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
  10125. if (ret != 0) {
  10126. ERROR_OUT(-7096, exit_gen_test);
  10127. }
  10128. #ifndef WOLFSSL_SP_MATH
  10129. /* Use API. */
  10130. ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz);
  10131. #if defined(WOLFSSL_ASYNC_CRYPT)
  10132. ret = wc_AsyncWait(ret, &smallKey.asyncDev, WC_ASYNC_FLAG_NONE);
  10133. #endif
  10134. if (ret != 0) {
  10135. ret = -7097;
  10136. }
  10137. #else
  10138. (void)rng;
  10139. ret = 0;
  10140. #endif
  10141. exit_gen_test:
  10142. wc_FreeDhKey(&smallKey);
  10143. return ret;
  10144. }
  10145. int dh_test(void)
  10146. {
  10147. int ret;
  10148. word32 bytes;
  10149. word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
  10150. byte tmp[1024];
  10151. byte priv[256];
  10152. byte pub[256];
  10153. byte priv2[256];
  10154. byte pub2[256];
  10155. byte agree[256];
  10156. byte agree2[256];
  10157. DhKey key;
  10158. DhKey key2;
  10159. WC_RNG rng;
  10160. #ifdef USE_CERT_BUFFERS_1024
  10161. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  10162. bytes = (size_t)sizeof_dh_key_der_1024;
  10163. #elif defined(USE_CERT_BUFFERS_2048)
  10164. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  10165. bytes = (size_t)sizeof_dh_key_der_2048;
  10166. #elif defined(NO_ASN)
  10167. /* don't use file, no DER parsing */
  10168. #elif !defined(NO_FILESYSTEM)
  10169. FILE* file = fopen(dhKey, "rb");
  10170. if (!file)
  10171. return -7100;
  10172. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  10173. fclose(file);
  10174. #else
  10175. /* No DH key to use. */
  10176. return -7101;
  10177. #endif /* USE_CERT_BUFFERS */
  10178. (void)idx;
  10179. (void)tmp;
  10180. (void)bytes;
  10181. /* Use API for coverage. */
  10182. ret = wc_InitDhKey(&key);
  10183. if (ret != 0) {
  10184. ERROR_OUT(-7102, done);
  10185. }
  10186. wc_FreeDhKey(&key);
  10187. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  10188. if (ret != 0) {
  10189. ERROR_OUT(-7103, done);
  10190. }
  10191. ret = wc_InitDhKey_ex(&key2, HEAP_HINT, devId);
  10192. if (ret != 0) {
  10193. ERROR_OUT(-7104, done);
  10194. }
  10195. #ifdef NO_ASN
  10196. ret = wc_DhSetKey(&key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  10197. if (ret != 0) {
  10198. ERROR_OUT(-7105, done);
  10199. }
  10200. ret = wc_DhSetKey(&key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  10201. if (ret != 0) {
  10202. ERROR_OUT(-7106, done);
  10203. }
  10204. #else
  10205. ret = wc_DhKeyDecode(tmp, &idx, &key, bytes);
  10206. if (ret != 0) {
  10207. ERROR_OUT(-7107, done);
  10208. }
  10209. idx = 0;
  10210. ret = wc_DhKeyDecode(tmp, &idx, &key2, bytes);
  10211. if (ret != 0) {
  10212. ERROR_OUT(-7108, done);
  10213. }
  10214. #endif
  10215. #ifndef HAVE_FIPS
  10216. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10217. #else
  10218. ret = wc_InitRng(&rng);
  10219. #endif
  10220. if (ret != 0) {
  10221. ERROR_OUT(-7109, done);
  10222. }
  10223. ret = wc_DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  10224. #if defined(WOLFSSL_ASYNC_CRYPT)
  10225. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  10226. #endif
  10227. if (ret != 0) {
  10228. ERROR_OUT(-7110, done);
  10229. }
  10230. ret = wc_DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  10231. #if defined(WOLFSSL_ASYNC_CRYPT)
  10232. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  10233. #endif
  10234. if (ret != 0) {
  10235. ERROR_OUT(-7111, done);
  10236. }
  10237. ret = wc_DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  10238. #if defined(WOLFSSL_ASYNC_CRYPT)
  10239. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  10240. #endif
  10241. if (ret != 0) {
  10242. ERROR_OUT(-7112, done);
  10243. }
  10244. ret = wc_DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  10245. #if defined(WOLFSSL_ASYNC_CRYPT)
  10246. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  10247. #endif
  10248. if (ret != 0) {
  10249. ERROR_OUT(-7113, done);
  10250. }
  10251. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  10252. ERROR_OUT(-7114, done);
  10253. }
  10254. ret = dh_generate_test(&rng);
  10255. if (ret == 0)
  10256. ret = dh_fips_generate_test(&rng);
  10257. done:
  10258. wc_FreeDhKey(&key);
  10259. wc_FreeDhKey(&key2);
  10260. wc_FreeRng(&rng);
  10261. return ret;
  10262. }
  10263. #endif /* NO_DH */
  10264. #ifndef NO_DSA
  10265. int dsa_test(void)
  10266. {
  10267. int ret, answer;
  10268. word32 bytes;
  10269. word32 idx = 0;
  10270. byte tmp[1024];
  10271. DsaKey key;
  10272. WC_RNG rng;
  10273. wc_Sha sha;
  10274. byte hash[WC_SHA_DIGEST_SIZE];
  10275. byte signature[40];
  10276. #ifdef USE_CERT_BUFFERS_1024
  10277. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  10278. bytes = sizeof_dsa_key_der_1024;
  10279. #elif defined(USE_CERT_BUFFERS_2048)
  10280. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  10281. bytes = sizeof_dsa_key_der_2048;
  10282. #else
  10283. FILE* file = fopen(dsaKey, "rb");
  10284. if (!file)
  10285. return -7200;
  10286. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  10287. fclose(file);
  10288. #endif /* USE_CERT_BUFFERS */
  10289. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  10290. if (ret != 0)
  10291. return -7201;
  10292. wc_ShaUpdate(&sha, tmp, bytes);
  10293. wc_ShaFinal(&sha, hash);
  10294. wc_ShaFree(&sha);
  10295. ret = wc_InitDsaKey(&key);
  10296. if (ret != 0) return -7202;
  10297. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  10298. if (ret != 0) return -7203;
  10299. #ifndef HAVE_FIPS
  10300. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10301. #else
  10302. ret = wc_InitRng(&rng);
  10303. #endif
  10304. if (ret != 0) return -7204;
  10305. ret = wc_DsaSign(hash, signature, &key, &rng);
  10306. if (ret != 0) return -7205;
  10307. ret = wc_DsaVerify(hash, signature, &key, &answer);
  10308. if (ret != 0) return -7206;
  10309. if (answer != 1) return -7207;
  10310. wc_FreeDsaKey(&key);
  10311. #ifdef WOLFSSL_KEY_GEN
  10312. {
  10313. byte* der;
  10314. byte* pem;
  10315. int derSz = 0;
  10316. DsaKey derIn;
  10317. DsaKey genKey;
  10318. ret = wc_InitDsaKey(&genKey);
  10319. if (ret != 0) return -7208;
  10320. ret = wc_MakeDsaParameters(&rng, 1024, &genKey);
  10321. if (ret != 0) {
  10322. wc_FreeDsaKey(&genKey);
  10323. return -7209;
  10324. }
  10325. ret = wc_MakeDsaKey(&rng, &genKey);
  10326. if (ret != 0) {
  10327. wc_FreeDsaKey(&genKey);
  10328. return -7210;
  10329. }
  10330. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10331. if (der == NULL) {
  10332. wc_FreeDsaKey(&genKey);
  10333. return -7211;
  10334. }
  10335. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10336. if (pem == NULL) {
  10337. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10338. wc_FreeDsaKey(&genKey);
  10339. return -7212;
  10340. }
  10341. derSz = wc_DsaKeyToDer(&genKey, der, FOURK_BUF);
  10342. if (derSz < 0) {
  10343. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10344. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10345. return -7213;
  10346. }
  10347. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, keyDerFile,
  10348. keyPemFile, DSA_PRIVATEKEY_TYPE, -5814);
  10349. if (ret != 0) {
  10350. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10351. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10352. wc_FreeDsaKey(&genKey);
  10353. return ret;
  10354. }
  10355. ret = wc_InitDsaKey(&derIn);
  10356. if (ret != 0) {
  10357. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10358. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10359. wc_FreeDsaKey(&genKey);
  10360. return -7214;
  10361. }
  10362. idx = 0;
  10363. ret = wc_DsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  10364. if (ret != 0) {
  10365. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10366. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10367. wc_FreeDsaKey(&derIn);
  10368. wc_FreeDsaKey(&genKey);
  10369. return -7215;
  10370. }
  10371. wc_FreeDsaKey(&derIn);
  10372. wc_FreeDsaKey(&genKey);
  10373. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10374. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10375. }
  10376. #endif /* WOLFSSL_KEY_GEN */
  10377. wc_FreeRng(&rng);
  10378. return 0;
  10379. }
  10380. #endif /* NO_DSA */
  10381. #ifdef WOLFCRYPT_HAVE_SRP
  10382. static int generate_random_salt(byte *buf, word32 size)
  10383. {
  10384. int ret = -7216;
  10385. WC_RNG rng;
  10386. if(NULL == buf || !size)
  10387. return -7217;
  10388. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  10389. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  10390. wc_FreeRng(&rng);
  10391. }
  10392. return ret;
  10393. }
  10394. int srp_test(void)
  10395. {
  10396. Srp cli, srv;
  10397. int r;
  10398. byte clientPubKey[80]; /* A */
  10399. byte serverPubKey[80]; /* B */
  10400. word32 clientPubKeySz = 80;
  10401. word32 serverPubKeySz = 80;
  10402. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  10403. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  10404. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  10405. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  10406. byte username[] = "user";
  10407. word32 usernameSz = 4;
  10408. byte password[] = "password";
  10409. word32 passwordSz = 8;
  10410. byte N[] = {
  10411. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  10412. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  10413. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  10414. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  10415. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  10416. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  10417. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  10418. };
  10419. byte g[] = {
  10420. 0x02
  10421. };
  10422. byte salt[10];
  10423. byte verifier[80];
  10424. word32 v_size = sizeof(verifier);
  10425. /* set as 0's so if second init on srv not called SrpTerm is not on
  10426. * garbage values */
  10427. XMEMSET(&srv, 0, sizeof(Srp));
  10428. XMEMSET(&cli, 0, sizeof(Srp));
  10429. /* generating random salt */
  10430. r = generate_random_salt(salt, sizeof(salt));
  10431. /* client knows username and password. */
  10432. /* server knows N, g, salt and verifier. */
  10433. if (!r) r = wc_SrpInit(&cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  10434. if (!r) r = wc_SrpSetUsername(&cli, username, usernameSz);
  10435. /* loading N, g and salt in advance to generate the verifier. */
  10436. if (!r) r = wc_SrpSetParams(&cli, N, sizeof(N),
  10437. g, sizeof(g),
  10438. salt, sizeof(salt));
  10439. if (!r) r = wc_SrpSetPassword(&cli, password, passwordSz);
  10440. if (!r) r = wc_SrpGetVerifier(&cli, verifier, &v_size);
  10441. /* client sends username to server */
  10442. if (!r) r = wc_SrpInit(&srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  10443. if (!r) r = wc_SrpSetUsername(&srv, username, usernameSz);
  10444. if (!r) r = wc_SrpSetParams(&srv, N, sizeof(N),
  10445. g, sizeof(g),
  10446. salt, sizeof(salt));
  10447. if (!r) r = wc_SrpSetVerifier(&srv, verifier, v_size);
  10448. if (!r) r = wc_SrpGetPublic(&srv, serverPubKey, &serverPubKeySz);
  10449. /* server sends N, g, salt and B to client */
  10450. if (!r) r = wc_SrpGetPublic(&cli, clientPubKey, &clientPubKeySz);
  10451. if (!r) r = wc_SrpComputeKey(&cli, clientPubKey, clientPubKeySz,
  10452. serverPubKey, serverPubKeySz);
  10453. if (!r) r = wc_SrpGetProof(&cli, clientProof, &clientProofSz);
  10454. /* client sends A and M1 to server */
  10455. if (!r) r = wc_SrpComputeKey(&srv, clientPubKey, clientPubKeySz,
  10456. serverPubKey, serverPubKeySz);
  10457. if (!r) r = wc_SrpVerifyPeersProof(&srv, clientProof, clientProofSz);
  10458. if (!r) r = wc_SrpGetProof(&srv, serverProof, &serverProofSz);
  10459. /* server sends M2 to client */
  10460. if (!r) r = wc_SrpVerifyPeersProof(&cli, serverProof, serverProofSz);
  10461. wc_SrpTerm(&cli);
  10462. wc_SrpTerm(&srv);
  10463. return r;
  10464. }
  10465. #endif /* WOLFCRYPT_HAVE_SRP */
  10466. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  10467. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  10468. static int openssl_aes_test(void)
  10469. {
  10470. #ifdef HAVE_AES_CBC
  10471. #ifdef WOLFSSL_AES_128
  10472. {
  10473. /* EVP_CipherUpdate test */
  10474. const byte cbcPlain[] =
  10475. {
  10476. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10477. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  10478. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  10479. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  10480. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  10481. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  10482. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  10483. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  10484. };
  10485. byte key[] = "0123456789abcdef "; /* align */
  10486. byte iv[] = "1234567890abcdef "; /* align */
  10487. byte cipher[AES_BLOCK_SIZE * 4];
  10488. byte plain [AES_BLOCK_SIZE * 4];
  10489. EVP_CIPHER_CTX en;
  10490. EVP_CIPHER_CTX de;
  10491. int outlen ;
  10492. int total = 0;
  10493. EVP_CIPHER_CTX_init(&en);
  10494. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  10495. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  10496. return -7300;
  10497. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  10498. (byte*)cbcPlain, 9) == 0)
  10499. return -7301;
  10500. if (outlen != 0)
  10501. return -7302;
  10502. total += outlen;
  10503. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen,
  10504. (byte*)&cbcPlain[9] , 9) == 0)
  10505. return -7303;
  10506. if (outlen != 16)
  10507. return -7304;
  10508. total += outlen;
  10509. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  10510. return -7305;
  10511. if (outlen != 16)
  10512. return -7306;
  10513. total += outlen;
  10514. if (total != 32)
  10515. return 3408;
  10516. total = 0;
  10517. EVP_CIPHER_CTX_init(&de);
  10518. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  10519. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  10520. return -7307;
  10521. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  10522. return -7308;
  10523. if (outlen != 0)
  10524. return -7309;
  10525. total += outlen;
  10526. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  10527. (byte*)&cipher[6], 12) == 0)
  10528. return -7310;
  10529. if (outlen != 0)
  10530. total += outlen;
  10531. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  10532. (byte*)&cipher[6+12], 14) == 0)
  10533. return -7311;
  10534. if (outlen != 16)
  10535. return -7312;
  10536. total += outlen;
  10537. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  10538. return -7313;
  10539. if (outlen != 2)
  10540. return -7314;
  10541. total += outlen;
  10542. if (total != 18)
  10543. return 3427;
  10544. if (XMEMCMP(plain, cbcPlain, 18))
  10545. return -7315;
  10546. }
  10547. { /* evp_cipher test: EVP_aes_128_cbc */
  10548. EVP_CIPHER_CTX ctx;
  10549. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  10550. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  10551. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  10552. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  10553. };
  10554. const byte verify[] =
  10555. {
  10556. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  10557. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  10558. };
  10559. byte key[] = "0123456789abcdef "; /* align */
  10560. byte iv[] = "1234567890abcdef "; /* align */
  10561. byte cipher[AES_BLOCK_SIZE * 4];
  10562. byte plain [AES_BLOCK_SIZE * 4];
  10563. EVP_CIPHER_CTX_init(&ctx);
  10564. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  10565. return -7316;
  10566. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  10567. return -7317;
  10568. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  10569. return -7318;
  10570. EVP_CIPHER_CTX_init(&ctx);
  10571. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  10572. return -7319;
  10573. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  10574. return -7320;
  10575. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  10576. return -7321;
  10577. } /* end evp_cipher test: EVP_aes_128_cbc*/
  10578. #endif /* WOLFSSL_AES_128 */
  10579. #endif /* HAVE_AES_CBC */
  10580. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  10581. { /* evp_cipher test: EVP_aes_256_ecb*/
  10582. EVP_CIPHER_CTX ctx;
  10583. const byte msg[] =
  10584. {
  10585. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10586. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10587. };
  10588. const byte verify[] =
  10589. {
  10590. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  10591. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  10592. };
  10593. const byte key[] =
  10594. {
  10595. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10596. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10597. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10598. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10599. };
  10600. byte cipher[AES_BLOCK_SIZE * 4];
  10601. byte plain [AES_BLOCK_SIZE * 4];
  10602. EVP_CIPHER_CTX_init(&ctx);
  10603. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  10604. return -7322;
  10605. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  10606. return -7323;
  10607. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  10608. return -7324;
  10609. EVP_CIPHER_CTX_init(&ctx);
  10610. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  10611. return -7325;
  10612. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  10613. return -7326;
  10614. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  10615. return -7327;
  10616. } /* end evp_cipher test */
  10617. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  10618. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  10619. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  10620. {
  10621. /* Test: AES_encrypt/decrypt/set Key */
  10622. AES_KEY enc;
  10623. #ifdef HAVE_AES_DECRYPT
  10624. AES_KEY dec;
  10625. #endif
  10626. const byte msg[] =
  10627. {
  10628. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10629. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10630. };
  10631. const byte verify[] =
  10632. {
  10633. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  10634. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  10635. };
  10636. const byte key[] =
  10637. {
  10638. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10639. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10640. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10641. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10642. };
  10643. byte plain[sizeof(msg)];
  10644. byte cipher[sizeof(msg)];
  10645. AES_set_encrypt_key(key, sizeof(key)*8, &enc);
  10646. AES_set_decrypt_key(key, sizeof(key)*8, &dec);
  10647. AES_encrypt(msg, cipher, &enc);
  10648. #ifdef HAVE_AES_DECRYPT
  10649. AES_decrypt(cipher, plain, &dec);
  10650. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  10651. return -7328;
  10652. #endif /* HAVE_AES_DECRYPT */
  10653. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  10654. return -7329;
  10655. }
  10656. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  10657. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  10658. #ifdef WOLFSSL_AES_COUNTER
  10659. {
  10660. byte plainBuff [64];
  10661. byte cipherBuff[64];
  10662. #ifdef WOLFSSL_AES_128
  10663. const byte ctrKey[] =
  10664. {
  10665. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  10666. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  10667. };
  10668. const byte ctrIv[] =
  10669. {
  10670. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  10671. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  10672. };
  10673. const byte ctrPlain[] =
  10674. {
  10675. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10676. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  10677. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  10678. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  10679. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  10680. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  10681. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  10682. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  10683. };
  10684. const byte ctrCipher[] =
  10685. {
  10686. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  10687. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  10688. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  10689. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  10690. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  10691. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  10692. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  10693. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  10694. };
  10695. const byte oddCipher[] =
  10696. {
  10697. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  10698. 0xc2
  10699. };
  10700. #endif
  10701. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  10702. * NIST Special Publication 800-38A */
  10703. #ifdef WOLFSSL_AES_192
  10704. const byte ctr192Key[] =
  10705. {
  10706. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  10707. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  10708. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  10709. };
  10710. const byte ctr192Iv[] =
  10711. {
  10712. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  10713. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  10714. };
  10715. const byte ctr192Plain[] =
  10716. {
  10717. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10718. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10719. };
  10720. const byte ctr192Cipher[] =
  10721. {
  10722. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  10723. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  10724. };
  10725. #endif /* WOLFSSL_AES_192 */
  10726. #ifdef WOLFSSL_AES_256
  10727. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  10728. * NIST Special Publication 800-38A */
  10729. const byte ctr256Key[] =
  10730. {
  10731. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  10732. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  10733. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  10734. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  10735. };
  10736. const byte ctr256Iv[] =
  10737. {
  10738. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  10739. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  10740. };
  10741. const byte ctr256Plain[] =
  10742. {
  10743. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10744. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  10745. };
  10746. const byte ctr256Cipher[] =
  10747. {
  10748. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  10749. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  10750. };
  10751. #endif /* WOLFSSL_AES_256 */
  10752. EVP_CIPHER_CTX en;
  10753. EVP_CIPHER_CTX de;
  10754. #ifdef WOLFSSL_AES_128
  10755. EVP_CIPHER_CTX *p_en;
  10756. EVP_CIPHER_CTX *p_de;
  10757. EVP_CIPHER_CTX_init(&en);
  10758. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  10759. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  10760. return -7330;
  10761. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain,
  10762. AES_BLOCK_SIZE*4) == 0)
  10763. return -7331;
  10764. EVP_CIPHER_CTX_init(&de);
  10765. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  10766. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  10767. return -7332;
  10768. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  10769. AES_BLOCK_SIZE*4) == 0)
  10770. return -7333;
  10771. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  10772. return -7334;
  10773. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  10774. return -7335;
  10775. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  10776. if (p_en == NULL)
  10777. return -7336;
  10778. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  10779. if (p_de == NULL)
  10780. return -7337;
  10781. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  10782. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  10783. return -7338;
  10784. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  10785. AES_BLOCK_SIZE*4) == 0)
  10786. return -7339;
  10787. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  10788. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  10789. return -7340;
  10790. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  10791. AES_BLOCK_SIZE*4) == 0)
  10792. return -7341;
  10793. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  10794. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  10795. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  10796. return -7342;
  10797. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  10798. return -7343;
  10799. EVP_CIPHER_CTX_init(&en);
  10800. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  10801. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  10802. return -7344;
  10803. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  10804. return -7345;
  10805. EVP_CIPHER_CTX_init(&de);
  10806. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  10807. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  10808. return -7346;
  10809. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  10810. return -7347;
  10811. if (XMEMCMP(plainBuff, ctrPlain, 9))
  10812. return -7348;
  10813. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  10814. return -7349;
  10815. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  10816. return -7350;
  10817. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  10818. return -7351;
  10819. if (XMEMCMP(plainBuff, ctrPlain, 9))
  10820. return -7352;
  10821. if (XMEMCMP(cipherBuff, oddCipher, 9))
  10822. return -7353;
  10823. #endif /* WOLFSSL_AES_128 */
  10824. #ifdef WOLFSSL_AES_192
  10825. EVP_CIPHER_CTX_init(&en);
  10826. if (EVP_CipherInit(&en, EVP_aes_192_ctr(),
  10827. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  10828. return -7354;
  10829. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr192Plain,
  10830. AES_BLOCK_SIZE) == 0)
  10831. return -7355;
  10832. EVP_CIPHER_CTX_init(&de);
  10833. if (EVP_CipherInit(&de, EVP_aes_192_ctr(),
  10834. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  10835. return -7356;
  10836. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  10837. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  10838. AES_BLOCK_SIZE) == 0)
  10839. return -7357;
  10840. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  10841. return -7358;
  10842. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  10843. return -7359;
  10844. #endif /* WOLFSSL_AES_192 */
  10845. #ifdef WOLFSSL_AES_256
  10846. EVP_CIPHER_CTX_init(&en);
  10847. if (EVP_CipherInit(&en, EVP_aes_256_ctr(),
  10848. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  10849. return -7360;
  10850. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr256Plain,
  10851. AES_BLOCK_SIZE) == 0)
  10852. return -7361;
  10853. EVP_CIPHER_CTX_init(&de);
  10854. if (EVP_CipherInit(&de, EVP_aes_256_ctr(),
  10855. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  10856. return -7362;
  10857. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  10858. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  10859. AES_BLOCK_SIZE) == 0)
  10860. return -7363;
  10861. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  10862. return -7364;
  10863. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  10864. return -7365;
  10865. #endif /* WOLFSSL_AES_256 */
  10866. }
  10867. #endif /* HAVE_AES_COUNTER */
  10868. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  10869. {
  10870. AES_KEY enc;
  10871. AES_KEY dec;
  10872. const byte setIv[] = {
  10873. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  10874. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  10875. };
  10876. const byte key[] =
  10877. {
  10878. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  10879. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  10880. };
  10881. const byte cipher1[] =
  10882. {
  10883. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  10884. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  10885. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  10886. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  10887. };
  10888. const byte msg[] =
  10889. {
  10890. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  10891. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  10892. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  10893. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  10894. };
  10895. byte cipher[AES_BLOCK_SIZE * 2];
  10896. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  10897. int num = 0;
  10898. XMEMCPY(iv, setIv, sizeof(setIv));
  10899. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, &enc);
  10900. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, &dec);
  10901. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, &enc, iv,
  10902. &num, AES_ENCRYPT);
  10903. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  10904. return -7366;
  10905. if (num != 15) /* should have used 15 of the 16 bytes */
  10906. return -7367;
  10907. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  10908. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, &enc, iv,
  10909. &num, AES_ENCRYPT);
  10910. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  10911. return -7368;
  10912. if (num != 0)
  10913. return -7369;
  10914. }
  10915. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  10916. return 0;
  10917. }
  10918. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  10919. int openssl_test(void)
  10920. {
  10921. EVP_MD_CTX md_ctx;
  10922. testVector a, b, c, d, e, f;
  10923. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  10924. (void)a;
  10925. (void)b;
  10926. (void)c;
  10927. (void)e;
  10928. (void)f;
  10929. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  10930. {
  10931. byte* p;
  10932. p = (byte*)CRYPTO_malloc(10, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10933. if (p == NULL) {
  10934. return -7400;
  10935. }
  10936. XMEMSET(p, 0, 10);
  10937. CRYPTO_free(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10938. }
  10939. #ifndef NO_MD5
  10940. a.input = "1234567890123456789012345678901234567890123456789012345678"
  10941. "9012345678901234567890";
  10942. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  10943. "\x7a";
  10944. a.inLen = XSTRLEN(a.input);
  10945. a.outLen = WC_MD5_DIGEST_SIZE;
  10946. EVP_MD_CTX_init(&md_ctx);
  10947. EVP_DigestInit(&md_ctx, EVP_md5());
  10948. EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  10949. EVP_DigestFinal(&md_ctx, hash, 0);
  10950. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  10951. return -7401;
  10952. #endif /* NO_MD5 */
  10953. #ifndef NO_SHA
  10954. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10955. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  10956. "aaaaaaaaaa";
  10957. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  10958. "\x53\x99\x5E\x26\xA0";
  10959. b.inLen = XSTRLEN(b.input);
  10960. b.outLen = WC_SHA_DIGEST_SIZE;
  10961. EVP_MD_CTX_init(&md_ctx);
  10962. EVP_DigestInit(&md_ctx, EVP_sha1());
  10963. EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  10964. EVP_DigestFinal(&md_ctx, hash, 0);
  10965. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  10966. return -7402;
  10967. #endif /* NO_SHA */
  10968. #ifdef WOLFSSL_SHA224
  10969. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  10970. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  10971. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  10972. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  10973. e.inLen = XSTRLEN(e.input);
  10974. e.outLen = WC_SHA224_DIGEST_SIZE;
  10975. EVP_MD_CTX_init(&md_ctx);
  10976. EVP_DigestInit(&md_ctx, EVP_sha224());
  10977. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  10978. EVP_DigestFinal(&md_ctx, hash, 0);
  10979. if (XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0)
  10980. return -7403;
  10981. #endif /* WOLFSSL_SHA224 */
  10982. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  10983. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  10984. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  10985. "\x06\xC1";
  10986. d.inLen = XSTRLEN(d.input);
  10987. d.outLen = WC_SHA256_DIGEST_SIZE;
  10988. EVP_MD_CTX_init(&md_ctx);
  10989. EVP_DigestInit(&md_ctx, EVP_sha256());
  10990. EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  10991. EVP_DigestFinal(&md_ctx, hash, 0);
  10992. if (XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0)
  10993. return -7404;
  10994. #ifdef WOLFSSL_SHA384
  10995. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  10996. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  10997. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  10998. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  10999. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  11000. "\x74\x60\x39";
  11001. e.inLen = XSTRLEN(e.input);
  11002. e.outLen = WC_SHA384_DIGEST_SIZE;
  11003. EVP_MD_CTX_init(&md_ctx);
  11004. EVP_DigestInit(&md_ctx, EVP_sha384());
  11005. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  11006. EVP_DigestFinal(&md_ctx, hash, 0);
  11007. if (XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0)
  11008. return -7405;
  11009. #endif /* WOLFSSL_SHA384 */
  11010. #ifdef WOLFSSL_SHA512
  11011. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  11012. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  11013. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  11014. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  11015. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  11016. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  11017. "\x87\x4b\xe9\x09";
  11018. f.inLen = XSTRLEN(f.input);
  11019. f.outLen = WC_SHA512_DIGEST_SIZE;
  11020. EVP_MD_CTX_init(&md_ctx);
  11021. EVP_DigestInit(&md_ctx, EVP_sha512());
  11022. EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  11023. EVP_DigestFinal(&md_ctx, hash, 0);
  11024. if (XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0)
  11025. return -7406;
  11026. #endif /* WOLFSSL_SHA512 */
  11027. #ifndef NO_MD5
  11028. if (RAND_bytes(hash, sizeof(hash)) != 1)
  11029. return -7407;
  11030. c.input = "what do ya want for nothing?";
  11031. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  11032. "\x76";
  11033. c.inLen = XSTRLEN(c.input);
  11034. c.outLen = WC_MD5_DIGEST_SIZE;
  11035. HMAC(EVP_md5(),
  11036. "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen, hash, 0);
  11037. if (XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  11038. return -7408;
  11039. #endif /* NO_MD5 */
  11040. #ifndef NO_DES3
  11041. { /* des test */
  11042. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  11043. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11044. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11045. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11046. };
  11047. byte plain[24];
  11048. byte cipher[24];
  11049. const_DES_cblock key =
  11050. {
  11051. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  11052. };
  11053. DES_cblock iv =
  11054. {
  11055. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  11056. };
  11057. DES_key_schedule sched;
  11058. const byte verify[] =
  11059. {
  11060. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  11061. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  11062. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  11063. };
  11064. DES_key_sched(&key, &sched);
  11065. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  11066. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  11067. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  11068. return -7409;
  11069. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  11070. return -7410;
  11071. /* test changing iv */
  11072. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  11073. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  11074. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  11075. return -7411;
  11076. } /* end des test */
  11077. #endif /* NO_DES3 */
  11078. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  11079. if (openssl_aes_test() != 0)
  11080. return -7412;
  11081. #ifdef WOLFSSL_AES_128
  11082. { /* evp_cipher test: EVP_aes_128_cbc */
  11083. EVP_CIPHER_CTX ctx;
  11084. int idx, cipherSz, plainSz;
  11085. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  11086. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11087. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11088. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11089. };
  11090. const byte verify[] =
  11091. {
  11092. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  11093. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  11094. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  11095. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  11096. };
  11097. const byte verify2[] =
  11098. {
  11099. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  11100. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  11101. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  11102. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  11103. };
  11104. byte key[] = "0123456789abcdef "; /* align */
  11105. byte iv[] = "1234567890abcdef "; /* align */
  11106. byte cipher[AES_BLOCK_SIZE * 4];
  11107. byte plain [AES_BLOCK_SIZE * 4];
  11108. EVP_CIPHER_CTX_init(&ctx);
  11109. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  11110. return -7413;
  11111. if (EVP_CipherUpdate(&ctx, cipher, &idx, (byte*)msg, sizeof(msg)) == 0)
  11112. return -7414;
  11113. cipherSz = idx;
  11114. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0)
  11115. return -7415;
  11116. cipherSz += idx;
  11117. if ((cipherSz != (int)sizeof(verify)) &&
  11118. XMEMCMP(cipher, verify, cipherSz))
  11119. return -7416;
  11120. EVP_CIPHER_CTX_init(&ctx);
  11121. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  11122. return -7417;
  11123. if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, cipherSz) == 0)
  11124. return -7418;
  11125. plainSz = idx;
  11126. if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) == 0)
  11127. return -7419;
  11128. plainSz += idx;
  11129. if ((plainSz != sizeof(msg)) || XMEMCMP(plain, msg, sizeof(msg)))
  11130. return -7420;
  11131. EVP_CIPHER_CTX_init(&ctx);
  11132. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  11133. return -7421;
  11134. if (EVP_CipherUpdate(&ctx, cipher, &idx, msg, AES_BLOCK_SIZE) == 0)
  11135. return -7422;
  11136. cipherSz = idx;
  11137. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0)
  11138. return -7423;
  11139. cipherSz += idx;
  11140. if ((cipherSz != (int)sizeof(verify2)) ||
  11141. XMEMCMP(cipher, verify2, cipherSz))
  11142. return -7424;
  11143. } /* end evp_cipher test: EVP_aes_128_cbc*/
  11144. #endif /* WOLFSSL_AES_128 */
  11145. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  11146. { /* evp_cipher test: EVP_aes_256_ecb*/
  11147. EVP_CIPHER_CTX ctx;
  11148. const byte msg[] =
  11149. {
  11150. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11151. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11152. };
  11153. const byte verify[] =
  11154. {
  11155. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  11156. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  11157. };
  11158. const byte key[] =
  11159. {
  11160. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11161. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11162. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11163. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11164. };
  11165. byte cipher[AES_BLOCK_SIZE * 4];
  11166. byte plain [AES_BLOCK_SIZE * 4];
  11167. EVP_CIPHER_CTX_init(&ctx);
  11168. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key,
  11169. NULL, 1) == 0)
  11170. return -7425;
  11171. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  11172. return -7426;
  11173. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  11174. return -7427;
  11175. EVP_CIPHER_CTX_init(&ctx);
  11176. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key,
  11177. NULL, 0) == 0)
  11178. return -7428;
  11179. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  11180. return -7429;
  11181. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  11182. return -7430;
  11183. } /* end evp_cipher test */
  11184. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  11185. #define OPENSSL_TEST_ERROR (-10000)
  11186. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  11187. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  11188. {
  11189. /* Test: AES_encrypt/decrypt/set Key */
  11190. AES_KEY enc;
  11191. #ifdef HAVE_AES_DECRYPT
  11192. AES_KEY dec;
  11193. #endif
  11194. const byte msg[] =
  11195. {
  11196. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11197. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11198. };
  11199. const byte verify[] =
  11200. {
  11201. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  11202. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  11203. };
  11204. const byte key[] =
  11205. {
  11206. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11207. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11208. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11209. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11210. };
  11211. byte plain[sizeof(msg)];
  11212. byte cipher[sizeof(msg)];
  11213. printf("openSSL extra test\n") ;
  11214. AES_set_encrypt_key(key, sizeof(key)*8, &enc);
  11215. AES_set_decrypt_key(key, sizeof(key)*8, &dec);
  11216. AES_encrypt(msg, cipher, &enc);
  11217. #ifdef HAVE_AES_DECRYPT
  11218. AES_decrypt(cipher, plain, &dec);
  11219. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  11220. return OPENSSL_TEST_ERROR-60;
  11221. #endif /* HAVE_AES_DECRYPT */
  11222. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  11223. return OPENSSL_TEST_ERROR-61;
  11224. }
  11225. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  11226. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  11227. #ifdef WOLFSSL_AES_COUNTER
  11228. {
  11229. byte plainBuff [64];
  11230. byte cipherBuff[64];
  11231. #ifdef WOLFSSL_AES_128
  11232. const byte ctrKey[] =
  11233. {
  11234. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  11235. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  11236. };
  11237. const byte ctrIv[] =
  11238. {
  11239. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11240. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11241. };
  11242. const byte ctrPlain[] =
  11243. {
  11244. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11245. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  11246. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  11247. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  11248. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  11249. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  11250. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  11251. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  11252. };
  11253. const byte ctrCipher[] =
  11254. {
  11255. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  11256. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  11257. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  11258. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  11259. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  11260. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  11261. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  11262. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  11263. };
  11264. const byte oddCipher[] =
  11265. {
  11266. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  11267. 0xc2
  11268. };
  11269. #endif /* WOLFSSL_AES_128 */
  11270. #ifdef WOLFSSL_AES_192
  11271. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  11272. * NIST Special Publication 800-38A */
  11273. const byte ctr192Key[] =
  11274. {
  11275. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  11276. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  11277. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  11278. };
  11279. const byte ctr192Iv[] =
  11280. {
  11281. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11282. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11283. };
  11284. const byte ctr192Plain[] =
  11285. {
  11286. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11287. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11288. };
  11289. const byte ctr192Cipher[] =
  11290. {
  11291. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  11292. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  11293. };
  11294. #endif /* WOLFSSL_AES_192 */
  11295. #ifdef WOLFSSL_AES_256
  11296. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  11297. * NIST Special Publication 800-38A */
  11298. const byte ctr256Key[] =
  11299. {
  11300. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  11301. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  11302. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  11303. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  11304. };
  11305. const byte ctr256Iv[] =
  11306. {
  11307. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  11308. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  11309. };
  11310. const byte ctr256Plain[] =
  11311. {
  11312. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11313. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  11314. };
  11315. const byte ctr256Cipher[] =
  11316. {
  11317. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  11318. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  11319. };
  11320. #endif /* WOLFSSL_AES_256 */
  11321. EVP_CIPHER_CTX en;
  11322. EVP_CIPHER_CTX de;
  11323. #ifdef WOLFSSL_AES_128
  11324. EVP_CIPHER_CTX *p_en;
  11325. EVP_CIPHER_CTX *p_de;
  11326. EVP_CIPHER_CTX_init(&en);
  11327. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  11328. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  11329. return -7431;
  11330. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain,
  11331. AES_BLOCK_SIZE*4) == 0)
  11332. return -7432;
  11333. EVP_CIPHER_CTX_init(&de);
  11334. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  11335. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  11336. return -7433;
  11337. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  11338. AES_BLOCK_SIZE*4) == 0)
  11339. return -7434;
  11340. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  11341. return -7435;
  11342. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  11343. return -7436;
  11344. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  11345. if(p_en == NULL)return -7437;
  11346. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  11347. if(p_de == NULL)return -7438;
  11348. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  11349. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  11350. return -7439;
  11351. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  11352. AES_BLOCK_SIZE*4) == 0)
  11353. return -7440;
  11354. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  11355. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  11356. return -7441;
  11357. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  11358. AES_BLOCK_SIZE*4) == 0)
  11359. return -7442;
  11360. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  11361. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  11362. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  11363. return -7443;
  11364. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  11365. return -7444;
  11366. EVP_CIPHER_CTX_init(&en);
  11367. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  11368. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  11369. return -7445;
  11370. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  11371. return -7446;
  11372. EVP_CIPHER_CTX_init(&de);
  11373. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  11374. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  11375. return -7447;
  11376. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  11377. return -7448;
  11378. if (XMEMCMP(plainBuff, ctrPlain, 9))
  11379. return -7449;
  11380. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  11381. return -7450;
  11382. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  11383. return -7451;
  11384. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  11385. return -7452;
  11386. if (XMEMCMP(plainBuff, ctrPlain, 9))
  11387. return -7453;
  11388. if (XMEMCMP(cipherBuff, oddCipher, 9))
  11389. return -7454;
  11390. #endif /* WOLFSSL_AES_128 */
  11391. #ifdef WOLFSSL_AES_192
  11392. EVP_CIPHER_CTX_init(&en);
  11393. if (EVP_CipherInit(&en, EVP_aes_192_ctr(),
  11394. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  11395. return -7455;
  11396. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr192Plain,
  11397. AES_BLOCK_SIZE) == 0)
  11398. return -7456;
  11399. EVP_CIPHER_CTX_init(&de);
  11400. if (EVP_CipherInit(&de, EVP_aes_192_ctr(),
  11401. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  11402. return -7457;
  11403. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  11404. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  11405. AES_BLOCK_SIZE) == 0)
  11406. return -7458;
  11407. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  11408. return -7459;
  11409. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  11410. return -7460;
  11411. #endif /* WOLFSSL_AES_192 */
  11412. #ifdef WOLFSSL_AES_256
  11413. EVP_CIPHER_CTX_init(&en);
  11414. if (EVP_CipherInit(&en, EVP_aes_256_ctr(),
  11415. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  11416. return -7461;
  11417. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr256Plain,
  11418. AES_BLOCK_SIZE) == 0)
  11419. return -7462;
  11420. EVP_CIPHER_CTX_init(&de);
  11421. if (EVP_CipherInit(&de, EVP_aes_256_ctr(),
  11422. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  11423. return -7463;
  11424. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  11425. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  11426. AES_BLOCK_SIZE) == 0)
  11427. return -7464;
  11428. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  11429. return -7465;
  11430. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  11431. return -7466;
  11432. #endif /* WOLFSSL_AES_256 */
  11433. }
  11434. #endif /* HAVE_AES_COUNTER */
  11435. #ifdef WOLFSSL_AES_128
  11436. {
  11437. /* EVP_CipherUpdate test */
  11438. const byte cbcPlain[] =
  11439. {
  11440. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  11441. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  11442. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  11443. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  11444. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  11445. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  11446. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  11447. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  11448. };
  11449. byte key[] = "0123456789abcdef "; /* align */
  11450. byte iv[] = "1234567890abcdef "; /* align */
  11451. byte cipher[AES_BLOCK_SIZE * 4];
  11452. byte plain [AES_BLOCK_SIZE * 4];
  11453. EVP_CIPHER_CTX en;
  11454. EVP_CIPHER_CTX de;
  11455. int outlen ;
  11456. int total = 0;
  11457. EVP_CIPHER_CTX_init(&en);
  11458. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  11459. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  11460. return -7467;
  11461. /* openSSL compatibility, if(inlen == 0)return 1; */
  11462. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  11463. (byte*)cbcPlain, 0) != 1)
  11464. return -7468;
  11465. EVP_CIPHER_CTX_init(&en);
  11466. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  11467. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  11468. return -7469;
  11469. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  11470. (byte*)cbcPlain, 9) == 0)
  11471. return -7470;
  11472. if(outlen != 0)
  11473. return -7471;
  11474. total += outlen;
  11475. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen,
  11476. (byte*)&cbcPlain[9] , 9) == 0)
  11477. return -7472;
  11478. if(outlen != 16)
  11479. return -7473;
  11480. total += outlen;
  11481. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  11482. return -7474;
  11483. if(outlen != 16)
  11484. return -7475;
  11485. total += outlen;
  11486. if(total != 32)
  11487. return -7476;
  11488. total = 0;
  11489. EVP_CIPHER_CTX_init(&de);
  11490. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  11491. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  11492. return -7477;
  11493. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  11494. return -7478;
  11495. if(outlen != 0)
  11496. return -7479;
  11497. total += outlen;
  11498. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  11499. (byte*)&cipher[6], 12) == 0)
  11500. return -7480;
  11501. if(outlen != 0)
  11502. total += outlen;
  11503. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  11504. (byte*)&cipher[6+12], 14) == 0)
  11505. return -7481;
  11506. if(outlen != 16)
  11507. return -7482;
  11508. total += outlen;
  11509. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  11510. return -7483;
  11511. if(outlen != 2)
  11512. return -7484;
  11513. total += outlen;
  11514. if(total != 18)
  11515. return -7485;
  11516. if (XMEMCMP(plain, cbcPlain, 18))
  11517. return -7486;
  11518. total = 0;
  11519. EVP_CIPHER_CTX_init(&en);
  11520. if (EVP_EncryptInit(&en, EVP_aes_128_cbc(),
  11521. (unsigned char*)key, (unsigned char*)iv) == 0)
  11522. return -7487;
  11523. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  11524. return -7488;
  11525. if(outlen != 0)
  11526. return -7489;
  11527. total += outlen;
  11528. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  11529. return -7490;
  11530. if(outlen != 16)
  11531. return -7491;
  11532. total += outlen;
  11533. if (EVP_EncryptFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  11534. return -7492;
  11535. if(outlen != 16)
  11536. return -7493;
  11537. total += outlen;
  11538. if(total != 32)
  11539. return 3438;
  11540. total = 0;
  11541. EVP_CIPHER_CTX_init(&de);
  11542. if (EVP_DecryptInit(&de, EVP_aes_128_cbc(),
  11543. (unsigned char*)key, (unsigned char*)iv) == 0)
  11544. return -7494;
  11545. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  11546. return -7495;
  11547. if(outlen != 0)
  11548. return -7496;
  11549. total += outlen;
  11550. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  11551. return -7497;
  11552. if(outlen != 0)
  11553. total += outlen;
  11554. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  11555. return -7498;
  11556. if(outlen != 16)
  11557. return -7499;
  11558. total += outlen;
  11559. if (EVP_DecryptFinal(&de, (byte*)&plain[total], &outlen) == 0)
  11560. return -7500;
  11561. if(outlen != 2)
  11562. return -7501;
  11563. total += outlen;
  11564. if(total != 18)
  11565. return 3447;
  11566. if (XMEMCMP(plain, cbcPlain, 18))
  11567. return -7502;
  11568. }
  11569. #endif /* WOLFSSL_AES_128 */
  11570. #endif /* ifndef NO_AES */
  11571. return 0;
  11572. }
  11573. int openSSL_evpMD_test(void)
  11574. {
  11575. int ret = 0;
  11576. #if !defined(NO_SHA256) && !defined(NO_SHA)
  11577. WOLFSSL_EVP_MD_CTX* ctx;
  11578. WOLFSSL_EVP_MD_CTX* ctx2;
  11579. ctx = EVP_MD_CTX_create();
  11580. ctx2 = EVP_MD_CTX_create();
  11581. ret = EVP_DigestInit(ctx, EVP_sha256());
  11582. if (ret != SSL_SUCCESS) {
  11583. ret = -7600;
  11584. goto openSSL_evpMD_test_done;
  11585. }
  11586. ret = EVP_MD_CTX_copy(ctx2, ctx);
  11587. if (ret != SSL_SUCCESS) {
  11588. ret = -7601;
  11589. goto openSSL_evpMD_test_done;
  11590. }
  11591. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  11592. ret = -7602;
  11593. goto openSSL_evpMD_test_done;
  11594. }
  11595. ret = EVP_DigestInit(ctx, EVP_sha1());
  11596. if (ret != SSL_SUCCESS) {
  11597. ret = -7603;
  11598. goto openSSL_evpMD_test_done;
  11599. }
  11600. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  11601. ret = -7604;
  11602. goto openSSL_evpMD_test_done;
  11603. }
  11604. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  11605. if (ret != SSL_SUCCESS) {
  11606. ret = -7605;
  11607. goto openSSL_evpMD_test_done;
  11608. }
  11609. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  11610. ret = -7606;
  11611. goto openSSL_evpMD_test_done;
  11612. }
  11613. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  11614. ret = -7607;
  11615. goto openSSL_evpMD_test_done;
  11616. }
  11617. ret = 0; /* got to success state without jumping to end with a fail */
  11618. openSSL_evpMD_test_done:
  11619. EVP_MD_CTX_destroy(ctx);
  11620. EVP_MD_CTX_destroy(ctx2);
  11621. #endif /* NO_SHA256 */
  11622. return ret;
  11623. }
  11624. #ifdef DEBUG_SIGN
  11625. static void show(const char *title, const char *p, unsigned int s) {
  11626. char* i;
  11627. printf("%s: ", title);
  11628. for (i = p;
  11629. i < p + s;
  11630. printf("%c", *i), i++);
  11631. printf("\n");
  11632. }
  11633. #else
  11634. #define show(a,b,c)
  11635. #endif
  11636. #define FOURK_BUFF 4096
  11637. #define ERR_BASE_PKEY -5000
  11638. int openssl_pkey0_test(void)
  11639. {
  11640. int ret = 0;
  11641. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  11642. byte* prvTmp;
  11643. byte* pubTmp;
  11644. int prvBytes;
  11645. int pubBytes;
  11646. RSA *prvRsa = NULL;
  11647. RSA *pubRsa = NULL;
  11648. EVP_PKEY *prvPkey = NULL;
  11649. EVP_PKEY *pubPkey = NULL;
  11650. EVP_PKEY_CTX *enc = NULL;
  11651. EVP_PKEY_CTX *dec = NULL;
  11652. byte in[] = "Everyone gets Friday off.";
  11653. byte out[256];
  11654. size_t outlen;
  11655. size_t keySz;
  11656. byte plain[256];
  11657. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11658. FILE *keyFile, *keypubFile;
  11659. char cliKey[] = "./certs/client-key.der";
  11660. char cliKeypub[] = "./certs/client-keyPub.der";
  11661. #endif
  11662. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11663. if (prvTmp == NULL)
  11664. return ERR_BASE_PKEY-1;
  11665. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11666. if (pubTmp == NULL) {
  11667. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11668. return ERR_BASE_PKEY-2;
  11669. }
  11670. #ifdef USE_CERT_BUFFERS_1024
  11671. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  11672. prvBytes = sizeof_client_key_der_1024;
  11673. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  11674. pubBytes = sizeof_client_keypub_der_1024;
  11675. #elif defined(USE_CERT_BUFFERS_2048)
  11676. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  11677. prvBytes = sizeof_client_key_der_2048;
  11678. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  11679. pubBytes = sizeof_client_keypub_der_2048;
  11680. #else
  11681. keyFile = fopen(cliKey, "rb");
  11682. if (!keyFile) {
  11683. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11684. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11685. err_sys("can't open ./certs/client-key.der, "
  11686. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  11687. return ERR_BASE_PKEY-3;
  11688. }
  11689. prvBytes = (int)fread(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  11690. fclose(keyFile);
  11691. keypubFile = fopen(cliKeypub, "rb");
  11692. if (!keypubFile) {
  11693. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11694. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11695. err_sys("can't open ./certs/client-cert.der, "
  11696. "Please run from wolfSSL home dir", -4);
  11697. return ERR_BASE_PKEY-4;
  11698. }
  11699. pubBytes = (int)fread(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  11700. fclose(keypubFile);
  11701. #endif /* USE_CERT_BUFFERS */
  11702. prvRsa = wolfSSL_RSA_new();
  11703. pubRsa = wolfSSL_RSA_new();
  11704. if((prvRsa == NULL) || (pubRsa == NULL)){
  11705. printf("error with RSA_new\n");
  11706. ret = ERR_BASE_PKEY-10;
  11707. goto openssl_pkey0_test_done;
  11708. }
  11709. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  11710. if(ret != SSL_SUCCESS){
  11711. printf("error with RSA_LoadDer_ex\n");
  11712. ret = ERR_BASE_PKEY-11;
  11713. goto openssl_pkey0_test_done;
  11714. }
  11715. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  11716. if(ret != SSL_SUCCESS){
  11717. printf("error with RSA_LoadDer_ex\n");
  11718. ret = ERR_BASE_PKEY-12;
  11719. goto openssl_pkey0_test_done;
  11720. }
  11721. keySz = (size_t)RSA_size(pubRsa);
  11722. prvPkey = wolfSSL_PKEY_new();
  11723. pubPkey = wolfSSL_PKEY_new();
  11724. if((prvPkey == NULL) || (pubPkey == NULL)){
  11725. printf("error with PKEY_new\n");
  11726. ret = ERR_BASE_PKEY-13;
  11727. goto openssl_pkey0_test_done;
  11728. }
  11729. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  11730. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  11731. if(ret != 2){
  11732. printf("error with PKEY_set1_RSA\n");
  11733. ret = ERR_BASE_PKEY-14;
  11734. goto openssl_pkey0_test_done;
  11735. }
  11736. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  11737. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  11738. if((dec == NULL)||(enc==NULL)){
  11739. printf("error with EVP_PKEY_CTX_new\n");
  11740. ret = ERR_BASE_PKEY-15;
  11741. goto openssl_pkey0_test_done;
  11742. }
  11743. ret = EVP_PKEY_decrypt_init(dec);
  11744. if (ret != 1) {
  11745. printf("error with decrypt init\n");
  11746. ret = ERR_BASE_PKEY-16;
  11747. goto openssl_pkey0_test_done;
  11748. }
  11749. ret = EVP_PKEY_encrypt_init(enc);
  11750. if (ret != 1) {
  11751. printf("error with encrypt init\n");
  11752. ret = ERR_BASE_PKEY-17;
  11753. goto openssl_pkey0_test_done;
  11754. }
  11755. XMEMSET(out, 0, sizeof(out));
  11756. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  11757. if (ret != 1) {
  11758. printf("error encrypting msg\n");
  11759. ret = ERR_BASE_PKEY-18;
  11760. goto openssl_pkey0_test_done;
  11761. }
  11762. show("encrypted msg", out, outlen);
  11763. XMEMSET(plain, 0, sizeof(plain));
  11764. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  11765. if (ret != 1) {
  11766. printf("error decrypting msg\n");
  11767. ret = ERR_BASE_PKEY-19;
  11768. goto openssl_pkey0_test_done;
  11769. }
  11770. show("decrypted msg", plain, outlen);
  11771. /* RSA_PKCS1_OAEP_PADDING test */
  11772. ret = EVP_PKEY_decrypt_init(dec);
  11773. if (ret != 1) {
  11774. printf("error with decrypt init\n");
  11775. ret = ERR_BASE_PKEY-30;
  11776. goto openssl_pkey0_test_done;
  11777. }
  11778. ret = EVP_PKEY_encrypt_init(enc);
  11779. if (ret != 1) {
  11780. printf("error with encrypt init\n");
  11781. ret = ERR_BASE_PKEY-31;
  11782. goto openssl_pkey0_test_done;
  11783. }
  11784. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  11785. printf("first set rsa padding error\n");
  11786. ret = ERR_BASE_PKEY-32;
  11787. goto openssl_pkey0_test_done;
  11788. }
  11789. #ifndef HAVE_FIPS
  11790. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  11791. printf("second set rsa padding error\n");
  11792. ret = ERR_BASE_PKEY-33;
  11793. goto openssl_pkey0_test_done;
  11794. }
  11795. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  11796. printf("third set rsa padding error\n");
  11797. ret = ERR_BASE_PKEY-34;
  11798. goto openssl_pkey0_test_done;
  11799. }
  11800. #endif
  11801. XMEMSET(out, 0, sizeof(out));
  11802. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  11803. if (ret != 1) {
  11804. printf("error encrypting msg\n");
  11805. ret = ERR_BASE_PKEY-35;
  11806. goto openssl_pkey0_test_done;
  11807. }
  11808. show("encrypted msg", out, outlen);
  11809. XMEMSET(plain, 0, sizeof(plain));
  11810. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  11811. if (ret != 1) {
  11812. printf("error decrypting msg\n");
  11813. ret = ERR_BASE_PKEY-36;
  11814. goto openssl_pkey0_test_done;
  11815. }
  11816. show("decrypted msg", plain, outlen);
  11817. ret = 0; /* made it to this point without error then set success */
  11818. openssl_pkey0_test_done:
  11819. wolfSSL_RSA_free(prvRsa);
  11820. wolfSSL_RSA_free(pubRsa);
  11821. EVP_PKEY_free(pubPkey);
  11822. EVP_PKEY_free(prvPkey);
  11823. EVP_PKEY_CTX_free(dec);
  11824. EVP_PKEY_CTX_free(enc);
  11825. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11826. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11827. #endif /* NO_RSA */
  11828. return ret;
  11829. }
  11830. int openssl_pkey1_test(void)
  11831. {
  11832. int ret = 0;
  11833. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  11834. !defined(NO_SHA) && !defined(USE_CERT_BUFFERS_1024)
  11835. EVP_PKEY_CTX* dec = NULL;
  11836. EVP_PKEY_CTX* enc = NULL;
  11837. EVP_PKEY* pubKey = NULL;
  11838. EVP_PKEY* prvKey = NULL;
  11839. X509* x509 = NULL;
  11840. const unsigned char msg[] = "sugar slapped";
  11841. const unsigned char* clikey;
  11842. unsigned char tmp[FOURK_BUF];
  11843. long cliKeySz;
  11844. unsigned char cipher[256];
  11845. unsigned char plain[256];
  11846. size_t outlen = sizeof(cipher);
  11847. #if defined(USE_CERT_BUFFERS_2048)
  11848. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  11849. cliKeySz = (long)sizeof_client_key_der_2048;
  11850. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  11851. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  11852. #else
  11853. FILE* f;
  11854. f = fopen(clientKey, "rb");
  11855. if (!f) {
  11856. err_sys("can't open ./certs/client-key.der, "
  11857. "Please run from wolfSSL home dir", -41);
  11858. return -7700;
  11859. }
  11860. cliKeySz = (long)fread(tmp, 1, FOURK_BUF, f);
  11861. fclose(f);
  11862. /* using existing wolfSSL api to get public and private key */
  11863. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  11864. #endif /* USE_CERT_BUFFERS */
  11865. clikey = tmp;
  11866. if ((prvKey = EVP_PKEY_new()) == NULL) {
  11867. return -7701;
  11868. }
  11869. EVP_PKEY_free(prvKey);
  11870. prvKey = NULL;
  11871. if (x509 == NULL) {
  11872. ret = -7702;
  11873. goto openssl_pkey1_test_done;
  11874. }
  11875. pubKey = X509_get_pubkey(x509);
  11876. if (pubKey == NULL) {
  11877. ret = -7703;
  11878. goto openssl_pkey1_test_done;
  11879. }
  11880. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  11881. if (prvKey == NULL) {
  11882. ret = -7704;
  11883. goto openssl_pkey1_test_done;
  11884. }
  11885. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  11886. if (EVP_PKEY_bits(prvKey) != 2048) {
  11887. ret = -7705;
  11888. goto openssl_pkey1_test_done;
  11889. }
  11890. if (EVP_PKEY_size(prvKey) != 256) {
  11891. ret = -7706;
  11892. goto openssl_pkey1_test_done;
  11893. }
  11894. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  11895. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  11896. if (dec == NULL || enc == NULL) {
  11897. ret = -7707;
  11898. goto openssl_pkey1_test_done;
  11899. }
  11900. if (EVP_PKEY_decrypt_init(dec) != 1) {
  11901. ret = -7708;
  11902. goto openssl_pkey1_test_done;
  11903. }
  11904. if (EVP_PKEY_encrypt_init(enc) != 1) {
  11905. ret = -7709;
  11906. goto openssl_pkey1_test_done;
  11907. }
  11908. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  11909. ret = -7710;
  11910. goto openssl_pkey1_test_done;
  11911. }
  11912. #ifndef HAVE_FIPS
  11913. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  11914. ret = -7711;
  11915. goto openssl_pkey1_test_done;
  11916. }
  11917. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  11918. ret = -7712;
  11919. goto openssl_pkey1_test_done;
  11920. }
  11921. #endif
  11922. XMEMSET(cipher, 0, sizeof(cipher));
  11923. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  11924. ret = -7713;
  11925. goto openssl_pkey1_test_done;
  11926. }
  11927. XMEMSET(plain, 0, sizeof(plain));
  11928. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, sizeof(cipher)) != 1) {
  11929. ret = -7714;
  11930. goto openssl_pkey1_test_done;
  11931. }
  11932. openssl_pkey1_test_done:
  11933. if (pubKey != NULL) {
  11934. EVP_PKEY_free(pubKey);
  11935. }
  11936. if (prvKey != NULL) {
  11937. EVP_PKEY_free(prvKey);
  11938. }
  11939. if (dec != NULL) {
  11940. EVP_PKEY_CTX_free(dec);
  11941. }
  11942. if (enc != NULL) {
  11943. EVP_PKEY_CTX_free(enc);
  11944. }
  11945. if (x509 != NULL) {
  11946. X509_free(x509);
  11947. }
  11948. #endif
  11949. return ret;
  11950. }
  11951. #define ERR_BASE_EVPSIG -5100
  11952. int openssl_evpSig_test(void)
  11953. {
  11954. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  11955. byte* prvTmp;
  11956. byte* pubTmp;
  11957. int prvBytes;
  11958. int pubBytes;
  11959. RSA *prvRsa;
  11960. RSA *pubRsa;
  11961. EVP_PKEY *prvPkey;
  11962. EVP_PKEY *pubPkey;
  11963. EVP_MD_CTX* sign;
  11964. EVP_MD_CTX* verf;
  11965. char msg[] = "see spot run";
  11966. unsigned char sig[256];
  11967. unsigned int sigSz;
  11968. const void* pt;
  11969. unsigned int count;
  11970. int ret, ret1, ret2;
  11971. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11972. FILE *keyFile, *keypubFile;
  11973. char cliKey[] = "./certs/client-key.der";
  11974. char cliKeypub[] = "./certs/client-keyPub.der";
  11975. #endif
  11976. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11977. if (prvTmp == NULL)
  11978. return ERR_BASE_EVPSIG-1;
  11979. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11980. if (pubTmp == NULL) {
  11981. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11982. return ERR_BASE_EVPSIG-2;
  11983. }
  11984. #ifdef USE_CERT_BUFFERS_1024
  11985. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  11986. prvBytes = sizeof_client_key_der_1024;
  11987. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  11988. pubBytes = sizeof_client_keypub_der_1024;
  11989. #elif defined(USE_CERT_BUFFERS_2048)
  11990. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  11991. prvBytes = sizeof_client_key_der_2048;
  11992. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  11993. pubBytes = sizeof_client_keypub_der_2048;
  11994. #else
  11995. keyFile = fopen(cliKey, "rb");
  11996. if (!keyFile) {
  11997. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11998. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11999. err_sys("can't open ./certs/client-key.der, "
  12000. "Please run from wolfSSL home dir", -40);
  12001. return ERR_BASE_EVPSIG-3;
  12002. }
  12003. prvBytes = (int)fread(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  12004. fclose(keyFile);
  12005. keypubFile = fopen(cliKeypub, "rb");
  12006. if (!keypubFile) {
  12007. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12008. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12009. err_sys("can't open ./certs/client-cert.der, "
  12010. "Please run from wolfSSL home dir", -41);
  12011. return ERR_BASE_EVPSIG-4;
  12012. }
  12013. pubBytes = (int)fread(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  12014. fclose(keypubFile);
  12015. #endif /* USE_CERT_BUFFERS */
  12016. prvRsa = wolfSSL_RSA_new();
  12017. pubRsa = wolfSSL_RSA_new();
  12018. if((prvRsa == NULL) || (pubRsa == NULL)){
  12019. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12020. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12021. err_sys("ERROR with RSA_new", -41);
  12022. return ERR_BASE_EVPSIG-5;
  12023. }
  12024. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  12025. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  12026. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  12027. printf("error with RSA_LoadDer_ex\n");
  12028. return ERR_BASE_EVPSIG-6;
  12029. }
  12030. prvPkey = wolfSSL_PKEY_new();
  12031. pubPkey = wolfSSL_PKEY_new();
  12032. if((prvPkey == NULL) || (pubPkey == NULL)){
  12033. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12034. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12035. printf("error with KEY_new\n");
  12036. return ERR_BASE_EVPSIG-7;
  12037. }
  12038. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  12039. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  12040. if((ret1 != 1) || (ret2 != 1)){
  12041. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12042. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12043. printf("error with EVP_PKEY_set1_RSA\n");
  12044. return ERR_BASE_EVPSIG-8;
  12045. }
  12046. /****************** sign and verify *******************/
  12047. sign = EVP_MD_CTX_create();
  12048. verf = EVP_MD_CTX_create();
  12049. if((sign == NULL)||(verf == NULL)){
  12050. printf("error with EVP_MD_CTX_create\n");
  12051. return ERR_BASE_EVPSIG-10;
  12052. }
  12053. ret = EVP_SignInit(sign, EVP_sha1());
  12054. if(ret != SSL_SUCCESS){
  12055. printf("error with EVP_SignInit\n");
  12056. return ERR_BASE_EVPSIG-11;
  12057. }
  12058. count = sizeof(msg);
  12059. show("message = ", (char *)msg, count);
  12060. /* sign */
  12061. XMEMSET(sig, 0, sizeof(sig));
  12062. pt = (const void*)msg;
  12063. ret1 = EVP_SignUpdate(sign, pt, count);
  12064. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  12065. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  12066. printf("error with EVP_MD_CTX_create\n");
  12067. return ERR_BASE_EVPSIG-12;
  12068. }
  12069. show("signature = ", (char *)sig, sigSz);
  12070. /* verify */
  12071. pt = (const void*)msg;
  12072. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  12073. ret2 = EVP_VerifyUpdate(verf, pt, count);
  12074. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  12075. printf("error with EVP_Verify\n");
  12076. return ERR_BASE_EVPSIG-13;
  12077. }
  12078. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  12079. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12080. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12081. printf("error with EVP_VerifyFinal\n");
  12082. return ERR_BASE_EVPSIG-14;
  12083. }
  12084. /* expect fail without update */
  12085. EVP_VerifyInit(verf, EVP_sha1());
  12086. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  12087. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12088. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12089. printf("EVP_VerifyInit without update not detected\n");
  12090. return ERR_BASE_EVPSIG-15;
  12091. }
  12092. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12093. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12094. EVP_MD_CTX_destroy(sign);
  12095. EVP_MD_CTX_destroy(verf);
  12096. wolfSSL_RSA_free(prvRsa);
  12097. wolfSSL_RSA_free(pubRsa);
  12098. EVP_PKEY_free(pubPkey);
  12099. EVP_PKEY_free(prvPkey);
  12100. #endif /* NO_RSA */
  12101. return 0;
  12102. }
  12103. #endif /* OPENSSL_EXTRA */
  12104. #ifndef NO_PWDBASED
  12105. #ifdef HAVE_SCRYPT
  12106. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  12107. int scrypt_test(void)
  12108. {
  12109. int ret;
  12110. byte derived[64];
  12111. const byte verify1[] = {
  12112. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  12113. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  12114. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  12115. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  12116. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  12117. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  12118. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  12119. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  12120. };
  12121. const byte verify2[] = {
  12122. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  12123. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  12124. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  12125. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  12126. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  12127. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  12128. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  12129. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  12130. };
  12131. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_INTEL_QA)
  12132. const byte verify3[] = {
  12133. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  12134. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  12135. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  12136. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  12137. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  12138. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  12139. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  12140. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  12141. };
  12142. #endif
  12143. #ifdef SCRYPT_TEST_ALL
  12144. /* Test case is very slow.
  12145. * Use for confirmation after code change or new platform.
  12146. */
  12147. const byte verify4[] = {
  12148. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  12149. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  12150. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  12151. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  12152. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  12153. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  12154. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  12155. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  12156. };
  12157. #endif
  12158. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  12159. if (ret != 0)
  12160. return -7800;
  12161. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  12162. return -7801;
  12163. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  12164. sizeof(verify2));
  12165. if (ret != 0)
  12166. return -7802;
  12167. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  12168. return -7803;
  12169. /* Don't run these test on embedded, since they use large mallocs */
  12170. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_INTEL_QA)
  12171. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  12172. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  12173. if (ret != 0)
  12174. return -7804;
  12175. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  12176. return -7805;
  12177. #ifdef SCRYPT_TEST_ALL
  12178. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  12179. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  12180. if (ret != 0)
  12181. return -7806;
  12182. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  12183. return -7807;
  12184. #endif
  12185. #endif /* !BENCH_EMBEDDED && !HAVE_INTEL_QA */
  12186. return 0;
  12187. }
  12188. #endif
  12189. int pkcs12_test(void)
  12190. {
  12191. const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  12192. 0x00, 0x00 };
  12193. const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  12194. const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  12195. 0x00, 0x67, 0x00, 0x00 };
  12196. const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  12197. byte derived[64];
  12198. const byte verify[] = {
  12199. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  12200. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  12201. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  12202. };
  12203. const byte verify2[] = {
  12204. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  12205. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  12206. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  12207. };
  12208. int id = 1;
  12209. int kLen = 24;
  12210. int iterations = 1;
  12211. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  12212. iterations, kLen, WC_SHA256, id);
  12213. if (ret < 0)
  12214. return -7900;
  12215. if ( (ret = XMEMCMP(derived, verify, kLen)) != 0)
  12216. return -7901;
  12217. iterations = 1000;
  12218. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  12219. iterations, kLen, WC_SHA256, id);
  12220. if (ret < 0)
  12221. return -7902;
  12222. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  12223. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  12224. if (ret < 0)
  12225. return -7903;
  12226. if ( (ret = XMEMCMP(derived, verify2, 24)) != 0)
  12227. return -7904;
  12228. return 0;
  12229. }
  12230. int pbkdf2_test(void)
  12231. {
  12232. char passwd[] = "passwordpassword";
  12233. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  12234. int iterations = 2048;
  12235. int kLen = 24;
  12236. byte derived[64];
  12237. const byte verify[] = {
  12238. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  12239. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  12240. };
  12241. int ret = wc_PBKDF2(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt, 8,
  12242. iterations, kLen, WC_SHA256);
  12243. if (ret != 0)
  12244. return ret;
  12245. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  12246. return -8000;
  12247. return 0;
  12248. }
  12249. #ifndef NO_SHA
  12250. int pbkdf1_test(void)
  12251. {
  12252. char passwd[] = "password";
  12253. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  12254. int iterations = 1000;
  12255. int kLen = 16;
  12256. byte derived[16];
  12257. const byte verify[] = {
  12258. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F, 0xAF, 0x10, 0xEB, 0xFB,
  12259. 0x4A, 0x3D, 0x2A, 0x20
  12260. };
  12261. wc_PBKDF1(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt, 8, iterations,
  12262. kLen, WC_SHA);
  12263. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  12264. return -8100;
  12265. return 0;
  12266. }
  12267. #endif
  12268. int pwdbased_test(void)
  12269. {
  12270. int ret = 0;
  12271. #ifndef NO_SHA
  12272. ret = pbkdf1_test();
  12273. if (ret != 0)
  12274. return ret;
  12275. #endif
  12276. ret = pbkdf2_test();
  12277. if (ret != 0)
  12278. return ret;
  12279. ret = pkcs12_test();
  12280. if (ret != 0)
  12281. return ret;
  12282. #ifdef HAVE_SCRYPT
  12283. ret = scrypt_test();
  12284. if (ret != 0)
  12285. return ret;
  12286. #endif
  12287. return ret;
  12288. }
  12289. #endif /* NO_PWDBASED */
  12290. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  12291. int hkdf_test(void)
  12292. {
  12293. int ret;
  12294. int L = 42;
  12295. byte okm1[42];
  12296. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  12297. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  12298. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  12299. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12300. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  12301. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  12302. 0xf8, 0xf9 };
  12303. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  12304. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  12305. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  12306. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  12307. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  12308. 0x49, 0x18 };
  12309. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  12310. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  12311. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  12312. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  12313. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  12314. 0xf8, 0x96 };
  12315. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  12316. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  12317. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  12318. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  12319. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  12320. 0x96, 0xc8 };
  12321. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  12322. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  12323. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  12324. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  12325. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  12326. 0x58, 0x65 };
  12327. (void)res1;
  12328. (void)res2;
  12329. (void)res3;
  12330. (void)res4;
  12331. (void)salt1;
  12332. (void)info1;
  12333. #ifndef NO_SHA
  12334. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  12335. if (ret != 0)
  12336. return -8200;
  12337. if (XMEMCMP(okm1, res1, L) != 0)
  12338. return -8201;
  12339. #ifndef HAVE_FIPS
  12340. /* fips can't have key size under 14 bytes, salt is key too */
  12341. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  12342. if (ret != 0)
  12343. return -8202;
  12344. if (XMEMCMP(okm1, res2, L) != 0)
  12345. return -8203;
  12346. #endif /* HAVE_FIPS */
  12347. #endif /* NO_SHA */
  12348. #ifndef NO_SHA256
  12349. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  12350. if (ret != 0)
  12351. return -8204;
  12352. if (XMEMCMP(okm1, res3, L) != 0)
  12353. return -8205;
  12354. #ifndef HAVE_FIPS
  12355. /* fips can't have key size under 14 bytes, salt is key too */
  12356. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  12357. if (ret != 0)
  12358. return -8206;
  12359. if (XMEMCMP(okm1, res4, L) != 0)
  12360. return -8207;
  12361. #endif /* HAVE_FIPS */
  12362. #endif /* NO_SHA256 */
  12363. return 0;
  12364. }
  12365. #endif /* HAVE_HKDF */
  12366. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  12367. int x963kdf_test(void)
  12368. {
  12369. int ret;
  12370. byte kek[128];
  12371. #ifndef NO_SHA
  12372. /* SHA-1, COUNT = 0
  12373. * shared secret length: 192
  12374. * SharedInfo length: 0
  12375. * key data length: 128
  12376. */
  12377. const byte Z[] = {
  12378. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  12379. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  12380. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  12381. };
  12382. const byte verify[] = {
  12383. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  12384. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  12385. };
  12386. #endif
  12387. #ifndef NO_SHA256
  12388. /* SHA-256, COUNT = 3
  12389. * shared secret length: 192
  12390. * SharedInfo length: 0
  12391. * key data length: 128
  12392. */
  12393. const byte Z2[] = {
  12394. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  12395. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  12396. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  12397. };
  12398. const byte verify2[] = {
  12399. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  12400. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  12401. };
  12402. #endif
  12403. #ifdef WOLFSSL_SHA512
  12404. /* SHA-512, COUNT = 0
  12405. * shared secret length: 192
  12406. * SharedInfo length: 0
  12407. * key data length: 128
  12408. */
  12409. const byte Z3[] = {
  12410. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  12411. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  12412. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  12413. };
  12414. const byte verify3[] = {
  12415. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  12416. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  12417. };
  12418. /* SHA-512, COUNT = 0
  12419. * shared secret length: 521
  12420. * SharedInfo length: 128
  12421. * key data length: 1024
  12422. */
  12423. const byte Z4[] = {
  12424. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  12425. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  12426. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  12427. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  12428. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  12429. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  12430. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  12431. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  12432. 0x5a, 0x2d
  12433. };
  12434. const byte info4[] = {
  12435. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  12436. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  12437. };
  12438. const byte verify4[] = {
  12439. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  12440. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  12441. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  12442. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  12443. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  12444. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  12445. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  12446. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  12447. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  12448. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  12449. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  12450. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  12451. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  12452. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  12453. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  12454. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  12455. };
  12456. #endif
  12457. #ifndef NO_SHA
  12458. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  12459. kek, sizeof(verify));
  12460. if (ret != 0)
  12461. return -8300;
  12462. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  12463. return -8301;
  12464. #endif
  12465. #ifndef NO_SHA256
  12466. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  12467. kek, sizeof(verify2));
  12468. if (ret != 0)
  12469. return -8302;
  12470. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  12471. return -8303;
  12472. #endif
  12473. #ifdef WOLFSSL_SHA512
  12474. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  12475. kek, sizeof(verify3));
  12476. if (ret != 0)
  12477. return -8304;
  12478. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  12479. return -8305;
  12480. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  12481. sizeof(info4), kek, sizeof(verify4));
  12482. if (ret != 0)
  12483. return -8306;
  12484. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  12485. return -8307;
  12486. #endif
  12487. return 0;
  12488. }
  12489. #endif /* HAVE_X963_KDF */
  12490. #ifdef HAVE_ECC
  12491. #ifdef BENCH_EMBEDDED
  12492. #define ECC_SHARED_SIZE 128
  12493. #else
  12494. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  12495. #endif
  12496. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  12497. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  12498. #ifndef NO_ECC_VECTOR_TEST
  12499. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  12500. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  12501. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  12502. #define HAVE_ECC_VECTOR_TEST
  12503. #endif
  12504. #endif
  12505. #ifdef HAVE_ECC_VECTOR_TEST
  12506. typedef struct eccVector {
  12507. const char* msg; /* SHA-1 Encoded Message */
  12508. const char* Qx;
  12509. const char* Qy;
  12510. const char* d; /* Private Key */
  12511. const char* R;
  12512. const char* S;
  12513. const char* curveName;
  12514. word32 msgLen;
  12515. word32 keySize;
  12516. #ifndef NO_ASN
  12517. const byte* r;
  12518. word32 rSz;
  12519. const byte* s;
  12520. word32 sSz;
  12521. #endif
  12522. } eccVector;
  12523. static int ecc_test_vector_item(const eccVector* vector)
  12524. {
  12525. int ret = 0, verify = 0;
  12526. word32 sigSz;
  12527. ecc_key userA;
  12528. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  12529. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  12530. word32 sigRawSz;
  12531. DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  12532. #endif
  12533. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  12534. if (ret != 0) {
  12535. FREE_VAR(sig, HEAP_HINT);
  12536. return ret;
  12537. }
  12538. ret = wc_ecc_import_raw(&userA, vector->Qx, vector->Qy,
  12539. vector->d, vector->curveName);
  12540. if (ret != 0)
  12541. goto done;
  12542. XMEMSET(sig, 0, ECC_SIG_SIZE);
  12543. sigSz = ECC_SIG_SIZE;
  12544. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  12545. if (ret != 0)
  12546. goto done;
  12547. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  12548. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  12549. sigRawSz = ECC_SIG_SIZE;
  12550. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  12551. sigRaw, &sigRawSz);
  12552. if (ret != 0)
  12553. goto done;
  12554. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  12555. ret = -8308;
  12556. goto done;
  12557. }
  12558. #endif
  12559. do {
  12560. #if defined(WOLFSSL_ASYNC_CRYPT)
  12561. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12562. #endif
  12563. if (ret >= 0) {
  12564. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  12565. vector->msgLen, &verify, &userA);
  12566. }
  12567. } while (ret == WC_PENDING_E);
  12568. if (ret != 0)
  12569. goto done;
  12570. if (verify != 1)
  12571. ret = -8309;
  12572. done:
  12573. wc_ecc_free(&userA);
  12574. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  12575. FREE_VAR(sigRaw, HEAP_HINT);
  12576. #endif
  12577. FREE_VAR(sig, HEAP_HINT);
  12578. return ret;
  12579. }
  12580. static int ecc_test_vector(int keySize)
  12581. {
  12582. int ret;
  12583. eccVector vec;
  12584. XMEMSET(&vec, 0, sizeof(vec));
  12585. vec.keySize = (word32)keySize;
  12586. switch(keySize) {
  12587. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  12588. case 14:
  12589. return 0;
  12590. #endif /* HAVE_ECC112 */
  12591. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  12592. case 16:
  12593. return 0;
  12594. #endif /* HAVE_ECC128 */
  12595. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  12596. case 20:
  12597. return 0;
  12598. #endif /* HAVE_ECC160 */
  12599. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  12600. case 24:
  12601. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  12602. #if 1
  12603. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  12604. vec.msgLen = 20;
  12605. #else
  12606. /* This is the raw message prior to SHA-1 */
  12607. vec.msg =
  12608. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  12609. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  12610. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  12611. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  12612. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  12613. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  12614. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  12615. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  12616. vec.msgLen = 128;
  12617. #endif
  12618. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  12619. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  12620. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  12621. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  12622. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  12623. vec.curveName = "SECP192R1";
  12624. #ifndef NO_ASN
  12625. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  12626. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  12627. vec.rSz = 24;
  12628. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  12629. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  12630. vec.sSz = 24;
  12631. #endif
  12632. break;
  12633. #endif /* HAVE_ECC192 */
  12634. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  12635. case 28:
  12636. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  12637. #if 1
  12638. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  12639. vec.msgLen = 20;
  12640. #else
  12641. /* This is the raw message prior to SHA-1 */
  12642. vec.msg =
  12643. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  12644. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  12645. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  12646. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  12647. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  12648. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  12649. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  12650. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  12651. vec.msgLen = 128;
  12652. #endif
  12653. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  12654. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  12655. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  12656. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  12657. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  12658. vec.curveName = "SECP224R1";
  12659. #ifndef NO_ASN
  12660. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  12661. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  12662. "\xbc\x16\x71\xa7";
  12663. vec.rSz = 28;
  12664. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  12665. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  12666. "\x6a\xf3\xad\x5b";
  12667. vec.sSz = 28;
  12668. #endif
  12669. break;
  12670. #endif /* HAVE_ECC224 */
  12671. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  12672. case 30:
  12673. return 0;
  12674. #endif /* HAVE_ECC239 */
  12675. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  12676. case 32:
  12677. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  12678. #if 1
  12679. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  12680. vec.msgLen = 20;
  12681. #else
  12682. /* This is the raw message prior to SHA-1 */
  12683. vec.msg =
  12684. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  12685. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  12686. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  12687. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  12688. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  12689. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  12690. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  12691. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  12692. vec.msgLen = 128;
  12693. #endif
  12694. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  12695. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  12696. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  12697. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  12698. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  12699. #ifndef NO_ASN
  12700. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  12701. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  12702. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  12703. vec.rSz = 32;
  12704. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  12705. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  12706. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  12707. vec.sSz = 32;
  12708. #endif
  12709. vec.curveName = "SECP256R1";
  12710. break;
  12711. #endif /* !NO_ECC256 */
  12712. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  12713. case 40:
  12714. return 0;
  12715. #endif /* HAVE_ECC320 */
  12716. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  12717. case 48:
  12718. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  12719. #if 1
  12720. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  12721. vec.msgLen = 20;
  12722. #else
  12723. /* This is the raw message prior to SHA-1 */
  12724. vec.msg =
  12725. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  12726. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  12727. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  12728. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  12729. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  12730. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  12731. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  12732. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  12733. vec.msgLen = 128;
  12734. #endif
  12735. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  12736. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  12737. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  12738. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  12739. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  12740. vec.curveName = "SECP384R1";
  12741. #ifndef NO_ASN
  12742. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  12743. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  12744. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  12745. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  12746. vec.rSz = 48;
  12747. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  12748. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  12749. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  12750. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  12751. vec.sSz = 48;
  12752. #endif
  12753. break;
  12754. #endif /* HAVE_ECC384 */
  12755. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  12756. case 64:
  12757. return 0;
  12758. #endif /* HAVE_ECC512 */
  12759. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  12760. case 66:
  12761. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  12762. #if 1
  12763. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  12764. vec.msgLen = 20;
  12765. #else
  12766. /* This is the raw message prior to SHA-1 */
  12767. vec.msg =
  12768. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  12769. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  12770. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  12771. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  12772. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  12773. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  12774. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  12775. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  12776. vec.msgLen = 128;
  12777. #endif
  12778. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  12779. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  12780. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  12781. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  12782. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  12783. vec.curveName = "SECP521R1";
  12784. #ifndef NO_ASN
  12785. vec.r = (byte*)"\x00\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77"
  12786. "\x78\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf"
  12787. "\x5b\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c"
  12788. "\x7f\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac"
  12789. "\xb6\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba"
  12790. "\x0a\xa3\xbb\x15\x21\xbe";
  12791. vec.rSz = 66;
  12792. vec.s = (byte*)"\x00\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3"
  12793. "\x23\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd"
  12794. "\xc5\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13"
  12795. "\x4b\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79"
  12796. "\x11\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b"
  12797. "\x6c\x3d\x22\xf2\x48\x0c";
  12798. vec.sSz = 66;
  12799. #endif
  12800. break;
  12801. #endif /* HAVE_ECC521 */
  12802. default:
  12803. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  12804. }; /* Switch */
  12805. ret = ecc_test_vector_item(&vec);
  12806. if (ret < 0) {
  12807. return ret;
  12808. }
  12809. return 0;
  12810. }
  12811. #ifdef HAVE_ECC_CDH
  12812. static int ecc_test_cdh_vectors(void)
  12813. {
  12814. int ret;
  12815. ecc_key pub_key, priv_key;
  12816. byte sharedA[32] = {0}, sharedB[32] = {0};
  12817. word32 x, z;
  12818. const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  12819. const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  12820. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  12821. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  12822. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  12823. const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  12824. /* setup private and public keys */
  12825. ret = wc_ecc_init(&pub_key);
  12826. if (ret != 0)
  12827. return ret;
  12828. ret = wc_ecc_init(&priv_key);
  12829. if (ret != 0) {
  12830. wc_ecc_free(&pub_key);
  12831. goto done;
  12832. }
  12833. wc_ecc_set_flags(&pub_key, WC_ECC_FLAG_COFACTOR);
  12834. wc_ecc_set_flags(&priv_key, WC_ECC_FLAG_COFACTOR);
  12835. ret = wc_ecc_import_raw(&pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  12836. if (ret != 0)
  12837. goto done;
  12838. ret = wc_ecc_import_raw(&priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  12839. if (ret != 0)
  12840. goto done;
  12841. /* compute ECC Cofactor shared secret */
  12842. x = sizeof(sharedA);
  12843. ret = wc_ecc_shared_secret(&priv_key, &pub_key, sharedA, &x);
  12844. if (ret != 0) {
  12845. goto done;
  12846. }
  12847. /* read in expected Z */
  12848. z = sizeof(sharedB);
  12849. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  12850. if (ret != 0)
  12851. goto done;
  12852. /* compare results */
  12853. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  12854. ERROR_OUT(-8310, done);
  12855. }
  12856. done:
  12857. wc_ecc_free(&priv_key);
  12858. wc_ecc_free(&pub_key);
  12859. return ret;
  12860. }
  12861. #endif /* HAVE_ECC_CDH */
  12862. #endif /* HAVE_ECC_VECTOR_TEST */
  12863. #ifdef HAVE_ECC_KEY_IMPORT
  12864. /* returns 0 on success */
  12865. static int ecc_test_make_pub(WC_RNG* rng)
  12866. {
  12867. ecc_key key;
  12868. unsigned char* exportBuf = NULL;
  12869. unsigned char* tmp = NULL;
  12870. unsigned char msg[] = "test wolfSSL ECC public gen";
  12871. word32 x, tmpSz;
  12872. int ret = 0;
  12873. ecc_point* pubPoint = NULL;
  12874. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  12875. ecc_key pub;
  12876. #endif
  12877. #ifdef HAVE_ECC_VERIFY
  12878. int verify = 0;
  12879. #endif
  12880. #ifndef USE_CERT_BUFFERS_256
  12881. FILE* file;
  12882. #endif
  12883. wc_ecc_init(&key);
  12884. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12885. if (tmp == NULL) {
  12886. return -8311;
  12887. }
  12888. exportBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12889. if (exportBuf == NULL) {
  12890. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12891. return -8312;
  12892. }
  12893. #ifdef USE_CERT_BUFFERS_256
  12894. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  12895. tmpSz = (size_t)sizeof_ecc_key_der_256;
  12896. #else
  12897. file = fopen(eccKeyDerFile, "rb");
  12898. if (!file) {
  12899. ERROR_OUT(-8313, done);
  12900. }
  12901. tmpSz = (word32)fread(tmp, 1, FOURK_BUF, file);
  12902. fclose(file);
  12903. #endif /* USE_CERT_BUFFERS_256 */
  12904. /* import private only then test with */
  12905. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  12906. if (ret == 0) {
  12907. ERROR_OUT(-8314, done);
  12908. }
  12909. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, &key);
  12910. if (ret == 0) {
  12911. ERROR_OUT(-8315, done);
  12912. }
  12913. x = 0;
  12914. ret = wc_EccPrivateKeyDecode(tmp, &x, &key, tmpSz);
  12915. if (ret != 0) {
  12916. ERROR_OUT(-8316, done);
  12917. }
  12918. #ifdef HAVE_ECC_KEY_EXPORT
  12919. x = FOURK_BUF;
  12920. ret = wc_ecc_export_private_only(&key, exportBuf, &x);
  12921. if (ret != 0) {
  12922. ERROR_OUT(-8317, done);
  12923. }
  12924. /* make private only key */
  12925. wc_ecc_free(&key);
  12926. wc_ecc_init(&key);
  12927. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, &key);
  12928. if (ret != 0) {
  12929. ERROR_OUT(-8318, done);
  12930. }
  12931. x = FOURK_BUF;
  12932. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  12933. if (ret == 0) {
  12934. ERROR_OUT(-8319, done);
  12935. }
  12936. #endif /* HAVE_ECC_KEY_EXPORT */
  12937. ret = wc_ecc_make_pub(NULL, NULL);
  12938. if (ret == 0) {
  12939. ERROR_OUT(-8320, done);
  12940. }
  12941. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  12942. if (pubPoint == NULL) {
  12943. ERROR_OUT(-8321, done);
  12944. }
  12945. ret = wc_ecc_make_pub(&key, pubPoint);
  12946. if (ret != 0) {
  12947. ERROR_OUT(-8322, done);
  12948. }
  12949. #ifdef HAVE_ECC_KEY_EXPORT
  12950. /* export should still fail, is private only key */
  12951. x = FOURK_BUF;
  12952. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  12953. if (ret == 0) {
  12954. ERROR_OUT(-8323, done);
  12955. }
  12956. #endif /* HAVE_ECC_KEY_EXPORT */
  12957. #ifdef HAVE_ECC_SIGN
  12958. tmpSz = FOURK_BUF;
  12959. ret = wc_ecc_sign_hash(msg, sizeof(msg), tmp, &tmpSz, rng, &key);
  12960. if (ret != 0) {
  12961. ERROR_OUT(-8324, done);
  12962. }
  12963. #ifdef HAVE_ECC_VERIFY
  12964. /* try verify with private only key */
  12965. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, sizeof(msg), &verify, &key);
  12966. if (ret != 0) {
  12967. ERROR_OUT(-8325, done);
  12968. }
  12969. if (verify != 1) {
  12970. ERROR_OUT(-8326, done);
  12971. }
  12972. #ifdef HAVE_ECC_KEY_EXPORT
  12973. /* exporting the public part should now work */
  12974. x = FOURK_BUF;
  12975. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  12976. if (ret != 0) {
  12977. ERROR_OUT(-8327, done);
  12978. }
  12979. #endif /* HAVE_ECC_KEY_EXPORT */
  12980. #endif /* HAVE_ECC_VERIFY */
  12981. #endif /* HAVE_ECC_SIGN */
  12982. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  12983. /* now test private only key with creating a shared secret */
  12984. x = FOURK_BUF;
  12985. ret = wc_ecc_export_private_only(&key, exportBuf, &x);
  12986. if (ret != 0) {
  12987. ERROR_OUT(-8328, done);
  12988. }
  12989. /* make private only key */
  12990. wc_ecc_free(&key);
  12991. wc_ecc_init(&key);
  12992. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, &key);
  12993. if (ret != 0) {
  12994. ERROR_OUT(-8329, done);
  12995. }
  12996. /* check that public export fails with private only key */
  12997. x = FOURK_BUF;
  12998. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  12999. if (ret == 0) {
  13000. ERROR_OUT(-8330, done);
  13001. }
  13002. /* make public key for shared secret */
  13003. wc_ecc_init(&pub);
  13004. ret = wc_ecc_make_key(rng, 32, &pub);
  13005. #if defined(WOLFSSL_ASYNC_CRYPT)
  13006. ret = wc_AsyncWait(ret, &pub.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13007. #endif
  13008. if (ret != 0) {
  13009. ERROR_OUT(-8331, done);
  13010. }
  13011. x = FOURK_BUF;
  13012. ret = wc_ecc_shared_secret(&key, &pub, exportBuf, &x);
  13013. wc_ecc_free(&pub);
  13014. if (ret != 0) {
  13015. ERROR_OUT(-8332, done);
  13016. }
  13017. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT */
  13018. ret = 0;
  13019. done:
  13020. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13021. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13022. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  13023. wc_ecc_free(&key);
  13024. return ret;
  13025. }
  13026. #endif /* HAVE_ECC_KEY_IMPORT */
  13027. #ifdef WOLFSSL_KEY_GEN
  13028. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  13029. {
  13030. int ret = 0;
  13031. int derSz;
  13032. word32 pkcs8Sz;
  13033. byte* der;
  13034. byte* pem;
  13035. ecc_key userA;
  13036. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13037. if (der == NULL) {
  13038. return -8333;
  13039. }
  13040. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13041. if (pem == NULL) {
  13042. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13043. return -8334;
  13044. }
  13045. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  13046. if (ret != 0)
  13047. goto done;
  13048. ret = wc_ecc_make_key(rng, keySize, &userA);
  13049. #if defined(WOLFSSL_ASYNC_CRYPT)
  13050. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13051. #endif
  13052. if (ret != 0)
  13053. goto done;
  13054. ret = wc_ecc_check_key(&userA);
  13055. if (ret != 0)
  13056. goto done;
  13057. derSz = wc_EccKeyToDer(&userA, der, FOURK_BUF);
  13058. if (derSz < 0) {
  13059. ERROR_OUT(derSz, done);
  13060. }
  13061. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, eccCaKeyTempFile,
  13062. eccCaKeyPemFile, ECC_PRIVATEKEY_TYPE, -6510);
  13063. if (ret != 0) {
  13064. goto done;
  13065. }
  13066. /* test export of public key */
  13067. derSz = wc_EccPublicKeyToDer(&userA, der, FOURK_BUF, 1);
  13068. if (derSz < 0) {
  13069. ERROR_OUT(derSz, done);
  13070. }
  13071. if (derSz == 0) {
  13072. ERROR_OUT(-8335, done);
  13073. }
  13074. ret = SaveDerAndPem(der, derSz, NULL, 0, eccPubKeyDerFile,
  13075. NULL, 0, -6515);
  13076. if (ret != 0) {
  13077. goto done;
  13078. }
  13079. /* test export of PKCS#8 unecrypted private key */
  13080. pkcs8Sz = FOURK_BUF;
  13081. derSz = wc_EccPrivateKeyToPKCS8(&userA, der, &pkcs8Sz);
  13082. if (derSz < 0) {
  13083. ERROR_OUT(derSz, done);
  13084. }
  13085. if (derSz == 0) {
  13086. ERROR_OUT(-8336, done);
  13087. }
  13088. ret = SaveDerAndPem(der, derSz, NULL, 0, eccPkcs8KeyDerFile,
  13089. NULL, 0, -6517);
  13090. if (ret != 0) {
  13091. goto done;
  13092. }
  13093. done:
  13094. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13095. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13096. wc_ecc_free(&userA);
  13097. return ret;
  13098. }
  13099. #endif /* WOLFSSL_KEY_GEN */
  13100. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  13101. int curve_id, const ecc_set_type* dp)
  13102. {
  13103. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  13104. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  13105. #ifdef HAVE_ECC_KEY_EXPORT
  13106. byte exportBuf[MAX_ECC_BYTES * 2 + 32];
  13107. #endif
  13108. word32 x, y;
  13109. #ifdef HAVE_ECC_SIGN
  13110. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  13111. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  13112. int i;
  13113. #ifdef HAVE_ECC_VERIFY
  13114. int verify;
  13115. #endif /* HAVE_ECC_VERIFY */
  13116. #endif /* HAVE_ECC_SIGN */
  13117. int ret;
  13118. ecc_key userA, userB, pubKey;
  13119. int curveSize;
  13120. (void)testVerifyCount;
  13121. (void)dp;
  13122. XMEMSET(&userA, 0, sizeof(ecc_key));
  13123. XMEMSET(&userB, 0, sizeof(ecc_key));
  13124. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  13125. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  13126. if (ret != 0)
  13127. goto done;
  13128. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  13129. if (ret != 0)
  13130. goto done;
  13131. ret = wc_ecc_init_ex(&pubKey, HEAP_HINT, devId);
  13132. if (ret != 0)
  13133. goto done;
  13134. #ifdef WOLFSSL_CUSTOM_CURVES
  13135. if (dp != NULL) {
  13136. ret = wc_ecc_set_custom_curve(&userA, dp);
  13137. if (ret != 0)
  13138. goto done;
  13139. ret = wc_ecc_set_custom_curve(&userB, dp);
  13140. if (ret != 0)
  13141. goto done;
  13142. }
  13143. #endif
  13144. ret = wc_ecc_make_key_ex(rng, keySize, &userA, curve_id);
  13145. #if defined(WOLFSSL_ASYNC_CRYPT)
  13146. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13147. #endif
  13148. if (ret != 0)
  13149. goto done;
  13150. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  13151. curveSize = wc_ecc_get_curve_size_from_id(userA.dp->id);
  13152. if (curveSize != userA.dp->size) {
  13153. ret = -8337;
  13154. goto done;
  13155. }
  13156. }
  13157. ret = wc_ecc_check_key(&userA);
  13158. if (ret != 0)
  13159. goto done;
  13160. ret = wc_ecc_make_key_ex(rng, keySize, &userB, curve_id);
  13161. #if defined(WOLFSSL_ASYNC_CRYPT)
  13162. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13163. #endif
  13164. if (ret != 0)
  13165. goto done;
  13166. /* only perform the below tests if the key size matches */
  13167. if (dp == NULL && keySize > 0 && wc_ecc_size(&userA) != keySize) {
  13168. ret = ECC_CURVE_OID_E;
  13169. goto done;
  13170. }
  13171. #ifdef HAVE_ECC_DHE
  13172. x = ECC_SHARED_SIZE;
  13173. do {
  13174. #if defined(WOLFSSL_ASYNC_CRYPT)
  13175. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13176. #endif
  13177. if (ret >= 0)
  13178. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  13179. } while (ret == WC_PENDING_E);
  13180. if (ret != 0) {
  13181. goto done;
  13182. }
  13183. y = ECC_SHARED_SIZE;
  13184. do {
  13185. #if defined(WOLFSSL_ASYNC_CRYPT)
  13186. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13187. #endif
  13188. if (ret >= 0)
  13189. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  13190. } while (ret == WC_PENDING_E);
  13191. if (ret != 0)
  13192. goto done;
  13193. if (y != x)
  13194. ERROR_OUT(-8338, done);
  13195. if (XMEMCMP(sharedA, sharedB, x))
  13196. ERROR_OUT(-8339, done);
  13197. #endif /* HAVE_ECC_DHE */
  13198. #ifdef HAVE_ECC_CDH
  13199. /* add cofactor flag */
  13200. wc_ecc_set_flags(&userA, WC_ECC_FLAG_COFACTOR);
  13201. wc_ecc_set_flags(&userB, WC_ECC_FLAG_COFACTOR);
  13202. x = sizeof(sharedA);
  13203. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  13204. if (ret != 0) {
  13205. goto done;
  13206. }
  13207. y = sizeof(sharedB);
  13208. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  13209. if (ret != 0)
  13210. goto done;
  13211. if (y != x)
  13212. ERROR_OUT(-8340, done);
  13213. if (XMEMCMP(sharedA, sharedB, x))
  13214. ERROR_OUT(-8341, done);
  13215. /* remove cofactor flag */
  13216. wc_ecc_set_flags(&userA, 0);
  13217. wc_ecc_set_flags(&userB, 0);
  13218. #endif /* HAVE_ECC_CDH */
  13219. #ifdef HAVE_ECC_KEY_EXPORT
  13220. x = sizeof(exportBuf);
  13221. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 0);
  13222. if (ret != 0)
  13223. goto done;
  13224. #ifdef HAVE_ECC_KEY_IMPORT
  13225. #ifdef WOLFSSL_CUSTOM_CURVES
  13226. if (dp != NULL) {
  13227. ret = wc_ecc_set_custom_curve(&pubKey, dp);
  13228. if (ret != 0) goto done;
  13229. }
  13230. #endif
  13231. ret = wc_ecc_import_x963_ex(exportBuf, x, &pubKey, curve_id);
  13232. if (ret != 0)
  13233. goto done;
  13234. #ifdef HAVE_ECC_DHE
  13235. y = ECC_SHARED_SIZE;
  13236. do {
  13237. #if defined(WOLFSSL_ASYNC_CRYPT)
  13238. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13239. #endif
  13240. if (ret >= 0)
  13241. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  13242. } while (ret == WC_PENDING_E);
  13243. if (ret != 0)
  13244. goto done;
  13245. if (XMEMCMP(sharedA, sharedB, y))
  13246. ERROR_OUT(-8342, done);
  13247. #endif /* HAVE_ECC_DHE */
  13248. #ifdef HAVE_COMP_KEY
  13249. /* try compressed export / import too */
  13250. x = sizeof(exportBuf);
  13251. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 1);
  13252. if (ret != 0)
  13253. goto done;
  13254. wc_ecc_free(&pubKey);
  13255. ret = wc_ecc_init_ex(&pubKey, HEAP_HINT, devId);
  13256. if (ret != 0)
  13257. goto done;
  13258. #ifdef WOLFSSL_CUSTOM_CURVES
  13259. if (dp != NULL) {
  13260. ret = wc_ecc_set_custom_curve(&pubKey, dp);
  13261. if (ret != 0) goto done;
  13262. }
  13263. #endif
  13264. ret = wc_ecc_import_x963_ex(exportBuf, x, &pubKey, curve_id);
  13265. if (ret != 0)
  13266. goto done;
  13267. #ifdef HAVE_ECC_DHE
  13268. y = ECC_SHARED_SIZE;
  13269. do {
  13270. #if defined(WOLFSSL_ASYNC_CRYPT)
  13271. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13272. #endif
  13273. if (ret >= 0)
  13274. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  13275. } while (ret == WC_PENDING_E);
  13276. if (ret != 0)
  13277. goto done;
  13278. if (XMEMCMP(sharedA, sharedB, y))
  13279. ERROR_OUT(-8343, done);
  13280. #endif /* HAVE_ECC_DHE */
  13281. #endif /* HAVE_COMP_KEY */
  13282. #endif /* HAVE_ECC_KEY_IMPORT */
  13283. #endif /* HAVE_ECC_KEY_EXPORT */
  13284. #ifdef HAVE_ECC_SIGN
  13285. /* ECC w/out Shamir has issue with all 0 digest */
  13286. /* WC_BIGINT doesn't have 0 len well on hardware */
  13287. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT)
  13288. /* test DSA sign hash with zeros */
  13289. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  13290. digest[i] = 0;
  13291. }
  13292. x = ECC_SIG_SIZE;
  13293. do {
  13294. #if defined(WOLFSSL_ASYNC_CRYPT)
  13295. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13296. #endif
  13297. if (ret >= 0)
  13298. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  13299. &userA);
  13300. } while (ret == WC_PENDING_E);
  13301. if (ret != 0)
  13302. goto done;
  13303. #ifdef HAVE_ECC_VERIFY
  13304. for (i=0; i<testVerifyCount; i++) {
  13305. verify = 0;
  13306. do {
  13307. #if defined(WOLFSSL_ASYNC_CRYPT)
  13308. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13309. #endif
  13310. if (ret >= 0)
  13311. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  13312. &verify, &userA);
  13313. } while (ret == WC_PENDING_E);
  13314. if (ret != 0)
  13315. goto done;
  13316. if (verify != 1)
  13317. ERROR_OUT(-8344, done);
  13318. }
  13319. #endif /* HAVE_ECC_VERIFY */
  13320. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT */
  13321. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  13322. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  13323. digest[i] = (byte)i;
  13324. }
  13325. x = ECC_SIG_SIZE;
  13326. do {
  13327. #if defined(WOLFSSL_ASYNC_CRYPT)
  13328. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13329. #endif
  13330. if (ret >= 0)
  13331. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  13332. &userA);
  13333. } while (ret == WC_PENDING_E);
  13334. if (ret != 0)
  13335. ERROR_OUT(-8345, done);
  13336. #ifdef HAVE_ECC_VERIFY
  13337. for (i=0; i<testVerifyCount; i++) {
  13338. verify = 0;
  13339. do {
  13340. #if defined(WOLFSSL_ASYNC_CRYPT)
  13341. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13342. #endif
  13343. if (ret >= 0)
  13344. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  13345. &verify, &userA);
  13346. } while (ret == WC_PENDING_E);
  13347. if (ret != 0)
  13348. goto done;
  13349. if (verify != 1)
  13350. ERROR_OUT(-8346, done);
  13351. }
  13352. #endif /* HAVE_ECC_VERIFY */
  13353. #endif /* HAVE_ECC_SIGN */
  13354. #ifdef HAVE_ECC_KEY_EXPORT
  13355. x = sizeof(exportBuf);
  13356. ret = wc_ecc_export_private_only(&userA, exportBuf, &x);
  13357. if (ret != 0)
  13358. goto done;
  13359. #endif /* HAVE_ECC_KEY_EXPORT */
  13360. done:
  13361. wc_ecc_free(&pubKey);
  13362. wc_ecc_free(&userB);
  13363. wc_ecc_free(&userA);
  13364. FREE_VAR(sharedA, HEAP_HINT);
  13365. FREE_VAR(sharedB, HEAP_HINT);
  13366. #ifdef HAVE_ECC_SIGN
  13367. FREE_VAR(sig, HEAP_HINT);
  13368. FREE_VAR(digest, HEAP_HINT);
  13369. #endif
  13370. return ret;
  13371. }
  13372. #undef ECC_TEST_VERIFY_COUNT
  13373. #define ECC_TEST_VERIFY_COUNT 2
  13374. static int ecc_test_curve(WC_RNG* rng, int keySize)
  13375. {
  13376. int ret;
  13377. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  13378. ECC_CURVE_DEF, NULL);
  13379. if (ret < 0) {
  13380. if (ret == ECC_CURVE_OID_E) {
  13381. /* ignore error for curves not found */
  13382. /* some curve sizes are only available with:
  13383. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  13384. and HAVE_ECC_KOBLITZ */
  13385. }
  13386. else {
  13387. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  13388. return ret;
  13389. }
  13390. }
  13391. #ifdef HAVE_ECC_VECTOR_TEST
  13392. ret = ecc_test_vector(keySize);
  13393. if (ret < 0) {
  13394. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  13395. return ret;
  13396. }
  13397. #endif
  13398. #ifdef WOLFSSL_KEY_GEN
  13399. ret = ecc_test_key_gen(rng, keySize);
  13400. if (ret < 0) {
  13401. if (ret == ECC_CURVE_OID_E) {
  13402. /* ignore error for curves not found */
  13403. }
  13404. else {
  13405. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  13406. return ret;
  13407. }
  13408. }
  13409. #endif
  13410. return 0;
  13411. }
  13412. #if !defined(WOLFSSL_ATECC508A) && defined(HAVE_ECC_KEY_IMPORT) && \
  13413. defined(HAVE_ECC_KEY_EXPORT)
  13414. static int ecc_point_test(void)
  13415. {
  13416. int ret;
  13417. ecc_point* point;
  13418. ecc_point* point2;
  13419. #ifdef HAVE_COMP_KEY
  13420. ecc_point* point3;
  13421. ecc_point* point4;
  13422. #endif
  13423. word32 outLen;
  13424. byte out[65];
  13425. byte der[] = { 0x04, /* = Uncompressed */
  13426. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13427. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13428. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13429. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13430. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13431. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13432. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13433. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13434. #ifdef HAVE_COMP_KEY
  13435. byte derComp0[] = { 0x02, /* = Compressed, y even */
  13436. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13437. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13438. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13439. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13440. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  13441. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13442. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13443. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  13444. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13445. #endif
  13446. byte altDer[] = { 0x04, /* = Uncompressed */
  13447. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13448. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13449. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13450. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13451. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13452. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13453. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13454. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  13455. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  13456. /* if curve P256 is not enabled then test should not fail */
  13457. if (curve_idx == ECC_CURVE_INVALID)
  13458. return 0;
  13459. outLen = sizeof(out);
  13460. point = wc_ecc_new_point();
  13461. if (point == NULL)
  13462. return -8400;
  13463. point2 = wc_ecc_new_point();
  13464. if (point2 == NULL) {
  13465. wc_ecc_del_point(point);
  13466. return -8401;
  13467. }
  13468. #ifdef HAVE_COMP_KEY
  13469. point3 = wc_ecc_new_point();
  13470. if (point3 == NULL) {
  13471. wc_ecc_del_point(point2);
  13472. wc_ecc_del_point(point);
  13473. return -8402;
  13474. }
  13475. point4 = wc_ecc_new_point();
  13476. if (point4 == NULL) {
  13477. wc_ecc_del_point(point3);
  13478. wc_ecc_del_point(point2);
  13479. wc_ecc_del_point(point);
  13480. return -8403;
  13481. }
  13482. #endif
  13483. /* Parameter Validation testing. */
  13484. wc_ecc_del_point(NULL);
  13485. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  13486. if (ret != ECC_BAD_ARG_E) {
  13487. ret = -8404;
  13488. goto done;
  13489. }
  13490. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  13491. if (ret != ECC_BAD_ARG_E) {
  13492. ret = -8405;
  13493. goto done;
  13494. }
  13495. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  13496. if (ret != ECC_BAD_ARG_E) {
  13497. ret = -8406;
  13498. goto done;
  13499. }
  13500. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  13501. if (ret != ECC_BAD_ARG_E) {
  13502. ret = -8407;
  13503. goto done;
  13504. }
  13505. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  13506. if (ret != ECC_BAD_ARG_E) {
  13507. ret = -8408;
  13508. goto done;
  13509. }
  13510. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  13511. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  13512. ret = -8409;
  13513. goto done;
  13514. }
  13515. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  13516. if (ret != ECC_BAD_ARG_E) {
  13517. ret = -8410;
  13518. goto done;
  13519. }
  13520. outLen = 0;
  13521. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  13522. if (ret != BUFFER_E) {
  13523. ret = -8411;
  13524. goto done;
  13525. }
  13526. ret = wc_ecc_copy_point(NULL, NULL);
  13527. if (ret != ECC_BAD_ARG_E) {
  13528. ret = -8412;
  13529. goto done;
  13530. }
  13531. ret = wc_ecc_copy_point(NULL, point2);
  13532. if (ret != ECC_BAD_ARG_E) {
  13533. ret = -8413;
  13534. goto done;
  13535. }
  13536. ret = wc_ecc_copy_point(point, NULL);
  13537. if (ret != ECC_BAD_ARG_E) {
  13538. ret = -8414;
  13539. goto done;
  13540. }
  13541. ret = wc_ecc_cmp_point(NULL, NULL);
  13542. if (ret != BAD_FUNC_ARG) {
  13543. ret = -8415;
  13544. goto done;
  13545. }
  13546. ret = wc_ecc_cmp_point(NULL, point2);
  13547. if (ret != BAD_FUNC_ARG) {
  13548. ret = -8416;
  13549. goto done;
  13550. }
  13551. ret = wc_ecc_cmp_point(point, NULL);
  13552. if (ret != BAD_FUNC_ARG) {
  13553. ret = -8417;
  13554. goto done;
  13555. }
  13556. /* Use API. */
  13557. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  13558. if (ret != 0) {
  13559. ret = -8418;
  13560. goto done;
  13561. }
  13562. outLen = sizeof(out);
  13563. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  13564. if (ret != 0) {
  13565. ret = -8419;
  13566. goto done;
  13567. }
  13568. if (outLen != sizeof(der)) {
  13569. ret = -8420;
  13570. goto done;
  13571. }
  13572. if (XMEMCMP(out, der, outLen) != 0) {
  13573. ret = -8421;
  13574. goto done;
  13575. }
  13576. ret = wc_ecc_copy_point(point2, point);
  13577. if (ret != MP_OKAY) {
  13578. ret = -8422;
  13579. goto done;
  13580. }
  13581. ret = wc_ecc_cmp_point(point2, point);
  13582. if (ret != MP_EQ) {
  13583. ret = -8423;
  13584. goto done;
  13585. }
  13586. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  13587. if (ret != 0) {
  13588. ret = -8424;
  13589. goto done;
  13590. }
  13591. ret = wc_ecc_cmp_point(point2, point);
  13592. if (ret != MP_GT) {
  13593. ret = -8425;
  13594. goto done;
  13595. }
  13596. #ifdef HAVE_COMP_KEY
  13597. ret = wc_ecc_import_point_der(derComp0, sizeof(der), curve_idx, point3);
  13598. if (ret != 0) {
  13599. ret = -8426;
  13600. goto done;
  13601. }
  13602. ret = wc_ecc_import_point_der(derComp1, sizeof(der), curve_idx, point4);
  13603. if (ret != 0) {
  13604. ret = -8427;
  13605. goto done;
  13606. }
  13607. #endif
  13608. done:
  13609. #ifdef HAVE_COMP_KEY
  13610. wc_ecc_del_point(point4);
  13611. wc_ecc_del_point(point3);
  13612. #endif
  13613. wc_ecc_del_point(point2);
  13614. wc_ecc_del_point(point);
  13615. return ret;
  13616. }
  13617. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  13618. #ifndef NO_SIG_WRAPPER
  13619. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  13620. {
  13621. int ret;
  13622. word32 sigSz;
  13623. int size;
  13624. byte out[ECC_MAX_SIG_SIZE];
  13625. byte in[] = "Everyone gets Friday off.";
  13626. const byte hash[] = {
  13627. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  13628. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  13629. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  13630. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  13631. };
  13632. word32 inLen = (word32)XSTRLEN((char*)in);
  13633. size = wc_ecc_sig_size(key);
  13634. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  13635. if (ret != size)
  13636. return -8428;
  13637. sigSz = (word32)ret;
  13638. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  13639. inLen, out, &sigSz, key, sizeof(*key), rng);
  13640. if (ret != 0)
  13641. return -8429;
  13642. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  13643. inLen, out, sigSz, key, sizeof(*key));
  13644. if (ret != 0)
  13645. return -8430;
  13646. sigSz = (word32)sizeof(out);
  13647. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  13648. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  13649. if (ret != 0)
  13650. return -8431;
  13651. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  13652. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  13653. if (ret != 0)
  13654. return -8432;
  13655. return 0;
  13656. }
  13657. #endif
  13658. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  13659. static int ecc_exp_imp_test(ecc_key* key)
  13660. {
  13661. int ret;
  13662. int curve_id;
  13663. ecc_key keyImp;
  13664. byte priv[32];
  13665. word32 privLen;
  13666. byte pub[65];
  13667. word32 pubLen, pubLenX, pubLenY;
  13668. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  13669. "ce46cbc947616d0cbaa82323818a793d";
  13670. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  13671. "922f8b30ea6e8811742ac7238fe87308";
  13672. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  13673. "2e7a493ec1153b3a95feb8a4873f8d08";
  13674. wc_ecc_init(&keyImp);
  13675. privLen = sizeof(priv);
  13676. ret = wc_ecc_export_private_only(key, priv, &privLen);
  13677. if (ret != 0) {
  13678. ret = -8433;
  13679. goto done;
  13680. }
  13681. pubLen = sizeof(pub);
  13682. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  13683. if (ret != 0) {
  13684. ret = -8434;
  13685. goto done;
  13686. }
  13687. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, &keyImp);
  13688. if (ret != 0) {
  13689. ret = -8435;
  13690. goto done;
  13691. }
  13692. wc_ecc_free(&keyImp);
  13693. wc_ecc_init(&keyImp);
  13694. ret = wc_ecc_import_raw_ex(&keyImp, qx, qy, d, ECC_SECP256R1);
  13695. if (ret != 0) {
  13696. ret = -8436;
  13697. goto done;
  13698. }
  13699. wc_ecc_free(&keyImp);
  13700. wc_ecc_init(&keyImp);
  13701. curve_id = wc_ecc_get_curve_id(key->idx);
  13702. if (curve_id < 0) {
  13703. ret = -8437;
  13704. goto done;
  13705. }
  13706. /* test import private only */
  13707. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, &keyImp,
  13708. curve_id);
  13709. if (ret != 0) {
  13710. ret = -8438;
  13711. goto done;
  13712. }
  13713. wc_ecc_free(&keyImp);
  13714. wc_ecc_init(&keyImp);
  13715. /* test export public raw */
  13716. pubLenX = pubLenY = 32;
  13717. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  13718. if (ret != 0) {
  13719. ret = -8439;
  13720. goto done;
  13721. }
  13722. #ifndef HAVE_SELFTEST
  13723. /* test import of public */
  13724. ret = wc_ecc_import_unsigned(&keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  13725. if (ret != 0) {
  13726. ret = -8440;
  13727. goto done;
  13728. }
  13729. #endif
  13730. wc_ecc_free(&keyImp);
  13731. wc_ecc_init(&keyImp);
  13732. /* test export private and public raw */
  13733. pubLenX = pubLenY = privLen = 32;
  13734. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  13735. priv, &privLen);
  13736. if (ret != 0) {
  13737. ret = -8441;
  13738. goto done;
  13739. }
  13740. #ifndef HAVE_SELFTEST
  13741. /* test import of private and public */
  13742. ret = wc_ecc_import_unsigned(&keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  13743. if (ret != 0) {
  13744. ret = -8442;
  13745. goto done;
  13746. }
  13747. #endif
  13748. done:
  13749. wc_ecc_free(&keyImp);
  13750. return ret;
  13751. }
  13752. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  13753. #ifndef WOLFSSL_ATECC508A
  13754. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  13755. static int ecc_mulmod_test(ecc_key* key1)
  13756. {
  13757. int ret;
  13758. ecc_key key2;
  13759. ecc_key key3;
  13760. wc_ecc_init(&key2);
  13761. wc_ecc_init(&key3);
  13762. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  13763. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  13764. * private key in key2.
  13765. */
  13766. ret = wc_ecc_import_raw_ex(&key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  13767. ECC_SECP256R1);
  13768. if (ret != 0)
  13769. goto done;
  13770. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  13771. * in key3.
  13772. */
  13773. ret = wc_ecc_import_raw_ex(&key3, key1->dp->Gx, key1->dp->Gy,
  13774. key1->dp->prime, ECC_SECP256R1);
  13775. if (ret != 0)
  13776. goto done;
  13777. ret = wc_ecc_mulmod(&key1->k, &key2.pubkey, &key3.pubkey, &key2.k, &key3.k,
  13778. 1);
  13779. if (ret != 0) {
  13780. ret = -8443;
  13781. goto done;
  13782. }
  13783. done:
  13784. wc_ecc_free(&key3);
  13785. wc_ecc_free(&key2);
  13786. return ret;
  13787. }
  13788. #endif
  13789. static int ecc_ssh_test(ecc_key* key)
  13790. {
  13791. int ret;
  13792. byte out[128];
  13793. word32 outLen = sizeof(out);
  13794. /* Parameter Validation testing. */
  13795. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  13796. if (ret != BAD_FUNC_ARG)
  13797. return -8444;
  13798. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  13799. if (ret != BAD_FUNC_ARG)
  13800. return -8445;
  13801. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  13802. if (ret != BAD_FUNC_ARG)
  13803. return -8446;
  13804. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  13805. if (ret != BAD_FUNC_ARG)
  13806. return -8447;
  13807. /* Use API. */
  13808. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  13809. if (ret != 0)
  13810. return -8448;
  13811. return 0;
  13812. }
  13813. #endif
  13814. static int ecc_def_curve_test(WC_RNG *rng)
  13815. {
  13816. int ret;
  13817. ecc_key key;
  13818. wc_ecc_init(&key);
  13819. /* Use API */
  13820. ret = wc_ecc_set_flags(NULL, 0);
  13821. if (ret != BAD_FUNC_ARG) {
  13822. ret = -8449;
  13823. goto done;
  13824. }
  13825. ret = wc_ecc_set_flags(&key, 0);
  13826. if (ret != 0) {
  13827. ret = -8450;
  13828. goto done;
  13829. }
  13830. ret = wc_ecc_make_key(rng, 32, &key);
  13831. #if defined(WOLFSSL_ASYNC_CRYPT)
  13832. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13833. #endif
  13834. if (ret != 0) {
  13835. ret = -8451;
  13836. goto done;
  13837. }
  13838. #ifndef NO_SIG_WRAPPER
  13839. ret = ecc_sig_test(rng, &key);
  13840. if (ret < 0)
  13841. goto done;
  13842. #endif
  13843. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  13844. ret = ecc_exp_imp_test(&key);
  13845. if (ret < 0)
  13846. goto done;
  13847. #endif
  13848. #ifndef WOLFSSL_ATECC508A
  13849. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  13850. ret = ecc_mulmod_test(&key);
  13851. if (ret < 0)
  13852. goto done;
  13853. #endif
  13854. ret = ecc_ssh_test(&key);
  13855. if (ret < 0)
  13856. goto done;
  13857. #endif /* WOLFSSL_ATECC508A */
  13858. done:
  13859. wc_ecc_free(&key);
  13860. return ret;
  13861. }
  13862. #ifdef WOLFSSL_CERT_EXT
  13863. static int ecc_decode_test(void)
  13864. {
  13865. int ret;
  13866. word32 inSz;
  13867. word32 inOutIdx;
  13868. ecc_key key;
  13869. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  13870. static const byte good[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  13871. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  13872. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  13873. static const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  13874. 0x00, 0x04, 0x01, 0x01 };
  13875. static const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  13876. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  13877. static const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  13878. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  13879. static const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  13880. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  13881. static const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  13882. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  13883. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  13884. static const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  13885. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  13886. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  13887. static const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  13888. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  13889. 0x03, 0x03, 0x04, 0x01, 0x01 };
  13890. static const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  13891. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  13892. 0x03, 0x03, 0x00, 0x04, 0x01 };
  13893. XMEMSET(&key, 0, sizeof(key));
  13894. wc_ecc_init(&key);
  13895. inSz = sizeof(good);
  13896. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, &key, inSz);
  13897. if (ret != BAD_FUNC_ARG) {
  13898. ret = -8500;
  13899. goto done;
  13900. }
  13901. ret = wc_EccPublicKeyDecode(good, NULL, &key, inSz);
  13902. if (ret != BAD_FUNC_ARG) {
  13903. ret = -8501;
  13904. goto done;
  13905. }
  13906. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  13907. if (ret != BAD_FUNC_ARG) {
  13908. ret = -8502;
  13909. goto done;
  13910. }
  13911. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, 0);
  13912. if (ret != BAD_FUNC_ARG) {
  13913. ret = -8503;
  13914. goto done;
  13915. }
  13916. /* Change offset to produce bad input data. */
  13917. inOutIdx = 2;
  13918. inSz = sizeof(good) - inOutIdx;
  13919. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  13920. if (ret != ASN_PARSE_E) {
  13921. ret = -8504;
  13922. goto done;
  13923. }
  13924. inOutIdx = 4;
  13925. inSz = sizeof(good) - inOutIdx;
  13926. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  13927. if (ret != ASN_PARSE_E) {
  13928. ret = -8505;
  13929. goto done;
  13930. }
  13931. /* Bad data. */
  13932. inSz = sizeof(badNoObjId);
  13933. inOutIdx = 0;
  13934. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, &key, inSz);
  13935. if (ret != ASN_OBJECT_ID_E) {
  13936. ret = -8506;
  13937. goto done;
  13938. }
  13939. inSz = sizeof(badOneObjId);
  13940. inOutIdx = 0;
  13941. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, &key, inSz);
  13942. if (ret != ASN_OBJECT_ID_E) {
  13943. ret = -8507;
  13944. goto done;
  13945. }
  13946. inSz = sizeof(badObjId1Len);
  13947. inOutIdx = 0;
  13948. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, &key, inSz);
  13949. if (ret != ASN_PARSE_E) {
  13950. ret = -8508;
  13951. goto done;
  13952. }
  13953. inSz = sizeof(badObj2d1Len);
  13954. inOutIdx = 0;
  13955. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, &key, inSz);
  13956. if (ret != ASN_PARSE_E) {
  13957. ret = -8509;
  13958. goto done;
  13959. }
  13960. inSz = sizeof(badNotBitStr);
  13961. inOutIdx = 0;
  13962. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, &key, inSz);
  13963. if (ret != ASN_BITSTR_E) {
  13964. ret = -8510;
  13965. goto done;
  13966. }
  13967. inSz = sizeof(badBitStrLen);
  13968. inOutIdx = 0;
  13969. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, &key, inSz);
  13970. if (ret != ASN_PARSE_E) {
  13971. ret = -8511;
  13972. goto done;
  13973. }
  13974. inSz = sizeof(badNoBitStrZero);
  13975. inOutIdx = 0;
  13976. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, &key, inSz);
  13977. if (ret != ASN_EXPECT_0_E) {
  13978. ret = -8512;
  13979. goto done;
  13980. }
  13981. inSz = sizeof(badPoint);
  13982. inOutIdx = 0;
  13983. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, &key, inSz);
  13984. if (ret != ASN_ECC_KEY_E) {
  13985. ret = -8513;
  13986. goto done;
  13987. }
  13988. inSz = sizeof(good);
  13989. inOutIdx = 0;
  13990. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  13991. if (ret != 0) {
  13992. ret = -8514;
  13993. goto done;
  13994. }
  13995. done:
  13996. wc_ecc_free(&key);
  13997. return ret;
  13998. }
  13999. #endif /* WOLFSSL_CERT_EXT */
  14000. #ifdef WOLFSSL_CUSTOM_CURVES
  14001. static const byte eccKeyExplicitCurve[] = {
  14002. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  14003. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  14004. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  14005. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  14006. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  14007. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  14008. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  14009. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  14010. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  14011. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  14012. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  14013. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  14014. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  14015. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  14016. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  14017. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  14018. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  14019. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  14020. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  14021. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  14022. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  14023. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  14024. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  14025. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  14026. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  14027. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  14028. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  14029. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  14030. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  14031. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  14032. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  14033. };
  14034. static int ecc_test_custom_curves(WC_RNG* rng)
  14035. {
  14036. int ret;
  14037. word32 inOutIdx;
  14038. ecc_key key;
  14039. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  14040. const word32 ecc_oid_brainpoolp256r1_sum = 104;
  14041. const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  14042. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  14043. };
  14044. const ecc_set_type ecc_dp_brainpool256r1 = {
  14045. 32, /* size/bytes */
  14046. ECC_CURVE_CUSTOM, /* ID */
  14047. "BRAINPOOLP256R1", /* curve name */
  14048. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  14049. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  14050. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  14051. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  14052. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  14053. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  14054. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  14055. sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t),
  14056. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  14057. 1, /* cofactor */
  14058. };
  14059. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  14060. &ecc_dp_brainpool256r1);
  14061. if (ret != 0) {
  14062. printf("ECC test for custom curve failed! %d\n", ret);
  14063. return ret;
  14064. }
  14065. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  14066. {
  14067. int curve_id;
  14068. #ifdef HAVE_ECC_BRAINPOOL
  14069. curve_id = ECC_BRAINPOOLP256R1;
  14070. #else
  14071. curve_id = ECC_SECP256K1;
  14072. #endif
  14073. /* Test and demonstrate use of non-SECP curve */
  14074. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  14075. if (ret < 0) {
  14076. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  14077. return ret;
  14078. }
  14079. }
  14080. #endif
  14081. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  14082. if (ret != 0) {
  14083. return -8515;
  14084. }
  14085. inOutIdx = 0;
  14086. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, &key,
  14087. sizeof(eccKeyExplicitCurve));
  14088. if (ret != 0)
  14089. return -8516;
  14090. wc_ecc_free(&key);
  14091. return ret;
  14092. }
  14093. #endif /* WOLFSSL_CUSTOM_CURVES */
  14094. #ifdef WOLFSSL_CERT_GEN
  14095. /* Make Cert / Sign example for ECC cert and ECC CA */
  14096. static int ecc_test_cert_gen(WC_RNG* rng)
  14097. {
  14098. int ret;
  14099. Cert myCert;
  14100. int certSz;
  14101. size_t bytes;
  14102. word32 idx = 0;
  14103. #ifndef USE_CERT_BUFFERS_256
  14104. FILE* file;
  14105. #endif
  14106. #ifdef WOLFSSL_TEST_CERT
  14107. DecodedCert decode;
  14108. #endif
  14109. byte* der = NULL;
  14110. byte* pem = NULL;
  14111. ecc_key caEccKey;
  14112. ecc_key certPubKey;
  14113. XMEMSET(&caEccKey, 0, sizeof(caEccKey));
  14114. XMEMSET(&certPubKey, 0, sizeof(certPubKey));
  14115. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14116. if (der == NULL) {
  14117. ERROR_OUT(-8517, exit);
  14118. }
  14119. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14120. if (pem == NULL) {
  14121. ERROR_OUT(-8518, exit);
  14122. }
  14123. /* Get cert private key */
  14124. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14125. /* Get Cert Key 384 */
  14126. #ifdef USE_CERT_BUFFERS_256
  14127. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  14128. bytes = sizeof_ca_ecc_key_der_384;
  14129. #else
  14130. file = fopen(eccCaKey384File, "rb");
  14131. if (!file) {
  14132. ERROR_OUT(-8519, exit);
  14133. }
  14134. bytes = fread(der, 1, FOURK_BUF, file);
  14135. fclose(file);
  14136. (void)eccCaKeyFile;
  14137. #endif /* USE_CERT_BUFFERS_256 */
  14138. #else
  14139. #ifdef USE_CERT_BUFFERS_256
  14140. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  14141. bytes = sizeof_ca_ecc_key_der_256;
  14142. #else
  14143. file = fopen(eccCaKeyFile, "rb");
  14144. if (!file) {
  14145. ERROR_OUT(-8520, exit);
  14146. }
  14147. bytes = fread(der, 1, FOURK_BUF, file);
  14148. fclose(file);
  14149. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14150. (void)eccCaKey384File;
  14151. #endif
  14152. #endif /* USE_CERT_BUFFERS_256 */
  14153. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  14154. /* Get CA Key */
  14155. ret = wc_ecc_init_ex(&caEccKey, HEAP_HINT, devId);
  14156. if (ret != 0) {
  14157. ERROR_OUT(-8521, exit);
  14158. }
  14159. ret = wc_EccPrivateKeyDecode(der, &idx, &caEccKey, (word32)bytes);
  14160. if (ret != 0) {
  14161. ERROR_OUT(-8522, exit);
  14162. }
  14163. /* Make a public key */
  14164. ret = wc_ecc_init_ex(&certPubKey, HEAP_HINT, devId);
  14165. if (ret != 0) {
  14166. ERROR_OUT(-8523, exit);
  14167. }
  14168. ret = wc_ecc_make_key(rng, 32, &certPubKey);
  14169. #if defined(WOLFSSL_ASYNC_CRYPT)
  14170. ret = wc_AsyncWait(ret, &certPubKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14171. #endif
  14172. if (ret != 0) {
  14173. ERROR_OUT(-8524, exit);
  14174. }
  14175. /* Setup Certificate */
  14176. if (wc_InitCert(&myCert)) {
  14177. ERROR_OUT(-8525, exit);
  14178. }
  14179. #ifndef NO_SHA256
  14180. myCert.sigType = CTC_SHA256wECDSA;
  14181. #else
  14182. myCert.sigType = CTC_SHAwECDSA;
  14183. #endif
  14184. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  14185. #ifdef WOLFSSL_CERT_EXT
  14186. /* add Policies */
  14187. XSTRNCPY(myCert.certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  14188. CTC_MAX_CERTPOL_SZ);
  14189. XSTRNCPY(myCert.certPolicies[1], "1.2.13025.489.1.113549",
  14190. CTC_MAX_CERTPOL_SZ);
  14191. myCert.certPoliciesNb = 2;
  14192. /* add SKID from the Public Key */
  14193. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, NULL, &certPubKey) != 0) {
  14194. ERROR_OUT(-8526, exit);
  14195. }
  14196. /* add AKID from the Public Key */
  14197. if (wc_SetAuthKeyIdFromPublicKey(&myCert, NULL, &caEccKey) != 0) {
  14198. ERROR_OUT(-8527, exit);
  14199. }
  14200. /* add Key Usage */
  14201. if (wc_SetKeyUsage(&myCert, certKeyUsage) != 0) {
  14202. ERROR_OUT(-8528, exit);
  14203. }
  14204. #endif /* WOLFSSL_CERT_EXT */
  14205. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14206. #if defined(USE_CERT_BUFFERS_256)
  14207. ret = wc_SetIssuerBuffer(&myCert, ca_ecc_cert_der_384,
  14208. sizeof_ca_ecc_cert_der_384);
  14209. #else
  14210. ret = wc_SetIssuer(&myCert, eccCaCert384File);
  14211. (void)eccCaCertFile;
  14212. #endif
  14213. #else
  14214. #if defined(USE_CERT_BUFFERS_256)
  14215. ret = wc_SetIssuerBuffer(&myCert, ca_ecc_cert_der_256,
  14216. sizeof_ca_ecc_cert_der_256);
  14217. #else
  14218. ret = wc_SetIssuer(&myCert, eccCaCertFile);
  14219. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  14220. (void)eccCaCert384File;
  14221. #endif
  14222. #endif
  14223. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  14224. if (ret < 0) {
  14225. ERROR_OUT(-8529, exit);
  14226. }
  14227. certSz = wc_MakeCert(&myCert, der, FOURK_BUF, NULL, &certPubKey, rng);
  14228. if (certSz < 0) {
  14229. ERROR_OUT(-8530, exit);
  14230. }
  14231. ret = 0;
  14232. do {
  14233. #if defined(WOLFSSL_ASYNC_CRYPT)
  14234. ret = wc_AsyncWait(ret, &caEccKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14235. #endif
  14236. if (ret >= 0) {
  14237. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der,
  14238. FOURK_BUF, NULL, &caEccKey, rng);
  14239. }
  14240. } while (ret == WC_PENDING_E);
  14241. if (ret < 0) {
  14242. ERROR_OUT(-8531, exit);
  14243. }
  14244. certSz = ret;
  14245. #ifdef WOLFSSL_TEST_CERT
  14246. InitDecodedCert(&decode, der, certSz, 0);
  14247. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  14248. if (ret != 0) {
  14249. FreeDecodedCert(&decode);
  14250. ERROR_OUT(-8532, exit);
  14251. }
  14252. FreeDecodedCert(&decode);
  14253. #endif
  14254. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, certEccDerFile,
  14255. certEccPemFile, CERT_TYPE, -6735);
  14256. if (ret != 0) {
  14257. goto exit;
  14258. }
  14259. exit:
  14260. wc_ecc_free(&certPubKey);
  14261. wc_ecc_free(&caEccKey);
  14262. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14263. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14264. return ret;
  14265. }
  14266. #endif /* WOLFSSL_CERT_GEN */
  14267. int ecc_test(void)
  14268. {
  14269. int ret;
  14270. WC_RNG rng;
  14271. #ifdef WOLFSSL_CERT_EXT
  14272. ret = ecc_decode_test();
  14273. if (ret < 0)
  14274. return ret;
  14275. #endif
  14276. #ifndef HAVE_FIPS
  14277. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14278. #else
  14279. ret = wc_InitRng(&rng);
  14280. #endif
  14281. if (ret != 0)
  14282. return -8600;
  14283. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  14284. ret = ecc_test_curve(&rng, 14);
  14285. if (ret < 0) {
  14286. goto done;
  14287. }
  14288. #endif /* HAVE_ECC112 */
  14289. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  14290. ret = ecc_test_curve(&rng, 16);
  14291. if (ret < 0) {
  14292. goto done;
  14293. }
  14294. #endif /* HAVE_ECC128 */
  14295. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  14296. ret = ecc_test_curve(&rng, 20);
  14297. if (ret < 0) {
  14298. goto done;
  14299. }
  14300. #endif /* HAVE_ECC160 */
  14301. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  14302. ret = ecc_test_curve(&rng, 24);
  14303. if (ret < 0) {
  14304. goto done;
  14305. }
  14306. #endif /* HAVE_ECC192 */
  14307. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  14308. ret = ecc_test_curve(&rng, 28);
  14309. if (ret < 0) {
  14310. goto done;
  14311. }
  14312. #endif /* HAVE_ECC224 */
  14313. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  14314. ret = ecc_test_curve(&rng, 30);
  14315. if (ret < 0) {
  14316. goto done;
  14317. }
  14318. #endif /* HAVE_ECC239 */
  14319. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  14320. ret = ecc_test_curve(&rng, 32);
  14321. if (ret < 0) {
  14322. goto done;
  14323. }
  14324. #if !defined(WOLFSSL_ATECC508A) && defined(HAVE_ECC_KEY_IMPORT) && \
  14325. defined(HAVE_ECC_KEY_EXPORT)
  14326. ret = ecc_point_test();
  14327. if (ret < 0) {
  14328. goto done;
  14329. }
  14330. #endif
  14331. ret = ecc_def_curve_test(&rng);
  14332. if (ret < 0) {
  14333. goto done;
  14334. }
  14335. #endif /* !NO_ECC256 */
  14336. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  14337. ret = ecc_test_curve(&rng, 40);
  14338. if (ret < 0) {
  14339. goto done;
  14340. }
  14341. #endif /* HAVE_ECC320 */
  14342. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  14343. ret = ecc_test_curve(&rng, 48);
  14344. if (ret < 0) {
  14345. goto done;
  14346. }
  14347. #endif /* HAVE_ECC384 */
  14348. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  14349. ret = ecc_test_curve(&rng, 64);
  14350. if (ret < 0) {
  14351. goto done;
  14352. }
  14353. #endif /* HAVE_ECC512 */
  14354. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  14355. ret = ecc_test_curve(&rng, 66);
  14356. if (ret < 0) {
  14357. goto done;
  14358. }
  14359. #endif /* HAVE_ECC521 */
  14360. #if defined(WOLFSSL_CUSTOM_CURVES)
  14361. ret = ecc_test_custom_curves(&rng);
  14362. if (ret != 0) {
  14363. goto done;
  14364. }
  14365. #endif
  14366. #ifdef HAVE_ECC_CDH
  14367. ret = ecc_test_cdh_vectors();
  14368. if (ret != 0) {
  14369. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  14370. goto done;
  14371. }
  14372. #endif
  14373. ret = ecc_test_make_pub(&rng);
  14374. if (ret != 0) {
  14375. printf("ecc_test_make_pub failed!: %d\n", ret);
  14376. goto done;
  14377. }
  14378. #ifdef WOLFSSL_CERT_GEN
  14379. ret = ecc_test_cert_gen(&rng);
  14380. if (ret != 0) {
  14381. printf("ecc_test_cert_gen failed!: %d\n", ret);
  14382. goto done;
  14383. }
  14384. #endif
  14385. done:
  14386. wc_FreeRng(&rng);
  14387. return ret;
  14388. }
  14389. #if defined(HAVE_ECC_ENCRYPT) && defined(WOLFSSL_AES_128)
  14390. int ecc_encrypt_test(void)
  14391. {
  14392. WC_RNG rng;
  14393. int ret = 0;
  14394. ecc_key userA, userB;
  14395. byte msg[48];
  14396. byte plain[48];
  14397. byte out[80];
  14398. word32 outSz = sizeof(out);
  14399. word32 plainSz = sizeof(plain);
  14400. int i;
  14401. ecEncCtx* cliCtx = NULL;
  14402. ecEncCtx* srvCtx = NULL;
  14403. byte cliSalt[EXCHANGE_SALT_SZ];
  14404. byte srvSalt[EXCHANGE_SALT_SZ];
  14405. const byte* tmpSalt;
  14406. byte msg2[48];
  14407. byte plain2[48];
  14408. byte out2[80];
  14409. word32 outSz2 = sizeof(out2);
  14410. word32 plainSz2 = sizeof(plain2);
  14411. #ifndef HAVE_FIPS
  14412. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14413. #else
  14414. ret = wc_InitRng(&rng);
  14415. #endif
  14416. if (ret != 0)
  14417. return -8700;
  14418. XMEMSET(&userA, 0, sizeof(userA));
  14419. XMEMSET(&userB, 0, sizeof(userB));
  14420. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  14421. if (ret != 0)
  14422. goto done;
  14423. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  14424. if (ret != 0)
  14425. goto done;
  14426. ret = wc_ecc_make_key(&rng, 32, &userA);
  14427. #if defined(WOLFSSL_ASYNC_CRYPT)
  14428. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  14429. #endif
  14430. if (ret != 0){
  14431. ret = -8701; goto done;
  14432. }
  14433. ret = wc_ecc_make_key(&rng, 32, &userB);
  14434. #if defined(WOLFSSL_ASYNC_CRYPT)
  14435. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  14436. #endif
  14437. if (ret != 0){
  14438. ret = -8702; goto done;
  14439. }
  14440. /* set message to incrementing 0,1,2,etc... */
  14441. for (i = 0; i < (int)sizeof(msg); i++)
  14442. msg[i] = i;
  14443. /* encrypt msg to B */
  14444. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
  14445. if (ret != 0) {
  14446. ret = -8703; goto done;
  14447. }
  14448. /* decrypt msg from A */
  14449. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL);
  14450. if (ret != 0) {
  14451. ret = -8704; goto done;
  14452. }
  14453. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  14454. ret = -8705; goto done;
  14455. }
  14456. /* let's verify message exchange works, A is client, B is server */
  14457. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  14458. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  14459. if (cliCtx == NULL || srvCtx == NULL) {
  14460. ret = -8706; goto done;
  14461. }
  14462. /* get salt to send to peer */
  14463. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  14464. if (tmpSalt == NULL) {
  14465. ret = -8707; goto done;
  14466. }
  14467. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  14468. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  14469. if (tmpSalt == NULL) {
  14470. ret = -8708; goto done;
  14471. }
  14472. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  14473. /* in actual use, we'd get the peer's salt over the transport */
  14474. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  14475. if (ret != 0)
  14476. goto done;
  14477. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  14478. if (ret != 0)
  14479. goto done;
  14480. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  14481. if (ret != 0)
  14482. goto done;
  14483. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  14484. if (ret != 0)
  14485. goto done;
  14486. /* get encrypted msg (request) to send to B */
  14487. outSz = sizeof(out);
  14488. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx);
  14489. if (ret != 0)
  14490. goto done;
  14491. /* B decrypts msg (request) from A */
  14492. plainSz = sizeof(plain);
  14493. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx);
  14494. if (ret != 0)
  14495. goto done;
  14496. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  14497. ret = -8709; goto done;
  14498. }
  14499. /* msg2 (response) from B to A */
  14500. for (i = 0; i < (int)sizeof(msg2); i++)
  14501. msg2[i] = i + sizeof(msg2);
  14502. /* get encrypted msg (response) to send to B */
  14503. ret = wc_ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2,
  14504. &outSz2, srvCtx);
  14505. if (ret != 0)
  14506. goto done;
  14507. /* A decrypts msg (response) from B */
  14508. ret = wc_ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2,
  14509. cliCtx);
  14510. if (ret != 0)
  14511. goto done;
  14512. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  14513. ret = -8710; goto done;
  14514. }
  14515. done:
  14516. /* cleanup */
  14517. wc_ecc_ctx_free(srvCtx);
  14518. wc_ecc_ctx_free(cliCtx);
  14519. wc_ecc_free(&userB);
  14520. wc_ecc_free(&userA);
  14521. wc_FreeRng(&rng);
  14522. return ret;
  14523. }
  14524. #endif /* HAVE_ECC_ENCRYPT */
  14525. #ifdef USE_CERT_BUFFERS_256
  14526. int ecc_test_buffers(void) {
  14527. size_t bytes;
  14528. ecc_key cliKey;
  14529. ecc_key servKey;
  14530. WC_RNG rng;
  14531. word32 idx = 0;
  14532. int ret;
  14533. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  14534. byte in[] = "Everyone gets Friday off. ecc p";
  14535. word32 inLen = (word32)XSTRLEN((char*)in);
  14536. byte out[256];
  14537. byte plain[256];
  14538. int verify = 0;
  14539. word32 x;
  14540. ret = wc_ecc_init_ex(&cliKey, HEAP_HINT, devId);
  14541. if (ret != 0)
  14542. return -8721;
  14543. ret = wc_ecc_init_ex(&servKey, HEAP_HINT, devId);
  14544. if (ret != 0)
  14545. return -8722;
  14546. bytes = (size_t)sizeof_ecc_clikey_der_256;
  14547. /* place client key into ecc_key struct cliKey */
  14548. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &cliKey,
  14549. (word32)bytes);
  14550. if (ret != 0)
  14551. return -8711;
  14552. idx = 0;
  14553. bytes = (size_t)sizeof_ecc_key_der_256;
  14554. /* place server key into ecc_key struct servKey */
  14555. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, &servKey,
  14556. (word32)bytes);
  14557. if (ret != 0)
  14558. return -8712;
  14559. #ifndef HAVE_FIPS
  14560. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14561. #else
  14562. ret = wc_InitRng(&rng);
  14563. #endif
  14564. if (ret != 0)
  14565. return -8713;
  14566. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF)
  14567. {
  14568. word32 y;
  14569. /* test encrypt and decrypt if they're available */
  14570. x = sizeof(out);
  14571. ret = wc_ecc_encrypt(&cliKey, &servKey, in, sizeof(in), out, &x, NULL);
  14572. if (ret < 0)
  14573. return -8714;
  14574. y = sizeof(plain);
  14575. ret = wc_ecc_decrypt(&cliKey, &servKey, out, x, plain, &y, NULL);
  14576. if (ret < 0)
  14577. return -8715;
  14578. if (XMEMCMP(plain, in, inLen))
  14579. return -8716;
  14580. }
  14581. #endif
  14582. x = sizeof(out);
  14583. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, &cliKey);
  14584. if (ret < 0)
  14585. return -8717;
  14586. XMEMSET(plain, 0, sizeof(plain));
  14587. ret = wc_ecc_verify_hash(out, x, plain, sizeof(plain), &verify, &cliKey);
  14588. if (ret < 0)
  14589. return -8718;
  14590. if (XMEMCMP(plain, in, (word32)ret))
  14591. return -8719;
  14592. #ifdef WOLFSSL_CERT_EXT
  14593. idx = 0;
  14594. bytes = sizeof_ecc_clikeypub_der_256;
  14595. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, &cliKey,
  14596. (word32) bytes);
  14597. if (ret != 0)
  14598. return -8720;
  14599. #endif
  14600. wc_ecc_free(&cliKey);
  14601. wc_ecc_free(&servKey);
  14602. wc_FreeRng(&rng);
  14603. return 0;
  14604. }
  14605. #endif /* USE_CERT_BUFFERS_256 */
  14606. #endif /* HAVE_ECC */
  14607. #ifdef HAVE_CURVE25519
  14608. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  14609. defined(HAVE_CURVE25519_KEY_IMPORT)
  14610. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  14611. #define X25519_TEST_CNT 5
  14612. #else
  14613. #define X25519_TEST_CNT 1
  14614. #endif
  14615. static int curve25519_overflow_test(void)
  14616. {
  14617. /* secret key for party a */
  14618. byte sa[X25519_TEST_CNT][32] = {
  14619. {
  14620. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  14621. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  14622. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  14623. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  14624. },
  14625. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  14626. {
  14627. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  14628. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  14629. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  14630. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  14631. },
  14632. {
  14633. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  14634. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  14635. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  14636. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  14637. },
  14638. {
  14639. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  14640. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  14641. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  14642. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  14643. },
  14644. {
  14645. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  14646. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  14647. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  14648. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  14649. }
  14650. #endif
  14651. };
  14652. /* public key for party b */
  14653. byte pb[X25519_TEST_CNT][32] = {
  14654. {
  14655. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  14656. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  14657. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  14658. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  14659. },
  14660. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  14661. {
  14662. /* 0xff first byte in original - invalid! */
  14663. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  14664. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  14665. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  14666. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  14667. },
  14668. {
  14669. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  14670. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  14671. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  14672. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  14673. },
  14674. {
  14675. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  14676. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  14677. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  14678. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  14679. },
  14680. {
  14681. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  14682. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  14683. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  14684. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  14685. }
  14686. #endif
  14687. };
  14688. /* expected shared key */
  14689. byte ss[X25519_TEST_CNT][32] = {
  14690. {
  14691. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  14692. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  14693. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  14694. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  14695. },
  14696. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  14697. {
  14698. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  14699. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  14700. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  14701. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  14702. },
  14703. {
  14704. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14705. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14706. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14707. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  14708. },
  14709. {
  14710. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14711. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14712. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14713. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  14714. },
  14715. {
  14716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14719. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  14720. }
  14721. #endif
  14722. };
  14723. int i;
  14724. word32 y;
  14725. byte shared[32];
  14726. curve25519_key userA;
  14727. wc_curve25519_init(&userA);
  14728. for (i = 0; i < X25519_TEST_CNT; i++) {
  14729. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  14730. sizeof(pb[i]), &userA) != 0)
  14731. return -8750 - i;
  14732. /* test against known test vector */
  14733. XMEMSET(shared, 0, sizeof(shared));
  14734. y = sizeof(shared);
  14735. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0)
  14736. return -8755 - i;
  14737. if (XMEMCMP(ss[i], shared, y))
  14738. return -8760 - i;
  14739. }
  14740. return 0;
  14741. }
  14742. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  14743. int curve25519_test(void)
  14744. {
  14745. WC_RNG rng;
  14746. int ret;
  14747. #ifdef HAVE_CURVE25519_SHARED_SECRET
  14748. byte sharedA[32];
  14749. byte sharedB[32];
  14750. word32 y;
  14751. #endif
  14752. #ifdef HAVE_CURVE25519_KEY_EXPORT
  14753. byte exportBuf[32];
  14754. #endif
  14755. word32 x;
  14756. curve25519_key userA, userB, pubKey;
  14757. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  14758. defined(HAVE_CURVE25519_KEY_IMPORT)
  14759. /* test vectors from
  14760. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  14761. */
  14762. /* secret key for party a */
  14763. byte sa[] = {
  14764. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  14765. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  14766. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  14767. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  14768. };
  14769. /* public key for party a */
  14770. byte pa[] = {
  14771. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  14772. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  14773. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  14774. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  14775. };
  14776. /* secret key for party b */
  14777. byte sb[] = {
  14778. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  14779. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  14780. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  14781. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  14782. };
  14783. /* public key for party b */
  14784. byte pb[] = {
  14785. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  14786. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  14787. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  14788. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  14789. };
  14790. /* expected shared key */
  14791. byte ss[] = {
  14792. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  14793. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  14794. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  14795. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  14796. };
  14797. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  14798. #ifndef HAVE_FIPS
  14799. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14800. #else
  14801. ret = wc_InitRng(&rng);
  14802. #endif
  14803. if (ret != 0)
  14804. return -8800;
  14805. wc_curve25519_init(&userA);
  14806. wc_curve25519_init(&userB);
  14807. wc_curve25519_init(&pubKey);
  14808. /* make curve25519 keys */
  14809. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  14810. return -8801;
  14811. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  14812. return -8802;
  14813. #ifdef HAVE_CURVE25519_SHARED_SECRET
  14814. /* find shared secret key */
  14815. x = sizeof(sharedA);
  14816. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  14817. return -8803;
  14818. y = sizeof(sharedB);
  14819. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  14820. return -8804;
  14821. /* compare shared secret keys to test they are the same */
  14822. if (y != x)
  14823. return -8805;
  14824. if (XMEMCMP(sharedA, sharedB, x))
  14825. return -8806;
  14826. #endif
  14827. #ifdef HAVE_CURVE25519_KEY_EXPORT
  14828. /* export a public key and import it for another user */
  14829. x = sizeof(exportBuf);
  14830. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  14831. return -8807;
  14832. #ifdef HAVE_CURVE25519_KEY_IMPORT
  14833. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  14834. return -8808;
  14835. #endif
  14836. #endif
  14837. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  14838. defined(HAVE_CURVE25519_KEY_IMPORT)
  14839. /* test shared key after importing a public key */
  14840. XMEMSET(sharedB, 0, sizeof(sharedB));
  14841. y = sizeof(sharedB);
  14842. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  14843. return -8809;
  14844. if (XMEMCMP(sharedA, sharedB, y))
  14845. return -8810;
  14846. /* import RFC test vectors and compare shared key */
  14847. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  14848. != 0)
  14849. return -8811;
  14850. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  14851. != 0)
  14852. return -8812;
  14853. /* test against known test vector */
  14854. XMEMSET(sharedB, 0, sizeof(sharedB));
  14855. y = sizeof(sharedB);
  14856. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  14857. return -8813;
  14858. if (XMEMCMP(ss, sharedB, y))
  14859. return -8814;
  14860. /* test swaping roles of keys and generating same shared key */
  14861. XMEMSET(sharedB, 0, sizeof(sharedB));
  14862. y = sizeof(sharedB);
  14863. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  14864. return -8815;
  14865. if (XMEMCMP(ss, sharedB, y))
  14866. return -8816;
  14867. /* test with 1 generated key and 1 from known test vector */
  14868. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  14869. != 0)
  14870. return -8817;
  14871. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  14872. return -8818;
  14873. x = sizeof(sharedA);
  14874. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  14875. return -8819;
  14876. y = sizeof(sharedB);
  14877. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  14878. return -8820;
  14879. /* compare shared secret keys to test they are the same */
  14880. if (y != x)
  14881. return -8821;
  14882. if (XMEMCMP(sharedA, sharedB, x))
  14883. return -8822;
  14884. ret = curve25519_overflow_test();
  14885. if (ret != 0)
  14886. return ret;
  14887. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  14888. /* clean up keys when done */
  14889. wc_curve25519_free(&pubKey);
  14890. wc_curve25519_free(&userB);
  14891. wc_curve25519_free(&userA);
  14892. wc_FreeRng(&rng);
  14893. return 0;
  14894. }
  14895. #endif /* HAVE_CURVE25519 */
  14896. #ifdef HAVE_ED25519
  14897. #ifdef WOLFSSL_TEST_CERT
  14898. static int ed25519_test_cert(void)
  14899. {
  14900. DecodedCert cert[2];
  14901. DecodedCert* serverCert = NULL;
  14902. DecodedCert* caCert = NULL;
  14903. #ifdef HAVE_ED25519_VERIFY
  14904. ed25519_key key;
  14905. ed25519_key* pubKey = NULL;
  14906. int verify;
  14907. #endif /* HAVE_ED25519_VERIFY */
  14908. int ret;
  14909. byte* tmp;
  14910. size_t bytes;
  14911. FILE* file;
  14912. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14913. if (tmp == NULL) {
  14914. ERROR_OUT(-8823, done);
  14915. }
  14916. #ifdef USE_CERT_BUFFERS_256
  14917. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  14918. bytes = sizeof_ca_ed25519_cert;
  14919. #elif !defined(NO_FILESYSTEM)
  14920. file = fopen(caEd25519Cert, "rb");
  14921. if (file == NULL) {
  14922. ERROR_OUT(-8824, done);
  14923. }
  14924. bytes = fread(tmp, 1, FOURK_BUF, file);
  14925. fclose(file);
  14926. #else
  14927. /* No certificate to use. */
  14928. ERROR_OUT(-8825, done);
  14929. #endif
  14930. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  14931. caCert = &cert[0];
  14932. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  14933. if (ret != 0) {
  14934. ERROR_OUT(-8826, done);
  14935. }
  14936. #ifdef USE_CERT_BUFFERS_256
  14937. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  14938. bytes = sizeof_server_ed25519_cert;
  14939. #elif !defined(NO_FILESYSTEM)
  14940. file = fopen(serverEd25519Cert, "rb");
  14941. if (file == NULL) {
  14942. ERROR_OUT(-8827, done);
  14943. }
  14944. bytes = fread(tmp, 1, FOURK_BUF, file);
  14945. fclose(file);
  14946. #else
  14947. /* No certificate to use. */
  14948. ERROR_OUT(-8828, done);
  14949. #endif
  14950. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  14951. serverCert = &cert[1];
  14952. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  14953. if (ret != 0) {
  14954. ERROR_OUT(-8829, done);
  14955. }
  14956. #ifdef HAVE_ED25519_VERIFY
  14957. ret = wc_ed25519_init(&key);
  14958. if (ret < 0) {
  14959. ERROR_OUT(-8830, done);
  14960. }
  14961. pubKey = &key;
  14962. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  14963. pubKey);
  14964. if (ret < 0) {
  14965. ERROR_OUT(-8831, done);
  14966. }
  14967. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  14968. serverCert->source + serverCert->certBegin,
  14969. serverCert->sigIndex - serverCert->certBegin,
  14970. &verify, pubKey) < 0 || verify != 1) {
  14971. ERROR_OUT(-8832, done);
  14972. }
  14973. #endif /* HAVE_ED25519_VERIFY */
  14974. done:
  14975. if (tmp != NULL)
  14976. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14977. #ifdef HAVE_ED25519_VERIFY
  14978. wc_ed25519_free(pubKey);
  14979. #endif /* HAVE_ED25519_VERIFY */
  14980. if (caCert != NULL)
  14981. FreeDecodedCert(caCert);
  14982. if (serverCert != NULL)
  14983. FreeDecodedCert(serverCert);
  14984. return ret;
  14985. }
  14986. static int ed25519_test_make_cert(void)
  14987. {
  14988. WC_RNG rng;
  14989. Cert cert;
  14990. DecodedCert decode;
  14991. ed25519_key key;
  14992. ed25519_key* privKey = NULL;
  14993. int ret = 0;
  14994. byte* tmp = NULL;
  14995. wc_InitCert(&cert);
  14996. #ifndef HAVE_FIPS
  14997. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14998. #else
  14999. ret = wc_InitRng(&rng);
  15000. #endif
  15001. if (ret != 0)
  15002. return -8833;
  15003. wc_ed25519_init(&key);
  15004. privKey = &key;
  15005. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  15006. cert.daysValid = 365 * 2;
  15007. cert.selfSigned = 1;
  15008. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  15009. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  15010. cert.isCA = 0;
  15011. #ifdef WOLFSSL_CERT_EXT
  15012. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  15013. if (ret < 0) {
  15014. ERROR_OUT(-8834, done);
  15015. }
  15016. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  15017. if (ret < 0) {
  15018. ERROR_OUT(-8835, done);
  15019. }
  15020. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  15021. if (ret < 0) {
  15022. ERROR_OUT(-8836, done);
  15023. }
  15024. #endif
  15025. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15026. if (tmp == NULL) {
  15027. ERROR_OUT(-8837, done);
  15028. }
  15029. cert.sigType = CTC_ED25519;
  15030. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  15031. if (ret < 0) {
  15032. ERROR_OUT(-8838, done);
  15033. }
  15034. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  15035. ED25519_TYPE, privKey, &rng);
  15036. if (ret < 0) {
  15037. ERROR_OUT(-8839, done);
  15038. }
  15039. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  15040. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  15041. FreeDecodedCert(&decode);
  15042. if (ret != 0) {
  15043. ERROR_OUT(-8840, done);
  15044. }
  15045. done:
  15046. if (tmp != NULL)
  15047. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15048. wc_ed25519_free(privKey);
  15049. wc_FreeRng(&rng);
  15050. return ret;
  15051. }
  15052. #endif /* WOLFSSL_TEST_CERT */
  15053. int ed25519_test(void)
  15054. {
  15055. int ret;
  15056. WC_RNG rng;
  15057. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  15058. defined(HAVE_ED25519_KEY_IMPORT)
  15059. byte out[ED25519_SIG_SIZE];
  15060. byte exportPKey[ED25519_KEY_SIZE];
  15061. byte exportSKey[ED25519_KEY_SIZE];
  15062. word32 exportPSz;
  15063. word32 exportSSz;
  15064. int i;
  15065. word32 outlen;
  15066. #ifdef HAVE_ED25519_VERIFY
  15067. int verify;
  15068. #endif /* HAVE_ED25519_VERIFY */
  15069. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  15070. word32 keySz, sigSz;
  15071. ed25519_key key;
  15072. ed25519_key key2;
  15073. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  15074. defined(HAVE_ED25519_KEY_IMPORT)
  15075. /* test vectors from
  15076. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  15077. */
  15078. static const byte sKey1[] = {
  15079. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  15080. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  15081. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  15082. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  15083. };
  15084. static const byte sKey2[] = {
  15085. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  15086. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  15087. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  15088. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  15089. };
  15090. static const byte sKey3[] = {
  15091. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  15092. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  15093. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  15094. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  15095. };
  15096. /* uncompressed test */
  15097. static const byte sKey4[] = {
  15098. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  15099. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  15100. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  15101. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  15102. };
  15103. /* compressed prefix test */
  15104. static const byte sKey5[] = {
  15105. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  15106. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  15107. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  15108. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  15109. };
  15110. static const byte sKey6[] = {
  15111. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  15112. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  15113. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  15114. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  15115. };
  15116. static const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  15117. static const byte pKey1[] = {
  15118. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  15119. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  15120. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  15121. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  15122. };
  15123. static const byte pKey2[] = {
  15124. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  15125. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  15126. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  15127. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  15128. };
  15129. static const byte pKey3[] = {
  15130. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  15131. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  15132. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  15133. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  15134. };
  15135. /* uncompressed test */
  15136. static const byte pKey4[] = {
  15137. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  15138. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  15139. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  15140. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  15141. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  15142. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  15143. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  15144. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  15145. 0xd7
  15146. };
  15147. /* compressed prefix */
  15148. static const byte pKey5[] = {
  15149. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  15150. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  15151. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  15152. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  15153. };
  15154. static const byte pKey6[] = {
  15155. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  15156. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  15157. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  15158. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  15159. };
  15160. static const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  15161. static const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  15162. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  15163. static const byte sig1[] = {
  15164. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  15165. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  15166. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  15167. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  15168. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  15169. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  15170. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  15171. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  15172. };
  15173. static const byte sig2[] = {
  15174. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  15175. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  15176. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  15177. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  15178. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  15179. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  15180. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  15181. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  15182. };
  15183. static const byte sig3[] = {
  15184. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  15185. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  15186. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  15187. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  15188. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  15189. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  15190. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  15191. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  15192. };
  15193. /* uncompressed test */
  15194. static const byte sig4[] = {
  15195. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  15196. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  15197. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  15198. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  15199. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  15200. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  15201. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  15202. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  15203. };
  15204. /* compressed prefix */
  15205. static const byte sig5[] = {
  15206. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  15207. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  15208. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  15209. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  15210. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  15211. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  15212. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  15213. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  15214. };
  15215. static const byte sig6[] = {
  15216. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  15217. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  15218. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  15219. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  15220. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  15221. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  15222. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  15223. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  15224. };
  15225. static const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  15226. static const byte msg1[] = {0x0 };
  15227. static const byte msg2[] = {0x72};
  15228. static const byte msg3[] = {0xAF,0x82};
  15229. /* test of a 1024 byte long message */
  15230. static const byte msg4[] = {
  15231. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  15232. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  15233. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  15234. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  15235. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  15236. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  15237. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  15238. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  15239. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  15240. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  15241. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  15242. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  15243. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  15244. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  15245. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  15246. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  15247. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  15248. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  15249. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  15250. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  15251. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  15252. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  15253. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  15254. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  15255. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  15256. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  15257. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  15258. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  15259. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  15260. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  15261. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  15262. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  15263. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  15264. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  15265. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  15266. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  15267. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  15268. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  15269. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  15270. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  15271. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  15272. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  15273. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  15274. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  15275. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  15276. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  15277. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  15278. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  15279. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  15280. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  15281. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  15282. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  15283. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  15284. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  15285. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  15286. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  15287. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  15288. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  15289. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  15290. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  15291. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  15292. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  15293. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  15294. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  15295. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  15296. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  15297. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  15298. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  15299. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  15300. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  15301. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  15302. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  15303. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  15304. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  15305. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  15306. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  15307. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  15308. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  15309. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  15310. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  15311. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  15312. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  15313. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  15314. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  15315. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  15316. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  15317. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  15318. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  15319. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  15320. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  15321. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  15322. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  15323. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  15324. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  15325. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  15326. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  15327. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  15328. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  15329. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  15330. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  15331. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  15332. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  15333. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  15334. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  15335. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  15336. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  15337. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  15338. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  15339. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  15340. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  15341. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  15342. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  15343. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  15344. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  15345. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  15346. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  15347. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  15348. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  15349. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  15350. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  15351. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  15352. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  15353. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  15354. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  15355. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  15356. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  15357. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  15358. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  15359. };
  15360. static const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  15361. static const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  15362. sizeof(msg2),
  15363. sizeof(msg3),
  15364. 0 /*sizeof(msg1)*/,
  15365. 0 /*sizeof(msg1)*/,
  15366. sizeof(msg4)
  15367. };
  15368. #ifndef NO_ASN
  15369. static byte privateEd25519[] = {
  15370. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  15371. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  15372. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  15373. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  15374. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  15375. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  15376. };
  15377. static byte publicEd25519[] = {
  15378. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  15379. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  15380. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  15381. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  15382. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  15383. 0xf7,0x07,0x51,0x1a
  15384. };
  15385. static byte privPubEd25519[] = {
  15386. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  15387. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  15388. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  15389. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  15390. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  15391. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  15392. 0xa1,0x22,0x04,0x20,0xd7,0x5a,0x98,0x01,
  15393. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  15394. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  15395. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  15396. 0xf7,0x07,0x51,0x1a
  15397. };
  15398. word32 idx;
  15399. ed25519_key key3;
  15400. #endif /* NO_ASN */
  15401. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  15402. /* create ed25519 keys */
  15403. #ifndef HAVE_FIPS
  15404. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15405. #else
  15406. ret = wc_InitRng(&rng);
  15407. #endif
  15408. if (ret != 0)
  15409. return -8900;
  15410. wc_ed25519_init(&key);
  15411. wc_ed25519_init(&key2);
  15412. #ifndef NO_ASN
  15413. wc_ed25519_init(&key3);
  15414. #endif
  15415. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  15416. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  15417. /* helper functions for signature and key size */
  15418. keySz = wc_ed25519_size(&key);
  15419. sigSz = wc_ed25519_sig_size(&key);
  15420. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  15421. defined(HAVE_ED25519_KEY_IMPORT)
  15422. for (i = 0; i < 6; i++) {
  15423. outlen = sizeof(out);
  15424. XMEMSET(out, 0, sizeof(out));
  15425. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  15426. pKeySz[i], &key) != 0)
  15427. return -8901 - i;
  15428. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  15429. return -8911 - i;
  15430. if (XMEMCMP(out, sigs[i], 64))
  15431. return -8921 - i;
  15432. #if defined(HAVE_ED25519_VERIFY)
  15433. /* test verify on good msg */
  15434. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  15435. &key) != 0 || verify != 1)
  15436. return -8931 - i;
  15437. /* test verify on bad msg */
  15438. out[outlen-1] = out[outlen-1] + 1;
  15439. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  15440. &key) == 0 || verify == 1)
  15441. return -8941 - i;
  15442. #endif /* HAVE_ED25519_VERIFY */
  15443. /* test api for import/exporting keys */
  15444. exportPSz = sizeof(exportPKey);
  15445. exportSSz = sizeof(exportSKey);
  15446. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  15447. return -8951 - i;
  15448. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  15449. return -8961 - i;
  15450. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  15451. return -8971 - i;
  15452. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  15453. exportPKey, exportPSz, &key2) != 0)
  15454. return -8981 - i;
  15455. /* clear "out" buffer and test sign with imported keys */
  15456. outlen = sizeof(out);
  15457. XMEMSET(out, 0, sizeof(out));
  15458. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  15459. return -8991 - i;
  15460. #if defined(HAVE_ED25519_VERIFY)
  15461. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  15462. &key2) != 0 || verify != 1)
  15463. return -9001 - i;
  15464. if (XMEMCMP(out, sigs[i], 64))
  15465. return -9011 - i;
  15466. #endif /* HAVE_ED25519_VERIFY */
  15467. }
  15468. #ifndef NO_ASN
  15469. /* Try ASN.1 encoded private-only key and public key. */
  15470. idx = 0;
  15471. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  15472. sizeof(privateEd25519)) != 0)
  15473. return -7230 - i;
  15474. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  15475. != BAD_FUNC_ARG)
  15476. return -7231 - i;
  15477. idx = 0;
  15478. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  15479. sizeof(publicEd25519)) != 0)
  15480. return -7232 - i;
  15481. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  15482. return -7233 - i;
  15483. if (XMEMCMP(out, sigs[0], 64))
  15484. return -7234 - i;
  15485. #if defined(HAVE_ED25519_VERIFY)
  15486. /* test verify on good msg */
  15487. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  15488. != 0 || verify != 1)
  15489. return -7233 - i;
  15490. #endif /* HAVE_ED25519_VERIFY */
  15491. wc_ed25519_free(&key3);
  15492. wc_ed25519_init(&key3);
  15493. idx = 0;
  15494. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  15495. sizeof(privPubEd25519)) != 0)
  15496. return -7230 - i;
  15497. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  15498. return -7233 - i;
  15499. if (XMEMCMP(out, sigs[0], 64))
  15500. return -7234 - i;
  15501. wc_ed25519_free(&key3);
  15502. #endif /* NO_ASN */
  15503. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  15504. /* clean up keys when done */
  15505. wc_ed25519_free(&key);
  15506. wc_ed25519_free(&key2);
  15507. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  15508. wc_FreeRng(&rng);
  15509. #endif
  15510. /* hush warnings of unused keySz and sigSz */
  15511. (void)keySz;
  15512. (void)sigSz;
  15513. #ifdef WOLFSSL_TEST_CERT
  15514. ret = ed25519_test_cert();
  15515. if (ret < 0)
  15516. return ret;
  15517. #ifdef WOLFSSL_CERT_GEN
  15518. ret = ed25519_test_make_cert();
  15519. if (ret < 0)
  15520. return ret;
  15521. #endif /* WOLFSSL_CERT_GEN */
  15522. #endif /* WOLFSSL_TEST_CERT */
  15523. return 0;
  15524. }
  15525. #endif /* HAVE_ED25519 */
  15526. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  15527. typedef struct CMAC_Test_Case {
  15528. int type;
  15529. int partial;
  15530. const byte* m;
  15531. word32 mSz;
  15532. const byte* k;
  15533. word32 kSz;
  15534. const byte* t;
  15535. word32 tSz;
  15536. } CMAC_Test_Case;
  15537. int cmac_test(void)
  15538. {
  15539. #ifdef WOLFSSL_AES_128
  15540. const byte k128[] =
  15541. {
  15542. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  15543. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  15544. };
  15545. #define KLEN_128 (sizeof(k128))
  15546. #endif
  15547. #ifdef WOLFSSL_AES_192
  15548. const byte k192[] =
  15549. {
  15550. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  15551. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  15552. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  15553. };
  15554. #define KLEN_192 (sizeof(k192))
  15555. #endif
  15556. #ifdef WOLFSSL_AES_256
  15557. const byte k256[] =
  15558. {
  15559. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  15560. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  15561. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  15562. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  15563. };
  15564. #define KLEN_256 (sizeof(k256))
  15565. #endif
  15566. const byte m[] =
  15567. {
  15568. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  15569. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  15570. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  15571. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  15572. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  15573. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  15574. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  15575. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  15576. };
  15577. #define MLEN_0 (0)
  15578. #define MLEN_128 (128/8)
  15579. #define MLEN_320 (320/8)
  15580. #define MLEN_319 (MLEN_320 - 1)
  15581. #define MLEN_512 (512/8)
  15582. #ifdef WOLFSSL_AES_128
  15583. const byte t128_0[] =
  15584. {
  15585. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  15586. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  15587. };
  15588. const byte t128_128[] =
  15589. {
  15590. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  15591. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  15592. };
  15593. const byte t128_319[] =
  15594. {
  15595. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  15596. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  15597. };
  15598. const byte t128_320[] =
  15599. {
  15600. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  15601. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  15602. };
  15603. const byte t128_512[] =
  15604. {
  15605. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  15606. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  15607. };
  15608. #endif
  15609. #ifdef WOLFSSL_AES_192
  15610. const byte t192_0[] =
  15611. {
  15612. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  15613. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  15614. };
  15615. const byte t192_128[] =
  15616. {
  15617. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  15618. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  15619. };
  15620. const byte t192_320[] =
  15621. {
  15622. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  15623. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  15624. };
  15625. const byte t192_512[] =
  15626. {
  15627. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  15628. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  15629. };
  15630. #endif
  15631. #ifdef WOLFSSL_AES_256
  15632. const byte t256_0[] =
  15633. {
  15634. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  15635. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  15636. };
  15637. const byte t256_128[] =
  15638. {
  15639. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  15640. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  15641. };
  15642. const byte t256_320[] =
  15643. {
  15644. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  15645. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  15646. };
  15647. const byte t256_512[] =
  15648. {
  15649. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  15650. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  15651. };
  15652. #endif
  15653. const CMAC_Test_Case testCases[] =
  15654. {
  15655. #ifdef WOLFSSL_AES_128
  15656. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  15657. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  15658. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  15659. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  15660. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  15661. #endif
  15662. #ifdef WOLFSSL_AES_192
  15663. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  15664. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  15665. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  15666. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  15667. #endif
  15668. #ifdef WOLFSSL_AES_256
  15669. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  15670. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  15671. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  15672. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  15673. #endif
  15674. #ifdef WOLFSSL_AES_128
  15675. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  15676. #endif
  15677. };
  15678. Cmac cmac;
  15679. byte tag[AES_BLOCK_SIZE];
  15680. const CMAC_Test_Case* tc;
  15681. word32 i, tagSz;
  15682. for (i = 0, tc = testCases;
  15683. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  15684. i++, tc++) {
  15685. XMEMSET(tag, 0, sizeof(tag));
  15686. tagSz = AES_BLOCK_SIZE;
  15687. if (wc_InitCmac(&cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  15688. return -9100;
  15689. if (tc->partial) {
  15690. if (wc_CmacUpdate(&cmac, tc->m,
  15691. tc->mSz/2 - tc->partial) != 0)
  15692. return -9101;
  15693. if (wc_CmacUpdate(&cmac, tc->m + tc->mSz/2 - tc->partial,
  15694. tc->mSz/2 + tc->partial) != 0)
  15695. return -9102;
  15696. }
  15697. else {
  15698. if (wc_CmacUpdate(&cmac, tc->m, tc->mSz) != 0)
  15699. return -9103;
  15700. }
  15701. if (wc_CmacFinal(&cmac, tag, &tagSz) != 0)
  15702. return -9104;
  15703. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  15704. return -9105;
  15705. XMEMSET(tag, 0, sizeof(tag));
  15706. tagSz = sizeof(tag);
  15707. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  15708. tc->k, tc->kSz) != 0)
  15709. return -9106;
  15710. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  15711. return -9107;
  15712. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  15713. tc->k, tc->kSz) != 0)
  15714. return -9108;
  15715. }
  15716. return 0;
  15717. }
  15718. #endif /* NO_AES && WOLFSSL_CMAC */
  15719. #ifdef HAVE_LIBZ
  15720. const byte sample_text[] =
  15721. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  15722. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  15723. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  15724. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  15725. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  15726. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  15727. "\n"
  15728. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  15729. "four loko you probably haven't heard of them high life. Messenger bag\n"
  15730. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  15731. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  15732. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  15733. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  15734. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  15735. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  15736. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  15737. "four loko whatever street art yr farm-to-table.\n"
  15738. "\n"
  15739. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  15740. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  15741. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  15742. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  15743. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  15744. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  15745. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  15746. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  15747. "locavore.\n"
  15748. "\n"
  15749. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  15750. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  15751. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  15752. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  15753. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  15754. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  15755. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  15756. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  15757. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  15758. "neutra PBR selvage.\n"
  15759. "\n"
  15760. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  15761. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  15762. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  15763. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  15764. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  15765. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  15766. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  15767. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  15768. "mollit magna, sriracha sartorial helvetica.\n"
  15769. "\n"
  15770. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  15771. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  15772. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  15773. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  15774. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  15775. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  15776. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  15777. "\n"
  15778. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  15779. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  15780. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  15781. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  15782. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  15783. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  15784. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  15785. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  15786. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  15787. "\n"
  15788. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  15789. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  15790. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  15791. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  15792. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  15793. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  15794. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  15795. "bag dolor terry richardson sapiente.\n";
  15796. int compress_test(void)
  15797. {
  15798. int ret = 0;
  15799. word32 dSz = sizeof(sample_text);
  15800. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  15801. byte *c = NULL;
  15802. byte *d = NULL;
  15803. c = XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15804. d = XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15805. if (c == NULL || d == NULL) {
  15806. ERROR_OUT(-9200, exit);
  15807. }
  15808. /* follow calloc and initialize to 0 */
  15809. XMEMSET(c, 0, cSz);
  15810. XMEMSET(d, 0, dSz);
  15811. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  15812. ERROR_OUT(-9201, exit);
  15813. }
  15814. cSz = (word32)ret;
  15815. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  15816. ERROR_OUT(-9202, exit);
  15817. }
  15818. if (XMEMCMP(d, sample_text, dSz)) {
  15819. ERROR_OUT(-9203, exit);
  15820. }
  15821. ret = 0;
  15822. exit:
  15823. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15824. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15825. return ret;
  15826. }
  15827. #endif /* HAVE_LIBZ */
  15828. #ifdef HAVE_PKCS7
  15829. /* External Debugging/Testing Note:
  15830. *
  15831. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  15832. * additional testing. To dump bundles to files DER encoded files, please
  15833. * define:
  15834. *
  15835. * #define PKCS7_OUTPUT_TEST_BUNDLES
  15836. */
  15837. /* Loads certs and keys for use with PKCS7 tests, from either files
  15838. * or buffers.
  15839. *
  15840. * rsaCert - output buffer for RSA cert
  15841. * rsaCertSz - IN/OUT size of output buffer, size of RSA cert
  15842. * rsaPrivKey - output buffer for RSA private key
  15843. * rsaPrivKeySz - IN/OUT size of output buffer, size of RSA key
  15844. * eccCert - output buffer for ECC cert
  15845. * eccCertSz - IN/OUT size of output buffer, size of ECC cert
  15846. * eccPrivKey - output buffer for ECC private key
  15847. * eccPrivKeySz - IN/OUT size of output buffer, size of ECC private key
  15848. *
  15849. * Returns 0 on success, negative on error
  15850. */
  15851. static int pkcs7_load_certs_keys(byte* rsaCert, word32* rsaCertSz,
  15852. byte* rsaPrivKey, word32* rsaPrivKeySz,
  15853. byte* eccCert, word32* eccCertSz,
  15854. byte* eccPrivKey, word32* eccPrivKeySz)
  15855. {
  15856. #ifndef NO_FILESYSTEM
  15857. FILE* certFile;
  15858. FILE* keyFile;
  15859. #endif
  15860. #ifndef NO_RSA
  15861. if (rsaCert == NULL || rsaCertSz == NULL ||
  15862. rsaPrivKey == NULL || rsaPrivKeySz == NULL)
  15863. return BAD_FUNC_ARG;
  15864. #endif
  15865. #ifdef HAVE_ECC
  15866. if (eccCert == NULL || eccCertSz == NULL ||
  15867. eccPrivKey == NULL || eccPrivKeySz == NULL)
  15868. return BAD_FUNC_ARG;
  15869. #endif
  15870. /* RSA */
  15871. #ifndef NO_RSA
  15872. #ifdef USE_CERT_BUFFERS_1024
  15873. if (*rsaCertSz < (word32)sizeof_client_cert_der_1024)
  15874. return -9204;
  15875. XMEMCPY(rsaCert, client_cert_der_1024, sizeof_client_cert_der_1024);
  15876. *rsaCertSz = sizeof_client_cert_der_1024;
  15877. #elif defined(USE_CERT_BUFFERS_2048)
  15878. if (*rsaCertSz < (word32)sizeof_client_cert_der_2048)
  15879. return -9205;
  15880. XMEMCPY(rsaCert, client_cert_der_2048, sizeof_client_cert_der_2048);
  15881. *rsaCertSz = sizeof_client_cert_der_2048;
  15882. #else
  15883. certFile = fopen(clientCert, "rb");
  15884. if (!certFile)
  15885. return -9206;
  15886. *rsaCertSz = (word32)fread(rsaCert, 1, *rsaCertSz, certFile);
  15887. fclose(certFile);
  15888. #endif
  15889. #ifdef USE_CERT_BUFFERS_1024
  15890. if (*rsaPrivKeySz < (word32)sizeof_client_key_der_1024)
  15891. return -9207;
  15892. XMEMCPY(rsaPrivKey, client_key_der_1024, sizeof_client_key_der_1024);
  15893. *rsaPrivKeySz = sizeof_client_key_der_1024;
  15894. #elif defined(USE_CERT_BUFFERS_2048)
  15895. if (*rsaPrivKeySz < (word32)sizeof_client_key_der_2048)
  15896. return -9208;
  15897. XMEMCPY(rsaPrivKey, client_key_der_2048, sizeof_client_key_der_2048);
  15898. *rsaPrivKeySz = sizeof_client_key_der_2048;
  15899. #else
  15900. keyFile = fopen(clientKey, "rb");
  15901. if (!keyFile)
  15902. return -9209;
  15903. *rsaPrivKeySz = (word32)fread(rsaPrivKey, 1, *rsaPrivKeySz, keyFile);
  15904. fclose(keyFile);
  15905. #endif /* USE_CERT_BUFFERS */
  15906. #endif /* NO_RSA */
  15907. /* ECC */
  15908. #ifdef HAVE_ECC
  15909. #ifdef USE_CERT_BUFFERS_256
  15910. if (*eccCertSz < (word32)sizeof_cliecc_cert_der_256)
  15911. return -9210;
  15912. XMEMCPY(eccCert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  15913. *eccCertSz = sizeof_cliecc_cert_der_256;
  15914. #else
  15915. certFile = fopen(eccClientCert, "rb");
  15916. if (!certFile)
  15917. return -9211;
  15918. *eccCertSz = (word32)fread(eccCert, 1, *eccCertSz, certFile);
  15919. fclose(certFile);
  15920. #endif /* USE_CERT_BUFFERS_256 */
  15921. #ifdef USE_CERT_BUFFERS_256
  15922. if (*eccPrivKeySz < (word32)sizeof_ecc_clikey_der_256)
  15923. return -9212;
  15924. XMEMCPY(eccPrivKey, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  15925. *eccPrivKeySz = sizeof_ecc_clikey_der_256;
  15926. #else
  15927. keyFile = fopen(eccClientKey, "rb");
  15928. if (!keyFile)
  15929. return -9213;
  15930. *eccPrivKeySz = (word32)fread(eccPrivKey, 1, *eccPrivKeySz, keyFile);
  15931. fclose(keyFile);
  15932. #endif /* USE_CERT_BUFFERS_256 */
  15933. #endif /* HAVE_ECC */
  15934. #ifdef NO_RSA
  15935. (void)rsaCert;
  15936. (void)rsaCertSz;
  15937. (void)rsaPrivKey;
  15938. (void)rsaPrivKeySz;
  15939. #endif
  15940. #ifndef HAVE_ECC
  15941. (void)eccCert;
  15942. (void)eccCertSz;
  15943. (void)eccPrivKey;
  15944. (void)eccPrivKeySz;
  15945. #endif
  15946. #ifndef NO_FILESYSTEM
  15947. (void)certFile;
  15948. (void)keyFile;
  15949. #endif
  15950. return 0;
  15951. }
  15952. typedef struct {
  15953. const byte* content;
  15954. word32 contentSz;
  15955. int contentOID;
  15956. int encryptOID;
  15957. int keyWrapOID;
  15958. int keyAgreeOID;
  15959. byte* cert;
  15960. size_t certSz;
  15961. byte* privateKey;
  15962. word32 privateKeySz;
  15963. byte* optionalUkm;
  15964. word32 optionalUkmSz;
  15965. const char* outFileName;
  15966. } pkcs7EnvelopedVector;
  15967. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  15968. byte* rsaPrivKey, word32 rsaPrivKeySz,
  15969. byte* eccCert, word32 eccCertSz,
  15970. byte* eccPrivKey, word32 eccPrivKeySz)
  15971. {
  15972. int ret, testSz, i;
  15973. int envelopedSz, decodedSz;
  15974. byte enveloped[2048];
  15975. byte decoded[2048];
  15976. PKCS7* pkcs7;
  15977. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  15978. FILE* pkcs7File;
  15979. #endif
  15980. const byte data[] = { /* Hello World */
  15981. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  15982. 0x72,0x6c,0x64
  15983. };
  15984. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  15985. defined(WOLFSSL_SHA512)
  15986. byte optionalUkm[] = {
  15987. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  15988. };
  15989. #endif /* NO_AES */
  15990. const pkcs7EnvelopedVector testVectors[] =
  15991. {
  15992. /* key transport key encryption technique */
  15993. #ifndef NO_RSA
  15994. #ifndef NO_DES3
  15995. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  15996. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataDES3.der"},
  15997. #endif
  15998. #ifndef NO_AES
  15999. #ifdef WOLFSSL_AES_128
  16000. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  16001. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataAES128CBC.der"},
  16002. #endif
  16003. #ifdef WOLFSSL_AES_192
  16004. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  16005. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataAES192CBC.der"},
  16006. #endif
  16007. #ifdef WOLFSSL_AES_256
  16008. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  16009. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataAES256CBC.der"},
  16010. #endif
  16011. #endif /* NO_AES */
  16012. #endif
  16013. /* key agreement key encryption technique*/
  16014. #ifdef HAVE_ECC
  16015. #ifndef NO_AES
  16016. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  16017. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  16018. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  16019. eccPrivKeySz, NULL, 0,
  16020. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  16021. #endif
  16022. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  16023. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  16024. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  16025. eccPrivKeySz, NULL, 0,
  16026. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  16027. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  16028. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  16029. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  16030. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  16031. eccPrivKeySz, NULL, 0,
  16032. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  16033. /* with optional user keying material (ukm) */
  16034. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  16035. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  16036. eccPrivKeySz, optionalUkm, sizeof(optionalUkm),
  16037. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  16038. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  16039. #endif /* NO_AES */
  16040. #endif
  16041. };
  16042. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  16043. for (i = 0; i < testSz; i++) {
  16044. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  16045. #ifdef WOLFSSL_ASYNC_CRYPT
  16046. INVALID_DEVID /* async PKCS7 is not supported */
  16047. #else
  16048. devId
  16049. #endif
  16050. );
  16051. if (pkcs7 == NULL)
  16052. return -9214;
  16053. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  16054. (word32)testVectors[i].certSz);
  16055. if (ret != 0) {
  16056. wc_PKCS7_Free(pkcs7);
  16057. return -9215;
  16058. }
  16059. pkcs7->content = (byte*)testVectors[i].content;
  16060. pkcs7->contentSz = testVectors[i].contentSz;
  16061. pkcs7->contentOID = testVectors[i].contentOID;
  16062. pkcs7->encryptOID = testVectors[i].encryptOID;
  16063. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  16064. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  16065. pkcs7->privateKey = testVectors[i].privateKey;
  16066. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  16067. pkcs7->ukm = testVectors[i].optionalUkm;
  16068. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  16069. /* encode envelopedData */
  16070. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  16071. sizeof(enveloped));
  16072. if (envelopedSz <= 0) {
  16073. printf("DEBUG: i = %d, envelopedSz = %d\n", i, envelopedSz);
  16074. wc_PKCS7_Free(pkcs7);
  16075. return -9216;
  16076. }
  16077. /* decode envelopedData */
  16078. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  16079. decoded, sizeof(decoded));
  16080. if (decodedSz <= 0) {
  16081. wc_PKCS7_Free(pkcs7);
  16082. return -9217;
  16083. }
  16084. /* test decode result */
  16085. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  16086. wc_PKCS7_Free(pkcs7);
  16087. return -9218;
  16088. }
  16089. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  16090. /* output pkcs7 envelopedData for external testing */
  16091. pkcs7File = fopen(testVectors[i].outFileName, "wb");
  16092. if (!pkcs7File) {
  16093. wc_PKCS7_Free(pkcs7);
  16094. return -9219;
  16095. }
  16096. ret = (int)fwrite(enveloped, 1, envelopedSz, pkcs7File);
  16097. fclose(pkcs7File);
  16098. if (ret != envelopedSz) {
  16099. wc_PKCS7_Free(pkcs7);
  16100. return -9220;
  16101. }
  16102. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  16103. wc_PKCS7_Free(pkcs7);
  16104. pkcs7 = NULL;
  16105. }
  16106. #if !defined(HAVE_ECC) || defined(NO_AES)
  16107. (void)eccCert;
  16108. (void)eccCertSz;
  16109. (void)eccPrivKey;
  16110. (void)eccPrivKeySz;
  16111. #endif
  16112. #ifdef NO_RSA
  16113. (void)rsaCert;
  16114. (void)rsaCertSz;
  16115. (void)rsaPrivKey;
  16116. (void)rsaPrivKeySz;
  16117. #endif
  16118. return 0;
  16119. }
  16120. int pkcs7enveloped_test(void)
  16121. {
  16122. int ret = 0;
  16123. byte* rsaCert = NULL;
  16124. byte* rsaPrivKey = NULL;
  16125. word32 rsaCertSz = 0;
  16126. word32 rsaPrivKeySz = 0;
  16127. byte* eccCert = NULL;
  16128. byte* eccPrivKey = NULL;
  16129. word32 eccCertSz = 0;
  16130. word32 eccPrivKeySz = 0;
  16131. #ifndef NO_RSA
  16132. /* read client RSA cert and key in DER format */
  16133. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16134. if (rsaCert == NULL)
  16135. return -9300;
  16136. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16137. if (rsaPrivKey == NULL) {
  16138. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16139. return -9301;
  16140. }
  16141. rsaCertSz = FOURK_BUF;
  16142. rsaPrivKeySz = FOURK_BUF;
  16143. #endif /* NO_RSA */
  16144. #ifdef HAVE_ECC
  16145. /* read client ECC cert and key in DER format */
  16146. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16147. if (eccCert == NULL) {
  16148. #ifndef NO_RSA
  16149. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16150. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16151. #endif
  16152. return -9302;
  16153. }
  16154. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16155. if (eccPrivKey == NULL) {
  16156. #ifndef NO_RSA
  16157. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16158. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16159. #endif
  16160. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16161. return -9303;
  16162. }
  16163. eccCertSz = FOURK_BUF;
  16164. eccPrivKeySz = FOURK_BUF;
  16165. #endif /* HAVE_ECC */
  16166. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  16167. &rsaPrivKeySz, eccCert, &eccCertSz,
  16168. eccPrivKey, &eccPrivKeySz);
  16169. if (ret < 0) {
  16170. #ifndef NO_RSA
  16171. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16172. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16173. #endif
  16174. #ifdef HAVE_ECC
  16175. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16176. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16177. #endif
  16178. return ret;
  16179. }
  16180. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  16181. rsaPrivKey, (word32)rsaPrivKeySz,
  16182. eccCert, (word32)eccCertSz,
  16183. eccPrivKey, (word32)eccPrivKeySz);
  16184. #ifndef NO_RSA
  16185. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16186. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16187. #endif
  16188. #ifdef HAVE_ECC
  16189. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16190. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16191. #endif
  16192. return ret;
  16193. }
  16194. #ifndef NO_PKCS7_ENCRYPTED_DATA
  16195. typedef struct {
  16196. const byte* content;
  16197. word32 contentSz;
  16198. int contentOID;
  16199. int encryptOID;
  16200. byte* encryptionKey;
  16201. word32 encryptionKeySz;
  16202. PKCS7Attrib* attribs;
  16203. word32 attribsSz;
  16204. const char* outFileName;
  16205. } pkcs7EncryptedVector;
  16206. int pkcs7encrypted_test(void)
  16207. {
  16208. int ret = 0;
  16209. int i, testSz;
  16210. int encryptedSz, decodedSz, attribIdx;
  16211. PKCS7* pkcs7;
  16212. byte encrypted[2048];
  16213. byte decoded[2048];
  16214. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  16215. FILE* pkcs7File;
  16216. #endif
  16217. PKCS7Attrib* expectedAttrib;
  16218. PKCS7DecodedAttrib* decodedAttrib;
  16219. const byte data[] = { /* Hello World */
  16220. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  16221. 0x72,0x6c,0x64
  16222. };
  16223. #ifndef NO_DES3
  16224. byte desKey[] = {
  16225. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  16226. };
  16227. byte des3Key[] = {
  16228. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  16229. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  16230. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  16231. };
  16232. #endif
  16233. #ifndef NO_AES
  16234. #ifdef WOLFSSL_AES_128
  16235. byte aes128Key[] = {
  16236. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  16237. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  16238. };
  16239. #endif
  16240. #ifdef WOLFSSL_AES_192
  16241. byte aes192Key[] = {
  16242. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  16243. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  16244. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  16245. };
  16246. #endif
  16247. #ifdef WOLFSSL_AES_256
  16248. byte aes256Key[] = {
  16249. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  16250. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  16251. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  16252. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  16253. };
  16254. #endif
  16255. #ifdef WOLFSSL_AES_256
  16256. /* Attribute example from RFC 4134, Section 7.2
  16257. * OID = 1.2.5555
  16258. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  16259. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  16260. static byte genAttr[] = { 0x04, 47,
  16261. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  16262. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  16263. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  16264. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  16265. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  16266. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  16267. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  16268. static byte genAttr2[] = { 0x04, 47,
  16269. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  16270. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  16271. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  16272. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  16273. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  16274. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  16275. PKCS7Attrib attribs[] =
  16276. {
  16277. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  16278. };
  16279. PKCS7Attrib multiAttribs[] =
  16280. {
  16281. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  16282. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  16283. };
  16284. #endif
  16285. #endif /* NO_AES */
  16286. const pkcs7EncryptedVector testVectors[] =
  16287. {
  16288. #ifndef NO_DES3
  16289. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  16290. NULL, 0, "pkcs7encryptedDataDES3.der"},
  16291. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  16292. NULL, 0, "pkcs7encryptedDataDES.der"},
  16293. #endif /* NO_DES3 */
  16294. #ifndef NO_AES
  16295. #ifdef WOLFSSL_AES_128
  16296. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  16297. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  16298. #endif
  16299. #ifdef WOLFSSL_AES_192
  16300. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  16301. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  16302. #endif
  16303. #ifdef WOLFSSL_AES_256
  16304. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  16305. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  16306. /* test with optional unprotected attributes */
  16307. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  16308. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16309. "pkcs7encryptedDataAES256CBC_attribs.der"},
  16310. /* test with multiple optional unprotected attributes */
  16311. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  16312. sizeof(aes256Key), multiAttribs,
  16313. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  16314. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  16315. #endif
  16316. #endif /* NO_AES */
  16317. };
  16318. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  16319. for (i = 0; i < testSz; i++) {
  16320. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  16321. if (pkcs7 == NULL)
  16322. return -9400;
  16323. pkcs7->content = (byte*)testVectors[i].content;
  16324. pkcs7->contentSz = testVectors[i].contentSz;
  16325. pkcs7->contentOID = testVectors[i].contentOID;
  16326. pkcs7->encryptOID = testVectors[i].encryptOID;
  16327. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  16328. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  16329. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  16330. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  16331. /* encode encryptedData */
  16332. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  16333. sizeof(encrypted));
  16334. if (encryptedSz <= 0) {
  16335. wc_PKCS7_Free(pkcs7);
  16336. return -9401;
  16337. }
  16338. /* decode encryptedData */
  16339. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  16340. decoded, sizeof(decoded));
  16341. if (decodedSz <= 0){
  16342. wc_PKCS7_Free(pkcs7);
  16343. return -9402;
  16344. }
  16345. /* test decode result */
  16346. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  16347. wc_PKCS7_Free(pkcs7);
  16348. return -9403;
  16349. }
  16350. /* verify decoded unprotected attributes */
  16351. if (pkcs7->decodedAttrib != NULL) {
  16352. decodedAttrib = pkcs7->decodedAttrib;
  16353. attribIdx = 1;
  16354. while (decodedAttrib != NULL) {
  16355. /* expected attribute, stored list is reversed */
  16356. expectedAttrib = &(pkcs7->unprotectedAttribs
  16357. [pkcs7->unprotectedAttribsSz - attribIdx]);
  16358. /* verify oid */
  16359. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  16360. decodedAttrib->oidSz) != 0) {
  16361. wc_PKCS7_Free(pkcs7);
  16362. return -9404;
  16363. }
  16364. /* verify value */
  16365. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  16366. decodedAttrib->valueSz) != 0) {
  16367. wc_PKCS7_Free(pkcs7);
  16368. return -9405;
  16369. }
  16370. decodedAttrib = decodedAttrib->next;
  16371. attribIdx++;
  16372. }
  16373. }
  16374. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  16375. /* output pkcs7 envelopedData for external testing */
  16376. pkcs7File = fopen(testVectors[i].outFileName, "wb");
  16377. if (!pkcs7File) {
  16378. wc_PKCS7_Free(pkcs7);
  16379. return -9406;
  16380. }
  16381. ret = (int)fwrite(encrypted, encryptedSz, 1, pkcs7File);
  16382. fclose(pkcs7File);
  16383. if (ret > 0)
  16384. ret = 0;
  16385. #endif
  16386. wc_PKCS7_Free(pkcs7);
  16387. }
  16388. return ret;
  16389. }
  16390. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  16391. typedef struct {
  16392. const byte* content;
  16393. word32 contentSz;
  16394. int hashOID;
  16395. int encryptOID;
  16396. byte* privateKey;
  16397. word32 privateKeySz;
  16398. byte* cert;
  16399. size_t certSz;
  16400. PKCS7Attrib* signedAttribs;
  16401. word32 signedAttribsSz;
  16402. const char* outFileName;
  16403. } pkcs7SignedVector;
  16404. static int pkcs7signed_run_vectors(byte* rsaCert, word32 rsaCertSz,
  16405. byte* rsaPrivKey, word32 rsaPrivKeySz,
  16406. byte* eccCert, word32 eccCertSz,
  16407. byte* eccPrivKey, word32 eccPrivKeySz)
  16408. {
  16409. int ret, testSz, i;
  16410. int encodedSz;
  16411. byte* out;
  16412. word32 outSz;
  16413. WC_RNG rng;
  16414. PKCS7* pkcs7;
  16415. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  16416. FILE* file;
  16417. #endif
  16418. const byte data[] = { /* Hello World */
  16419. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  16420. 0x72,0x6c,0x64
  16421. };
  16422. static byte transIdOid[] =
  16423. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  16424. 0x09, 0x07 };
  16425. static byte messageTypeOid[] =
  16426. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  16427. 0x09, 0x02 };
  16428. static byte senderNonceOid[] =
  16429. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  16430. 0x09, 0x05 };
  16431. #ifndef NO_SHA
  16432. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  16433. #else
  16434. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  16435. #endif
  16436. static byte messageType[] = { 0x13, 2, '1', '9' };
  16437. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  16438. PKCS7Attrib attribs[] =
  16439. {
  16440. { transIdOid, sizeof(transIdOid), transId,
  16441. sizeof(transId) - 1 }, /* take off the null */
  16442. { messageTypeOid, sizeof(messageTypeOid), messageType,
  16443. sizeof(messageType) },
  16444. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  16445. sizeof(senderNonce) }
  16446. };
  16447. const pkcs7SignedVector testVectors[] =
  16448. {
  16449. #ifndef NO_RSA
  16450. #ifndef NO_SHA
  16451. /* RSA with SHA */
  16452. {data, (word32)sizeof(data), SHAh, RSAk, rsaPrivKey, rsaPrivKeySz,
  16453. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16454. "pkcs7signedData_RSA_SHA.der"},
  16455. /* RSA with SHA, no signed attributes */
  16456. {data, (word32)sizeof(data), SHAh, RSAk, rsaPrivKey, rsaPrivKeySz,
  16457. rsaCert, rsaCertSz, NULL, 0,
  16458. "pkcs7signedData_RSA_SHA_noattr.der"},
  16459. #endif
  16460. #ifdef WOLFSSL_SHA224
  16461. /* RSA with SHA224 */
  16462. {data, (word32)sizeof(data), SHA224h, RSAk, rsaPrivKey, rsaPrivKeySz,
  16463. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16464. "pkcs7signedData_RSA_SHA224.der"},
  16465. #endif
  16466. #ifndef NO_SHA256
  16467. /* RSA with SHA256 */
  16468. {data, (word32)sizeof(data), SHA256h, RSAk, rsaPrivKey, rsaPrivKeySz,
  16469. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16470. "pkcs7signedData_RSA_SHA256.der"},
  16471. #endif
  16472. #if defined(WOLFSSL_SHA384)
  16473. /* RSA with SHA384 */
  16474. {data, (word32)sizeof(data), SHA384h, RSAk, rsaPrivKey, rsaPrivKeySz,
  16475. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16476. "pkcs7signedData_RSA_SHA384.der"},
  16477. #endif
  16478. #if defined(WOLFSSL_SHA512)
  16479. /* RSA with SHA512 */
  16480. {data, (word32)sizeof(data), SHA512h, RSAk, rsaPrivKey, rsaPrivKeySz,
  16481. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16482. "pkcs7signedData_RSA_SHA512.der"},
  16483. #endif
  16484. #endif /* NO_RSA */
  16485. #ifdef HAVE_ECC
  16486. #ifndef NO_SHA
  16487. /* ECDSA with SHA */
  16488. {data, (word32)sizeof(data), SHAh, ECDSAk, eccPrivKey, eccPrivKeySz,
  16489. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16490. "pkcs7signedData_ECDSA_SHA.der"},
  16491. /* ECDSA with SHA, no signed attributes */
  16492. {data, (word32)sizeof(data), SHAh, ECDSAk, eccPrivKey, eccPrivKeySz,
  16493. eccCert, eccCertSz, NULL, 0,
  16494. "pkcs7signedData_ECDSA_SHA_noattr.der"},
  16495. #endif
  16496. #ifdef WOLFSSL_SHA224
  16497. /* ECDSA with SHA224 */
  16498. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccPrivKey, eccPrivKeySz,
  16499. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16500. "pkcs7signedData_ECDSA_SHA224.der"},
  16501. #endif
  16502. #ifndef NO_SHA256
  16503. /* ECDSA with SHA256 */
  16504. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccPrivKey, eccPrivKeySz,
  16505. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16506. "pkcs7signedData_ECDSA_SHA256.der"},
  16507. #endif
  16508. #ifdef WOLFSSL_SHA384
  16509. /* ECDSA with SHA384 */
  16510. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccPrivKey, eccPrivKeySz,
  16511. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16512. "pkcs7signedData_ECDSA_SHA384.der"},
  16513. #endif
  16514. #ifdef WOLFSSL_SHA512
  16515. /* ECDSA with SHA512 */
  16516. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccPrivKey, eccPrivKeySz,
  16517. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  16518. "pkcs7signedData_ECDSA_SHA512.der"},
  16519. #endif
  16520. #endif /* HAVE_ECC */
  16521. };
  16522. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  16523. outSz = FOURK_BUF;
  16524. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16525. if (out == NULL)
  16526. return -9407;
  16527. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  16528. if (ret < 0) {
  16529. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16530. return -9408;
  16531. }
  16532. #ifndef HAVE_FIPS
  16533. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  16534. #else
  16535. ret = wc_InitRng(&rng);
  16536. #endif
  16537. if (ret != 0) {
  16538. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16539. return -9409;
  16540. }
  16541. for (i = 0; i < testSz; i++) {
  16542. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  16543. if (pkcs7 == NULL)
  16544. return -9410;
  16545. pkcs7->heap = HEAP_HINT;
  16546. pkcs7->devId = INVALID_DEVID;
  16547. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  16548. (word32)testVectors[i].certSz);
  16549. if (ret != 0) {
  16550. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16551. wc_PKCS7_Free(pkcs7);
  16552. return -9410;
  16553. }
  16554. pkcs7->rng = &rng;
  16555. pkcs7->content = (byte*)testVectors[i].content;
  16556. pkcs7->contentSz = testVectors[i].contentSz;
  16557. pkcs7->hashOID = testVectors[i].hashOID;
  16558. pkcs7->encryptOID = testVectors[i].encryptOID;
  16559. pkcs7->privateKey = testVectors[i].privateKey;
  16560. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  16561. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  16562. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  16563. /* generate senderNonce */
  16564. {
  16565. senderNonce[0] = 0x04;
  16566. senderNonce[1] = PKCS7_NONCE_SZ;
  16567. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  16568. if (ret != 0) {
  16569. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16570. wc_PKCS7_Free(pkcs7);
  16571. return -9411;
  16572. }
  16573. }
  16574. /* generate transactionID (used with SCEP) */
  16575. {
  16576. #ifndef NO_SHA
  16577. wc_Sha sha;
  16578. byte digest[WC_SHA_DIGEST_SIZE];
  16579. #else
  16580. wc_Sha256 sha;
  16581. byte digest[WC_SHA256_DIGEST_SIZE];
  16582. #endif
  16583. int j,k;
  16584. transId[0] = 0x13;
  16585. transId[1] = sizeof(digest) * 2;
  16586. #ifndef NO_SHA
  16587. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  16588. if (ret != 0) {
  16589. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16590. wc_PKCS7_Free(pkcs7);
  16591. return -9412;
  16592. }
  16593. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  16594. wc_ShaFinal(&sha, digest);
  16595. wc_ShaFree(&sha);
  16596. #else
  16597. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  16598. if (ret != 0) {
  16599. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16600. wc_PKCS7_Free(pkcs7);
  16601. return -9413;
  16602. }
  16603. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  16604. wc_Sha256Final(&sha, digest);
  16605. wc_Sha256Free(&sha);
  16606. #endif
  16607. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  16608. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  16609. }
  16610. }
  16611. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  16612. if (encodedSz < 0) {
  16613. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16614. wc_PKCS7_Free(pkcs7);
  16615. return -9414;
  16616. }
  16617. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  16618. /* write PKCS#7 to output file for more testing */
  16619. file = fopen(testVectors[i].outFileName, "wb");
  16620. if (!file) {
  16621. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16622. wc_PKCS7_Free(pkcs7);
  16623. return -9415;
  16624. }
  16625. ret = (int)fwrite(out, 1, encodedSz, file);
  16626. fclose(file);
  16627. if (ret != (int)encodedSz) {
  16628. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16629. wc_PKCS7_Free(pkcs7);
  16630. return -9416;
  16631. }
  16632. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  16633. wc_PKCS7_Free(pkcs7);
  16634. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  16635. if (pkcs7 == NULL)
  16636. return -9410;
  16637. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  16638. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  16639. if (ret < 0) {
  16640. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16641. wc_PKCS7_Free(pkcs7);
  16642. return -9417;
  16643. }
  16644. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0) {
  16645. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16646. wc_PKCS7_Free(pkcs7);
  16647. return -9418;
  16648. }
  16649. {
  16650. /* check getting signed attributes */
  16651. #ifndef NO_SHA
  16652. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  16653. #else
  16654. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  16655. #endif
  16656. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  16657. int oidSz = (int)sizeof(transIdOid) - 2;
  16658. int bufSz = 0;
  16659. if (testVectors[i].signedAttribs != NULL &&
  16660. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  16661. NULL, (word32*)&bufSz) != LENGTH_ONLY_E) {
  16662. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16663. wc_PKCS7_Free(pkcs7);
  16664. return -9419;
  16665. }
  16666. if (bufSz > (int)sizeof(buf)) {
  16667. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16668. wc_PKCS7_Free(pkcs7);
  16669. return -9420;
  16670. }
  16671. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  16672. buf, (word32*)&bufSz);
  16673. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  16674. (testVectors[i].signedAttribs == NULL && bufSz > 0)) {
  16675. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16676. wc_PKCS7_Free(pkcs7);
  16677. return -9421;
  16678. }
  16679. }
  16680. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  16681. file = fopen("./pkcs7cert.der", "wb");
  16682. if (!file) {
  16683. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16684. wc_PKCS7_Free(pkcs7);
  16685. return -9422;
  16686. }
  16687. ret = (int)fwrite(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  16688. fclose(file);
  16689. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  16690. wc_PKCS7_Free(pkcs7);
  16691. }
  16692. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16693. wc_FreeRng(&rng);
  16694. if (ret > 0)
  16695. return 0;
  16696. #ifndef HAVE_ECC
  16697. (void)eccCert;
  16698. (void)eccCertSz;
  16699. (void)eccPrivKey;
  16700. (void)eccPrivKeySz;
  16701. #endif
  16702. #ifdef NO_RSA
  16703. (void)rsaCert;
  16704. (void)rsaCertSz;
  16705. (void)rsaPrivKey;
  16706. (void)rsaPrivKeySz;
  16707. #endif
  16708. return ret;
  16709. }
  16710. int pkcs7signed_test(void)
  16711. {
  16712. int ret = 0;
  16713. byte* rsaCert = NULL;
  16714. byte* eccCert = NULL;
  16715. byte* rsaPrivKey = NULL;
  16716. byte* eccPrivKey = NULL;
  16717. word32 rsaCertSz = 0;
  16718. word32 eccCertSz = 0;
  16719. word32 rsaPrivKeySz = 0;
  16720. word32 eccPrivKeySz = 0;
  16721. #ifndef NO_RSA
  16722. /* read client RSA cert and key in DER format */
  16723. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16724. if (rsaCert == NULL)
  16725. return -9500;
  16726. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16727. if (rsaPrivKey == NULL) {
  16728. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16729. return -9501;
  16730. }
  16731. rsaCertSz = FOURK_BUF;
  16732. rsaPrivKeySz = FOURK_BUF;
  16733. #endif /* NO_RSA */
  16734. #ifdef HAVE_ECC
  16735. /* read client ECC cert and key in DER format */
  16736. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16737. if (eccCert == NULL) {
  16738. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16739. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16740. return -9502;
  16741. }
  16742. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16743. if (eccPrivKey == NULL) {
  16744. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16745. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16746. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16747. return -9503;
  16748. }
  16749. eccCertSz = FOURK_BUF;
  16750. eccPrivKeySz = FOURK_BUF;
  16751. #endif /* HAVE_ECC */
  16752. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  16753. &rsaPrivKeySz, eccCert, &eccCertSz,
  16754. eccPrivKey, &eccPrivKeySz);
  16755. if (ret < 0) {
  16756. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16757. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16758. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16759. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16760. return ret;
  16761. }
  16762. ret = pkcs7signed_run_vectors(rsaCert, (word32)rsaCertSz,
  16763. rsaPrivKey, (word32)rsaPrivKeySz,
  16764. eccCert, (word32)eccCertSz,
  16765. eccPrivKey, (word32)eccPrivKeySz);
  16766. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16767. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16768. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16769. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16770. return ret;
  16771. }
  16772. #endif /* HAVE_PKCS7 */
  16773. #ifdef HAVE_VALGRIND
  16774. /* Need a static build to have access to symbols. */
  16775. /* Maximum number of bytes in a number to test. */
  16776. #define MP_MAX_TEST_BYTE_LEN 16
  16777. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  16778. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  16779. {
  16780. byte d[MP_MAX_TEST_BYTE_LEN];
  16781. int ret;
  16782. (void)heap;
  16783. do {
  16784. ret = wc_RNG_GenerateBlock(rng, d, len);
  16785. if (ret != 0)
  16786. return ret;
  16787. ret = mp_read_unsigned_bin(n, d, len);
  16788. if (ret != 0)
  16789. return ret;
  16790. } while (mp_iszero(n));
  16791. return 0;
  16792. }
  16793. #endif
  16794. int mp_test(void)
  16795. {
  16796. WC_RNG rng;
  16797. int ret;
  16798. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  16799. int i, j, k;
  16800. mp_digit d;
  16801. #endif
  16802. mp_int a, b, r1, r2, p;
  16803. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  16804. if (ret != 0)
  16805. return -9600;
  16806. mp_init_copy(&p, &a);
  16807. #ifndef HAVE_FIPS
  16808. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  16809. #else
  16810. ret = wc_InitRng(&rng);
  16811. #endif
  16812. if (ret != 0)
  16813. goto done;
  16814. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  16815. mp_set_int(&a, 0);
  16816. if (a.used != 0 || a.dp[0] != 0)
  16817. return -9601;
  16818. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  16819. for (i = 0; i < 4 * j; i++) {
  16820. /* New values to use. */
  16821. ret = randNum(&p, j, &rng, NULL);
  16822. if (ret != 0)
  16823. return -9602;
  16824. ret = randNum(&a, j, &rng, NULL);
  16825. if (ret != 0)
  16826. return -9603;
  16827. ret = randNum(&b, j, &rng, NULL);
  16828. if (ret != 0)
  16829. return -9604;
  16830. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  16831. if (ret != 0)
  16832. return -9605;
  16833. d &= MP_MASK;
  16834. /* Ensure sqrmod produce same result as mulmod. */
  16835. ret = mp_sqrmod(&a, &p, &r1);
  16836. if (ret != 0)
  16837. return -9606;
  16838. ret = mp_mulmod(&a, &a, &p, &r2);
  16839. if (ret != 0)
  16840. return -9607;
  16841. if (mp_cmp(&r1, &r2) != 0)
  16842. return -9608;
  16843. /* Ensure add with mod produce same result as sub with mod. */
  16844. ret = mp_addmod(&a, &b, &p, &r1);
  16845. if (ret != 0)
  16846. return -9609;
  16847. b.sign ^= 1;
  16848. ret = mp_submod(&a, &b, &p, &r2);
  16849. if (ret != 0)
  16850. return -9610;
  16851. if (mp_cmp(&r1, &r2) != 0)
  16852. return -9611;
  16853. /* Ensure add digit produce same result as sub digit. */
  16854. ret = mp_add_d(&a, d, &r1);
  16855. if (ret != 0)
  16856. return -9612;
  16857. ret = mp_sub_d(&r1, d, &r2);
  16858. if (ret != 0)
  16859. return -9613;
  16860. if (mp_cmp(&a, &r2) != 0)
  16861. return -9614;
  16862. /* Invert - if p is even it will use the slow impl.
  16863. * - if p and a are even it will fail.
  16864. */
  16865. ret = mp_invmod(&a, &p, &r1);
  16866. if (ret != 0 && ret != MP_VAL)
  16867. return -9615;
  16868. ret = 0;
  16869. /* Shift up and down number all bits in a digit. */
  16870. for (k = 0; k < DIGIT_BIT; k++) {
  16871. mp_mul_2d(&a, k, &r1);
  16872. mp_div_2d(&r1, k, &r2, &p);
  16873. if (mp_cmp(&a, &r2) != 0)
  16874. return -9616;
  16875. if (!mp_iszero(&p))
  16876. return -9617;
  16877. mp_rshb(&r1, k);
  16878. if (mp_cmp(&a, &r1) != 0)
  16879. return -9618;
  16880. }
  16881. }
  16882. }
  16883. /* Check that setting a 32-bit digit works. */
  16884. d &= 0xffffffff;
  16885. mp_set_int(&a, d);
  16886. if (a.used != 1 || a.dp[0] != d)
  16887. return -9619;
  16888. /* Check setting a bit and testing a bit works. */
  16889. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  16890. mp_zero(&a);
  16891. mp_set_bit(&a, i);
  16892. if (!mp_is_bit_set(&a, i))
  16893. return -9620;
  16894. }
  16895. #endif
  16896. done:
  16897. mp_clear(&p);
  16898. mp_clear(&r2);
  16899. mp_clear(&r1);
  16900. mp_clear(&b);
  16901. mp_clear(&a);
  16902. wc_FreeRng(&rng);
  16903. return ret;
  16904. }
  16905. #endif
  16906. #ifdef WOLFSSL_PUBLIC_MP
  16907. typedef struct pairs_t {
  16908. const unsigned char* coeff;
  16909. int coeffSz;
  16910. int exp;
  16911. } pairs_t;
  16912. /*
  16913. n =p1p2p3, where pi = ki(p1−1)+1 with (k2,k3) = (173,293)
  16914. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  16915. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  16916. */
  16917. static const unsigned char c192a[] =
  16918. {
  16919. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  16920. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  16921. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  16922. };
  16923. static const unsigned char c0a[] =
  16924. {
  16925. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  16926. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  16927. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  16928. };
  16929. static const pairs_t ecPairsA[] =
  16930. {
  16931. {c192a, sizeof(c192a), 192},
  16932. {c0a, sizeof(c0a), 0}
  16933. };
  16934. static const int kA[] = {173, 293};
  16935. static const unsigned char controlPrime[] = {
  16936. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  16937. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  16938. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  16939. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  16940. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  16941. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  16942. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  16943. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  16944. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  16945. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  16946. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  16947. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  16948. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  16949. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  16950. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  16951. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  16952. };
  16953. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  16954. {
  16955. int ret;
  16956. ret = mp_sub_d(p1, 1, p2);
  16957. if (ret == 0)
  16958. ret = mp_mul_d(p2, k, p2);
  16959. if (ret == 0)
  16960. ret = mp_add_d(p2, 1, p2);
  16961. return ret;
  16962. }
  16963. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  16964. const pairs_t* ecPairs, int ecPairsSz,
  16965. const int* k)
  16966. {
  16967. mp_int x,y;
  16968. int ret, i;
  16969. ret = mp_init(&x);
  16970. if (ret == 0) {
  16971. ret = mp_init(&y);
  16972. if (ret != 0) {
  16973. mp_clear(&x);
  16974. return MP_MEM;
  16975. }
  16976. }
  16977. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  16978. ret = mp_read_unsigned_bin(&x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  16979. /* p1 = 2^exp */
  16980. if (ret == 0)
  16981. ret = mp_2expt(&y, ecPairs[i].exp);
  16982. /* p1 = p1 * m */
  16983. if (ret == 0)
  16984. ret = mp_mul(&x, &y, &x);
  16985. /* p1 += */
  16986. if (ret == 0)
  16987. ret = mp_add(p1, &x, p1);
  16988. mp_zero(&x);
  16989. mp_zero(&y);
  16990. }
  16991. mp_clear(&x);
  16992. mp_clear(&y);
  16993. if (ret == 0)
  16994. ret = GenerateNextP(p1, p2, k[0]);
  16995. if (ret == 0)
  16996. ret = GenerateNextP(p1, p3, k[1]);
  16997. return ret;
  16998. }
  16999. int prime_test(void)
  17000. {
  17001. mp_int n, p1, p2, p3;
  17002. int ret, isPrime = 0;
  17003. WC_RNG rng;
  17004. ret = wc_InitRng(&rng);
  17005. if (ret == 0)
  17006. ret = mp_init_multi(&n, &p1, &p2, &p3, NULL, NULL);
  17007. if (ret == 0)
  17008. ret = GenerateP(&p1, &p2, &p3,
  17009. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  17010. if (ret == 0)
  17011. ret = mp_mul(&p1, &p2, &n);
  17012. if (ret == 0)
  17013. ret = mp_mul(&n, &p3, &n);
  17014. if (ret != 0)
  17015. return -9650;
  17016. /* Check the old prime test using the number that false positives.
  17017. * This test result should indicate as not prime. */
  17018. ret = mp_prime_is_prime(&n, 40, &isPrime);
  17019. if (ret != 0)
  17020. return -9651;
  17021. if (isPrime)
  17022. return -9652;
  17023. /* This test result should fail. It should indicate the value as prime. */
  17024. ret = mp_prime_is_prime(&n, 8, &isPrime);
  17025. if (ret != 0)
  17026. return -9653;
  17027. if (!isPrime)
  17028. return -9654;
  17029. /* This test result should indicate the value as not prime. */
  17030. ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng);
  17031. if (ret != 0)
  17032. return -9655;
  17033. if (isPrime)
  17034. return -9656;
  17035. ret = mp_read_unsigned_bin(&n, controlPrime, sizeof(controlPrime));
  17036. if (ret != 0)
  17037. return -9657;
  17038. /* This test result should indicate the value as prime. */
  17039. ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng);
  17040. if (ret != 0)
  17041. return -9658;
  17042. if (!isPrime)
  17043. return -9659;
  17044. /* This test result should indicate the value as prime. */
  17045. isPrime = -1;
  17046. ret = mp_prime_is_prime(&n, 8, &isPrime);
  17047. if (ret != 0)
  17048. return -9660;
  17049. if (!isPrime)
  17050. return -9661;
  17051. mp_clear(&p3);
  17052. mp_clear(&p2);
  17053. mp_clear(&p1);
  17054. mp_clear(&n);
  17055. wc_FreeRng(&rng);
  17056. return 0;
  17057. }
  17058. #endif /* WOLFSSL_PUBLIC_MP */
  17059. #if defined(ASN_BER_TO_DER) && \
  17060. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  17061. defined(OPENSSL_EXTRA_X509_SMALL))
  17062. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  17063. typedef struct berDerTestData {
  17064. const byte *in;
  17065. word32 inSz;
  17066. const byte *out;
  17067. word32 outSz;
  17068. } berDerTestData;
  17069. int berder_test(void)
  17070. {
  17071. int ret;
  17072. int i;
  17073. word32 len = 0, l;
  17074. byte out[32];
  17075. static const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  17076. static const byte good1_out[] = { 0x30, 0x00 };
  17077. static const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  17078. static const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  17079. static const byte good3_in[] = {
  17080. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  17081. };
  17082. static const byte good3_out[] = { 0x04, 0x1, 0x01 };
  17083. static const byte good4_in[] = {
  17084. 0x30, 0x80,
  17085. 0x02, 0x01, 0x01,
  17086. 0x30, 0x80,
  17087. 0x24, 0x80,
  17088. 0x04, 0x01, 0x01,
  17089. 0x04, 0x02, 0x02, 0x03,
  17090. 0x00, 0x00,
  17091. 0x06, 0x01, 0x01,
  17092. 0x00, 0x00,
  17093. 0x31, 0x80,
  17094. 0x06, 0x01, 0x01,
  17095. 0x00, 0x00,
  17096. 0x00, 0x00,
  17097. };
  17098. static const byte good4_out[] = {
  17099. 0x30, 0x0d,
  17100. 0x02, 0x01, 0x01,
  17101. 0x30, 0x08,
  17102. 0x04, 0x03, 0x01, 0x02, 0x03,
  17103. 0x06, 0x01, 0x01,
  17104. 0x31, 0x03,
  17105. 0x06, 0x01, 0x01
  17106. };
  17107. berDerTestData testData[] = {
  17108. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  17109. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  17110. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  17111. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  17112. };
  17113. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  17114. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  17115. if (ret != LENGTH_ONLY_E)
  17116. return -9700 - i;
  17117. if (len != testData[i].outSz)
  17118. return -9710 - i;
  17119. len = testData[i].outSz;
  17120. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  17121. if (ret != 0)
  17122. return -9720 - i;
  17123. if (XMEMCMP(out, testData[i].out, len) != 0)
  17124. return -9730 - i;
  17125. for (l = 1; l < testData[i].inSz; l++) {
  17126. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  17127. if (ret != ASN_PARSE_E)
  17128. return -9740;
  17129. len = testData[i].outSz;
  17130. ret = wc_BerToDer(testData[i].in, l, out, &len);
  17131. if (ret != ASN_PARSE_E)
  17132. return -9741;
  17133. }
  17134. }
  17135. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  17136. if (ret != BAD_FUNC_ARG)
  17137. return -9742;
  17138. ret = wc_BerToDer(out, 4, NULL, NULL);
  17139. if (ret != BAD_FUNC_ARG)
  17140. return -9743;
  17141. ret = wc_BerToDer(NULL, 4, NULL, &len);
  17142. if (ret != BAD_FUNC_ARG)
  17143. return -9744;
  17144. ret = wc_BerToDer(NULL, 4, out, NULL);
  17145. if (ret != BAD_FUNC_ARG)
  17146. return -9745;
  17147. ret = wc_BerToDer(out, 4, out, NULL);
  17148. if (ret != BAD_FUNC_ARG)
  17149. return -9746;
  17150. ret = wc_BerToDer(NULL, 4, out, &len);
  17151. if (ret != BAD_FUNC_ARG)
  17152. return -9747;
  17153. for (l = 1; l < sizeof(good4_out); l++) {
  17154. len = l;
  17155. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  17156. if (ret != BUFFER_E)
  17157. return -9748;
  17158. }
  17159. return 0;
  17160. }
  17161. #endif
  17162. #ifdef DEBUG_WOLFSSL
  17163. static THREAD_LS_T int log_cnt = 0;
  17164. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  17165. {
  17166. (void)logLevel;
  17167. (void)logMessage;
  17168. log_cnt++;
  17169. }
  17170. #endif /* DEBUG_WOLFSSL */
  17171. int logging_test(void)
  17172. {
  17173. #ifdef DEBUG_WOLFSSL
  17174. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  17175. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  17176. byte b[256];
  17177. int i;
  17178. for (i = 0; i < (int)sizeof(b); i++)
  17179. b[i] = i;
  17180. if (wolfSSL_Debugging_ON() != 0)
  17181. return -9800;
  17182. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  17183. return -9801;
  17184. WOLFSSL_MSG(msg);
  17185. WOLFSSL_BUFFER(a, sizeof(a));
  17186. WOLFSSL_BUFFER(b, sizeof(b));
  17187. WOLFSSL_BUFFER(NULL, 0);
  17188. WOLFSSL_ERROR(MEMORY_E);
  17189. WOLFSSL_ERROR_MSG(msg);
  17190. /* turn off logs */
  17191. wolfSSL_Debugging_OFF();
  17192. /* capture log count */
  17193. i = log_cnt;
  17194. /* validate no logs are output when disabled */
  17195. WOLFSSL_MSG(msg);
  17196. WOLFSSL_BUFFER(a, sizeof(a));
  17197. WOLFSSL_BUFFER(b, sizeof(b));
  17198. WOLFSSL_BUFFER(NULL, 0);
  17199. WOLFSSL_ERROR(MEMORY_E);
  17200. WOLFSSL_ERROR_MSG(msg);
  17201. /* check the logs were disabled */
  17202. if (i != log_cnt)
  17203. return -9802;
  17204. /* restore callback and leave logging enabled */
  17205. wolfSSL_SetLoggingCb(NULL);
  17206. wolfSSL_Debugging_ON();
  17207. /* suppress unused args */
  17208. (void)a;
  17209. (void)b;
  17210. #else
  17211. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  17212. return -9803;
  17213. wolfSSL_Debugging_OFF();
  17214. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  17215. return -9804;
  17216. #endif /* DEBUG_WOLFSSL */
  17217. return 0;
  17218. }
  17219. int mutex_test(void)
  17220. {
  17221. #ifdef WOLFSSL_PTHREADS
  17222. wolfSSL_Mutex m;
  17223. #endif
  17224. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  17225. if (mm == NULL)
  17226. return -9900;
  17227. wc_FreeMutex(mm);
  17228. XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX);
  17229. #ifdef WOLFSSL_PTHREADS
  17230. if (wc_InitMutex(&m) != 0)
  17231. return -9901;
  17232. if (wc_LockMutex(&m) != 0)
  17233. return -9902;
  17234. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  17235. return -9903;
  17236. if (wc_UnLockMutex(&m) != 0)
  17237. return -9904;
  17238. if (wc_FreeMutex(&m) != 0)
  17239. return -9905;
  17240. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  17241. return -9906;
  17242. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  17243. return -9907;
  17244. #endif
  17245. return 0;
  17246. }
  17247. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  17248. static int malloc_cnt = 0;
  17249. static int realloc_cnt = 0;
  17250. static int free_cnt = 0;
  17251. static void *my_Malloc_cb(size_t size)
  17252. {
  17253. malloc_cnt++;
  17254. return malloc(size);
  17255. }
  17256. static void my_Free_cb(void *ptr)
  17257. {
  17258. free_cnt++;
  17259. free(ptr);
  17260. }
  17261. static void *my_Realloc_cb(void *ptr, size_t size)
  17262. {
  17263. realloc_cnt++;
  17264. return realloc(ptr, size);
  17265. }
  17266. int memcb_test(void)
  17267. {
  17268. int ret = 0;
  17269. byte* b = NULL;
  17270. wolfSSL_Malloc_cb mc;
  17271. wolfSSL_Free_cb fc;
  17272. wolfSSL_Realloc_cb rc;
  17273. /* Save existing memory callbacks */
  17274. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  17275. return -10000;
  17276. /* test realloc */
  17277. b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17278. if (b == NULL) {
  17279. ERROR_OUT(-10001, exit_memcb);
  17280. }
  17281. XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17282. b = NULL;
  17283. /* Use API. */
  17284. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)(void*)&my_Malloc_cb,
  17285. (wolfSSL_Free_cb)(void*)&my_Free_cb,
  17286. (wolfSSL_Realloc_cb)(void*)&my_Realloc_cb) != 0) {
  17287. ERROR_OUT(-10005, exit_memcb);
  17288. }
  17289. b = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17290. b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17291. XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17292. #ifndef WOLFSSL_STATIC_MEMORY
  17293. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  17294. #else
  17295. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  17296. #endif
  17297. ret = -10006;
  17298. exit_memcb:
  17299. /* restore memory callbacks */
  17300. wolfSSL_SetAllocators(mc, fc, rc);
  17301. return ret;
  17302. }
  17303. #endif
  17304. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  17305. int blob_test(void)
  17306. {
  17307. int ret = 0;
  17308. byte out[112];
  17309. byte blob[112];
  17310. word32 outSz;
  17311. const byte iv[] =
  17312. {
  17313. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17314. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17315. };
  17316. const byte text[] =
  17317. {
  17318. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17319. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17320. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17321. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17322. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17323. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17324. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17325. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17326. };
  17327. XMEMSET(blob, 0, sizeof(blob));
  17328. outSz = sizeof(blob);
  17329. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  17330. if (ret != 0) {
  17331. ERROR_OUT(-10100, exit_blob);
  17332. }
  17333. blob[outSz - 2] += 1;
  17334. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  17335. if (ret == 0) { /* should fail with altered blob */
  17336. ERROR_OUT(-10101, exit_blob);
  17337. }
  17338. XMEMSET(blob, 0, sizeof(blob));
  17339. outSz = sizeof(blob);
  17340. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  17341. if (ret != 0) {
  17342. ERROR_OUT(-10102, exit_blob);
  17343. }
  17344. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  17345. if (ret != 0) {
  17346. ERROR_OUT(-10103, exit_blob);
  17347. }
  17348. if (XMEMCMP(out, iv, sizeof(iv))) {
  17349. ERROR_OUT(-10104, exit_blob);
  17350. }
  17351. XMEMSET(blob, 0, sizeof(blob));
  17352. outSz = sizeof(blob);
  17353. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  17354. if (ret != 0) {
  17355. ERROR_OUT(-10105, exit_blob);
  17356. }
  17357. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  17358. if (ret != 0) {
  17359. ERROR_OUT(-10106, exit_blob);
  17360. }
  17361. if (XMEMCMP(out, text, sizeof(text))) {
  17362. ERROR_OUT(-10107, exit_blob);
  17363. }
  17364. exit_blob:
  17365. return ret;
  17366. }
  17367. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  17368. #ifdef WOLF_CRYPTO_DEV
  17369. /* Example custom context for crypto callback */
  17370. typedef struct {
  17371. int exampleVar; /* example, not used */
  17372. } myCryptoDevCtx;
  17373. /* Example crypto dev callback function that calls software version */
  17374. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  17375. {
  17376. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  17377. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  17378. if (info == NULL)
  17379. return BAD_FUNC_ARG;
  17380. if (info->algo_type == WC_ALGO_TYPE_PK) {
  17381. #ifdef DEBUG_WOLFSSL
  17382. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  17383. #endif
  17384. #ifndef NO_RSA
  17385. if (info->pk.type == WC_PK_TYPE_RSA) {
  17386. /* set devId to invalid, so software is used */
  17387. info->pk.rsa.key->devId = INVALID_DEVID;
  17388. switch (info->pk.rsa.type) {
  17389. case RSA_PUBLIC_ENCRYPT:
  17390. case RSA_PUBLIC_DECRYPT:
  17391. /* perform software based RSA public op */
  17392. ret = wc_RsaFunction(
  17393. info->pk.rsa.in, info->pk.rsa.inLen,
  17394. info->pk.rsa.out, info->pk.rsa.outLen,
  17395. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  17396. break;
  17397. case RSA_PRIVATE_ENCRYPT:
  17398. case RSA_PRIVATE_DECRYPT:
  17399. /* perform software based RSA private op */
  17400. ret = wc_RsaFunction(
  17401. info->pk.rsa.in, info->pk.rsa.inLen,
  17402. info->pk.rsa.out, info->pk.rsa.outLen,
  17403. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  17404. break;
  17405. }
  17406. /* reset devId */
  17407. info->pk.rsa.key->devId = devIdArg;
  17408. }
  17409. #endif /* !NO_RSA */
  17410. #ifdef HAVE_ECC
  17411. if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  17412. /* set devId to invalid, so software is used */
  17413. info->pk.eccsign.key->devId = INVALID_DEVID;
  17414. ret = wc_ecc_sign_hash(
  17415. info->pk.eccsign.in, info->pk.eccsign.inlen,
  17416. info->pk.eccsign.out, info->pk.eccsign.outlen,
  17417. info->pk.eccsign.rng, info->pk.eccsign.key);
  17418. /* reset devId */
  17419. info->pk.eccsign.key->devId = devIdArg;
  17420. }
  17421. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  17422. /* set devId to invalid, so software is used */
  17423. info->pk.eccverify.key->devId = INVALID_DEVID;
  17424. ret = wc_ecc_verify_hash(
  17425. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  17426. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  17427. info->pk.eccverify.res, info->pk.eccverify.key);
  17428. /* reset devId */
  17429. info->pk.eccverify.key->devId = devIdArg;
  17430. }
  17431. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  17432. /* set devId to invalid, so software is used */
  17433. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  17434. ret = wc_ecc_shared_secret(
  17435. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  17436. info->pk.ecdh.out, info->pk.ecdh.outlen);
  17437. /* reset devId */
  17438. info->pk.ecdh.private_key->devId = devIdArg;
  17439. }
  17440. #endif /* HAVE_ECC */
  17441. }
  17442. (void)myCtx;
  17443. return ret;
  17444. }
  17445. int cryptodev_test(void)
  17446. {
  17447. int ret = 0;
  17448. myCryptoDevCtx myCtx;
  17449. /* example data for callback */
  17450. myCtx.exampleVar = 1;
  17451. /* set devId to something other than INVALID_DEVID */
  17452. devId = 1;
  17453. ret = wc_CryptoDev_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  17454. #ifndef NO_RSA
  17455. if (ret == 0)
  17456. ret = rsa_test();
  17457. #endif
  17458. #ifdef HAVE_ECC
  17459. if (ret == 0)
  17460. ret = ecc_test();
  17461. #endif
  17462. /* reset devId */
  17463. devId = INVALID_DEVID;
  17464. return ret;
  17465. }
  17466. #endif /* WOLF_CRYPTO_DEV */
  17467. #undef ERROR_OUT
  17468. #else
  17469. #ifndef NO_MAIN_DRIVER
  17470. int main() { return 0; }
  17471. #endif
  17472. #endif /* NO_CRYPT_TEST */